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"
43 #include "gfiledescriptorbased.h"
49 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
52 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
62 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
65 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
67 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
68 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
69 g_file_descriptor_based_iface_init)
72 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,);
76 /* Some of the file replacement code was based on the code from gedit,
77 * relicenced to LGPL with permissions from the authors.
80 #define BACKUP_EXTENSION "~"
82 struct _GLocalFileOutputStreamPrivate {
84 char *original_filename;
85 char *backup_filename;
87 guint sync_on_close : 1;
92 static gssize g_local_file_output_stream_write (GOutputStream *stream,
95 GCancellable *cancellable,
97 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
98 GCancellable *cancellable,
100 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
101 const char *attributes,
102 GCancellable *cancellable,
104 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
105 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
106 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
107 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
110 GCancellable *cancellable,
112 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
113 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
115 GCancellable *cancellable,
118 static int g_local_file_output_stream_get_fd (GFileDescriptorBased *stream);
122 g_local_file_output_stream_finalize (GObject *object)
124 GLocalFileOutputStream *file;
126 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
128 g_free (file->priv->tmp_filename);
129 g_free (file->priv->original_filename);
130 g_free (file->priv->backup_filename);
131 g_free (file->priv->etag);
133 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 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
146 gobject_class->finalize = g_local_file_output_stream_finalize;
148 stream_class->write_fn = g_local_file_output_stream_write;
149 stream_class->close_fn = g_local_file_output_stream_close;
150 file_stream_class->query_info = g_local_file_output_stream_query_info;
151 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
152 file_stream_class->tell = g_local_file_output_stream_tell;
153 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
154 file_stream_class->seek = g_local_file_output_stream_seek;
155 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
156 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
161 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
163 iface->get_fd = g_local_file_output_stream_get_fd;
168 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
170 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
171 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
172 GLocalFileOutputStreamPrivate);
173 stream->priv->do_close = TRUE;
177 g_local_file_output_stream_write (GOutputStream *stream,
180 GCancellable *cancellable,
183 GLocalFileOutputStream *file;
186 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
190 if (g_cancellable_set_error_if_cancelled (cancellable, error))
192 res = write (file->priv->fd, buffer, count);
200 g_set_error (error, G_IO_ERROR,
201 g_io_error_from_errno (errsv),
202 _("Error writing to file: %s"),
213 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
216 out->priv->do_close = do_close;
220 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
221 GCancellable *cancellable,
224 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 res = close (file->priv->fd);
254 g_set_error (error, G_IO_ERROR,
255 g_io_error_from_errno (errsv),
256 _("Error closing file: %s"),
263 if (file->priv->tmp_filename)
265 /* We need to move the temp file to its final place,
266 * and possibly create the backup file
269 if (file->priv->backup_filename)
271 if (g_cancellable_set_error_if_cancelled (cancellable, error))
275 /* create original -> backup link, the original is then renamed over */
276 if (g_unlink (file->priv->backup_filename) != 0 &&
281 g_set_error (error, G_IO_ERROR,
282 G_IO_ERROR_CANT_CREATE_BACKUP,
283 _("Error removing old backup link: %s"),
288 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
290 /* link failed or is not supported, try rename */
291 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
295 g_set_error (error, G_IO_ERROR,
296 G_IO_ERROR_CANT_CREATE_BACKUP,
297 _("Error creating backup copy: %s"),
303 /* If link not supported, just rename... */
304 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
308 g_set_error (error, G_IO_ERROR,
309 G_IO_ERROR_CANT_CREATE_BACKUP,
310 _("Error creating backup copy: %s"),
318 if (g_cancellable_set_error_if_cancelled (cancellable, error))
321 /* tmp -> original */
322 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
326 g_set_error (error, G_IO_ERROR,
327 g_io_error_from_errno (errsv),
328 _("Error renaming temporary file: %s"),
334 if (g_cancellable_set_error_if_cancelled (cancellable, error))
337 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
339 if (fstat (file->priv->fd, &final_stat) == 0)
340 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
344 res = close (file->priv->fd);
349 g_set_error (error, G_IO_ERROR,
350 g_io_error_from_errno (errsv),
351 _("Error closing file: %s"),
368 /* A simple try to close the fd in case we fail before the actual close */
369 close (file->priv->fd);
376 g_local_file_output_stream_close (GOutputStream *stream,
377 GCancellable *cancellable,
380 GLocalFileOutputStream *file;
382 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
384 if (file->priv->do_close)
385 return _g_local_file_output_stream_really_close (file,
392 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
394 GLocalFileOutputStream *file;
396 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
398 return g_strdup (file->priv->etag);
402 g_local_file_output_stream_tell (GFileOutputStream *stream)
404 GLocalFileOutputStream *file;
407 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
409 pos = lseek (file->priv->fd, 0, SEEK_CUR);
411 if (pos == (off_t)-1)
418 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
420 GLocalFileOutputStream *file;
423 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
425 pos = lseek (file->priv->fd, 0, SEEK_CUR);
427 if (pos == (off_t)-1 && errno == ESPIPE)
434 seek_type_to_lseek (GSeekType type)
451 g_local_file_output_stream_seek (GFileOutputStream *stream,
454 GCancellable *cancellable,
457 GLocalFileOutputStream *file;
460 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
462 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
464 if (pos == (off_t)-1)
468 g_set_error (error, G_IO_ERROR,
469 g_io_error_from_errno (errsv),
470 _("Error seeking in file: %s"),
479 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
481 /* We can't truncate pipes and stuff where we can't seek */
482 return g_local_file_output_stream_can_seek (stream);
486 g_local_file_output_stream_truncate (GFileOutputStream *stream,
488 GCancellable *cancellable,
491 GLocalFileOutputStream *file;
494 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
498 res = g_win32_ftruncate (file->priv->fd, size);
500 res = ftruncate (file->priv->fd, size);
509 if (g_cancellable_set_error_if_cancelled (cancellable, error))
514 g_set_error (error, G_IO_ERROR,
515 g_io_error_from_errno (errsv),
516 _("Error truncating file: %s"),
526 g_local_file_output_stream_query_info (GFileOutputStream *stream,
527 const char *attributes,
528 GCancellable *cancellable,
531 GLocalFileOutputStream *file;
533 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
535 if (g_cancellable_set_error_if_cancelled (cancellable, error))
538 return _g_local_file_info_get_from_fd (file->priv->fd,
544 _g_local_file_output_stream_new (int fd)
546 GLocalFileOutputStream *stream;
548 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
549 stream->priv->fd = fd;
550 return G_FILE_OUTPUT_STREAM (stream);
554 _g_local_file_output_stream_open (const char *filename,
556 GCancellable *cancellable,
559 GLocalFileOutputStream *stream;
563 if (g_cancellable_set_error_if_cancelled (cancellable, error))
566 open_flags = O_BINARY;
568 open_flags |= O_RDWR;
570 open_flags |= O_WRONLY;
572 fd = g_open (filename, open_flags, 0666);
578 /* This must be an invalid filename, on e.g. FAT */
579 g_set_error_literal (error, G_IO_ERROR,
580 G_IO_ERROR_INVALID_FILENAME,
581 _("Invalid filename"));
584 char *display_name = g_filename_display_name (filename);
585 g_set_error (error, G_IO_ERROR,
586 g_io_error_from_errno (errsv),
587 _("Error opening file '%s': %s"),
588 display_name, g_strerror (errsv));
589 g_free (display_name);
594 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
595 stream->priv->fd = fd;
596 return G_FILE_OUTPUT_STREAM (stream);
600 _g_local_file_output_stream_create (const char *filename,
602 GFileCreateFlags flags,
603 GCancellable *cancellable,
606 GLocalFileOutputStream *stream;
611 if (g_cancellable_set_error_if_cancelled (cancellable, error))
614 if (flags & G_FILE_CREATE_PRIVATE)
619 open_flags = O_CREAT | O_EXCL | O_BINARY;
621 open_flags |= O_RDWR;
623 open_flags |= O_WRONLY;
625 fd = g_open (filename, open_flags, mode);
631 /* This must be an invalid filename, on e.g. FAT */
632 g_set_error_literal (error, G_IO_ERROR,
633 G_IO_ERROR_INVALID_FILENAME,
634 _("Invalid filename"));
637 char *display_name = g_filename_display_name (filename);
638 g_set_error (error, G_IO_ERROR,
639 g_io_error_from_errno (errsv),
640 _("Error opening file '%s': %s"),
641 display_name, g_strerror (errsv));
642 g_free (display_name);
647 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
648 stream->priv->fd = fd;
649 return G_FILE_OUTPUT_STREAM (stream);
653 _g_local_file_output_stream_append (const char *filename,
654 GFileCreateFlags flags,
655 GCancellable *cancellable,
658 GLocalFileOutputStream *stream;
662 if (g_cancellable_set_error_if_cancelled (cancellable, error))
665 if (flags & G_FILE_CREATE_PRIVATE)
670 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
676 /* This must be an invalid filename, on e.g. FAT */
677 g_set_error_literal (error, G_IO_ERROR,
678 G_IO_ERROR_INVALID_FILENAME,
679 _("Invalid filename"));
682 char *display_name = g_filename_display_name (filename);
683 g_set_error (error, G_IO_ERROR,
684 g_io_error_from_errno (errsv),
685 _("Error opening file '%s': %s"),
686 display_name, g_strerror (errsv));
687 g_free (display_name);
692 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
693 stream->priv->fd = fd;
695 return G_FILE_OUTPUT_STREAM (stream);
699 create_backup_filename (const char *filename)
701 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
704 #define BUFSIZE 8192 /* size of normal write buffer */
707 copy_file_data (gint sfd,
713 const gchar *write_buffer;
715 gssize bytes_to_write;
716 gssize bytes_written;
718 buffer = g_malloc (BUFSIZE);
722 bytes_read = read (sfd, buffer, BUFSIZE);
723 if (bytes_read == -1)
730 g_set_error (error, G_IO_ERROR,
731 g_io_error_from_errno (errsv),
732 _("Error reading from file: %s"),
738 bytes_to_write = bytes_read;
739 write_buffer = buffer;
743 bytes_written = write (dfd, write_buffer, bytes_to_write);
744 if (bytes_written == -1)
751 g_set_error (error, G_IO_ERROR,
752 g_io_error_from_errno (errsv),
753 _("Error writing to file: %s"),
759 bytes_to_write -= bytes_written;
760 write_buffer += bytes_written;
762 while (bytes_to_write > 0);
764 } while ((bytes_read != 0) && (ret == TRUE));
772 handle_overwrite_open (const char *filename,
775 gboolean create_backup,
776 char **temp_filename,
777 GFileCreateFlags flags,
778 GCancellable *cancellable,
782 GLocalFileStat original_stat;
789 if (flags & G_FILE_CREATE_PRIVATE)
794 /* We only need read access to the original file if we are creating a backup.
795 * We also add O_CREATE to avoid a race if the file was just removed */
796 if (create_backup || readable)
797 open_flags = O_RDWR | O_CREAT | O_BINARY;
799 open_flags = O_WRONLY | O_CREAT | O_BINARY;
801 /* Some systems have O_NOFOLLOW, which lets us avoid some races
802 * when finding out if the file we opened was a symlink */
805 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
806 if (fd == -1 && errno == ELOOP)
808 /* Could be a symlink, or it could be a regular ELOOP error,
809 * but then the next open will fail too. */
811 fd = g_open (filename, open_flags, mode);
814 fd = g_open (filename, open_flags, mode);
815 /* This is racy, but we do it as soon as possible to minimize the race */
816 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
822 char *display_name = g_filename_display_name (filename);
823 g_set_error (error, G_IO_ERROR,
824 g_io_error_from_errno (errsv),
825 _("Error opening file '%s': %s"),
826 display_name, g_strerror (errsv));
827 g_free (display_name);
832 res = _fstati64 (fd, &original_stat);
834 res = fstat (fd, &original_stat);
840 char *display_name = g_filename_display_name (filename);
841 g_set_error (error, G_IO_ERROR,
842 g_io_error_from_errno (errsv),
843 _("Error when getting information for file '%s': %s"),
844 display_name, g_strerror (errsv));
845 g_free (display_name);
849 /* not a regular file */
850 if (!S_ISREG (original_stat.st_mode))
852 if (S_ISDIR (original_stat.st_mode))
853 g_set_error_literal (error,
855 G_IO_ERROR_IS_DIRECTORY,
856 _("Target file is a directory"));
858 g_set_error_literal (error,
860 G_IO_ERROR_NOT_REGULAR_FILE,
861 _("Target file is not a regular file"));
867 current_etag = _g_local_file_info_create_etag (&original_stat);
868 if (strcmp (etag, current_etag) != 0)
870 g_set_error_literal (error,
872 G_IO_ERROR_WRONG_ETAG,
873 _("The file was externally modified"));
874 g_free (current_etag);
877 g_free (current_etag);
880 /* We use two backup strategies.
881 * The first one (which is faster) consist in saving to a
882 * tmp file then rename the original file to the backup and the
883 * tmp file to the original name. This is fast but doesn't work
884 * when the file is a link (hard or symbolic) or when we can't
885 * write to the current dir or can't set the permissions on the
887 * The second strategy consist simply in copying the old file
888 * to a backup file and rewrite the contents of the file.
891 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
892 (!(original_stat.st_nlink > 1) && !is_symlink))
894 char *dirname, *tmp_filename;
897 dirname = g_path_get_dirname (filename);
898 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
901 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
904 g_free (tmp_filename);
905 goto fallback_strategy;
908 /* try to keep permissions (unless replacing) */
910 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
913 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
916 fchmod (tmpfd, original_stat.st_mode) == -1 ||
922 GLocalFileStat tmp_statbuf;
926 tres = _fstati64 (tmpfd, &tmp_statbuf);
928 tres = fstat (tmpfd, &tmp_statbuf);
930 /* Check that we really needed to change something */
932 original_stat.st_uid != tmp_statbuf.st_uid ||
933 original_stat.st_gid != tmp_statbuf.st_gid ||
934 original_stat.st_mode != tmp_statbuf.st_mode)
937 g_unlink (tmp_filename);
938 g_free (tmp_filename);
939 goto fallback_strategy;
944 *temp_filename = tmp_filename;
952 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
953 struct stat tmp_statbuf;
955 char *backup_filename;
958 backup_filename = create_backup_filename (filename);
960 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
962 g_set_error_literal (error,
964 G_IO_ERROR_CANT_CREATE_BACKUP,
965 _("Backup file creation failed"));
966 g_free (backup_filename);
970 bfd = g_open (backup_filename,
971 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
972 original_stat.st_mode & 0777);
976 g_set_error_literal (error,
978 G_IO_ERROR_CANT_CREATE_BACKUP,
979 _("Backup file creation failed"));
980 g_free (backup_filename);
984 /* If needed, Try to set the group of the backup same as the
985 * original file. If this fails, set the protection
986 * bits for the group same as the protection bits for
988 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
989 if (fstat (bfd, &tmp_statbuf) != 0)
991 g_set_error_literal (error,
993 G_IO_ERROR_CANT_CREATE_BACKUP,
994 _("Backup file creation failed"));
995 g_unlink (backup_filename);
996 g_free (backup_filename);
1000 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
1001 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
1004 (original_stat.st_mode & 0707) |
1005 ((original_stat.st_mode & 07) << 3)) != 0)
1007 g_set_error_literal (error,
1009 G_IO_ERROR_CANT_CREATE_BACKUP,
1010 _("Backup file creation failed"));
1011 g_unlink (backup_filename);
1013 g_free (backup_filename);
1019 if (!copy_file_data (fd, bfd, NULL))
1021 g_set_error_literal (error,
1023 G_IO_ERROR_CANT_CREATE_BACKUP,
1024 _("Backup file creation failed"));
1025 g_unlink (backup_filename);
1027 g_free (backup_filename);
1033 g_free (backup_filename);
1035 /* Seek back to the start of the file after the backup copy */
1036 if (lseek (fd, 0, SEEK_SET) == -1)
1040 g_set_error (error, G_IO_ERROR,
1041 g_io_error_from_errno (errsv),
1042 _("Error seeking in file: %s"),
1043 g_strerror (errsv));
1048 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1052 if (g_unlink (filename) != 0)
1056 g_set_error (error, G_IO_ERROR,
1057 g_io_error_from_errno (errsv),
1058 _("Error removing old file: %s"),
1059 g_strerror (errsv));
1064 open_flags = O_RDWR | O_CREAT | O_BINARY;
1066 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1067 fd = g_open (filename, open_flags, mode);
1071 char *display_name = g_filename_display_name (filename);
1072 g_set_error (error, G_IO_ERROR,
1073 g_io_error_from_errno (errsv),
1074 _("Error opening file '%s': %s"),
1075 display_name, g_strerror (errsv));
1076 g_free (display_name);
1082 /* Truncate the file at the start */
1084 if (g_win32_ftruncate (fd, 0) == -1)
1086 if (ftruncate (fd, 0) == -1)
1091 g_set_error (error, G_IO_ERROR,
1092 g_io_error_from_errno (errsv),
1093 _("Error truncating file: %s"),
1094 g_strerror (errsv));
1108 _g_local_file_output_stream_replace (const char *filename,
1111 gboolean create_backup,
1112 GFileCreateFlags flags,
1113 GCancellable *cancellable,
1116 GLocalFileOutputStream *stream;
1120 gboolean sync_on_close;
1123 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1128 if (flags & G_FILE_CREATE_PRIVATE)
1132 sync_on_close = FALSE;
1134 /* If the file doesn't exist, create it */
1135 open_flags = O_CREAT | O_EXCL | O_BINARY;
1137 open_flags |= O_RDWR;
1139 open_flags |= O_WRONLY;
1140 fd = g_open (filename, open_flags, mode);
1142 if (fd == -1 && errno == EEXIST)
1144 /* The file already exists */
1145 fd = handle_overwrite_open (filename, readable, etag,
1146 create_backup, &temp_file,
1147 flags, cancellable, error);
1151 /* If the final destination exists, we want to sync the newly written
1152 * file to ensure the data is on disk when we rename over the destination.
1153 * otherwise if we get a system crash we can lose both the new and the
1154 * old file on some filesystems. (I.E. those that don't guarantee the
1155 * data is written to the disk before the metadata.)
1157 sync_on_close = TRUE;
1163 if (errsv == EINVAL)
1164 /* This must be an invalid filename, on e.g. FAT */
1165 g_set_error_literal (error, G_IO_ERROR,
1166 G_IO_ERROR_INVALID_FILENAME,
1167 _("Invalid filename"));
1170 char *display_name = g_filename_display_name (filename);
1171 g_set_error (error, G_IO_ERROR,
1172 g_io_error_from_errno (errsv),
1173 _("Error opening file '%s': %s"),
1174 display_name, g_strerror (errsv));
1175 g_free (display_name);
1181 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1182 stream->priv->fd = fd;
1183 stream->priv->sync_on_close = sync_on_close;
1184 stream->priv->tmp_filename = temp_file;
1186 stream->priv->backup_filename = create_backup_filename (filename);
1187 stream->priv->original_filename = g_strdup (filename);
1189 return G_FILE_OUTPUT_STREAM (stream);
1193 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1195 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1196 return stream->priv->fd;
1201 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1203 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1204 return _g_local_file_output_stream_get_fd (stream);