Commit 0b15ee0e authored by Chris Graham's avatar Chris Graham
Browse files

Fixed MANTIS-4153 (Incorrect file write errors on race condition)

parent df3fc988
Loading
Loading
Loading
Loading
+0 −31
Original line number Diff line number Diff line
@@ -149,37 +149,6 @@ function cms_file_put_contents_safe($path, $contents, $flags = 4, $retry_depth =
        return _cms_file_put_contents_safe_failed($error_message, $path, $flags);
    }

    // Find file size
    if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
        clearstatcache(true, $path);
    } else {
        clearstatcache();
    }
    $size = @filesize($path);

    // Special condition: File already deleted
    if ($size === false) {
        return true; // We'll assume it was okay before something else deleted it
    }

    // Error condition: If somehow it said it saved but didn't actually (maybe a race condition on servers with buggy locking)
    if ($size != $num_bytes_to_save) {
        if ($retry_depth < 5) {
            return cms_file_put_contents_safe($path, $contents, $flags, $retry_depth + 1);
        }

        if (($flags & FILE_WRITE_FAILURE_SILENT) == 0) {
            if (function_exists('do_lang_tempcode')) {
                $error_message = do_lang_tempcode('COULD_NOT_SAVE_FILE', escape_html($path));
            } else {
                $error_message = 'Could not save file ' . htmlentities($path);
            }
        } else {
            $error_message = '';
        }
        return _cms_file_put_contents_safe_failed($error_message, $path, $flags);
    }

    // Extra requested operations
    if (($flags & FILE_WRITE_FIX_PERMISSIONS) != 0) {
        fix_permissions($path);