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)
63 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
66 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
68 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
69 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
70 g_file_descriptor_based_iface_init)
73 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,);
77 /* Some of the file replacement code was based on the code from gedit,
78 * relicenced to LGPL with permissions from the authors.
81 #define BACKUP_EXTENSION "~"
83 struct _GLocalFileOutputStreamPrivate {
85 char *original_filename;
86 char *backup_filename;
88 guint sync_on_close : 1;
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);
139 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
141 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
142 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
143 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
145 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
147 gobject_class->finalize = g_local_file_output_stream_finalize;
149 stream_class->write_fn = g_local_file_output_stream_write;
150 stream_class->close_fn = g_local_file_output_stream_close;
151 file_stream_class->query_info = g_local_file_output_stream_query_info;
152 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
153 file_stream_class->tell = g_local_file_output_stream_tell;
154 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
155 file_stream_class->seek = g_local_file_output_stream_seek;
156 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
157 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
162 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
164 iface->get_fd = g_local_file_output_stream_get_fd;
169 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
171 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
172 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
173 GLocalFileOutputStreamPrivate);
174 stream->priv->do_close = TRUE;
178 g_local_file_output_stream_write (GOutputStream *stream,
181 GCancellable *cancellable,
184 GLocalFileOutputStream *file;
187 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
191 if (g_cancellable_set_error_if_cancelled (cancellable, error))
193 res = write (file->priv->fd, buffer, count);
201 g_set_error (error, G_IO_ERROR,
202 g_io_error_from_errno (errsv),
203 _("Error writing to file: %s"),
214 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
217 out->priv->do_close = do_close;
221 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
222 GCancellable *cancellable,
225 GLocalFileStat final_stat;
228 if (file->priv->sync_on_close &&
229 fsync (file->priv->fd) != 0)
233 g_set_error (error, G_IO_ERROR,
234 g_io_error_from_errno (errsv),
235 _("Error writing to file: %s"),
243 /* Must close before renaming on Windows, so just do the close first
244 * in all cases for now.
246 if (_fstati64 (file->priv->fd, &final_stat) == 0)
247 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
249 if (!g_close (file->priv->fd, NULL))
253 g_set_error (error, G_IO_ERROR,
254 g_io_error_from_errno (errsv),
255 _("Error closing file: %s"),
262 if (file->priv->tmp_filename)
264 /* We need to move the temp file to its final place,
265 * and possibly create the backup file
268 if (file->priv->backup_filename)
270 if (g_cancellable_set_error_if_cancelled (cancellable, error))
274 /* create original -> backup link, the original is then renamed over */
275 if (g_unlink (file->priv->backup_filename) != 0 &&
280 g_set_error (error, G_IO_ERROR,
281 G_IO_ERROR_CANT_CREATE_BACKUP,
282 _("Error removing old backup link: %s"),
287 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
289 /* link failed or is not supported, try rename */
290 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
294 g_set_error (error, G_IO_ERROR,
295 G_IO_ERROR_CANT_CREATE_BACKUP,
296 _("Error creating backup copy: %s"),
302 /* If link not supported, just rename... */
303 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
307 g_set_error (error, G_IO_ERROR,
308 G_IO_ERROR_CANT_CREATE_BACKUP,
309 _("Error creating backup copy: %s"),
317 if (g_cancellable_set_error_if_cancelled (cancellable, error))
320 /* tmp -> original */
321 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
325 g_set_error (error, G_IO_ERROR,
326 g_io_error_from_errno (errsv),
327 _("Error renaming temporary file: %s"),
332 g_clear_pointer (&file->priv->tmp_filename, g_free);
335 if (g_cancellable_set_error_if_cancelled (cancellable, error))
338 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
340 if (fstat (file->priv->fd, &final_stat) == 0)
341 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
343 if (!g_close (file->priv->fd, NULL))
347 g_set_error (error, G_IO_ERROR,
348 g_io_error_from_errno (errsv),
349 _("Error closing file: %s"),
360 /* A simple try to close the fd in case we fail before the actual close */
361 (void) g_close (file->priv->fd, NULL);
363 if (file->priv->tmp_filename)
364 g_unlink (file->priv->tmp_filename);
371 g_local_file_output_stream_close (GOutputStream *stream,
372 GCancellable *cancellable,
375 GLocalFileOutputStream *file;
377 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
379 if (file->priv->do_close)
380 return _g_local_file_output_stream_really_close (file,
387 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
389 GLocalFileOutputStream *file;
391 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
393 return g_strdup (file->priv->etag);
397 g_local_file_output_stream_tell (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)
413 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
415 GLocalFileOutputStream *file;
418 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
420 pos = lseek (file->priv->fd, 0, SEEK_CUR);
422 if (pos == (off_t)-1 && errno == ESPIPE)
429 seek_type_to_lseek (GSeekType type)
446 g_local_file_output_stream_seek (GFileOutputStream *stream,
449 GCancellable *cancellable,
452 GLocalFileOutputStream *file;
455 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
457 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
459 if (pos == (off_t)-1)
463 g_set_error (error, G_IO_ERROR,
464 g_io_error_from_errno (errsv),
465 _("Error seeking in file: %s"),
474 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
476 /* We can't truncate pipes and stuff where we can't seek */
477 return g_local_file_output_stream_can_seek (stream);
481 g_local_file_output_stream_truncate (GFileOutputStream *stream,
483 GCancellable *cancellable,
486 GLocalFileOutputStream *file;
489 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
493 res = g_win32_ftruncate (file->priv->fd, size);
495 res = ftruncate (file->priv->fd, size);
504 if (g_cancellable_set_error_if_cancelled (cancellable, error))
509 g_set_error (error, G_IO_ERROR,
510 g_io_error_from_errno (errsv),
511 _("Error truncating file: %s"),
521 g_local_file_output_stream_query_info (GFileOutputStream *stream,
522 const char *attributes,
523 GCancellable *cancellable,
526 GLocalFileOutputStream *file;
528 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
530 if (g_cancellable_set_error_if_cancelled (cancellable, error))
533 return _g_local_file_info_get_from_fd (file->priv->fd,
539 _g_local_file_output_stream_new (int fd)
541 GLocalFileOutputStream *stream;
543 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
544 stream->priv->fd = fd;
545 return G_FILE_OUTPUT_STREAM (stream);
549 set_error_from_open_errno (const char *filename,
555 /* This must be an invalid filename, on e.g. FAT */
556 g_set_error_literal (error, G_IO_ERROR,
557 G_IO_ERROR_INVALID_FILENAME,
558 _("Invalid filename"));
561 char *display_name = g_filename_display_name (filename);
562 g_set_error (error, G_IO_ERROR,
563 g_io_error_from_errno (errsv),
564 _("Error opening file '%s': %s"),
565 display_name, g_strerror (errsv));
566 g_free (display_name);
570 static GFileOutputStream *
571 output_stream_open (const char *filename,
574 GCancellable *cancellable,
577 GLocalFileOutputStream *stream;
580 fd = g_open (filename, open_flags, mode);
583 set_error_from_open_errno (filename, error);
587 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
588 stream->priv->fd = fd;
589 return G_FILE_OUTPUT_STREAM (stream);
593 _g_local_file_output_stream_open (const char *filename,
595 GCancellable *cancellable,
600 if (g_cancellable_set_error_if_cancelled (cancellable, error))
603 open_flags = O_BINARY;
605 open_flags |= O_RDWR;
607 open_flags |= O_WRONLY;
609 return output_stream_open (filename, open_flags, 0666, cancellable, error);
613 mode_from_flags_or_info (GFileCreateFlags flags,
614 GFileInfo *reference_info)
616 if (flags & G_FILE_CREATE_PRIVATE)
618 else if (reference_info && g_file_info_has_attribute (reference_info, "unix::mode"))
619 return g_file_info_get_attribute_uint32 (reference_info, "unix::mode") & (~S_IFMT);
625 _g_local_file_output_stream_create (const char *filename,
627 GFileCreateFlags flags,
628 GFileInfo *reference_info,
629 GCancellable *cancellable,
635 if (g_cancellable_set_error_if_cancelled (cancellable, error))
638 mode = mode_from_flags_or_info (flags, reference_info);
640 open_flags = O_CREAT | O_EXCL | O_BINARY;
642 open_flags |= O_RDWR;
644 open_flags |= O_WRONLY;
646 return output_stream_open (filename, open_flags, mode, cancellable, error);
650 _g_local_file_output_stream_append (const char *filename,
651 GFileCreateFlags flags,
652 GCancellable *cancellable,
657 if (g_cancellable_set_error_if_cancelled (cancellable, error))
660 if (flags & G_FILE_CREATE_PRIVATE)
665 return output_stream_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode,
670 create_backup_filename (const char *filename)
672 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
675 #define BUFSIZE 8192 /* size of normal write buffer */
678 copy_file_data (gint sfd,
684 const gchar *write_buffer;
686 gssize bytes_to_write;
687 gssize bytes_written;
689 buffer = g_malloc (BUFSIZE);
693 bytes_read = read (sfd, buffer, BUFSIZE);
694 if (bytes_read == -1)
701 g_set_error (error, G_IO_ERROR,
702 g_io_error_from_errno (errsv),
703 _("Error reading from file: %s"),
709 bytes_to_write = bytes_read;
710 write_buffer = buffer;
714 bytes_written = write (dfd, write_buffer, bytes_to_write);
715 if (bytes_written == -1)
722 g_set_error (error, G_IO_ERROR,
723 g_io_error_from_errno (errsv),
724 _("Error writing to file: %s"),
730 bytes_to_write -= bytes_written;
731 write_buffer += bytes_written;
733 while (bytes_to_write > 0);
735 } while ((bytes_read != 0) && (ret == TRUE));
743 handle_overwrite_open (const char *filename,
746 gboolean create_backup,
747 char **temp_filename,
748 GFileCreateFlags flags,
749 GFileInfo *reference_info,
750 GCancellable *cancellable,
754 GLocalFileStat original_stat;
761 mode = mode_from_flags_or_info (flags, reference_info);
763 /* We only need read access to the original file if we are creating a backup.
764 * We also add O_CREATE to avoid a race if the file was just removed */
765 if (create_backup || readable)
766 open_flags = O_RDWR | O_CREAT | O_BINARY;
768 open_flags = O_WRONLY | O_CREAT | O_BINARY;
770 /* Some systems have O_NOFOLLOW, which lets us avoid some races
771 * when finding out if the file we opened was a symlink */
774 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
775 if (fd == -1 && errno == ELOOP)
777 /* Could be a symlink, or it could be a regular ELOOP error,
778 * but then the next open will fail too. */
780 fd = g_open (filename, open_flags, mode);
783 fd = g_open (filename, open_flags, mode);
784 /* This is racy, but we do it as soon as possible to minimize the race */
785 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
791 char *display_name = g_filename_display_name (filename);
792 g_set_error (error, G_IO_ERROR,
793 g_io_error_from_errno (errsv),
794 _("Error opening file '%s': %s"),
795 display_name, g_strerror (errsv));
796 g_free (display_name);
801 res = _fstati64 (fd, &original_stat);
803 res = fstat (fd, &original_stat);
809 char *display_name = g_filename_display_name (filename);
810 g_set_error (error, G_IO_ERROR,
811 g_io_error_from_errno (errsv),
812 _("Error when getting information for file '%s': %s"),
813 display_name, g_strerror (errsv));
814 g_free (display_name);
818 /* not a regular file */
819 if (!S_ISREG (original_stat.st_mode))
821 if (S_ISDIR (original_stat.st_mode))
822 g_set_error_literal (error,
824 G_IO_ERROR_IS_DIRECTORY,
825 _("Target file is a directory"));
827 g_set_error_literal (error,
829 G_IO_ERROR_NOT_REGULAR_FILE,
830 _("Target file is not a regular file"));
836 current_etag = _g_local_file_info_create_etag (&original_stat);
837 if (strcmp (etag, current_etag) != 0)
839 g_set_error_literal (error,
841 G_IO_ERROR_WRONG_ETAG,
842 _("The file was externally modified"));
843 g_free (current_etag);
846 g_free (current_etag);
849 /* We use two backup strategies.
850 * The first one (which is faster) consist in saving to a
851 * tmp file then rename the original file to the backup and the
852 * tmp file to the original name. This is fast but doesn't work
853 * when the file is a link (hard or symbolic) or when we can't
854 * write to the current dir or can't set the permissions on the
856 * The second strategy consist simply in copying the old file
857 * to a backup file and rewrite the contents of the file.
860 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
861 (!(original_stat.st_nlink > 1) && !is_symlink))
863 char *dirname, *tmp_filename;
866 dirname = g_path_get_dirname (filename);
867 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
870 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
873 g_free (tmp_filename);
874 goto fallback_strategy;
877 /* try to keep permissions (unless replacing) */
879 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
882 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
885 fchmod (tmpfd, original_stat.st_mode) == -1 ||
891 GLocalFileStat tmp_statbuf;
895 tres = _fstati64 (tmpfd, &tmp_statbuf);
897 tres = fstat (tmpfd, &tmp_statbuf);
899 /* Check that we really needed to change something */
901 original_stat.st_uid != tmp_statbuf.st_uid ||
902 original_stat.st_gid != tmp_statbuf.st_gid ||
903 original_stat.st_mode != tmp_statbuf.st_mode)
905 (void) g_close (tmpfd, NULL);
906 g_unlink (tmp_filename);
907 g_free (tmp_filename);
908 goto fallback_strategy;
912 (void) g_close (fd, NULL);
913 *temp_filename = tmp_filename;
921 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
922 struct stat tmp_statbuf;
924 char *backup_filename;
927 backup_filename = create_backup_filename (filename);
929 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
931 g_set_error_literal (error,
933 G_IO_ERROR_CANT_CREATE_BACKUP,
934 _("Backup file creation failed"));
935 g_free (backup_filename);
939 bfd = g_open (backup_filename,
940 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
941 original_stat.st_mode & 0777);
945 g_set_error_literal (error,
947 G_IO_ERROR_CANT_CREATE_BACKUP,
948 _("Backup file creation failed"));
949 g_free (backup_filename);
953 /* If needed, Try to set the group of the backup same as the
954 * original file. If this fails, set the protection
955 * bits for the group same as the protection bits for
957 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
958 if (fstat (bfd, &tmp_statbuf) != 0)
960 g_set_error_literal (error,
962 G_IO_ERROR_CANT_CREATE_BACKUP,
963 _("Backup file creation failed"));
964 g_unlink (backup_filename);
965 g_free (backup_filename);
969 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
970 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
973 (original_stat.st_mode & 0707) |
974 ((original_stat.st_mode & 07) << 3)) != 0)
976 g_set_error_literal (error,
978 G_IO_ERROR_CANT_CREATE_BACKUP,
979 _("Backup file creation failed"));
980 g_unlink (backup_filename);
981 (void) g_close (bfd, NULL);
982 g_free (backup_filename);
988 if (!copy_file_data (fd, bfd, NULL))
990 g_set_error_literal (error,
992 G_IO_ERROR_CANT_CREATE_BACKUP,
993 _("Backup file creation failed"));
994 g_unlink (backup_filename);
995 (void) g_close (bfd, NULL);
996 g_free (backup_filename);
1001 (void) g_close (bfd, NULL);
1002 g_free (backup_filename);
1004 /* Seek back to the start of the file after the backup copy */
1005 if (lseek (fd, 0, SEEK_SET) == -1)
1009 g_set_error (error, G_IO_ERROR,
1010 g_io_error_from_errno (errsv),
1011 _("Error seeking in file: %s"),
1012 g_strerror (errsv));
1017 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1019 (void) g_close (fd, NULL);
1021 if (g_unlink (filename) != 0)
1025 g_set_error (error, G_IO_ERROR,
1026 g_io_error_from_errno (errsv),
1027 _("Error removing old file: %s"),
1028 g_strerror (errsv));
1033 open_flags = O_RDWR | O_CREAT | O_BINARY;
1035 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1036 fd = g_open (filename, open_flags, mode);
1040 char *display_name = g_filename_display_name (filename);
1041 g_set_error (error, G_IO_ERROR,
1042 g_io_error_from_errno (errsv),
1043 _("Error opening file '%s': %s"),
1044 display_name, g_strerror (errsv));
1045 g_free (display_name);
1051 /* Truncate the file at the start */
1053 if (g_win32_ftruncate (fd, 0) == -1)
1055 if (ftruncate (fd, 0) == -1)
1060 g_set_error (error, G_IO_ERROR,
1061 g_io_error_from_errno (errsv),
1062 _("Error truncating file: %s"),
1063 g_strerror (errsv));
1071 (void) g_close (fd, NULL);
1077 _g_local_file_output_stream_replace (const char *filename,
1080 gboolean create_backup,
1081 GFileCreateFlags flags,
1082 GFileInfo *reference_info,
1083 GCancellable *cancellable,
1086 GLocalFileOutputStream *stream;
1090 gboolean sync_on_close;
1093 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1098 mode = mode_from_flags_or_info (flags, reference_info);
1099 sync_on_close = FALSE;
1101 /* If the file doesn't exist, create it */
1102 open_flags = O_CREAT | O_EXCL | O_BINARY;
1104 open_flags |= O_RDWR;
1106 open_flags |= O_WRONLY;
1107 fd = g_open (filename, open_flags, mode);
1109 if (fd == -1 && errno == EEXIST)
1111 /* The file already exists */
1112 fd = handle_overwrite_open (filename, readable, etag,
1113 create_backup, &temp_file,
1114 flags, reference_info,
1115 cancellable, error);
1119 /* If the final destination exists, we want to sync the newly written
1120 * file to ensure the data is on disk when we rename over the destination.
1121 * otherwise if we get a system crash we can lose both the new and the
1122 * old file on some filesystems. (I.E. those that don't guarantee the
1123 * data is written to the disk before the metadata.)
1125 sync_on_close = TRUE;
1129 set_error_from_open_errno (filename, error);
1134 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1135 stream->priv->fd = fd;
1136 stream->priv->sync_on_close = sync_on_close;
1137 stream->priv->tmp_filename = temp_file;
1139 stream->priv->backup_filename = create_backup_filename (filename);
1140 stream->priv->original_filename = g_strdup (filename);
1142 return G_FILE_OUTPUT_STREAM (stream);
1146 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1148 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1149 return stream->priv->fd;
1154 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1156 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1157 return _g_local_file_output_stream_get_fd (stream);