1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
35 #include <glib/gstdio.h>
38 #include "gcancellable.h"
39 #include "glocalfileoutputstream.h"
40 #include "glocalfileinfo.h"
45 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
48 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
58 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
59 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
61 /* Some of the file replacement code was based on the code from gedit,
62 * relicenced to LGPL with permissions from the authors.
65 #define BACKUP_EXTENSION "~"
67 struct _GLocalFileOutputStreamPrivate {
69 char *original_filename;
70 char *backup_filename;
72 guint sync_on_close : 1;
77 static gssize g_local_file_output_stream_write (GOutputStream *stream,
80 GCancellable *cancellable,
82 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
83 GCancellable *cancellable,
85 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
86 const char *attributes,
87 GCancellable *cancellable,
89 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
90 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
91 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
92 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
95 GCancellable *cancellable,
97 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
98 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
100 GCancellable *cancellable,
104 g_local_file_output_stream_finalize (GObject *object)
106 GLocalFileOutputStream *file;
108 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
110 g_free (file->priv->tmp_filename);
111 g_free (file->priv->original_filename);
112 g_free (file->priv->backup_filename);
113 g_free (file->priv->etag);
115 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
119 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
122 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
123 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
125 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
127 gobject_class->finalize = g_local_file_output_stream_finalize;
129 stream_class->write_fn = g_local_file_output_stream_write;
130 stream_class->close_fn = g_local_file_output_stream_close;
131 file_stream_class->query_info = g_local_file_output_stream_query_info;
132 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
133 file_stream_class->tell = g_local_file_output_stream_tell;
134 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
135 file_stream_class->seek = g_local_file_output_stream_seek;
136 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
137 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
141 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
143 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
144 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
145 GLocalFileOutputStreamPrivate);
146 stream->priv->do_close = TRUE;
150 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *out)
152 return out->priv->fd;
156 g_local_file_output_stream_write (GOutputStream *stream,
159 GCancellable *cancellable,
162 GLocalFileOutputStream *file;
165 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
169 if (g_cancellable_set_error_if_cancelled (cancellable, error))
171 res = write (file->priv->fd, buffer, count);
179 g_set_error (error, G_IO_ERROR,
180 g_io_error_from_errno (errsv),
181 _("Error writing to file: %s"),
192 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
195 out->priv->do_close = do_close;
199 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
200 GCancellable *cancellable,
203 GLocalFileStat final_stat;
207 if (file->priv->sync_on_close &&
208 fsync (file->priv->fd) != 0)
212 g_set_error (error, G_IO_ERROR,
213 g_io_error_from_errno (errno),
214 _("Error writing to file: %s"),
222 /* Must close before renaming on Windows, so just do the close first
223 * in all cases for now.
225 if (_fstati64 (file->priv->fd, &final_stat) == 0)
226 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
228 res = close (file->priv->fd);
233 g_set_error (error, G_IO_ERROR,
234 g_io_error_from_errno (errsv),
235 _("Error closing file: %s"),
242 if (file->priv->tmp_filename)
244 /* We need to move the temp file to its final place,
245 * and possibly create the backup file
248 if (file->priv->backup_filename)
250 if (g_cancellable_set_error_if_cancelled (cancellable, error))
254 /* create original -> backup link, the original is then renamed over */
255 if (g_unlink (file->priv->backup_filename) != 0 &&
260 g_set_error (error, G_IO_ERROR,
261 G_IO_ERROR_CANT_CREATE_BACKUP,
262 _("Error removing old backup link: %s"),
267 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
269 /* link failed or is not supported, try rename */
270 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
274 g_set_error (error, G_IO_ERROR,
275 G_IO_ERROR_CANT_CREATE_BACKUP,
276 _("Error creating backup copy: %s"),
282 /* If link not supported, just rename... */
283 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
287 g_set_error (error, G_IO_ERROR,
288 G_IO_ERROR_CANT_CREATE_BACKUP,
289 _("Error creating backup copy: %s"),
297 if (g_cancellable_set_error_if_cancelled (cancellable, error))
300 /* tmp -> original */
301 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
305 g_set_error (error, G_IO_ERROR,
306 g_io_error_from_errno (errno),
307 _("Error renaming temporary file: %s"),
313 if (g_cancellable_set_error_if_cancelled (cancellable, error))
316 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
318 if (fstat (file->priv->fd, &final_stat) == 0)
319 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
323 res = close (file->priv->fd);
328 g_set_error (error, G_IO_ERROR,
329 g_io_error_from_errno (errsv),
330 _("Error closing file: %s"),
347 /* A simple try to close the fd in case we fail before the actual close */
348 close (file->priv->fd);
355 g_local_file_output_stream_close (GOutputStream *stream,
356 GCancellable *cancellable,
359 GLocalFileOutputStream *file;
361 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
363 if (file->priv->do_close)
364 return _g_local_file_output_stream_really_close (file,
371 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
373 GLocalFileOutputStream *file;
375 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
377 return g_strdup (file->priv->etag);
381 g_local_file_output_stream_tell (GFileOutputStream *stream)
383 GLocalFileOutputStream *file;
386 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
388 pos = lseek (file->priv->fd, 0, SEEK_CUR);
390 if (pos == (off_t)-1)
397 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
399 GLocalFileOutputStream *file;
402 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
404 pos = lseek (file->priv->fd, 0, SEEK_CUR);
406 if (pos == (off_t)-1 && errno == ESPIPE)
413 seek_type_to_lseek (GSeekType type)
430 g_local_file_output_stream_seek (GFileOutputStream *stream,
433 GCancellable *cancellable,
436 GLocalFileOutputStream *file;
439 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
441 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
443 if (pos == (off_t)-1)
447 g_set_error (error, G_IO_ERROR,
448 g_io_error_from_errno (errsv),
449 _("Error seeking in file: %s"),
458 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
460 /* We can't truncate pipes and stuff where we can't seek */
461 return g_local_file_output_stream_can_seek (stream);
465 g_local_file_output_stream_truncate (GFileOutputStream *stream,
467 GCancellable *cancellable,
470 GLocalFileOutputStream *file;
473 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
477 res = g_win32_ftruncate (file->priv->fd, size);
479 res = ftruncate (file->priv->fd, size);
488 if (g_cancellable_set_error_if_cancelled (cancellable, error))
493 g_set_error (error, G_IO_ERROR,
494 g_io_error_from_errno (errsv),
495 _("Error truncating file: %s"),
505 g_local_file_output_stream_query_info (GFileOutputStream *stream,
506 const char *attributes,
507 GCancellable *cancellable,
510 GLocalFileOutputStream *file;
512 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
514 if (g_cancellable_set_error_if_cancelled (cancellable, error))
517 return _g_local_file_info_get_from_fd (file->priv->fd,
523 _g_local_file_output_stream_open (const char *filename,
525 GCancellable *cancellable,
528 GLocalFileOutputStream *stream;
533 if (g_cancellable_set_error_if_cancelled (cancellable, error))
536 open_flags = O_BINARY;
538 open_flags |= O_RDWR;
540 open_flags |= O_WRONLY;
542 fd = g_open (filename, open_flags, 0666);
548 /* This must be an invalid filename, on e.g. FAT */
549 g_set_error_literal (error, G_IO_ERROR,
550 G_IO_ERROR_INVALID_FILENAME,
551 _("Invalid filename"));
554 char *display_name = g_filename_display_name (filename);
555 g_set_error (error, G_IO_ERROR,
556 g_io_error_from_errno (errsv),
557 _("Error opening file '%s': %s"),
558 display_name, g_strerror (errsv));
559 g_free (display_name);
564 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
565 stream->priv->fd = fd;
566 return G_FILE_OUTPUT_STREAM (stream);
570 _g_local_file_output_stream_create (const char *filename,
572 GFileCreateFlags flags,
573 GCancellable *cancellable,
576 GLocalFileOutputStream *stream;
581 if (g_cancellable_set_error_if_cancelled (cancellable, error))
584 if (flags & G_FILE_CREATE_PRIVATE)
589 open_flags = O_CREAT | O_EXCL | O_BINARY;
591 open_flags |= O_RDWR;
593 open_flags |= O_WRONLY;
595 fd = g_open (filename, open_flags, mode);
601 /* This must be an invalid filename, on e.g. FAT */
602 g_set_error_literal (error, G_IO_ERROR,
603 G_IO_ERROR_INVALID_FILENAME,
604 _("Invalid filename"));
607 char *display_name = g_filename_display_name (filename);
608 g_set_error (error, G_IO_ERROR,
609 g_io_error_from_errno (errsv),
610 _("Error opening file '%s': %s"),
611 display_name, g_strerror (errsv));
612 g_free (display_name);
617 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
618 stream->priv->fd = fd;
619 return G_FILE_OUTPUT_STREAM (stream);
623 _g_local_file_output_stream_append (const char *filename,
624 GFileCreateFlags flags,
625 GCancellable *cancellable,
628 GLocalFileOutputStream *stream;
632 if (g_cancellable_set_error_if_cancelled (cancellable, error))
635 if (flags & G_FILE_CREATE_PRIVATE)
640 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
646 /* This must be an invalid filename, on e.g. FAT */
647 g_set_error_literal (error, G_IO_ERROR,
648 G_IO_ERROR_INVALID_FILENAME,
649 _("Invalid filename"));
652 char *display_name = g_filename_display_name (filename);
653 g_set_error (error, G_IO_ERROR,
654 g_io_error_from_errno (errsv),
655 _("Error opening file '%s': %s"),
656 display_name, g_strerror (errsv));
657 g_free (display_name);
662 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
663 stream->priv->fd = fd;
665 return G_FILE_OUTPUT_STREAM (stream);
669 create_backup_filename (const char *filename)
671 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
674 #define BUFSIZE 8192 /* size of normal write buffer */
677 copy_file_data (gint sfd,
683 const gchar *write_buffer;
685 gssize bytes_to_write;
686 gssize bytes_written;
688 buffer = g_malloc (BUFSIZE);
692 bytes_read = read (sfd, buffer, BUFSIZE);
693 if (bytes_read == -1)
700 g_set_error (error, G_IO_ERROR,
701 g_io_error_from_errno (errsv),
702 _("Error reading from file: %s"),
708 bytes_to_write = bytes_read;
709 write_buffer = buffer;
713 bytes_written = write (dfd, write_buffer, bytes_to_write);
714 if (bytes_written == -1)
721 g_set_error (error, G_IO_ERROR,
722 g_io_error_from_errno (errsv),
723 _("Error writing to file: %s"),
729 bytes_to_write -= bytes_written;
730 write_buffer += bytes_written;
732 while (bytes_to_write > 0);
734 } while ((bytes_read != 0) && (ret == TRUE));
742 handle_overwrite_open (const char *filename,
745 gboolean create_backup,
746 char **temp_filename,
747 GFileCreateFlags flags,
748 GCancellable *cancellable,
752 GLocalFileStat original_stat;
759 if (flags & G_FILE_CREATE_PRIVATE)
764 /* We only need read access to the original file if we are creating a backup.
765 * We also add O_CREATE to avoid a race if the file was just removed */
766 if (create_backup || readable)
767 open_flags = O_RDWR | O_CREAT | O_BINARY;
769 open_flags = O_WRONLY | O_CREAT | O_BINARY;
771 /* Some systems have O_NOFOLLOW, which lets us avoid some races
772 * when finding out if the file we opened was a symlink */
775 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
776 if (fd == -1 && errno == ELOOP)
778 /* Could be a symlink, or it could be a regular ELOOP error,
779 * but then the next open will fail too. */
781 fd = g_open (filename, open_flags, mode);
784 fd = g_open (filename, open_flags, mode);
785 /* This is racy, but we do it as soon as possible to minimize the race */
786 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
792 char *display_name = g_filename_display_name (filename);
793 g_set_error (error, G_IO_ERROR,
794 g_io_error_from_errno (errsv),
795 _("Error opening file '%s': %s"),
796 display_name, g_strerror (errsv));
797 g_free (display_name);
802 res = _fstati64 (fd, &original_stat);
804 res = fstat (fd, &original_stat);
810 char *display_name = g_filename_display_name (filename);
811 g_set_error (error, G_IO_ERROR,
812 g_io_error_from_errno (errsv),
813 _("Error stating file '%s': %s"),
814 display_name, g_strerror (errsv));
815 g_free (display_name);
819 /* not a regular file */
820 if (!S_ISREG (original_stat.st_mode))
822 if (S_ISDIR (original_stat.st_mode))
823 g_set_error_literal (error,
825 G_IO_ERROR_IS_DIRECTORY,
826 _("Target file is a directory"));
828 g_set_error_literal (error,
830 G_IO_ERROR_NOT_REGULAR_FILE,
831 _("Target file is not a regular file"));
837 current_etag = _g_local_file_info_create_etag (&original_stat);
838 if (strcmp (etag, current_etag) != 0)
840 g_set_error_literal (error,
842 G_IO_ERROR_WRONG_ETAG,
843 _("The file was externally modified"));
844 g_free (current_etag);
847 g_free (current_etag);
850 /* We use two backup strategies.
851 * The first one (which is faster) consist in saving to a
852 * tmp file then rename the original file to the backup and the
853 * tmp file to the original name. This is fast but doesn't work
854 * when the file is a link (hard or symbolic) or when we can't
855 * write to the current dir or can't set the permissions on the
857 * The second strategy consist simply in copying the old file
858 * to a backup file and rewrite the contents of the file.
861 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
862 (!(original_stat.st_nlink > 1) && !is_symlink))
864 char *dirname, *tmp_filename;
867 dirname = g_path_get_dirname (filename);
868 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
871 tmpfd = g_mkstemp (tmp_filename);
874 g_free (tmp_filename);
875 goto fallback_strategy;
878 /* try to keep permissions (unless replacing) */
880 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
883 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
886 fchmod (tmpfd, original_stat.st_mode) == -1 ||
892 struct stat tmp_statbuf;
894 /* Check that we really needed to change something */
895 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
896 original_stat.st_uid != tmp_statbuf.st_uid ||
897 original_stat.st_gid != tmp_statbuf.st_gid ||
898 original_stat.st_mode != tmp_statbuf.st_mode)
901 g_unlink (tmp_filename);
902 g_free (tmp_filename);
903 goto fallback_strategy;
908 *temp_filename = tmp_filename;
916 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
917 struct stat tmp_statbuf;
919 char *backup_filename;
922 backup_filename = create_backup_filename (filename);
924 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
926 g_set_error_literal (error,
928 G_IO_ERROR_CANT_CREATE_BACKUP,
929 _("Backup file creation failed"));
930 g_free (backup_filename);
934 bfd = g_open (backup_filename,
935 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
936 original_stat.st_mode & 0777);
940 g_set_error_literal (error,
942 G_IO_ERROR_CANT_CREATE_BACKUP,
943 _("Backup file creation failed"));
944 g_free (backup_filename);
948 /* If needed, Try to set the group of the backup same as the
949 * original file. If this fails, set the protection
950 * bits for the group same as the protection bits for
952 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
953 if (fstat (bfd, &tmp_statbuf) != 0)
955 g_set_error_literal (error,
957 G_IO_ERROR_CANT_CREATE_BACKUP,
958 _("Backup file creation failed"));
959 g_unlink (backup_filename);
960 g_free (backup_filename);
964 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
965 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
968 (original_stat.st_mode & 0707) |
969 ((original_stat.st_mode & 07) << 3)) != 0)
971 g_set_error_literal (error,
973 G_IO_ERROR_CANT_CREATE_BACKUP,
974 _("Backup file creation failed"));
975 g_unlink (backup_filename);
977 g_free (backup_filename);
983 if (!copy_file_data (fd, bfd, NULL))
985 g_set_error_literal (error,
987 G_IO_ERROR_CANT_CREATE_BACKUP,
988 _("Backup file creation failed"));
989 g_unlink (backup_filename);
991 g_free (backup_filename);
997 g_free (backup_filename);
999 /* Seek back to the start of the file after the backup copy */
1000 if (lseek (fd, 0, SEEK_SET) == -1)
1004 g_set_error (error, G_IO_ERROR,
1005 g_io_error_from_errno (errsv),
1006 _("Error seeking in file: %s"),
1007 g_strerror (errsv));
1012 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1016 if (g_unlink (filename) != 0)
1020 g_set_error (error, G_IO_ERROR,
1021 g_io_error_from_errno (errsv),
1022 _("Error removing old file: %s"),
1023 g_strerror (errsv));
1028 open_flags = O_RDWR | O_CREAT | O_BINARY;
1030 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1031 fd = g_open (filename, open_flags, mode);
1035 char *display_name = g_filename_display_name (filename);
1036 g_set_error (error, G_IO_ERROR,
1037 g_io_error_from_errno (errsv),
1038 _("Error opening file '%s': %s"),
1039 display_name, g_strerror (errsv));
1040 g_free (display_name);
1046 /* Truncate the file at the start */
1048 if (g_win32_ftruncate (fd, 0) == -1)
1050 if (ftruncate (fd, 0) == -1)
1055 g_set_error (error, G_IO_ERROR,
1056 g_io_error_from_errno (errsv),
1057 _("Error truncating file: %s"),
1058 g_strerror (errsv));
1072 _g_local_file_output_stream_replace (const char *filename,
1075 gboolean create_backup,
1076 GFileCreateFlags flags,
1077 GCancellable *cancellable,
1080 GLocalFileOutputStream *stream;
1084 gboolean sync_on_close;
1087 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1092 if (flags & G_FILE_CREATE_PRIVATE)
1096 sync_on_close = FALSE;
1098 /* If the file doesn't exist, create it */
1099 open_flags = O_CREAT | O_EXCL | O_BINARY;
1101 open_flags |= O_RDWR;
1103 open_flags |= O_WRONLY;
1104 fd = g_open (filename, open_flags, mode);
1106 if (fd == -1 && errno == EEXIST)
1108 /* The file already exists */
1109 fd = handle_overwrite_open (filename, readable, etag,
1110 create_backup, &temp_file,
1111 flags, cancellable, error);
1115 /* If the final destination exists, we want to sync the newly written
1116 * file to ensure the data is on disk when we rename over the destination.
1117 * otherwise if we get a system crash we can lose both the new and the
1118 * old file on some filesystems. (I.E. those that don't guarantee the
1119 * data is written to the disk before the metadata.)
1121 sync_on_close = TRUE;
1127 if (errsv == EINVAL)
1128 /* This must be an invalid filename, on e.g. FAT */
1129 g_set_error_literal (error, G_IO_ERROR,
1130 G_IO_ERROR_INVALID_FILENAME,
1131 _("Invalid filename"));
1134 char *display_name = g_filename_display_name (filename);
1135 g_set_error (error, G_IO_ERROR,
1136 g_io_error_from_errno (errsv),
1137 _("Error opening file '%s': %s"),
1138 display_name, g_strerror (errsv));
1139 g_free (display_name);
1145 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1146 stream->priv->fd = fd;
1147 stream->priv->sync_on_close = sync_on_close;
1148 stream->priv->tmp_filename = temp_file;
1150 stream->priv->backup_filename = create_backup_filename (filename);
1151 stream->priv->original_filename = g_strdup (filename);
1153 return G_FILE_OUTPUT_STREAM (stream);