Commit d20a188b authored by Alexander Larsson's avatar Alexander Larsson
Browse files

Only fsync if the existing file is > 0 bytes

This means we don't sync in the case where we created an (empty)
temp file and now replace it with the data.

This fixes (among other things) the performance of trashing files.
parent 79a70dc8
......@@ -961,29 +961,34 @@ write_to_temp_file (const gchar *contents,
}
#ifdef HAVE_FSYNC
errno = 0;
/* If the final destination exists, we want to sync the newly written
* file to ensure the data is on disk when we rename over the destination.
* otherwise if we get a system crash we can lose both the new and the
* old file on some filesystems. (I.E. those that don't guarantee the
* data is written to the disk before the metadata.)
*/
if (g_file_test (dest_file, G_FILE_TEST_EXISTS) &&
fsync (fileno (file)) != 0)
{
save_errno = errno;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to write file '%s': fsync() failed: %s"),
display_name,
g_strerror (save_errno));
{
struct stat statbuf;
errno = 0;
/* If the final destination exists and is > 0 bytes, we want to sync the
* newly written file to ensure the data is on disk when we rename over
* the destination. Otherwise if we get a system crash we can lose both
* the new and the old file on some filesystems. (I.E. those that don't
* guarantee the data is written to the disk before the metadata.)
*/
if (g_lstat (dest_file, &statbuf) == 0 &&
statbuf.st_size > 0 &&
fsync (fileno (file)) != 0)
{
save_errno = errno;
g_set_error (err,
G_FILE_ERROR,
g_file_error_from_errno (save_errno),
_("Failed to write file '%s': fsync() failed: %s"),
display_name,
g_strerror (save_errno));
g_unlink (tmp_name);
goto out;
}
g_unlink (tmp_name);
goto out;
}
}
#endif
errno = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment