g_file_replace does not restore original file when there is errors while writing
Submitted by Ramūnas Gutkovas
Link to original bug (#602412)
Description
This happens in the glib HEAD and downwards.
From documentation:
http://library.gnome.org/devel/gio/stable/GFile.html#g-file-replace
This will try to replace the file in the safest way possible so that any errors during the writing will not affect an already existing copy of the file. For instance, for local files it may write to a temporary file and then atomically rename over the destination when the stream is closed.
Here is some test code to reproduce the bug:
#include <gio/gio.h>
int main (int argc, char **argv) { g_type_init ();
GError *error = NULL;
GFile *file = g_file_new_for_path("minifs/hello.txt");
GOutputStream* out =
g_file_replace(file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
int i = 0;
if (out == NULL) {
g_message("Error: %s\n", error->message);
g_error_free(error);
}
GError *err2 = NULL;
for (i = 0; i < 1024*1024/4; i++) {
err2 = NULL;
if (!g_output_stream_write_all(out, "AAAAAAAA", 8, NULL, NULL, &err2)) {
g_message("Error 2: %s\n", err2->message);
g_error_free(err2);
}
}
if (out) g_object_unref(out);
g_object_unref(file);
}
The contents of "minifs/hello.txt" say: "Hello, World!"
Here "minifs" is just a small file system, in which the writing of this will exhaust the space (an error state). And in "minifs/hello.txt" you'll find bunch of A's, rather than "Hello, World!"
This bugs other manifestation:
https://bugzilla.gnome.org/show_bug.cgi?id=561309
I also attach a patch.