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