Set umask before operations that create local files

this solves issues where "other php stuff" is messing with the umask

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2021-01-22 15:44:24 +01:00
parent 458feef3d4
commit 8f1bce0d04
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
2 changed files with 53 additions and 7 deletions

View File

@ -87,7 +87,11 @@ class Local extends \OC\Files\Storage\Common {
} }
public function mkdir($path) { public function mkdir($path) {
return @mkdir($this->getSourcePath($path), 0777, true); $sourcePath = $this->getSourcePath($path);
$oldMask = umask(022);
$result = @mkdir($sourcePath, 0777, true);
umask($oldMask);
return $result;
} }
public function rmdir($path) { public function rmdir($path) {
@ -256,11 +260,13 @@ class Local extends \OC\Files\Storage\Common {
if ($this->file_exists($path) and !$this->isUpdatable($path)) { if ($this->file_exists($path) and !$this->isUpdatable($path)) {
return false; return false;
} }
$oldMask = umask(022);
if (!is_null($mtime)) { if (!is_null($mtime)) {
$result = @touch($this->getSourcePath($path), $mtime); $result = @touch($this->getSourcePath($path), $mtime);
} else { } else {
$result = @touch($this->getSourcePath($path)); $result = @touch($this->getSourcePath($path));
} }
umask($oldMask);
if ($result) { if ($result) {
clearstatcache(true, $this->getSourcePath($path)); clearstatcache(true, $this->getSourcePath($path));
} }
@ -273,7 +279,10 @@ class Local extends \OC\Files\Storage\Common {
} }
public function file_put_contents($path, $data) { public function file_put_contents($path, $data) {
return file_put_contents($this->getSourcePath($path), $data); $oldMask = umask(022);
$result = file_put_contents($this->getSourcePath($path), $data);
umask($oldMask);
return $result;
} }
public function unlink($path) { public function unlink($path) {
@ -343,12 +352,18 @@ class Local extends \OC\Files\Storage\Common {
if ($this->is_dir($path1)) { if ($this->is_dir($path1)) {
return parent::copy($path1, $path2); return parent::copy($path1, $path2);
} else { } else {
return copy($this->getSourcePath($path1), $this->getSourcePath($path2)); $oldMask = umask(022);
$result = copy($this->getSourcePath($path1), $this->getSourcePath($path2));
umask($oldMask);
return $result;
} }
} }
public function fopen($path, $mode) { public function fopen($path, $mode) {
return fopen($this->getSourcePath($path), $mode); $oldMask = umask(022);
$result = fopen($this->getSourcePath($path), $mode);
umask($oldMask);
return $result;
} }
public function hash($type, $path, $raw = false) { public function hash($type, $path, $raw = false) {

View File

@ -108,4 +108,35 @@ class LocalTest extends Storage {
$storage->file_put_contents('sym/foo', 'bar'); $storage->file_put_contents('sym/foo', 'bar');
$this->addToAssertionCount(1); $this->addToAssertionCount(1);
} }
public function testWriteUmaskFilePutContents() {
$oldMask = umask(0333);
$this->instance->file_put_contents('test.txt', 'sad');
umask($oldMask);
$this->assertTrue($this->instance->isUpdatable('test.txt'));
}
public function testWriteUmaskMkdir() {
$oldMask = umask(0333);
$this->instance->mkdir('test.txt');
umask($oldMask);
$this->assertTrue($this->instance->isUpdatable('test.txt'));
}
public function testWriteUmaskFopen() {
$oldMask = umask(0333);
$handle = $this->instance->fopen('test.txt', 'w');
fwrite($handle, 'foo');
fclose($handle);
umask($oldMask);
$this->assertTrue($this->instance->isUpdatable('test.txt'));
}
public function testWriteUmaskCopy() {
$this->instance->file_put_contents('source.txt', 'sad');
$oldMask = umask(0333);
$this->instance->copy('source.txt', 'test.txt');
umask($oldMask);
$this->assertTrue($this->instance->isUpdatable('test.txt'));
}
} }