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>
32 #include <glib/gstdio.h>
35 #include "gcancellable.h"
36 #include "glocalfileoutputstream.h"
37 #include "gfileinfo.h"
38 #include "glocalfileinfo.h"
42 #include "gfiledescriptorbased.h"
48 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
51 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
59 struct _GLocalFileOutputStreamPrivate {
61 char *original_filename;
62 char *backup_filename;
64 guint sync_on_close : 1;
70 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
73 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
75 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
76 G_ADD_PRIVATE (GLocalFileOutputStream)
77 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
78 g_file_descriptor_based_iface_init))
80 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
81 G_ADD_PRIVATE (GLocalFileOutputStream))
85 /* Some of the file replacement code was based on the code from gedit,
86 * relicenced to LGPL with permissions from the authors.
89 #define BACKUP_EXTENSION "~"
91 static gssize g_local_file_output_stream_write (GOutputStream *stream,
94 GCancellable *cancellable,
96 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
97 GCancellable *cancellable,
99 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
100 const char *attributes,
101 GCancellable *cancellable,
103 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
104 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
105 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
106 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
109 GCancellable *cancellable,
111 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
112 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
114 GCancellable *cancellable,
117 static int g_local_file_output_stream_get_fd (GFileDescriptorBased *stream);
121 g_local_file_output_stream_finalize (GObject *object)
123 GLocalFileOutputStream *file;
125 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
127 g_free (file->priv->tmp_filename);
128 g_free (file->priv->original_filename);
129 g_free (file->priv->backup_filename);
130 g_free (file->priv->etag);
132 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
136 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
140 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
142 gobject_class->finalize = g_local_file_output_stream_finalize;
144 stream_class->write_fn = g_local_file_output_stream_write;
145 stream_class->close_fn = g_local_file_output_stream_close;
146 file_stream_class->query_info = g_local_file_output_stream_query_info;
147 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
148 file_stream_class->tell = g_local_file_output_stream_tell;
149 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
150 file_stream_class->seek = g_local_file_output_stream_seek;
151 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
152 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
157 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
159 iface->get_fd = g_local_file_output_stream_get_fd;
164 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
166 stream->priv = g_local_file_output_stream_get_instance_private (stream);
167 stream->priv->do_close = TRUE;
171 g_local_file_output_stream_write (GOutputStream *stream,
174 GCancellable *cancellable,
177 GLocalFileOutputStream *file;
180 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
184 if (g_cancellable_set_error_if_cancelled (cancellable, error))
186 res = write (file->priv->fd, buffer, count);
194 g_set_error (error, G_IO_ERROR,
195 g_io_error_from_errno (errsv),
196 _("Error writing to file: %s"),
207 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
210 out->priv->do_close = do_close;
214 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
215 GCancellable *cancellable,
218 GLocalFileStat final_stat;
221 if (file->priv->sync_on_close &&
222 fsync (file->priv->fd) != 0)
226 g_set_error (error, G_IO_ERROR,
227 g_io_error_from_errno (errsv),
228 _("Error writing to file: %s"),
236 /* Must close before renaming on Windows, so just do the close first
237 * in all cases for now.
239 if (_fstati64 (file->priv->fd, &final_stat) == 0)
240 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
242 if (!g_close (file->priv->fd, NULL))
246 g_set_error (error, G_IO_ERROR,
247 g_io_error_from_errno (errsv),
248 _("Error closing file: %s"),
255 if (file->priv->tmp_filename)
257 /* We need to move the temp file to its final place,
258 * and possibly create the backup file
261 if (file->priv->backup_filename)
263 if (g_cancellable_set_error_if_cancelled (cancellable, error))
267 /* create original -> backup link, the original is then renamed over */
268 if (g_unlink (file->priv->backup_filename) != 0 &&
273 g_set_error (error, G_IO_ERROR,
274 G_IO_ERROR_CANT_CREATE_BACKUP,
275 _("Error removing old backup link: %s"),
280 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
282 /* link failed or is not supported, try 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"),
295 /* If link not supported, just rename... */
296 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
300 g_set_error (error, G_IO_ERROR,
301 G_IO_ERROR_CANT_CREATE_BACKUP,
302 _("Error creating backup copy: %s"),
310 if (g_cancellable_set_error_if_cancelled (cancellable, error))
313 /* tmp -> original */
314 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
318 g_set_error (error, G_IO_ERROR,
319 g_io_error_from_errno (errsv),
320 _("Error renaming temporary file: %s"),
325 g_clear_pointer (&file->priv->tmp_filename, g_free);
328 if (g_cancellable_set_error_if_cancelled (cancellable, error))
331 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
333 if (fstat (file->priv->fd, &final_stat) == 0)
334 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
336 if (!g_close (file->priv->fd, NULL))
340 g_set_error (error, G_IO_ERROR,
341 g_io_error_from_errno (errsv),
342 _("Error closing file: %s"),
353 /* A simple try to close the fd in case we fail before the actual close */
354 (void) g_close (file->priv->fd, NULL);
356 if (file->priv->tmp_filename)
357 g_unlink (file->priv->tmp_filename);
364 g_local_file_output_stream_close (GOutputStream *stream,
365 GCancellable *cancellable,
368 GLocalFileOutputStream *file;
370 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
372 if (file->priv->do_close)
373 return _g_local_file_output_stream_really_close (file,
380 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
382 GLocalFileOutputStream *file;
384 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
386 return g_strdup (file->priv->etag);
390 g_local_file_output_stream_tell (GFileOutputStream *stream)
392 GLocalFileOutputStream *file;
395 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
397 pos = lseek (file->priv->fd, 0, SEEK_CUR);
399 if (pos == (off_t)-1)
406 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
408 GLocalFileOutputStream *file;
411 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
413 pos = lseek (file->priv->fd, 0, SEEK_CUR);
415 if (pos == (off_t)-1 && errno == ESPIPE)
422 seek_type_to_lseek (GSeekType type)
439 g_local_file_output_stream_seek (GFileOutputStream *stream,
442 GCancellable *cancellable,
445 GLocalFileOutputStream *file;
448 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
450 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
452 if (pos == (off_t)-1)
456 g_set_error (error, G_IO_ERROR,
457 g_io_error_from_errno (errsv),
458 _("Error seeking in file: %s"),
467 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
469 /* We can't truncate pipes and stuff where we can't seek */
470 return g_local_file_output_stream_can_seek (stream);
474 g_local_file_output_stream_truncate (GFileOutputStream *stream,
476 GCancellable *cancellable,
479 GLocalFileOutputStream *file;
482 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
486 res = g_win32_ftruncate (file->priv->fd, size);
488 res = ftruncate (file->priv->fd, size);
497 if (g_cancellable_set_error_if_cancelled (cancellable, error))
502 g_set_error (error, G_IO_ERROR,
503 g_io_error_from_errno (errsv),
504 _("Error truncating file: %s"),
514 g_local_file_output_stream_query_info (GFileOutputStream *stream,
515 const char *attributes,
516 GCancellable *cancellable,
519 GLocalFileOutputStream *file;
521 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
523 if (g_cancellable_set_error_if_cancelled (cancellable, error))
526 return _g_local_file_info_get_from_fd (file->priv->fd,
532 _g_local_file_output_stream_new (int fd)
534 GLocalFileOutputStream *stream;
536 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
537 stream->priv->fd = fd;
538 return G_FILE_OUTPUT_STREAM (stream);
542 set_error_from_open_errno (const char *filename,
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);
563 static GFileOutputStream *
564 output_stream_open (const char *filename,
567 GCancellable *cancellable,
570 GLocalFileOutputStream *stream;
573 fd = g_open (filename, open_flags, mode);
576 set_error_from_open_errno (filename, error);
580 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
581 stream->priv->fd = fd;
582 return G_FILE_OUTPUT_STREAM (stream);
586 _g_local_file_output_stream_open (const char *filename,
588 GCancellable *cancellable,
593 if (g_cancellable_set_error_if_cancelled (cancellable, error))
596 open_flags = O_BINARY;
598 open_flags |= O_RDWR;
600 open_flags |= O_WRONLY;
602 return output_stream_open (filename, open_flags, 0666, cancellable, error);
606 mode_from_flags_or_info (GFileCreateFlags flags,
607 GFileInfo *reference_info)
609 if (flags & G_FILE_CREATE_PRIVATE)
611 else if (reference_info && g_file_info_has_attribute (reference_info, "unix::mode"))
612 return g_file_info_get_attribute_uint32 (reference_info, "unix::mode") & (~S_IFMT);
618 _g_local_file_output_stream_create (const char *filename,
620 GFileCreateFlags flags,
621 GFileInfo *reference_info,
622 GCancellable *cancellable,
628 if (g_cancellable_set_error_if_cancelled (cancellable, error))
631 mode = mode_from_flags_or_info (flags, reference_info);
633 open_flags = O_CREAT | O_EXCL | O_BINARY;
635 open_flags |= O_RDWR;
637 open_flags |= O_WRONLY;
639 return output_stream_open (filename, open_flags, mode, cancellable, error);
643 _g_local_file_output_stream_append (const char *filename,
644 GFileCreateFlags flags,
645 GCancellable *cancellable,
650 if (g_cancellable_set_error_if_cancelled (cancellable, error))
653 if (flags & G_FILE_CREATE_PRIVATE)
658 return output_stream_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode,
663 create_backup_filename (const char *filename)
665 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
668 #define BUFSIZE 8192 /* size of normal write buffer */
671 copy_file_data (gint sfd,
677 const gchar *write_buffer;
679 gssize bytes_to_write;
680 gssize bytes_written;
682 buffer = g_malloc (BUFSIZE);
686 bytes_read = read (sfd, buffer, BUFSIZE);
687 if (bytes_read == -1)
694 g_set_error (error, G_IO_ERROR,
695 g_io_error_from_errno (errsv),
696 _("Error reading from file: %s"),
702 bytes_to_write = bytes_read;
703 write_buffer = buffer;
707 bytes_written = write (dfd, write_buffer, bytes_to_write);
708 if (bytes_written == -1)
715 g_set_error (error, G_IO_ERROR,
716 g_io_error_from_errno (errsv),
717 _("Error writing to file: %s"),
723 bytes_to_write -= bytes_written;
724 write_buffer += bytes_written;
726 while (bytes_to_write > 0);
728 } while ((bytes_read != 0) && (ret == TRUE));
736 handle_overwrite_open (const char *filename,
739 gboolean create_backup,
740 char **temp_filename,
741 GFileCreateFlags flags,
742 GFileInfo *reference_info,
743 GCancellable *cancellable,
747 GLocalFileStat original_stat;
754 mode = mode_from_flags_or_info (flags, reference_info);
756 /* We only need read access to the original file if we are creating a backup.
757 * We also add O_CREATE to avoid a race if the file was just removed */
758 if (create_backup || readable)
759 open_flags = O_RDWR | O_CREAT | O_BINARY;
761 open_flags = O_WRONLY | O_CREAT | O_BINARY;
763 /* Some systems have O_NOFOLLOW, which lets us avoid some races
764 * when finding out if the file we opened was a symlink */
767 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
768 if (fd == -1 && errno == ELOOP)
770 /* Could be a symlink, or it could be a regular ELOOP error,
771 * but then the next open will fail too. */
773 fd = g_open (filename, open_flags, mode);
776 fd = g_open (filename, open_flags, mode);
777 /* This is racy, but we do it as soon as possible to minimize the race */
778 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
784 char *display_name = g_filename_display_name (filename);
785 g_set_error (error, G_IO_ERROR,
786 g_io_error_from_errno (errsv),
787 _("Error opening file '%s': %s"),
788 display_name, g_strerror (errsv));
789 g_free (display_name);
794 res = _fstati64 (fd, &original_stat);
796 res = fstat (fd, &original_stat);
802 char *display_name = g_filename_display_name (filename);
803 g_set_error (error, G_IO_ERROR,
804 g_io_error_from_errno (errsv),
805 _("Error when getting information for file '%s': %s"),
806 display_name, g_strerror (errsv));
807 g_free (display_name);
811 /* not a regular file */
812 if (!S_ISREG (original_stat.st_mode))
814 if (S_ISDIR (original_stat.st_mode))
815 g_set_error_literal (error,
817 G_IO_ERROR_IS_DIRECTORY,
818 _("Target file is a directory"));
820 g_set_error_literal (error,
822 G_IO_ERROR_NOT_REGULAR_FILE,
823 _("Target file is not a regular file"));
829 current_etag = _g_local_file_info_create_etag (&original_stat);
830 if (strcmp (etag, current_etag) != 0)
832 g_set_error_literal (error,
834 G_IO_ERROR_WRONG_ETAG,
835 _("The file was externally modified"));
836 g_free (current_etag);
839 g_free (current_etag);
842 /* We use two backup strategies.
843 * The first one (which is faster) consist in saving to a
844 * tmp file then rename the original file to the backup and the
845 * tmp file to the original name. This is fast but doesn't work
846 * when the file is a link (hard or symbolic) or when we can't
847 * write to the current dir or can't set the permissions on the
849 * The second strategy consist simply in copying the old file
850 * to a backup file and rewrite the contents of the file.
853 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
854 (!(original_stat.st_nlink > 1) && !is_symlink))
856 char *dirname, *tmp_filename;
859 dirname = g_path_get_dirname (filename);
860 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
863 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
866 g_free (tmp_filename);
867 goto fallback_strategy;
870 /* try to keep permissions (unless replacing) */
872 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
875 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
878 fchmod (tmpfd, original_stat.st_mode) == -1 ||
884 GLocalFileStat tmp_statbuf;
888 tres = _fstati64 (tmpfd, &tmp_statbuf);
890 tres = fstat (tmpfd, &tmp_statbuf);
892 /* Check that we really needed to change something */
894 original_stat.st_uid != tmp_statbuf.st_uid ||
895 original_stat.st_gid != tmp_statbuf.st_gid ||
896 original_stat.st_mode != tmp_statbuf.st_mode)
898 (void) g_close (tmpfd, NULL);
899 g_unlink (tmp_filename);
900 g_free (tmp_filename);
901 goto fallback_strategy;
905 (void) g_close (fd, NULL);
906 *temp_filename = tmp_filename;
914 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
915 struct stat tmp_statbuf;
917 char *backup_filename;
920 backup_filename = create_backup_filename (filename);
922 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
924 g_set_error_literal (error,
926 G_IO_ERROR_CANT_CREATE_BACKUP,
927 _("Backup file creation failed"));
928 g_free (backup_filename);
932 bfd = g_open (backup_filename,
933 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
934 original_stat.st_mode & 0777);
938 g_set_error_literal (error,
940 G_IO_ERROR_CANT_CREATE_BACKUP,
941 _("Backup file creation failed"));
942 g_free (backup_filename);
946 /* If needed, Try to set the group of the backup same as the
947 * original file. If this fails, set the protection
948 * bits for the group same as the protection bits for
950 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
951 if (fstat (bfd, &tmp_statbuf) != 0)
953 g_set_error_literal (error,
955 G_IO_ERROR_CANT_CREATE_BACKUP,
956 _("Backup file creation failed"));
957 g_unlink (backup_filename);
958 g_free (backup_filename);
962 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
963 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
966 (original_stat.st_mode & 0707) |
967 ((original_stat.st_mode & 07) << 3)) != 0)
969 g_set_error_literal (error,
971 G_IO_ERROR_CANT_CREATE_BACKUP,
972 _("Backup file creation failed"));
973 g_unlink (backup_filename);
974 (void) g_close (bfd, NULL);
975 g_free (backup_filename);
981 if (!copy_file_data (fd, bfd, NULL))
983 g_set_error_literal (error,
985 G_IO_ERROR_CANT_CREATE_BACKUP,
986 _("Backup file creation failed"));
987 g_unlink (backup_filename);
988 (void) g_close (bfd, NULL);
989 g_free (backup_filename);
994 (void) g_close (bfd, NULL);
995 g_free (backup_filename);
997 /* Seek back to the start of the file after the backup copy */
998 if (lseek (fd, 0, SEEK_SET) == -1)
1002 g_set_error (error, G_IO_ERROR,
1003 g_io_error_from_errno (errsv),
1004 _("Error seeking in file: %s"),
1005 g_strerror (errsv));
1010 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1012 (void) g_close (fd, NULL);
1014 if (g_unlink (filename) != 0)
1018 g_set_error (error, G_IO_ERROR,
1019 g_io_error_from_errno (errsv),
1020 _("Error removing old file: %s"),
1021 g_strerror (errsv));
1026 open_flags = O_RDWR | O_CREAT | O_BINARY;
1028 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1029 fd = g_open (filename, open_flags, mode);
1033 char *display_name = g_filename_display_name (filename);
1034 g_set_error (error, G_IO_ERROR,
1035 g_io_error_from_errno (errsv),
1036 _("Error opening file '%s': %s"),
1037 display_name, g_strerror (errsv));
1038 g_free (display_name);
1044 /* Truncate the file at the start */
1046 if (g_win32_ftruncate (fd, 0) == -1)
1048 if (ftruncate (fd, 0) == -1)
1053 g_set_error (error, G_IO_ERROR,
1054 g_io_error_from_errno (errsv),
1055 _("Error truncating file: %s"),
1056 g_strerror (errsv));
1064 (void) g_close (fd, NULL);
1070 _g_local_file_output_stream_replace (const char *filename,
1073 gboolean create_backup,
1074 GFileCreateFlags flags,
1075 GFileInfo *reference_info,
1076 GCancellable *cancellable,
1079 GLocalFileOutputStream *stream;
1083 gboolean sync_on_close;
1086 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1091 mode = mode_from_flags_or_info (flags, reference_info);
1092 sync_on_close = FALSE;
1094 /* If the file doesn't exist, create it */
1095 open_flags = O_CREAT | O_EXCL | O_BINARY;
1097 open_flags |= O_RDWR;
1099 open_flags |= O_WRONLY;
1100 fd = g_open (filename, open_flags, mode);
1102 if (fd == -1 && errno == EEXIST)
1104 /* The file already exists */
1105 fd = handle_overwrite_open (filename, readable, etag,
1106 create_backup, &temp_file,
1107 flags, reference_info,
1108 cancellable, error);
1112 /* If the final destination exists, we want to sync the newly written
1113 * file to ensure the data is on disk when we rename over the destination.
1114 * otherwise if we get a system crash we can lose both the new and the
1115 * old file on some filesystems. (I.E. those that don't guarantee the
1116 * data is written to the disk before the metadata.)
1118 sync_on_close = TRUE;
1122 set_error_from_open_errno (filename, error);
1127 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1128 stream->priv->fd = fd;
1129 stream->priv->sync_on_close = sync_on_close;
1130 stream->priv->tmp_filename = temp_file;
1132 stream->priv->backup_filename = create_backup_filename (filename);
1133 stream->priv->original_filename = g_strdup (filename);
1135 return G_FILE_OUTPUT_STREAM (stream);
1139 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1141 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1142 return stream->priv->fd;
1147 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1149 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1150 return _g_local_file_output_stream_get_fd (stream);