[gio compiles and links on win32, not sure how much already works] ifdefed
[platform/upstream/glib.git] / gio / glocalfileoutputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <errno.h>
32 #include <string.h>
33
34 #include <glib.h>
35 #include <glib/gstdio.h>
36 #include "glibintl.h"
37 #include "gioerror.h"
38 #include "glocalfileoutputstream.h"
39 #include "glocalfileinfo.h"
40
41 #ifdef G_OS_WIN32
42 #include <io.h>
43 #ifndef S_ISDIR
44 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
45 #endif
46 #ifndef S_ISREG
47 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
48 #endif
49 #endif
50
51 #include "gioalias.h"
52
53 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
54 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
55
56 /* Some of the file replacement code was based on the code from gedit,
57  * relicenced to LGPL with permissions from the authors.
58  */
59
60 #define BACKUP_EXTENSION "~"
61
62 struct _GLocalFileOutputStreamPrivate {
63   char *tmp_filename;
64   char *original_filename;
65   char *backup_filename;
66   char *etag;
67   int fd;
68 };
69
70 static gssize     g_local_file_output_stream_write        (GOutputStream      *stream,
71                                                            const void         *buffer,
72                                                            gsize               count,
73                                                            GCancellable       *cancellable,
74                                                            GError            **error);
75 static gboolean   g_local_file_output_stream_close        (GOutputStream      *stream,
76                                                            GCancellable       *cancellable,
77                                                            GError            **error);
78 static GFileInfo *g_local_file_output_stream_query_info   (GFileOutputStream  *stream,
79                                                            char               *attributes,
80                                                            GCancellable       *cancellable,
81                                                            GError            **error);
82 static char *     g_local_file_output_stream_get_etag     (GFileOutputStream  *stream);
83 static goffset    g_local_file_output_stream_tell         (GFileOutputStream  *stream);
84 static gboolean   g_local_file_output_stream_can_seek     (GFileOutputStream  *stream);
85 static gboolean   g_local_file_output_stream_seek         (GFileOutputStream  *stream,
86                                                            goffset             offset,
87                                                            GSeekType           type,
88                                                            GCancellable       *cancellable,
89                                                            GError            **error);
90 static gboolean   g_local_file_output_stream_can_truncate (GFileOutputStream  *stream);
91 static gboolean   g_local_file_output_stream_truncate     (GFileOutputStream  *stream,
92                                                            goffset             size,
93                                                            GCancellable       *cancellable,
94                                                            GError            **error);
95
96 static void
97 g_local_file_output_stream_finalize (GObject *object)
98 {
99   GLocalFileOutputStream *file;
100   
101   file = G_LOCAL_FILE_OUTPUT_STREAM (object);
102   
103   g_free (file->priv->tmp_filename);
104   g_free (file->priv->original_filename);
105   g_free (file->priv->backup_filename);
106   g_free (file->priv->etag);
107   
108   if (G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize)
109     (*G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize) (object);
110 }
111
112 static void
113 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
114 {
115   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116   GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
117   GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
118   
119   g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
120   
121   gobject_class->finalize = g_local_file_output_stream_finalize;
122
123   stream_class->write_fn = g_local_file_output_stream_write;
124   stream_class->close_fn = g_local_file_output_stream_close;
125   file_stream_class->query_info = g_local_file_output_stream_query_info;
126   file_stream_class->get_etag = g_local_file_output_stream_get_etag;
127   file_stream_class->tell = g_local_file_output_stream_tell;
128   file_stream_class->can_seek = g_local_file_output_stream_can_seek;
129   file_stream_class->seek = g_local_file_output_stream_seek;
130   file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
131   file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
132 }
133
134 static void
135 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
136 {
137   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
138                                               G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
139                                               GLocalFileOutputStreamPrivate);
140 }
141
142 static gssize
143 g_local_file_output_stream_write (GOutputStream  *stream,
144                                   const void     *buffer,
145                                   gsize           count,
146                                   GCancellable   *cancellable,
147                                   GError        **error)
148 {
149   GLocalFileOutputStream *file;
150   gssize res;
151
152   file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
153
154   while (1)
155     {
156       if (g_cancellable_set_error_if_cancelled (cancellable, error))
157         return -1;
158       res = write (file->priv->fd, buffer, count);
159       if (res == -1)
160         {
161           if (errno == EINTR)
162             continue;
163           
164           g_set_error (error, G_IO_ERROR,
165                        g_io_error_from_errno (errno),
166                        _("Error writing to file: %s"),
167                        g_strerror (errno));
168         }
169       
170       break;
171     }
172   
173   return res;
174 }
175
176 static gboolean
177 g_local_file_output_stream_close (GOutputStream  *stream,
178                                   GCancellable   *cancellable,
179                                   GError        **error)
180 {
181   GLocalFileOutputStream *file;
182   struct stat final_stat;
183   int res;
184
185   file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
186
187   if (file->priv->tmp_filename)
188     {
189       /* We need to move the temp file to its final place,
190        * and possibly create the backup file
191        */
192
193       if (file->priv->backup_filename)
194         {
195           if (g_cancellable_set_error_if_cancelled (cancellable, error))
196             goto err_out;
197           
198 #ifdef HAVE_LINK
199           /* create original -> backup link, the original is then renamed over */
200           if (unlink (file->priv->backup_filename) != 0 &&
201               errno != ENOENT)
202             {
203               g_set_error (error, G_IO_ERROR,
204                            G_IO_ERROR_CANT_CREATE_BACKUP,
205                            _("Error removing old backup link: %s"),
206                            g_strerror (errno));
207               goto err_out;
208             }
209
210           if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
211             {
212               g_set_error (error, G_IO_ERROR,
213                            G_IO_ERROR_CANT_CREATE_BACKUP,
214                            _("Error creating backup link: %s"),
215                            g_strerror (errno));
216               goto err_out;
217             }
218 #else
219             /* If link not supported, just rename... */
220           if (!rename (file->priv->original_filename, file->priv->backup_filename) != 0)
221             {
222               g_set_error (error, G_IO_ERROR,
223                            G_IO_ERROR_CANT_CREATE_BACKUP,
224                            _("Error creating backup copy: %s"),
225                            g_strerror (errno));
226               goto err_out;
227             }
228 #endif
229         }
230       
231
232       if (g_cancellable_set_error_if_cancelled (cancellable, error))
233         goto err_out;
234
235       /* tmp -> original */
236       if (rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
237         {
238           g_set_error (error, G_IO_ERROR,
239                        g_io_error_from_errno (errno),
240                        _("Error renaming temporary file: %s"),
241                        g_strerror (errno));
242           goto err_out;
243         }
244     }
245   
246   if (g_cancellable_set_error_if_cancelled (cancellable, error))
247     goto err_out;
248       
249   if (fstat (file->priv->fd, &final_stat) == 0)
250     file->priv->etag = _g_local_file_info_create_etag (&final_stat);
251
252   while (1)
253     {
254       res = close (file->priv->fd);
255       if (res == -1)
256         {
257           g_set_error (error, G_IO_ERROR,
258                        g_io_error_from_errno (errno),
259                        _("Error closing file: %s"),
260                        g_strerror (errno));
261         }
262       break;
263     }
264   
265   return res != -1;
266
267  err_out:
268   /* A simple try to close the fd in case we fail before the actual close */
269   close (file->priv->fd);
270   return FALSE;
271 }
272
273 static char *
274 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
275 {
276   GLocalFileOutputStream *file;
277
278   file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
279   
280   return g_strdup (file->priv->etag);
281 }
282
283 static goffset
284 g_local_file_output_stream_tell (GFileOutputStream *stream)
285 {
286   GLocalFileOutputStream *file;
287   off_t pos;
288
289   file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
290   
291   pos = lseek (file->priv->fd, 0, SEEK_CUR);
292
293   if (pos == (off_t)-1)
294     return 0;
295   
296   return pos;
297 }
298
299 static gboolean
300 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
301 {
302   GLocalFileOutputStream *file;
303   off_t pos;
304
305   file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
306   
307   pos = lseek (file->priv->fd, 0, SEEK_CUR);
308
309   if (pos == (off_t)-1 &&
310       errno == ESPIPE)
311     return FALSE;
312   
313   return TRUE;
314 }
315
316 static int
317 seek_type_to_lseek (GSeekType type)
318 {
319   switch (type)
320     {
321     default:
322     case G_SEEK_CUR:
323       return SEEK_CUR;
324       
325     case G_SEEK_SET:
326       return SEEK_SET;
327       
328     case G_SEEK_END:
329       return SEEK_END;
330     }
331 }
332
333 static gboolean
334 g_local_file_output_stream_seek (GFileOutputStream  *stream,
335                                  goffset             offset,
336                                  GSeekType           type,
337                                  GCancellable       *cancellable,
338                                  GError            **error)
339 {
340   GLocalFileOutputStream *file;
341   off_t pos;
342
343   file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
344
345   pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
346
347   if (pos == (off_t)-1)
348     {
349       g_set_error (error, G_IO_ERROR,
350                    g_io_error_from_errno (errno),
351                    _("Error seeking in file: %s"),
352                    g_strerror (errno));
353       return FALSE;
354     }
355   
356   return TRUE;
357 }
358
359 static gboolean
360 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
361 {
362   /* We can't truncate pipes and stuff where we can't seek */
363   return g_local_file_output_stream_can_seek (stream);
364 }
365
366 static gboolean
367 g_local_file_output_stream_truncate (GFileOutputStream  *stream,
368                                      goffset             size,
369                                      GCancellable       *cancellable,
370                                      GError            **error)
371 {
372   GLocalFileOutputStream *file;
373   int res;
374
375   file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
376
377  restart:
378 #ifdef G_OS_WIN32
379   res = g_win32_ftruncate (file->priv->fd, size);
380 #else
381   res = ftruncate (file->priv->fd, size);
382 #endif
383   
384   if (res == -1)
385     {
386       if (errno == EINTR)
387         {
388           if (g_cancellable_set_error_if_cancelled (cancellable, error))
389             return FALSE;
390           goto restart;
391         }
392
393       g_set_error (error, G_IO_ERROR,
394                    g_io_error_from_errno (errno),
395                    _("Error truncating file: %s"),
396                    g_strerror (errno));
397       return FALSE;
398     }
399   
400   return TRUE;
401 }
402
403
404 static GFileInfo *
405 g_local_file_output_stream_query_info (GFileOutputStream  *stream,
406                                        char               *attributes,
407                                        GCancellable       *cancellable,
408                                        GError            **error)
409 {
410   GLocalFileOutputStream *file;
411
412   file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
413
414   if (g_cancellable_set_error_if_cancelled (cancellable, error))
415     return NULL;
416   
417   return _g_local_file_info_get_from_fd (file->priv->fd,
418                                          attributes,
419                                          error);
420 }
421
422 GFileOutputStream *
423 _g_local_file_output_stream_create  (const char        *filename,
424                                      GFileCreateFlags   flags,
425                                      GCancellable      *cancellable,
426                                      GError           **error)
427 {
428   GLocalFileOutputStream *stream;
429   int mode;
430   int fd;
431
432   if (g_cancellable_set_error_if_cancelled (cancellable, error))
433     return NULL;
434
435   if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
436     mode = 0600;
437   else
438     mode = 0666;
439   
440   fd = g_open (filename,
441                O_CREAT | O_EXCL | O_WRONLY,
442                0666);
443   if (fd == -1)
444     {
445       int errsv = errno;
446
447       if (errsv == EINVAL)
448         /* This must be an invalid filename, on e.g. FAT */
449         g_set_error (error, G_IO_ERROR,
450                      G_IO_ERROR_INVALID_FILENAME,
451                      _("Invalid filename"));
452       else
453         g_set_error (error, G_IO_ERROR,
454                      g_io_error_from_errno (errsv),
455                      _("Error opening file '%s': %s"),
456                      filename, g_strerror (errsv));
457       return NULL;
458     }
459   
460   stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
461   stream->priv->fd = fd;
462   return G_FILE_OUTPUT_STREAM (stream);
463 }
464
465 GFileOutputStream *
466 _g_local_file_output_stream_append  (const char        *filename,
467                                      GFileCreateFlags   flags,
468                                      GCancellable      *cancellable,
469                                      GError           **error)
470 {
471   GLocalFileOutputStream *stream;
472   int mode;
473   int fd;
474
475   if (g_cancellable_set_error_if_cancelled (cancellable, error))
476     return NULL;
477
478   if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
479     mode = 0600;
480   else
481     mode = 0666;
482
483   fd = g_open (filename,
484                O_CREAT | O_APPEND | O_WRONLY,
485                mode);
486   if (fd == -1)
487     {
488       int errsv = errno;
489
490       if (errsv == EINVAL)
491         /* This must be an invalid filename, on e.g. FAT */
492         g_set_error (error, G_IO_ERROR,
493                      G_IO_ERROR_INVALID_FILENAME,
494                      _("Invalid filename"));
495       else
496         g_set_error (error, G_IO_ERROR,
497                      g_io_error_from_errno (errsv),
498                      _("Error opening file '%s': %s"),
499                      filename, g_strerror (errsv));
500       return NULL;
501     }
502   
503   stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
504   stream->priv->fd = fd;
505   
506   return G_FILE_OUTPUT_STREAM (stream);
507 }
508
509 static char *
510 create_backup_filename (const char *filename)
511 {
512   return g_strconcat (filename, BACKUP_EXTENSION, NULL);
513 }
514
515 #define BUFSIZE 8192 /* size of normal write buffer */
516
517 static gboolean
518 copy_file_data (gint     sfd,
519                 gint     dfd,
520                 GError **error)
521 {
522   gboolean ret = TRUE;
523   gpointer buffer;
524   const gchar *write_buffer;
525   gssize bytes_read;
526   gssize bytes_to_write;
527   gssize bytes_written;
528   
529   buffer = g_malloc (BUFSIZE);
530   
531   do
532     {
533       bytes_read = read (sfd, buffer, BUFSIZE);
534       if (bytes_read == -1)
535         {
536           if (errno == EINTR)
537             continue;
538           
539           g_set_error (error, G_IO_ERROR,
540                        g_io_error_from_errno (errno),
541                        _("Error reading from file: %s"),
542                        g_strerror (errno));
543           ret = FALSE;
544           break;
545         }
546       
547       bytes_to_write = bytes_read;
548       write_buffer = buffer;
549       
550       do
551         {
552           bytes_written = write (dfd, write_buffer, bytes_to_write);
553           if (bytes_written == -1)
554             {
555               if (errno == EINTR)
556                 continue;
557               
558               g_set_error (error, G_IO_ERROR,
559                            g_io_error_from_errno (errno),
560                            _("Error writing to file: %s"),
561                            g_strerror (errno));
562               ret = FALSE;
563               break;
564             }
565           
566           bytes_to_write -= bytes_written;
567           write_buffer += bytes_written;
568         }
569       while (bytes_to_write > 0);
570       
571     } while ((bytes_read != 0) && (ret == TRUE));
572
573   g_free (buffer);
574   
575   return ret;
576 }
577
578 static int
579 handle_overwrite_open (const char    *filename,
580                        const char    *etag,
581                        gboolean       create_backup,
582                        char         **temp_filename,
583                        GCancellable  *cancellable,
584                        GError       **error)
585 {
586   int fd = -1;
587   struct stat original_stat;
588   char *current_etag;
589   gboolean is_symlink;
590   int open_flags;
591
592   /* We only need read access to the original file if we are creating a backup.
593    * We also add O_CREATE to avoid a race if the file was just removed */
594   if (create_backup)
595     open_flags = O_RDWR | O_CREAT;
596   else
597     open_flags = O_WRONLY | O_CREAT;
598   
599   /* Some systems have O_NOFOLLOW, which lets us avoid some races
600    * when finding out if the file we opened was a symlink */
601 #ifdef O_NOFOLLOW
602   is_symlink = FALSE;
603   fd = g_open (filename, open_flags | O_NOFOLLOW, 0666);
604   if (fd == -1 && errno == ELOOP)
605     {
606       /* Could be a symlink, or it could be a regular ELOOP error,
607        * but then the next open will fail too. */
608       is_symlink = TRUE;
609       fd = g_open (filename, open_flags, 0666);
610     }
611 #else
612   fd = g_open (filename, open_flags, 0666);
613   /* This is racy, but we do it as soon as possible to minimize the race */
614   is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
615 #endif
616     
617   if (fd == -1)
618     {
619       g_set_error (error, G_IO_ERROR,
620                    g_io_error_from_errno (errno),
621                    _("Error opening file '%s': %s"),
622                    filename, g_strerror (errno));
623       return -1;
624     }
625   
626   if (fstat (fd, &original_stat) != 0) 
627     {
628       g_set_error (error, G_IO_ERROR,
629                    g_io_error_from_errno (errno),
630                    _("Error stating file '%s': %s"),
631                    filename, g_strerror (errno));
632       goto err_out;
633     }
634   
635   /* not a regular file */
636   if (!S_ISREG (original_stat.st_mode))
637     {
638       if (S_ISDIR (original_stat.st_mode))
639         g_set_error (error,
640                      G_IO_ERROR,
641                      G_IO_ERROR_IS_DIRECTORY,
642                      _("Target file is a directory"));
643       else
644         g_set_error (error,
645                      G_IO_ERROR,
646                      G_IO_ERROR_NOT_REGULAR_FILE,
647                      _("Target file is not a regular file"));
648       goto err_out;
649     }
650   
651   if (etag != NULL)
652     {
653       current_etag = _g_local_file_info_create_etag (&original_stat);
654       if (strcmp (etag, current_etag) != 0)
655         {
656           g_set_error (error,
657                        G_IO_ERROR,
658                        G_IO_ERROR_WRONG_ETAG,
659                        _("The file was externally modified"));
660           g_free (current_etag);
661           goto err_out;
662         }
663       g_free (current_etag);
664     }
665
666   /* We use two backup strategies.
667    * The first one (which is faster) consist in saving to a
668    * tmp file then rename the original file to the backup and the
669    * tmp file to the original name. This is fast but doesn't work
670    * when the file is a link (hard or symbolic) or when we can't
671    * write to the current dir or can't set the permissions on the
672    * new file. 
673    * The second strategy consist simply in copying the old file
674    * to a backup file and rewrite the contents of the file.
675    */
676   
677   if (!(original_stat.st_nlink > 1) && !is_symlink)
678     {
679       char *dirname, *tmp_filename;
680       int tmpfd;
681       
682       dirname = g_path_get_dirname (filename);
683       tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
684       g_free (dirname);
685
686       tmpfd = g_mkstemp (tmp_filename);
687       if (tmpfd == -1)
688         {
689           g_free (tmp_filename);
690           goto fallback_strategy;
691         }
692       
693       /* try to keep permissions */
694
695       if (
696 #ifdef F_CHOWN
697           fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
698 #endif
699 #ifdef F_CHMOD
700           fchmod (tmpfd, original_stat.st_mode) == -1 ||
701 #endif
702           0
703           )
704         {
705           struct stat tmp_statbuf;
706           
707           /* Check that we really needed to change something */
708           if (fstat (tmpfd, &tmp_statbuf) != 0 ||
709               original_stat.st_uid != tmp_statbuf.st_uid ||
710               original_stat.st_gid != tmp_statbuf.st_gid ||
711               original_stat.st_mode != tmp_statbuf.st_mode)
712             {
713               close (tmpfd);
714               unlink (tmp_filename);
715               g_free (tmp_filename);
716               goto fallback_strategy;
717             }
718         }
719
720       close (fd);
721       *temp_filename = tmp_filename;
722       return tmpfd;
723     }
724
725  fallback_strategy:
726
727   if (create_backup)
728     {
729       struct stat tmp_statbuf;      
730       char *backup_filename;
731       int bfd;
732       
733       backup_filename = create_backup_filename (filename);
734
735       if (unlink (backup_filename) == -1 && errno != ENOENT)
736         {
737           g_set_error (error,
738                        G_IO_ERROR,
739                        G_IO_ERROR_CANT_CREATE_BACKUP,
740                        _("Backup file creation failed"));
741           g_free (backup_filename);
742           goto err_out;
743         }
744
745       bfd = g_open (backup_filename,
746                     O_WRONLY | O_CREAT | O_EXCL,
747                     original_stat.st_mode & 0777);
748
749       if (bfd == -1)
750         {
751           g_set_error (error,
752                        G_IO_ERROR,
753                        G_IO_ERROR_CANT_CREATE_BACKUP,
754                        _("Backup file creation failed"));
755           g_free (backup_filename);
756           goto err_out;
757         }
758
759       /* If needed, Try to set the group of the backup same as the
760        * original file. If this fails, set the protection
761        * bits for the group same as the protection bits for
762        * others. */
763 #ifdef HAVE_FCHOWN
764       if (fstat (bfd, &tmp_statbuf) != 0)
765         {
766           g_set_error (error,
767                        G_IO_ERROR,
768                        G_IO_ERROR_CANT_CREATE_BACKUP,
769                        _("Backup file creation failed"));
770           unlink (backup_filename);
771           g_free (backup_filename);
772           goto err_out;
773         }
774       
775       if ((original_stat.st_gid != tmp_statbuf.st_gid)  &&
776           fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
777         {
778           if (fchmod (bfd,
779                       (original_stat.st_mode & 0707) |
780                       ((original_stat.st_mode & 07) << 3)) != 0)
781             {
782               g_set_error (error,
783                            G_IO_ERROR,
784                            G_IO_ERROR_CANT_CREATE_BACKUP,
785                            _("Backup file creation failed"));
786               unlink (backup_filename);
787               close (bfd);
788               g_free (backup_filename);
789               goto err_out;
790             }
791         }
792 #endif
793
794       if (!copy_file_data (fd, bfd, NULL))
795         {
796           g_set_error (error,
797                        G_IO_ERROR,
798                        G_IO_ERROR_CANT_CREATE_BACKUP,
799                        _("Backup file creation failed"));
800           unlink (backup_filename);
801           close (bfd);
802           g_free (backup_filename);
803           
804           goto err_out;
805         }
806       
807       close (bfd);
808       g_free (backup_filename);
809
810       /* Seek back to the start of the file after the backup copy */
811       if (lseek (fd, 0, SEEK_SET) == -1)
812         {
813           g_set_error (error, G_IO_ERROR,
814                        g_io_error_from_errno (errno),
815                        _("Error seeking in file: %s"),
816                        g_strerror (errno));
817           goto err_out;
818         }
819     }
820
821   /* Truncate the file at the start */
822 #ifdef G_OS_WIN32
823   if (g_win32_ftruncate (fd, 0) == -1)
824 #else
825   if (ftruncate (fd, 0) == -1)
826 #endif
827     {
828       g_set_error (error, G_IO_ERROR,
829                    g_io_error_from_errno (errno),
830                    _("Error truncating file: %s"),
831                    g_strerror (errno));
832       goto err_out;
833     }
834     
835   return fd;
836
837  err_out:
838   close (fd);
839   return -1;
840 }
841
842 GFileOutputStream *
843 _g_local_file_output_stream_replace (const char        *filename,
844                                      const char        *etag,
845                                      gboolean           create_backup,
846                                      GFileCreateFlags   flags,
847                                      GCancellable      *cancellable,
848                                      GError           **error)
849 {
850   GLocalFileOutputStream *stream;
851   int mode;
852   int fd;
853   char *temp_file;
854
855   if (g_cancellable_set_error_if_cancelled (cancellable, error))
856     return NULL;
857
858   temp_file = NULL;
859
860   if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
861     mode = 0600;
862   else
863     mode = 0666;
864
865   /* If the file doesn't exist, create it */
866   fd = g_open (filename,
867                O_CREAT | O_EXCL | O_WRONLY,
868                mode);
869
870   if (fd == -1 && errno == EEXIST)
871     {
872       /* The file already exists */
873       fd = handle_overwrite_open (filename, etag, create_backup, &temp_file,
874                                   cancellable, error);
875       if (fd == -1)
876         return NULL;
877     }
878   else if (fd == -1)
879     {
880       int errsv = errno;
881
882       if (errsv == EINVAL)
883         /* This must be an invalid filename, on e.g. FAT */
884         g_set_error (error, G_IO_ERROR,
885                      G_IO_ERROR_INVALID_FILENAME,
886                      _("Invalid filename"));
887       else
888         g_set_error (error, G_IO_ERROR,
889                      g_io_error_from_errno (errsv),
890                      _("Error opening file '%s': %s"),
891                      filename, g_strerror (errsv));
892       return NULL;
893     }
894   
895  
896   stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
897   stream->priv->fd = fd;
898   stream->priv->tmp_filename = temp_file;
899   if (create_backup)
900     stream->priv->backup_filename = create_backup_filename (filename);
901   stream->priv->original_filename =  g_strdup (filename);
902   
903   return G_FILE_OUTPUT_STREAM (stream);
904 }