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)
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;
229 if (file->priv->sync_on_close &&
230 fsync (file->priv->fd) != 0)
234 g_set_error (error, G_IO_ERROR,
235 g_io_error_from_errno (errsv),
236 _("Error writing to file: %s"),
244 /* Must close before renaming on Windows, so just do the close first
245 * in all cases for now.
247 if (_fstati64 (file->priv->fd, &final_stat) == 0)
248 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
250 res = close (file->priv->fd);
255 g_set_error (error, G_IO_ERROR,
256 g_io_error_from_errno (errsv),
257 _("Error closing file: %s"),
264 if (file->priv->tmp_filename)
266 /* We need to move the temp file to its final place,
267 * and possibly create the backup file
270 if (file->priv->backup_filename)
272 if (g_cancellable_set_error_if_cancelled (cancellable, error))
276 /* create original -> backup link, the original is then renamed over */
277 if (g_unlink (file->priv->backup_filename) != 0 &&
282 g_set_error (error, G_IO_ERROR,
283 G_IO_ERROR_CANT_CREATE_BACKUP,
284 _("Error removing old backup link: %s"),
289 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
291 /* link failed or is not supported, try rename */
292 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
296 g_set_error (error, G_IO_ERROR,
297 G_IO_ERROR_CANT_CREATE_BACKUP,
298 _("Error creating backup copy: %s"),
304 /* If link not supported, just rename... */
305 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
309 g_set_error (error, G_IO_ERROR,
310 G_IO_ERROR_CANT_CREATE_BACKUP,
311 _("Error creating backup copy: %s"),
319 if (g_cancellable_set_error_if_cancelled (cancellable, error))
322 /* tmp -> original */
323 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
327 g_set_error (error, G_IO_ERROR,
328 g_io_error_from_errno (errsv),
329 _("Error renaming temporary file: %s"),
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);
345 res = close (file->priv->fd);
350 g_set_error (error, G_IO_ERROR,
351 g_io_error_from_errno (errsv),
352 _("Error closing file: %s"),
369 /* A simple try to close the fd in case we fail before the actual close */
370 close (file->priv->fd);
377 g_local_file_output_stream_close (GOutputStream *stream,
378 GCancellable *cancellable,
381 GLocalFileOutputStream *file;
383 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
385 if (file->priv->do_close)
386 return _g_local_file_output_stream_really_close (file,
393 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
395 GLocalFileOutputStream *file;
397 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
399 return g_strdup (file->priv->etag);
403 g_local_file_output_stream_tell (GFileOutputStream *stream)
405 GLocalFileOutputStream *file;
408 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
410 pos = lseek (file->priv->fd, 0, SEEK_CUR);
412 if (pos == (off_t)-1)
419 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
421 GLocalFileOutputStream *file;
424 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
426 pos = lseek (file->priv->fd, 0, SEEK_CUR);
428 if (pos == (off_t)-1 && errno == ESPIPE)
435 seek_type_to_lseek (GSeekType type)
452 g_local_file_output_stream_seek (GFileOutputStream *stream,
455 GCancellable *cancellable,
458 GLocalFileOutputStream *file;
461 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
463 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
465 if (pos == (off_t)-1)
469 g_set_error (error, G_IO_ERROR,
470 g_io_error_from_errno (errsv),
471 _("Error seeking in file: %s"),
480 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
482 /* We can't truncate pipes and stuff where we can't seek */
483 return g_local_file_output_stream_can_seek (stream);
487 g_local_file_output_stream_truncate (GFileOutputStream *stream,
489 GCancellable *cancellable,
492 GLocalFileOutputStream *file;
495 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
499 res = g_win32_ftruncate (file->priv->fd, size);
501 res = ftruncate (file->priv->fd, size);
510 if (g_cancellable_set_error_if_cancelled (cancellable, error))
515 g_set_error (error, G_IO_ERROR,
516 g_io_error_from_errno (errsv),
517 _("Error truncating file: %s"),
527 g_local_file_output_stream_query_info (GFileOutputStream *stream,
528 const char *attributes,
529 GCancellable *cancellable,
532 GLocalFileOutputStream *file;
534 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
536 if (g_cancellable_set_error_if_cancelled (cancellable, error))
539 return _g_local_file_info_get_from_fd (file->priv->fd,
545 _g_local_file_output_stream_open (const char *filename,
547 GCancellable *cancellable,
550 GLocalFileOutputStream *stream;
554 if (g_cancellable_set_error_if_cancelled (cancellable, error))
557 open_flags = O_BINARY;
559 open_flags |= O_RDWR;
561 open_flags |= O_WRONLY;
563 fd = g_open (filename, open_flags, 0666);
569 /* This must be an invalid filename, on e.g. FAT */
570 g_set_error_literal (error, G_IO_ERROR,
571 G_IO_ERROR_INVALID_FILENAME,
572 _("Invalid filename"));
575 char *display_name = g_filename_display_name (filename);
576 g_set_error (error, G_IO_ERROR,
577 g_io_error_from_errno (errsv),
578 _("Error opening file '%s': %s"),
579 display_name, g_strerror (errsv));
580 g_free (display_name);
585 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
586 stream->priv->fd = fd;
587 return G_FILE_OUTPUT_STREAM (stream);
591 _g_local_file_output_stream_create (const char *filename,
593 GFileCreateFlags flags,
594 GCancellable *cancellable,
597 GLocalFileOutputStream *stream;
602 if (g_cancellable_set_error_if_cancelled (cancellable, error))
605 if (flags & G_FILE_CREATE_PRIVATE)
610 open_flags = O_CREAT | O_EXCL | O_BINARY;
612 open_flags |= O_RDWR;
614 open_flags |= O_WRONLY;
616 fd = g_open (filename, open_flags, mode);
622 /* This must be an invalid filename, on e.g. FAT */
623 g_set_error_literal (error, G_IO_ERROR,
624 G_IO_ERROR_INVALID_FILENAME,
625 _("Invalid filename"));
628 char *display_name = g_filename_display_name (filename);
629 g_set_error (error, G_IO_ERROR,
630 g_io_error_from_errno (errsv),
631 _("Error opening file '%s': %s"),
632 display_name, g_strerror (errsv));
633 g_free (display_name);
638 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
639 stream->priv->fd = fd;
640 return G_FILE_OUTPUT_STREAM (stream);
644 _g_local_file_output_stream_append (const char *filename,
645 GFileCreateFlags flags,
646 GCancellable *cancellable,
649 GLocalFileOutputStream *stream;
653 if (g_cancellable_set_error_if_cancelled (cancellable, error))
656 if (flags & G_FILE_CREATE_PRIVATE)
661 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
667 /* This must be an invalid filename, on e.g. FAT */
668 g_set_error_literal (error, G_IO_ERROR,
669 G_IO_ERROR_INVALID_FILENAME,
670 _("Invalid filename"));
673 char *display_name = g_filename_display_name (filename);
674 g_set_error (error, G_IO_ERROR,
675 g_io_error_from_errno (errsv),
676 _("Error opening file '%s': %s"),
677 display_name, g_strerror (errsv));
678 g_free (display_name);
683 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
684 stream->priv->fd = fd;
686 return G_FILE_OUTPUT_STREAM (stream);
690 create_backup_filename (const char *filename)
692 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
695 #define BUFSIZE 8192 /* size of normal write buffer */
698 copy_file_data (gint sfd,
704 const gchar *write_buffer;
706 gssize bytes_to_write;
707 gssize bytes_written;
709 buffer = g_malloc (BUFSIZE);
713 bytes_read = read (sfd, buffer, BUFSIZE);
714 if (bytes_read == -1)
721 g_set_error (error, G_IO_ERROR,
722 g_io_error_from_errno (errsv),
723 _("Error reading from file: %s"),
729 bytes_to_write = bytes_read;
730 write_buffer = buffer;
734 bytes_written = write (dfd, write_buffer, bytes_to_write);
735 if (bytes_written == -1)
742 g_set_error (error, G_IO_ERROR,
743 g_io_error_from_errno (errsv),
744 _("Error writing to file: %s"),
750 bytes_to_write -= bytes_written;
751 write_buffer += bytes_written;
753 while (bytes_to_write > 0);
755 } while ((bytes_read != 0) && (ret == TRUE));
763 handle_overwrite_open (const char *filename,
766 gboolean create_backup,
767 char **temp_filename,
768 GFileCreateFlags flags,
769 GCancellable *cancellable,
773 GLocalFileStat original_stat;
780 if (flags & G_FILE_CREATE_PRIVATE)
785 /* We only need read access to the original file if we are creating a backup.
786 * We also add O_CREATE to avoid a race if the file was just removed */
787 if (create_backup || readable)
788 open_flags = O_RDWR | O_CREAT | O_BINARY;
790 open_flags = O_WRONLY | O_CREAT | O_BINARY;
792 /* Some systems have O_NOFOLLOW, which lets us avoid some races
793 * when finding out if the file we opened was a symlink */
796 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
797 if (fd == -1 && errno == ELOOP)
799 /* Could be a symlink, or it could be a regular ELOOP error,
800 * but then the next open will fail too. */
802 fd = g_open (filename, open_flags, mode);
805 fd = g_open (filename, open_flags, mode);
806 /* This is racy, but we do it as soon as possible to minimize the race */
807 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
813 char *display_name = g_filename_display_name (filename);
814 g_set_error (error, G_IO_ERROR,
815 g_io_error_from_errno (errsv),
816 _("Error opening file '%s': %s"),
817 display_name, g_strerror (errsv));
818 g_free (display_name);
823 res = _fstati64 (fd, &original_stat);
825 res = fstat (fd, &original_stat);
831 char *display_name = g_filename_display_name (filename);
832 g_set_error (error, G_IO_ERROR,
833 g_io_error_from_errno (errsv),
834 _("Error stating file '%s': %s"),
835 display_name, g_strerror (errsv));
836 g_free (display_name);
840 /* not a regular file */
841 if (!S_ISREG (original_stat.st_mode))
843 if (S_ISDIR (original_stat.st_mode))
844 g_set_error_literal (error,
846 G_IO_ERROR_IS_DIRECTORY,
847 _("Target file is a directory"));
849 g_set_error_literal (error,
851 G_IO_ERROR_NOT_REGULAR_FILE,
852 _("Target file is not a regular file"));
858 current_etag = _g_local_file_info_create_etag (&original_stat);
859 if (strcmp (etag, current_etag) != 0)
861 g_set_error_literal (error,
863 G_IO_ERROR_WRONG_ETAG,
864 _("The file was externally modified"));
865 g_free (current_etag);
868 g_free (current_etag);
871 /* We use two backup strategies.
872 * The first one (which is faster) consist in saving to a
873 * tmp file then rename the original file to the backup and the
874 * tmp file to the original name. This is fast but doesn't work
875 * when the file is a link (hard or symbolic) or when we can't
876 * write to the current dir or can't set the permissions on the
878 * The second strategy consist simply in copying the old file
879 * to a backup file and rewrite the contents of the file.
882 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
883 (!(original_stat.st_nlink > 1) && !is_symlink))
885 char *dirname, *tmp_filename;
888 dirname = g_path_get_dirname (filename);
889 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
892 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
895 g_free (tmp_filename);
896 goto fallback_strategy;
899 /* try to keep permissions (unless replacing) */
901 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
904 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
907 fchmod (tmpfd, original_stat.st_mode) == -1 ||
913 struct stat tmp_statbuf;
915 /* Check that we really needed to change something */
916 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
917 original_stat.st_uid != tmp_statbuf.st_uid ||
918 original_stat.st_gid != tmp_statbuf.st_gid ||
919 original_stat.st_mode != tmp_statbuf.st_mode)
922 g_unlink (tmp_filename);
923 g_free (tmp_filename);
924 goto fallback_strategy;
929 *temp_filename = tmp_filename;
937 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
938 struct stat tmp_statbuf;
940 char *backup_filename;
943 backup_filename = create_backup_filename (filename);
945 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
947 g_set_error_literal (error,
949 G_IO_ERROR_CANT_CREATE_BACKUP,
950 _("Backup file creation failed"));
951 g_free (backup_filename);
955 bfd = g_open (backup_filename,
956 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
957 original_stat.st_mode & 0777);
961 g_set_error_literal (error,
963 G_IO_ERROR_CANT_CREATE_BACKUP,
964 _("Backup file creation failed"));
965 g_free (backup_filename);
969 /* If needed, Try to set the group of the backup same as the
970 * original file. If this fails, set the protection
971 * bits for the group same as the protection bits for
973 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
974 if (fstat (bfd, &tmp_statbuf) != 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 g_free (backup_filename);
985 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
986 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
989 (original_stat.st_mode & 0707) |
990 ((original_stat.st_mode & 07) << 3)) != 0)
992 g_set_error_literal (error,
994 G_IO_ERROR_CANT_CREATE_BACKUP,
995 _("Backup file creation failed"));
996 g_unlink (backup_filename);
998 g_free (backup_filename);
1004 if (!copy_file_data (fd, bfd, NULL))
1006 g_set_error_literal (error,
1008 G_IO_ERROR_CANT_CREATE_BACKUP,
1009 _("Backup file creation failed"));
1010 g_unlink (backup_filename);
1012 g_free (backup_filename);
1018 g_free (backup_filename);
1020 /* Seek back to the start of the file after the backup copy */
1021 if (lseek (fd, 0, SEEK_SET) == -1)
1025 g_set_error (error, G_IO_ERROR,
1026 g_io_error_from_errno (errsv),
1027 _("Error seeking in file: %s"),
1028 g_strerror (errsv));
1033 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1037 if (g_unlink (filename) != 0)
1041 g_set_error (error, G_IO_ERROR,
1042 g_io_error_from_errno (errsv),
1043 _("Error removing old file: %s"),
1044 g_strerror (errsv));
1049 open_flags = O_RDWR | O_CREAT | O_BINARY;
1051 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1052 fd = g_open (filename, open_flags, mode);
1056 char *display_name = g_filename_display_name (filename);
1057 g_set_error (error, G_IO_ERROR,
1058 g_io_error_from_errno (errsv),
1059 _("Error opening file '%s': %s"),
1060 display_name, g_strerror (errsv));
1061 g_free (display_name);
1067 /* Truncate the file at the start */
1069 if (g_win32_ftruncate (fd, 0) == -1)
1071 if (ftruncate (fd, 0) == -1)
1076 g_set_error (error, G_IO_ERROR,
1077 g_io_error_from_errno (errsv),
1078 _("Error truncating file: %s"),
1079 g_strerror (errsv));
1093 _g_local_file_output_stream_replace (const char *filename,
1096 gboolean create_backup,
1097 GFileCreateFlags flags,
1098 GCancellable *cancellable,
1101 GLocalFileOutputStream *stream;
1105 gboolean sync_on_close;
1108 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1113 if (flags & G_FILE_CREATE_PRIVATE)
1117 sync_on_close = FALSE;
1119 /* If the file doesn't exist, create it */
1120 open_flags = O_CREAT | O_EXCL | O_BINARY;
1122 open_flags |= O_RDWR;
1124 open_flags |= O_WRONLY;
1125 fd = g_open (filename, open_flags, mode);
1127 if (fd == -1 && errno == EEXIST)
1129 /* The file already exists */
1130 fd = handle_overwrite_open (filename, readable, etag,
1131 create_backup, &temp_file,
1132 flags, cancellable, error);
1136 /* If the final destination exists, we want to sync the newly written
1137 * file to ensure the data is on disk when we rename over the destination.
1138 * otherwise if we get a system crash we can lose both the new and the
1139 * old file on some filesystems. (I.E. those that don't guarantee the
1140 * data is written to the disk before the metadata.)
1142 sync_on_close = TRUE;
1148 if (errsv == EINVAL)
1149 /* This must be an invalid filename, on e.g. FAT */
1150 g_set_error_literal (error, G_IO_ERROR,
1151 G_IO_ERROR_INVALID_FILENAME,
1152 _("Invalid filename"));
1155 char *display_name = g_filename_display_name (filename);
1156 g_set_error (error, G_IO_ERROR,
1157 g_io_error_from_errno (errsv),
1158 _("Error opening file '%s': %s"),
1159 display_name, g_strerror (errsv));
1160 g_free (display_name);
1166 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1167 stream->priv->fd = fd;
1168 stream->priv->sync_on_close = sync_on_close;
1169 stream->priv->tmp_filename = temp_file;
1171 stream->priv->backup_filename = create_backup_filename (filename);
1172 stream->priv->original_filename = g_strdup (filename);
1174 return G_FILE_OUTPUT_STREAM (stream);
1178 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1180 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1181 return stream->priv->fd;
1186 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1188 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1189 return _g_local_file_output_stream_get_fd (stream);