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