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