On Sat, 6 Nov 2010 11:33:41 +0200 Dimitar Zhekov dimitar.zhekov@gmail.com wrote:
I'll write a non-GIO variant first, as a proof of concept.
First, I feel quite reluctant to use non-GLib/GIO functions for writing instead of g_file_replace_contents because if there is a better implementation, why don't they add it?
Also, g_file_replace_contents does have a make_backup argument we could provide an option for. This might handle the disk exhaustion problem.
The current non-GIO is buggy anyway. First:
if (G_UNLIKELY(len != bytes_written)) err = errno;
but fwrite() is not guaranteed to set errno, only write() is.
Second, and more important, the result of fclose() is not checked, for a buffered file stream. On lack of disk space, on my system fwrite() happily returns written == len, but fclose() fails. YMMV.
If not anything else, we should use non-buffered I/O, with fsync(), and check the result of close() anyway.
Thanks for finding these problems. I accept fclose should be checked.
I'm not sure why write is better than fwrite - POSIX says both set errno. http://www.opengroup.org/onlinepubs/009695399/functions/fwrite.html
Can you explain why non-buffered I/O is better?
Nick
P.S. Thanks for sending the updated patch.