Skip to content

Commit

Permalink
MDL-71991 files: check return value of rename when writing files
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Jun 22, 2021
1 parent 338b60f commit 1af2572
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/filestorage/file_system_filedir.php
Expand Up @@ -393,7 +393,14 @@ public function add_file_from_path($pathname, $contenthash = null) {
ignore_user_abort($prev);
throw new file_exception('storedfilecannotcreatefile');
}
rename($hashfile.'.tmp', $hashfile);
if (!rename($hashfile.'.tmp', $hashfile)) {
// Something very strange went wrong.
@unlink($hashfile . '.tmp');
// Note, we don't try to clean up $hashfile. Almost certainly, if it exists
// (e.g. written by another process?) it will be right, so don't wipe it.
ignore_user_abort($prev);
throw new file_exception('storedfilecannotcreatefile');
}
chmod($hashfile, $this->filepermissions); // Fix permissions if needed.
if (file_exists($hashfile.'.tmp')) {
// Just in case anything fails in a weird way.
Expand Down Expand Up @@ -470,7 +477,14 @@ public function add_file_from_string($content) {
ignore_user_abort($prev);
throw new file_exception('storedfilecannotcreatefile');
}
rename($hashfile.'.tmp', $hashfile);
if (!rename($hashfile.'.tmp', $hashfile)) {
// Something very strange went wrong.
@unlink($hashfile . '.tmp');
// Note, we don't try to clean up $hashfile. Almost certainly, if it exists
// (e.g. written by another process?) it will be right, so don't wipe it.
ignore_user_abort($prev);
throw new file_exception('storedfilecannotcreatefile');
}
chmod($hashfile, $this->filepermissions); // Fix permissions if needed.
if (file_exists($hashfile.'.tmp')) {
// Just in case anything fails in a weird way.
Expand Down

0 comments on commit 1af2572

Please sign in to comment.