- g_set_error (error, G_IO_ERROR,
- g_io_error_from_errno (errno),
- _("Error truncating file: %s"),
- g_strerror (errno));
- goto err_out;
+ (void) g_close (fd, NULL);
+
+ if (g_unlink (filename) != 0)
+ {
+ int errsv = errno;
+
+ g_set_error (error, G_IO_ERROR,
+ g_io_error_from_errno (errsv),
+ _("Error removing old file: %s"),
+ g_strerror (errsv));
+ goto err_out2;
+ }
+
+ if (readable)
+ open_flags = O_RDWR | O_CREAT | O_BINARY;
+ else
+ open_flags = O_WRONLY | O_CREAT | O_BINARY;
+ fd = g_open (filename, open_flags, mode);
+ if (fd == -1)
+ {
+ int errsv = errno;
+ char *display_name = g_filename_display_name (filename);
+ g_set_error (error, G_IO_ERROR,
+ g_io_error_from_errno (errsv),
+ _("Error opening file '%s': %s"),
+ display_name, g_strerror (errsv));
+ g_free (display_name);
+ goto err_out2;
+ }
+ }
+ else
+ {
+ /* Truncate the file at the start */
+#ifdef G_OS_WIN32
+ if (g_win32_ftruncate (fd, 0) == -1)
+#else
+ if (ftruncate (fd, 0) == -1)
+#endif
+ {
+ int errsv = errno;
+
+ g_set_error (error, G_IO_ERROR,
+ g_io_error_from_errno (errsv),
+ _("Error truncating file: %s"),
+ g_strerror (errsv));
+ goto err_out;
+ }