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 "gfileinfo.h"
41 #include "glocalfileinfo.h"
44 #include "gfiledescriptorbased.h"
50 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
53 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
61 struct _GLocalFileOutputStreamPrivate {
63 char *original_filename;
64 char *backup_filename;
66 guint sync_on_close : 1;
72 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
75 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
77 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
78 G_ADD_PRIVATE (GLocalFileOutputStream)
79 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
80 g_file_descriptor_based_iface_init))
82 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
83 G_ADD_PRIVATE (GLocalFileOutputStream))
87 /* Some of the file replacement code was based on the code from gedit,
88 * relicenced to LGPL with permissions from the authors.
91 #define BACKUP_EXTENSION "~"
93 static gssize g_local_file_output_stream_write (GOutputStream *stream,
96 GCancellable *cancellable,
98 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
99 GCancellable *cancellable,
101 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
102 const char *attributes,
103 GCancellable *cancellable,
105 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
106 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
107 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
108 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
111 GCancellable *cancellable,
113 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
114 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
116 GCancellable *cancellable,
119 static int g_local_file_output_stream_get_fd (GFileDescriptorBased *stream);
123 g_local_file_output_stream_finalize (GObject *object)
125 GLocalFileOutputStream *file;
127 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
129 g_free (file->priv->tmp_filename);
130 g_free (file->priv->original_filename);
131 g_free (file->priv->backup_filename);
132 g_free (file->priv->etag);
134 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
138 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
140 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
141 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
142 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
144 gobject_class->finalize = g_local_file_output_stream_finalize;
146 stream_class->write_fn = g_local_file_output_stream_write;
147 stream_class->close_fn = g_local_file_output_stream_close;
148 file_stream_class->query_info = g_local_file_output_stream_query_info;
149 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
150 file_stream_class->tell = g_local_file_output_stream_tell;
151 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
152 file_stream_class->seek = g_local_file_output_stream_seek;
153 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
154 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
159 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
161 iface->get_fd = g_local_file_output_stream_get_fd;
166 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
168 stream->priv = g_local_file_output_stream_get_instance_private (stream);
169 stream->priv->do_close = TRUE;
173 g_local_file_output_stream_write (GOutputStream *stream,
176 GCancellable *cancellable,
179 GLocalFileOutputStream *file;
182 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
186 if (g_cancellable_set_error_if_cancelled (cancellable, error))
188 res = write (file->priv->fd, buffer, count);
196 g_set_error (error, G_IO_ERROR,
197 g_io_error_from_errno (errsv),
198 _("Error writing to file: %s"),
209 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
212 out->priv->do_close = do_close;
216 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
217 GCancellable *cancellable,
220 GLocalFileStat final_stat;
223 if (file->priv->sync_on_close &&
224 fsync (file->priv->fd) != 0)
228 g_set_error (error, G_IO_ERROR,
229 g_io_error_from_errno (errsv),
230 _("Error writing to file: %s"),
238 /* Must close before renaming on Windows, so just do the close first
239 * in all cases for now.
241 if (_fstati64 (file->priv->fd, &final_stat) == 0)
242 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
244 if (!g_close (file->priv->fd, NULL))
248 g_set_error (error, G_IO_ERROR,
249 g_io_error_from_errno (errsv),
250 _("Error closing file: %s"),
257 if (file->priv->tmp_filename)
259 /* We need to move the temp file to its final place,
260 * and possibly create the backup file
263 if (file->priv->backup_filename)
265 if (g_cancellable_set_error_if_cancelled (cancellable, error))
269 /* create original -> backup link, the original is then renamed over */
270 if (g_unlink (file->priv->backup_filename) != 0 &&
275 g_set_error (error, G_IO_ERROR,
276 G_IO_ERROR_CANT_CREATE_BACKUP,
277 _("Error removing old backup link: %s"),
282 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
284 /* link failed or is not supported, try rename */
285 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
289 g_set_error (error, G_IO_ERROR,
290 G_IO_ERROR_CANT_CREATE_BACKUP,
291 _("Error creating backup copy: %s"),
297 /* If link not supported, just rename... */
298 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
302 g_set_error (error, G_IO_ERROR,
303 G_IO_ERROR_CANT_CREATE_BACKUP,
304 _("Error creating backup copy: %s"),
312 if (g_cancellable_set_error_if_cancelled (cancellable, error))
315 /* tmp -> original */
316 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
320 g_set_error (error, G_IO_ERROR,
321 g_io_error_from_errno (errsv),
322 _("Error renaming temporary file: %s"),
327 g_clear_pointer (&file->priv->tmp_filename, g_free);
330 if (g_cancellable_set_error_if_cancelled (cancellable, error))
333 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
335 if (fstat (file->priv->fd, &final_stat) == 0)
336 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
338 if (!g_close (file->priv->fd, NULL))
342 g_set_error (error, G_IO_ERROR,
343 g_io_error_from_errno (errsv),
344 _("Error closing file: %s"),
355 /* A simple try to close the fd in case we fail before the actual close */
356 (void) g_close (file->priv->fd, NULL);
358 if (file->priv->tmp_filename)
359 g_unlink (file->priv->tmp_filename);
366 g_local_file_output_stream_close (GOutputStream *stream,
367 GCancellable *cancellable,
370 GLocalFileOutputStream *file;
372 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
374 if (file->priv->do_close)
375 return _g_local_file_output_stream_really_close (file,
382 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
384 GLocalFileOutputStream *file;
386 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
388 return g_strdup (file->priv->etag);
392 g_local_file_output_stream_tell (GFileOutputStream *stream)
394 GLocalFileOutputStream *file;
397 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
399 pos = lseek (file->priv->fd, 0, SEEK_CUR);
401 if (pos == (off_t)-1)
408 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
410 GLocalFileOutputStream *file;
413 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
415 pos = lseek (file->priv->fd, 0, SEEK_CUR);
417 if (pos == (off_t)-1 && errno == ESPIPE)
424 seek_type_to_lseek (GSeekType type)
441 g_local_file_output_stream_seek (GFileOutputStream *stream,
444 GCancellable *cancellable,
447 GLocalFileOutputStream *file;
450 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
452 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
454 if (pos == (off_t)-1)
458 g_set_error (error, G_IO_ERROR,
459 g_io_error_from_errno (errsv),
460 _("Error seeking in file: %s"),
469 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
471 /* We can't truncate pipes and stuff where we can't seek */
472 return g_local_file_output_stream_can_seek (stream);
476 g_local_file_output_stream_truncate (GFileOutputStream *stream,
478 GCancellable *cancellable,
481 GLocalFileOutputStream *file;
484 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
488 res = g_win32_ftruncate (file->priv->fd, size);
490 res = ftruncate (file->priv->fd, size);
499 if (g_cancellable_set_error_if_cancelled (cancellable, error))
504 g_set_error (error, G_IO_ERROR,
505 g_io_error_from_errno (errsv),
506 _("Error truncating file: %s"),
516 g_local_file_output_stream_query_info (GFileOutputStream *stream,
517 const char *attributes,
518 GCancellable *cancellable,
521 GLocalFileOutputStream *file;
523 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
525 if (g_cancellable_set_error_if_cancelled (cancellable, error))
528 return _g_local_file_info_get_from_fd (file->priv->fd,
534 _g_local_file_output_stream_new (int fd)
536 GLocalFileOutputStream *stream;
538 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
539 stream->priv->fd = fd;
540 return G_FILE_OUTPUT_STREAM (stream);
544 set_error_from_open_errno (const char *filename,
550 /* This must be an invalid filename, on e.g. FAT */
551 g_set_error_literal (error, G_IO_ERROR,
552 G_IO_ERROR_INVALID_FILENAME,
553 _("Invalid filename"));
556 char *display_name = g_filename_display_name (filename);
557 g_set_error (error, G_IO_ERROR,
558 g_io_error_from_errno (errsv),
559 _("Error opening file '%s': %s"),
560 display_name, g_strerror (errsv));
561 g_free (display_name);
565 static GFileOutputStream *
566 output_stream_open (const char *filename,
569 GCancellable *cancellable,
572 GLocalFileOutputStream *stream;
575 fd = g_open (filename, open_flags, mode);
578 set_error_from_open_errno (filename, error);
582 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
583 stream->priv->fd = fd;
584 return G_FILE_OUTPUT_STREAM (stream);
588 _g_local_file_output_stream_open (const char *filename,
590 GCancellable *cancellable,
595 if (g_cancellable_set_error_if_cancelled (cancellable, error))
598 open_flags = O_BINARY;
600 open_flags |= O_RDWR;
602 open_flags |= O_WRONLY;
604 return output_stream_open (filename, open_flags, 0666, cancellable, error);
608 mode_from_flags_or_info (GFileCreateFlags flags,
609 GFileInfo *reference_info)
611 if (flags & G_FILE_CREATE_PRIVATE)
613 else if (reference_info && g_file_info_has_attribute (reference_info, "unix::mode"))
614 return g_file_info_get_attribute_uint32 (reference_info, "unix::mode") & (~S_IFMT);
620 _g_local_file_output_stream_create (const char *filename,
622 GFileCreateFlags flags,
623 GFileInfo *reference_info,
624 GCancellable *cancellable,
630 if (g_cancellable_set_error_if_cancelled (cancellable, error))
633 mode = mode_from_flags_or_info (flags, reference_info);
635 open_flags = O_CREAT | O_EXCL | O_BINARY;
637 open_flags |= O_RDWR;
639 open_flags |= O_WRONLY;
641 return output_stream_open (filename, open_flags, mode, cancellable, error);
645 _g_local_file_output_stream_append (const char *filename,
646 GFileCreateFlags flags,
647 GCancellable *cancellable,
652 if (g_cancellable_set_error_if_cancelled (cancellable, error))
655 if (flags & G_FILE_CREATE_PRIVATE)
660 return output_stream_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode,
665 create_backup_filename (const char *filename)
667 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
670 #define BUFSIZE 8192 /* size of normal write buffer */
673 copy_file_data (gint sfd,
679 const gchar *write_buffer;
681 gssize bytes_to_write;
682 gssize bytes_written;
684 buffer = g_malloc (BUFSIZE);
688 bytes_read = read (sfd, buffer, BUFSIZE);
689 if (bytes_read == -1)
696 g_set_error (error, G_IO_ERROR,
697 g_io_error_from_errno (errsv),
698 _("Error reading from file: %s"),
704 bytes_to_write = bytes_read;
705 write_buffer = buffer;
709 bytes_written = write (dfd, write_buffer, bytes_to_write);
710 if (bytes_written == -1)
717 g_set_error (error, G_IO_ERROR,
718 g_io_error_from_errno (errsv),
719 _("Error writing to file: %s"),
725 bytes_to_write -= bytes_written;
726 write_buffer += bytes_written;
728 while (bytes_to_write > 0);
730 } while ((bytes_read != 0) && (ret == TRUE));
738 handle_overwrite_open (const char *filename,
741 gboolean create_backup,
742 char **temp_filename,
743 GFileCreateFlags flags,
744 GFileInfo *reference_info,
745 GCancellable *cancellable,
749 GLocalFileStat original_stat;
756 mode = mode_from_flags_or_info (flags, reference_info);
758 /* We only need read access to the original file if we are creating a backup.
759 * We also add O_CREATE to avoid a race if the file was just removed */
760 if (create_backup || readable)
761 open_flags = O_RDWR | O_CREAT | O_BINARY;
763 open_flags = O_WRONLY | O_CREAT | O_BINARY;
765 /* Some systems have O_NOFOLLOW, which lets us avoid some races
766 * when finding out if the file we opened was a symlink */
769 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
770 if (fd == -1 && errno == ELOOP)
772 /* Could be a symlink, or it could be a regular ELOOP error,
773 * but then the next open will fail too. */
775 fd = g_open (filename, open_flags, mode);
778 fd = g_open (filename, open_flags, mode);
779 /* This is racy, but we do it as soon as possible to minimize the race */
780 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
786 char *display_name = g_filename_display_name (filename);
787 g_set_error (error, G_IO_ERROR,
788 g_io_error_from_errno (errsv),
789 _("Error opening file '%s': %s"),
790 display_name, g_strerror (errsv));
791 g_free (display_name);
796 res = _fstati64 (fd, &original_stat);
798 res = fstat (fd, &original_stat);
804 char *display_name = g_filename_display_name (filename);
805 g_set_error (error, G_IO_ERROR,
806 g_io_error_from_errno (errsv),
807 _("Error when getting information for file '%s': %s"),
808 display_name, g_strerror (errsv));
809 g_free (display_name);
813 /* not a regular file */
814 if (!S_ISREG (original_stat.st_mode))
816 if (S_ISDIR (original_stat.st_mode))
817 g_set_error_literal (error,
819 G_IO_ERROR_IS_DIRECTORY,
820 _("Target file is a directory"));
822 g_set_error_literal (error,
824 G_IO_ERROR_NOT_REGULAR_FILE,
825 _("Target file is not a regular file"));
831 current_etag = _g_local_file_info_create_etag (&original_stat);
832 if (strcmp (etag, current_etag) != 0)
834 g_set_error_literal (error,
836 G_IO_ERROR_WRONG_ETAG,
837 _("The file was externally modified"));
838 g_free (current_etag);
841 g_free (current_etag);
844 /* We use two backup strategies.
845 * The first one (which is faster) consist in saving to a
846 * tmp file then rename the original file to the backup and the
847 * tmp file to the original name. This is fast but doesn't work
848 * when the file is a link (hard or symbolic) or when we can't
849 * write to the current dir or can't set the permissions on the
851 * The second strategy consist simply in copying the old file
852 * to a backup file and rewrite the contents of the file.
855 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
856 (!(original_stat.st_nlink > 1) && !is_symlink))
858 char *dirname, *tmp_filename;
861 dirname = g_path_get_dirname (filename);
862 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
865 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
868 g_free (tmp_filename);
869 goto fallback_strategy;
872 /* try to keep permissions (unless replacing) */
874 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
877 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
880 fchmod (tmpfd, original_stat.st_mode) == -1 ||
886 GLocalFileStat tmp_statbuf;
890 tres = _fstati64 (tmpfd, &tmp_statbuf);
892 tres = fstat (tmpfd, &tmp_statbuf);
894 /* Check that we really needed to change something */
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)
900 (void) g_close (tmpfd, NULL);
901 g_unlink (tmp_filename);
902 g_free (tmp_filename);
903 goto fallback_strategy;
907 (void) g_close (fd, NULL);
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);
976 (void) g_close (bfd, NULL);
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);
990 (void) g_close (bfd, NULL);
991 g_free (backup_filename);
996 (void) g_close (bfd, NULL);
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)
1014 (void) g_close (fd, NULL);
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));
1066 (void) g_close (fd, NULL);
1072 _g_local_file_output_stream_replace (const char *filename,
1075 gboolean create_backup,
1076 GFileCreateFlags flags,
1077 GFileInfo *reference_info,
1078 GCancellable *cancellable,
1081 GLocalFileOutputStream *stream;
1085 gboolean sync_on_close;
1088 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1093 mode = mode_from_flags_or_info (flags, reference_info);
1094 sync_on_close = FALSE;
1096 /* If the file doesn't exist, create it */
1097 open_flags = O_CREAT | O_EXCL | O_BINARY;
1099 open_flags |= O_RDWR;
1101 open_flags |= O_WRONLY;
1102 fd = g_open (filename, open_flags, mode);
1104 if (fd == -1 && errno == EEXIST)
1106 /* The file already exists */
1107 fd = handle_overwrite_open (filename, readable, etag,
1108 create_backup, &temp_file,
1109 flags, reference_info,
1110 cancellable, error);
1114 /* If the final destination exists, we want to sync the newly written
1115 * file to ensure the data is on disk when we rename over the destination.
1116 * otherwise if we get a system crash we can lose both the new and the
1117 * old file on some filesystems. (I.E. those that don't guarantee the
1118 * data is written to the disk before the metadata.)
1120 sync_on_close = TRUE;
1124 set_error_from_open_errno (filename, error);
1129 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1130 stream->priv->fd = fd;
1131 stream->priv->sync_on_close = sync_on_close;
1132 stream->priv->tmp_filename = temp_file;
1134 stream->priv->backup_filename = create_backup_filename (filename);
1135 stream->priv->original_filename = g_strdup (filename);
1137 return G_FILE_OUTPUT_STREAM (stream);
1141 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1143 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1144 return stream->priv->fd;
1149 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1151 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1152 return _g_local_file_output_stream_get_fd (stream);