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;
227 if (file->priv->sync_on_close &&
228 fsync (file->priv->fd) != 0)
232 g_set_error (error, G_IO_ERROR,
233 g_io_error_from_errno (errsv),
234 _("Error writing to file: %s"),
242 /* Must close before renaming on Windows, so just do the close first
243 * in all cases for now.
245 if (_fstati64 (file->priv->fd, &final_stat) == 0)
246 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
248 if (!g_close (file->priv->fd, NULL))
252 g_set_error (error, G_IO_ERROR,
253 g_io_error_from_errno (errsv),
254 _("Error closing file: %s"),
261 if (file->priv->tmp_filename)
263 /* We need to move the temp file to its final place,
264 * and possibly create the backup file
267 if (file->priv->backup_filename)
269 if (g_cancellable_set_error_if_cancelled (cancellable, error))
273 /* create original -> backup link, the original is then renamed over */
274 if (g_unlink (file->priv->backup_filename) != 0 &&
279 g_set_error (error, G_IO_ERROR,
280 G_IO_ERROR_CANT_CREATE_BACKUP,
281 _("Error removing old backup link: %s"),
286 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
288 /* link failed or is not supported, try rename */
289 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
293 g_set_error (error, G_IO_ERROR,
294 G_IO_ERROR_CANT_CREATE_BACKUP,
295 _("Error creating backup copy: %s"),
301 /* If link not supported, just rename... */
302 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
306 g_set_error (error, G_IO_ERROR,
307 G_IO_ERROR_CANT_CREATE_BACKUP,
308 _("Error creating backup copy: %s"),
316 if (g_cancellable_set_error_if_cancelled (cancellable, error))
319 /* tmp -> original */
320 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
324 g_set_error (error, G_IO_ERROR,
325 g_io_error_from_errno (errsv),
326 _("Error renaming temporary file: %s"),
331 g_clear_pointer (&file->priv->tmp_filename, g_free);
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);
342 if (!g_close (file->priv->fd, NULL))
346 g_set_error (error, G_IO_ERROR,
347 g_io_error_from_errno (errsv),
348 _("Error closing file: %s"),
359 /* A simple try to close the fd in case we fail before the actual close */
360 (void) g_close (file->priv->fd, NULL);
362 if (file->priv->tmp_filename)
363 g_unlink (file->priv->tmp_filename);
370 g_local_file_output_stream_close (GOutputStream *stream,
371 GCancellable *cancellable,
374 GLocalFileOutputStream *file;
376 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
378 if (file->priv->do_close)
379 return _g_local_file_output_stream_really_close (file,
386 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
388 GLocalFileOutputStream *file;
390 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
392 return g_strdup (file->priv->etag);
396 g_local_file_output_stream_tell (GFileOutputStream *stream)
398 GLocalFileOutputStream *file;
401 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
403 pos = lseek (file->priv->fd, 0, SEEK_CUR);
405 if (pos == (off_t)-1)
412 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
414 GLocalFileOutputStream *file;
417 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
419 pos = lseek (file->priv->fd, 0, SEEK_CUR);
421 if (pos == (off_t)-1 && errno == ESPIPE)
428 seek_type_to_lseek (GSeekType type)
445 g_local_file_output_stream_seek (GFileOutputStream *stream,
448 GCancellable *cancellable,
451 GLocalFileOutputStream *file;
454 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
456 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
458 if (pos == (off_t)-1)
462 g_set_error (error, G_IO_ERROR,
463 g_io_error_from_errno (errsv),
464 _("Error seeking in file: %s"),
473 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
475 /* We can't truncate pipes and stuff where we can't seek */
476 return g_local_file_output_stream_can_seek (stream);
480 g_local_file_output_stream_truncate (GFileOutputStream *stream,
482 GCancellable *cancellable,
485 GLocalFileOutputStream *file;
488 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
492 res = g_win32_ftruncate (file->priv->fd, size);
494 res = ftruncate (file->priv->fd, size);
503 if (g_cancellable_set_error_if_cancelled (cancellable, error))
508 g_set_error (error, G_IO_ERROR,
509 g_io_error_from_errno (errsv),
510 _("Error truncating file: %s"),
520 g_local_file_output_stream_query_info (GFileOutputStream *stream,
521 const char *attributes,
522 GCancellable *cancellable,
525 GLocalFileOutputStream *file;
527 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
529 if (g_cancellable_set_error_if_cancelled (cancellable, error))
532 return _g_local_file_info_get_from_fd (file->priv->fd,
538 _g_local_file_output_stream_new (int fd)
540 GLocalFileOutputStream *stream;
542 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
543 stream->priv->fd = fd;
544 return G_FILE_OUTPUT_STREAM (stream);
548 _g_local_file_output_stream_open (const char *filename,
550 GCancellable *cancellable,
553 GLocalFileOutputStream *stream;
557 if (g_cancellable_set_error_if_cancelled (cancellable, error))
560 open_flags = O_BINARY;
562 open_flags |= O_RDWR;
564 open_flags |= O_WRONLY;
566 fd = g_open (filename, open_flags, 0666);
572 /* This must be an invalid filename, on e.g. FAT */
573 g_set_error_literal (error, G_IO_ERROR,
574 G_IO_ERROR_INVALID_FILENAME,
575 _("Invalid filename"));
578 char *display_name = g_filename_display_name (filename);
579 g_set_error (error, G_IO_ERROR,
580 g_io_error_from_errno (errsv),
581 _("Error opening file '%s': %s"),
582 display_name, g_strerror (errsv));
583 g_free (display_name);
588 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
589 stream->priv->fd = fd;
590 return G_FILE_OUTPUT_STREAM (stream);
594 _g_local_file_output_stream_create (const char *filename,
596 GFileCreateFlags flags,
597 GCancellable *cancellable,
600 GLocalFileOutputStream *stream;
605 if (g_cancellable_set_error_if_cancelled (cancellable, error))
608 if (flags & G_FILE_CREATE_PRIVATE)
613 open_flags = O_CREAT | O_EXCL | O_BINARY;
615 open_flags |= O_RDWR;
617 open_flags |= O_WRONLY;
619 fd = g_open (filename, open_flags, mode);
625 /* This must be an invalid filename, on e.g. FAT */
626 g_set_error_literal (error, G_IO_ERROR,
627 G_IO_ERROR_INVALID_FILENAME,
628 _("Invalid filename"));
631 char *display_name = g_filename_display_name (filename);
632 g_set_error (error, G_IO_ERROR,
633 g_io_error_from_errno (errsv),
634 _("Error opening file '%s': %s"),
635 display_name, g_strerror (errsv));
636 g_free (display_name);
641 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
642 stream->priv->fd = fd;
643 return G_FILE_OUTPUT_STREAM (stream);
647 _g_local_file_output_stream_append (const char *filename,
648 GFileCreateFlags flags,
649 GCancellable *cancellable,
652 GLocalFileOutputStream *stream;
656 if (g_cancellable_set_error_if_cancelled (cancellable, error))
659 if (flags & G_FILE_CREATE_PRIVATE)
664 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
670 /* This must be an invalid filename, on e.g. FAT */
671 g_set_error_literal (error, G_IO_ERROR,
672 G_IO_ERROR_INVALID_FILENAME,
673 _("Invalid filename"));
676 char *display_name = g_filename_display_name (filename);
677 g_set_error (error, G_IO_ERROR,
678 g_io_error_from_errno (errsv),
679 _("Error opening file '%s': %s"),
680 display_name, g_strerror (errsv));
681 g_free (display_name);
686 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
687 stream->priv->fd = fd;
689 return G_FILE_OUTPUT_STREAM (stream);
693 create_backup_filename (const char *filename)
695 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
698 #define BUFSIZE 8192 /* size of normal write buffer */
701 copy_file_data (gint sfd,
707 const gchar *write_buffer;
709 gssize bytes_to_write;
710 gssize bytes_written;
712 buffer = g_malloc (BUFSIZE);
716 bytes_read = read (sfd, buffer, BUFSIZE);
717 if (bytes_read == -1)
724 g_set_error (error, G_IO_ERROR,
725 g_io_error_from_errno (errsv),
726 _("Error reading from file: %s"),
732 bytes_to_write = bytes_read;
733 write_buffer = buffer;
737 bytes_written = write (dfd, write_buffer, bytes_to_write);
738 if (bytes_written == -1)
745 g_set_error (error, G_IO_ERROR,
746 g_io_error_from_errno (errsv),
747 _("Error writing to file: %s"),
753 bytes_to_write -= bytes_written;
754 write_buffer += bytes_written;
756 while (bytes_to_write > 0);
758 } while ((bytes_read != 0) && (ret == TRUE));
766 handle_overwrite_open (const char *filename,
769 gboolean create_backup,
770 char **temp_filename,
771 GFileCreateFlags flags,
772 GCancellable *cancellable,
776 GLocalFileStat original_stat;
783 if (flags & G_FILE_CREATE_PRIVATE)
788 /* We only need read access to the original file if we are creating a backup.
789 * We also add O_CREATE to avoid a race if the file was just removed */
790 if (create_backup || readable)
791 open_flags = O_RDWR | O_CREAT | O_BINARY;
793 open_flags = O_WRONLY | O_CREAT | O_BINARY;
795 /* Some systems have O_NOFOLLOW, which lets us avoid some races
796 * when finding out if the file we opened was a symlink */
799 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
800 if (fd == -1 && errno == ELOOP)
802 /* Could be a symlink, or it could be a regular ELOOP error,
803 * but then the next open will fail too. */
805 fd = g_open (filename, open_flags, mode);
808 fd = g_open (filename, open_flags, mode);
809 /* This is racy, but we do it as soon as possible to minimize the race */
810 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
816 char *display_name = g_filename_display_name (filename);
817 g_set_error (error, G_IO_ERROR,
818 g_io_error_from_errno (errsv),
819 _("Error opening file '%s': %s"),
820 display_name, g_strerror (errsv));
821 g_free (display_name);
826 res = _fstati64 (fd, &original_stat);
828 res = fstat (fd, &original_stat);
834 char *display_name = g_filename_display_name (filename);
835 g_set_error (error, G_IO_ERROR,
836 g_io_error_from_errno (errsv),
837 _("Error when getting information for file '%s': %s"),
838 display_name, g_strerror (errsv));
839 g_free (display_name);
843 /* not a regular file */
844 if (!S_ISREG (original_stat.st_mode))
846 if (S_ISDIR (original_stat.st_mode))
847 g_set_error_literal (error,
849 G_IO_ERROR_IS_DIRECTORY,
850 _("Target file is a directory"));
852 g_set_error_literal (error,
854 G_IO_ERROR_NOT_REGULAR_FILE,
855 _("Target file is not a regular file"));
861 current_etag = _g_local_file_info_create_etag (&original_stat);
862 if (strcmp (etag, current_etag) != 0)
864 g_set_error_literal (error,
866 G_IO_ERROR_WRONG_ETAG,
867 _("The file was externally modified"));
868 g_free (current_etag);
871 g_free (current_etag);
874 /* We use two backup strategies.
875 * The first one (which is faster) consist in saving to a
876 * tmp file then rename the original file to the backup and the
877 * tmp file to the original name. This is fast but doesn't work
878 * when the file is a link (hard or symbolic) or when we can't
879 * write to the current dir or can't set the permissions on the
881 * The second strategy consist simply in copying the old file
882 * to a backup file and rewrite the contents of the file.
885 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
886 (!(original_stat.st_nlink > 1) && !is_symlink))
888 char *dirname, *tmp_filename;
891 dirname = g_path_get_dirname (filename);
892 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
895 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
898 g_free (tmp_filename);
899 goto fallback_strategy;
902 /* try to keep permissions (unless replacing) */
904 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
907 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
910 fchmod (tmpfd, original_stat.st_mode) == -1 ||
916 GLocalFileStat tmp_statbuf;
920 tres = _fstati64 (tmpfd, &tmp_statbuf);
922 tres = fstat (tmpfd, &tmp_statbuf);
924 /* Check that we really needed to change something */
926 original_stat.st_uid != tmp_statbuf.st_uid ||
927 original_stat.st_gid != tmp_statbuf.st_gid ||
928 original_stat.st_mode != tmp_statbuf.st_mode)
930 (void) g_close (tmpfd, NULL);
931 g_unlink (tmp_filename);
932 g_free (tmp_filename);
933 goto fallback_strategy;
937 (void) g_close (fd, NULL);
938 *temp_filename = tmp_filename;
946 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
947 struct stat tmp_statbuf;
949 char *backup_filename;
952 backup_filename = create_backup_filename (filename);
954 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
956 g_set_error_literal (error,
958 G_IO_ERROR_CANT_CREATE_BACKUP,
959 _("Backup file creation failed"));
960 g_free (backup_filename);
964 bfd = g_open (backup_filename,
965 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
966 original_stat.st_mode & 0777);
970 g_set_error_literal (error,
972 G_IO_ERROR_CANT_CREATE_BACKUP,
973 _("Backup file creation failed"));
974 g_free (backup_filename);
978 /* If needed, Try to set the group of the backup same as the
979 * original file. If this fails, set the protection
980 * bits for the group same as the protection bits for
982 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
983 if (fstat (bfd, &tmp_statbuf) != 0)
985 g_set_error_literal (error,
987 G_IO_ERROR_CANT_CREATE_BACKUP,
988 _("Backup file creation failed"));
989 g_unlink (backup_filename);
990 g_free (backup_filename);
994 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
995 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
998 (original_stat.st_mode & 0707) |
999 ((original_stat.st_mode & 07) << 3)) != 0)
1001 g_set_error_literal (error,
1003 G_IO_ERROR_CANT_CREATE_BACKUP,
1004 _("Backup file creation failed"));
1005 g_unlink (backup_filename);
1006 (void) g_close (bfd, NULL);
1007 g_free (backup_filename);
1013 if (!copy_file_data (fd, bfd, NULL))
1015 g_set_error_literal (error,
1017 G_IO_ERROR_CANT_CREATE_BACKUP,
1018 _("Backup file creation failed"));
1019 g_unlink (backup_filename);
1020 (void) g_close (bfd, NULL);
1021 g_free (backup_filename);
1026 (void) g_close (bfd, NULL);
1027 g_free (backup_filename);
1029 /* Seek back to the start of the file after the backup copy */
1030 if (lseek (fd, 0, SEEK_SET) == -1)
1034 g_set_error (error, G_IO_ERROR,
1035 g_io_error_from_errno (errsv),
1036 _("Error seeking in file: %s"),
1037 g_strerror (errsv));
1042 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1044 (void) g_close (fd, NULL);
1046 if (g_unlink (filename) != 0)
1050 g_set_error (error, G_IO_ERROR,
1051 g_io_error_from_errno (errsv),
1052 _("Error removing old file: %s"),
1053 g_strerror (errsv));
1058 open_flags = O_RDWR | O_CREAT | O_BINARY;
1060 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1061 fd = g_open (filename, open_flags, mode);
1065 char *display_name = g_filename_display_name (filename);
1066 g_set_error (error, G_IO_ERROR,
1067 g_io_error_from_errno (errsv),
1068 _("Error opening file '%s': %s"),
1069 display_name, g_strerror (errsv));
1070 g_free (display_name);
1076 /* Truncate the file at the start */
1078 if (g_win32_ftruncate (fd, 0) == -1)
1080 if (ftruncate (fd, 0) == -1)
1085 g_set_error (error, G_IO_ERROR,
1086 g_io_error_from_errno (errsv),
1087 _("Error truncating file: %s"),
1088 g_strerror (errsv));
1096 (void) g_close (fd, NULL);
1102 _g_local_file_output_stream_replace (const char *filename,
1105 gboolean create_backup,
1106 GFileCreateFlags flags,
1107 GCancellable *cancellable,
1110 GLocalFileOutputStream *stream;
1114 gboolean sync_on_close;
1117 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1122 if (flags & G_FILE_CREATE_PRIVATE)
1126 sync_on_close = FALSE;
1128 /* If the file doesn't exist, create it */
1129 open_flags = O_CREAT | O_EXCL | O_BINARY;
1131 open_flags |= O_RDWR;
1133 open_flags |= O_WRONLY;
1134 fd = g_open (filename, open_flags, mode);
1136 if (fd == -1 && errno == EEXIST)
1138 /* The file already exists */
1139 fd = handle_overwrite_open (filename, readable, etag,
1140 create_backup, &temp_file,
1141 flags, cancellable, error);
1145 /* If the final destination exists, we want to sync the newly written
1146 * file to ensure the data is on disk when we rename over the destination.
1147 * otherwise if we get a system crash we can lose both the new and the
1148 * old file on some filesystems. (I.E. those that don't guarantee the
1149 * data is written to the disk before the metadata.)
1151 sync_on_close = TRUE;
1157 if (errsv == EINVAL)
1158 /* This must be an invalid filename, on e.g. FAT */
1159 g_set_error_literal (error, G_IO_ERROR,
1160 G_IO_ERROR_INVALID_FILENAME,
1161 _("Invalid filename"));
1164 char *display_name = g_filename_display_name (filename);
1165 g_set_error (error, G_IO_ERROR,
1166 g_io_error_from_errno (errsv),
1167 _("Error opening file '%s': %s"),
1168 display_name, g_strerror (errsv));
1169 g_free (display_name);
1175 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1176 stream->priv->fd = fd;
1177 stream->priv->sync_on_close = sync_on_close;
1178 stream->priv->tmp_filename = temp_file;
1180 stream->priv->backup_filename = create_backup_filename (filename);
1181 stream->priv->original_filename = g_strdup (filename);
1183 return G_FILE_OUTPUT_STREAM (stream);
1187 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1189 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1190 return stream->priv->fd;
1195 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1197 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1198 return _g_local_file_output_stream_get_fd (stream);