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 "gfiledescriptorbased.h"
40 #include "glocalfileoutputstream.h"
41 #include "glocalfileinfo.h"
46 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
49 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
59 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
60 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
61 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
62 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
63 g_file_descriptor_based_iface_init));
66 /* Some of the file replacement code was based on the code from gedit,
67 * relicenced to LGPL with permissions from the authors.
70 #define BACKUP_EXTENSION "~"
72 struct _GLocalFileOutputStreamPrivate {
74 char *original_filename;
75 char *backup_filename;
77 guint sync_on_close : 1;
82 static gssize g_local_file_output_stream_write (GOutputStream *stream,
85 GCancellable *cancellable,
87 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
88 GCancellable *cancellable,
90 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
91 const char *attributes,
92 GCancellable *cancellable,
94 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
95 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
96 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
97 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
100 GCancellable *cancellable,
102 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
103 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
105 GCancellable *cancellable,
107 static int g_local_file_output_stream_get_fd (GFileDescriptorBased *stream);
110 g_local_file_output_stream_finalize (GObject *object)
112 GLocalFileOutputStream *file;
114 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
116 g_free (file->priv->tmp_filename);
117 g_free (file->priv->original_filename);
118 g_free (file->priv->backup_filename);
119 g_free (file->priv->etag);
121 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
126 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
128 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
129 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
130 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
132 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
134 gobject_class->finalize = g_local_file_output_stream_finalize;
136 stream_class->write_fn = g_local_file_output_stream_write;
137 stream_class->close_fn = g_local_file_output_stream_close;
138 file_stream_class->query_info = g_local_file_output_stream_query_info;
139 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
140 file_stream_class->tell = g_local_file_output_stream_tell;
141 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
142 file_stream_class->seek = g_local_file_output_stream_seek;
143 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
144 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
148 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
150 iface->get_fd = g_local_file_output_stream_get_fd;
154 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
156 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
157 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
158 GLocalFileOutputStreamPrivate);
159 stream->priv->do_close = TRUE;
163 g_local_file_output_stream_write (GOutputStream *stream,
166 GCancellable *cancellable,
169 GLocalFileOutputStream *file;
172 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
176 if (g_cancellable_set_error_if_cancelled (cancellable, error))
178 res = write (file->priv->fd, buffer, count);
186 g_set_error (error, G_IO_ERROR,
187 g_io_error_from_errno (errsv),
188 _("Error writing to file: %s"),
199 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
202 out->priv->do_close = do_close;
206 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
207 GCancellable *cancellable,
210 GLocalFileStat final_stat;
214 if (file->priv->sync_on_close &&
215 fsync (file->priv->fd) != 0)
219 g_set_error (error, G_IO_ERROR,
220 g_io_error_from_errno (errsv),
221 _("Error writing to file: %s"),
229 /* Must close before renaming on Windows, so just do the close first
230 * in all cases for now.
232 if (_fstati64 (file->priv->fd, &final_stat) == 0)
233 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
235 res = close (file->priv->fd);
240 g_set_error (error, G_IO_ERROR,
241 g_io_error_from_errno (errsv),
242 _("Error closing file: %s"),
249 if (file->priv->tmp_filename)
251 /* We need to move the temp file to its final place,
252 * and possibly create the backup file
255 if (file->priv->backup_filename)
257 if (g_cancellable_set_error_if_cancelled (cancellable, error))
261 /* create original -> backup link, the original is then renamed over */
262 if (g_unlink (file->priv->backup_filename) != 0 &&
267 g_set_error (error, G_IO_ERROR,
268 G_IO_ERROR_CANT_CREATE_BACKUP,
269 _("Error removing old backup link: %s"),
274 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
276 /* link failed or is not supported, try rename */
277 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
281 g_set_error (error, G_IO_ERROR,
282 G_IO_ERROR_CANT_CREATE_BACKUP,
283 _("Error creating backup copy: %s"),
289 /* If link not supported, just 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"),
304 if (g_cancellable_set_error_if_cancelled (cancellable, error))
307 /* tmp -> original */
308 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
312 g_set_error (error, G_IO_ERROR,
313 g_io_error_from_errno (errsv),
314 _("Error renaming temporary file: %s"),
320 if (g_cancellable_set_error_if_cancelled (cancellable, error))
323 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
325 if (fstat (file->priv->fd, &final_stat) == 0)
326 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
330 res = close (file->priv->fd);
335 g_set_error (error, G_IO_ERROR,
336 g_io_error_from_errno (errsv),
337 _("Error closing file: %s"),
354 /* A simple try to close the fd in case we fail before the actual close */
355 close (file->priv->fd);
362 g_local_file_output_stream_close (GOutputStream *stream,
363 GCancellable *cancellable,
366 GLocalFileOutputStream *file;
368 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
370 if (file->priv->do_close)
371 return _g_local_file_output_stream_really_close (file,
378 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
380 GLocalFileOutputStream *file;
382 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
384 return g_strdup (file->priv->etag);
388 g_local_file_output_stream_tell (GFileOutputStream *stream)
390 GLocalFileOutputStream *file;
393 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
395 pos = lseek (file->priv->fd, 0, SEEK_CUR);
397 if (pos == (off_t)-1)
404 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
406 GLocalFileOutputStream *file;
409 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
411 pos = lseek (file->priv->fd, 0, SEEK_CUR);
413 if (pos == (off_t)-1 && errno == ESPIPE)
420 seek_type_to_lseek (GSeekType type)
437 g_local_file_output_stream_seek (GFileOutputStream *stream,
440 GCancellable *cancellable,
443 GLocalFileOutputStream *file;
446 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
448 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
450 if (pos == (off_t)-1)
454 g_set_error (error, G_IO_ERROR,
455 g_io_error_from_errno (errsv),
456 _("Error seeking in file: %s"),
465 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
467 /* We can't truncate pipes and stuff where we can't seek */
468 return g_local_file_output_stream_can_seek (stream);
472 g_local_file_output_stream_truncate (GFileOutputStream *stream,
474 GCancellable *cancellable,
477 GLocalFileOutputStream *file;
480 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
484 res = g_win32_ftruncate (file->priv->fd, size);
486 res = ftruncate (file->priv->fd, size);
495 if (g_cancellable_set_error_if_cancelled (cancellable, error))
500 g_set_error (error, G_IO_ERROR,
501 g_io_error_from_errno (errsv),
502 _("Error truncating file: %s"),
512 g_local_file_output_stream_query_info (GFileOutputStream *stream,
513 const char *attributes,
514 GCancellable *cancellable,
517 GLocalFileOutputStream *file;
519 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
521 if (g_cancellable_set_error_if_cancelled (cancellable, error))
524 return _g_local_file_info_get_from_fd (file->priv->fd,
530 _g_local_file_output_stream_open (const char *filename,
532 GCancellable *cancellable,
535 GLocalFileOutputStream *stream;
539 if (g_cancellable_set_error_if_cancelled (cancellable, error))
542 open_flags = O_BINARY;
544 open_flags |= O_RDWR;
546 open_flags |= O_WRONLY;
548 fd = g_open (filename, open_flags, 0666);
554 /* This must be an invalid filename, on e.g. FAT */
555 g_set_error_literal (error, G_IO_ERROR,
556 G_IO_ERROR_INVALID_FILENAME,
557 _("Invalid filename"));
560 char *display_name = g_filename_display_name (filename);
561 g_set_error (error, G_IO_ERROR,
562 g_io_error_from_errno (errsv),
563 _("Error opening file '%s': %s"),
564 display_name, g_strerror (errsv));
565 g_free (display_name);
570 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
571 stream->priv->fd = fd;
572 return G_FILE_OUTPUT_STREAM (stream);
576 _g_local_file_output_stream_create (const char *filename,
578 GFileCreateFlags flags,
579 GCancellable *cancellable,
582 GLocalFileOutputStream *stream;
587 if (g_cancellable_set_error_if_cancelled (cancellable, error))
590 if (flags & G_FILE_CREATE_PRIVATE)
595 open_flags = O_CREAT | O_EXCL | O_BINARY;
597 open_flags |= O_RDWR;
599 open_flags |= O_WRONLY;
601 fd = g_open (filename, open_flags, mode);
607 /* This must be an invalid filename, on e.g. FAT */
608 g_set_error_literal (error, G_IO_ERROR,
609 G_IO_ERROR_INVALID_FILENAME,
610 _("Invalid filename"));
613 char *display_name = g_filename_display_name (filename);
614 g_set_error (error, G_IO_ERROR,
615 g_io_error_from_errno (errsv),
616 _("Error opening file '%s': %s"),
617 display_name, g_strerror (errsv));
618 g_free (display_name);
623 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
624 stream->priv->fd = fd;
625 return G_FILE_OUTPUT_STREAM (stream);
629 _g_local_file_output_stream_append (const char *filename,
630 GFileCreateFlags flags,
631 GCancellable *cancellable,
634 GLocalFileOutputStream *stream;
638 if (g_cancellable_set_error_if_cancelled (cancellable, error))
641 if (flags & G_FILE_CREATE_PRIVATE)
646 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
652 /* This must be an invalid filename, on e.g. FAT */
653 g_set_error_literal (error, G_IO_ERROR,
654 G_IO_ERROR_INVALID_FILENAME,
655 _("Invalid filename"));
658 char *display_name = g_filename_display_name (filename);
659 g_set_error (error, G_IO_ERROR,
660 g_io_error_from_errno (errsv),
661 _("Error opening file '%s': %s"),
662 display_name, g_strerror (errsv));
663 g_free (display_name);
668 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
669 stream->priv->fd = fd;
671 return G_FILE_OUTPUT_STREAM (stream);
675 create_backup_filename (const char *filename)
677 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
680 #define BUFSIZE 8192 /* size of normal write buffer */
683 copy_file_data (gint sfd,
689 const gchar *write_buffer;
691 gssize bytes_to_write;
692 gssize bytes_written;
694 buffer = g_malloc (BUFSIZE);
698 bytes_read = read (sfd, buffer, BUFSIZE);
699 if (bytes_read == -1)
706 g_set_error (error, G_IO_ERROR,
707 g_io_error_from_errno (errsv),
708 _("Error reading from file: %s"),
714 bytes_to_write = bytes_read;
715 write_buffer = buffer;
719 bytes_written = write (dfd, write_buffer, bytes_to_write);
720 if (bytes_written == -1)
727 g_set_error (error, G_IO_ERROR,
728 g_io_error_from_errno (errsv),
729 _("Error writing to file: %s"),
735 bytes_to_write -= bytes_written;
736 write_buffer += bytes_written;
738 while (bytes_to_write > 0);
740 } while ((bytes_read != 0) && (ret == TRUE));
748 handle_overwrite_open (const char *filename,
751 gboolean create_backup,
752 char **temp_filename,
753 GFileCreateFlags flags,
754 GCancellable *cancellable,
758 GLocalFileStat original_stat;
765 if (flags & G_FILE_CREATE_PRIVATE)
770 /* We only need read access to the original file if we are creating a backup.
771 * We also add O_CREATE to avoid a race if the file was just removed */
772 if (create_backup || readable)
773 open_flags = O_RDWR | O_CREAT | O_BINARY;
775 open_flags = O_WRONLY | O_CREAT | O_BINARY;
777 /* Some systems have O_NOFOLLOW, which lets us avoid some races
778 * when finding out if the file we opened was a symlink */
781 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
782 if (fd == -1 && errno == ELOOP)
784 /* Could be a symlink, or it could be a regular ELOOP error,
785 * but then the next open will fail too. */
787 fd = g_open (filename, open_flags, mode);
790 fd = g_open (filename, open_flags, mode);
791 /* This is racy, but we do it as soon as possible to minimize the race */
792 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
798 char *display_name = g_filename_display_name (filename);
799 g_set_error (error, G_IO_ERROR,
800 g_io_error_from_errno (errsv),
801 _("Error opening file '%s': %s"),
802 display_name, g_strerror (errsv));
803 g_free (display_name);
808 res = _fstati64 (fd, &original_stat);
810 res = fstat (fd, &original_stat);
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 stating file '%s': %s"),
820 display_name, g_strerror (errsv));
821 g_free (display_name);
825 /* not a regular file */
826 if (!S_ISREG (original_stat.st_mode))
828 if (S_ISDIR (original_stat.st_mode))
829 g_set_error_literal (error,
831 G_IO_ERROR_IS_DIRECTORY,
832 _("Target file is a directory"));
834 g_set_error_literal (error,
836 G_IO_ERROR_NOT_REGULAR_FILE,
837 _("Target file is not a regular file"));
843 current_etag = _g_local_file_info_create_etag (&original_stat);
844 if (strcmp (etag, current_etag) != 0)
846 g_set_error_literal (error,
848 G_IO_ERROR_WRONG_ETAG,
849 _("The file was externally modified"));
850 g_free (current_etag);
853 g_free (current_etag);
856 /* We use two backup strategies.
857 * The first one (which is faster) consist in saving to a
858 * tmp file then rename the original file to the backup and the
859 * tmp file to the original name. This is fast but doesn't work
860 * when the file is a link (hard or symbolic) or when we can't
861 * write to the current dir or can't set the permissions on the
863 * The second strategy consist simply in copying the old file
864 * to a backup file and rewrite the contents of the file.
867 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
868 (!(original_stat.st_nlink > 1) && !is_symlink))
870 char *dirname, *tmp_filename;
873 dirname = g_path_get_dirname (filename);
874 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
877 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
880 g_free (tmp_filename);
881 goto fallback_strategy;
884 /* try to keep permissions (unless replacing) */
886 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
889 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
892 fchmod (tmpfd, original_stat.st_mode) == -1 ||
898 struct stat tmp_statbuf;
900 /* Check that we really needed to change something */
901 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
902 original_stat.st_uid != tmp_statbuf.st_uid ||
903 original_stat.st_gid != tmp_statbuf.st_gid ||
904 original_stat.st_mode != tmp_statbuf.st_mode)
907 g_unlink (tmp_filename);
908 g_free (tmp_filename);
909 goto fallback_strategy;
914 *temp_filename = tmp_filename;
922 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
923 struct stat tmp_statbuf;
925 char *backup_filename;
928 backup_filename = create_backup_filename (filename);
930 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
932 g_set_error_literal (error,
934 G_IO_ERROR_CANT_CREATE_BACKUP,
935 _("Backup file creation failed"));
936 g_free (backup_filename);
940 bfd = g_open (backup_filename,
941 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
942 original_stat.st_mode & 0777);
946 g_set_error_literal (error,
948 G_IO_ERROR_CANT_CREATE_BACKUP,
949 _("Backup file creation failed"));
950 g_free (backup_filename);
954 /* If needed, Try to set the group of the backup same as the
955 * original file. If this fails, set the protection
956 * bits for the group same as the protection bits for
958 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
959 if (fstat (bfd, &tmp_statbuf) != 0)
961 g_set_error_literal (error,
963 G_IO_ERROR_CANT_CREATE_BACKUP,
964 _("Backup file creation failed"));
965 g_unlink (backup_filename);
966 g_free (backup_filename);
970 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
971 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
974 (original_stat.st_mode & 0707) |
975 ((original_stat.st_mode & 07) << 3)) != 0)
977 g_set_error_literal (error,
979 G_IO_ERROR_CANT_CREATE_BACKUP,
980 _("Backup file creation failed"));
981 g_unlink (backup_filename);
983 g_free (backup_filename);
989 if (!copy_file_data (fd, bfd, NULL))
991 g_set_error_literal (error,
993 G_IO_ERROR_CANT_CREATE_BACKUP,
994 _("Backup file creation failed"));
995 g_unlink (backup_filename);
997 g_free (backup_filename);
1003 g_free (backup_filename);
1005 /* Seek back to the start of the file after the backup copy */
1006 if (lseek (fd, 0, SEEK_SET) == -1)
1010 g_set_error (error, G_IO_ERROR,
1011 g_io_error_from_errno (errsv),
1012 _("Error seeking in file: %s"),
1013 g_strerror (errsv));
1018 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1022 if (g_unlink (filename) != 0)
1026 g_set_error (error, G_IO_ERROR,
1027 g_io_error_from_errno (errsv),
1028 _("Error removing old file: %s"),
1029 g_strerror (errsv));
1034 open_flags = O_RDWR | O_CREAT | O_BINARY;
1036 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1037 fd = g_open (filename, open_flags, mode);
1041 char *display_name = g_filename_display_name (filename);
1042 g_set_error (error, G_IO_ERROR,
1043 g_io_error_from_errno (errsv),
1044 _("Error opening file '%s': %s"),
1045 display_name, g_strerror (errsv));
1046 g_free (display_name);
1052 /* Truncate the file at the start */
1054 if (g_win32_ftruncate (fd, 0) == -1)
1056 if (ftruncate (fd, 0) == -1)
1061 g_set_error (error, G_IO_ERROR,
1062 g_io_error_from_errno (errsv),
1063 _("Error truncating file: %s"),
1064 g_strerror (errsv));
1078 _g_local_file_output_stream_replace (const char *filename,
1081 gboolean create_backup,
1082 GFileCreateFlags flags,
1083 GCancellable *cancellable,
1086 GLocalFileOutputStream *stream;
1090 gboolean sync_on_close;
1093 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1098 if (flags & G_FILE_CREATE_PRIVATE)
1102 sync_on_close = FALSE;
1104 /* If the file doesn't exist, create it */
1105 open_flags = O_CREAT | O_EXCL | O_BINARY;
1107 open_flags |= O_RDWR;
1109 open_flags |= O_WRONLY;
1110 fd = g_open (filename, open_flags, mode);
1112 if (fd == -1 && errno == EEXIST)
1114 /* The file already exists */
1115 fd = handle_overwrite_open (filename, readable, etag,
1116 create_backup, &temp_file,
1117 flags, cancellable, error);
1121 /* If the final destination exists, we want to sync the newly written
1122 * file to ensure the data is on disk when we rename over the destination.
1123 * otherwise if we get a system crash we can lose both the new and the
1124 * old file on some filesystems. (I.E. those that don't guarantee the
1125 * data is written to the disk before the metadata.)
1127 sync_on_close = TRUE;
1133 if (errsv == EINVAL)
1134 /* This must be an invalid filename, on e.g. FAT */
1135 g_set_error_literal (error, G_IO_ERROR,
1136 G_IO_ERROR_INVALID_FILENAME,
1137 _("Invalid filename"));
1140 char *display_name = g_filename_display_name (filename);
1141 g_set_error (error, G_IO_ERROR,
1142 g_io_error_from_errno (errsv),
1143 _("Error opening file '%s': %s"),
1144 display_name, g_strerror (errsv));
1145 g_free (display_name);
1151 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1152 stream->priv->fd = fd;
1153 stream->priv->sync_on_close = sync_on_close;
1154 stream->priv->tmp_filename = temp_file;
1156 stream->priv->backup_filename = create_backup_filename (filename);
1157 stream->priv->original_filename = g_strdup (filename);
1159 return G_FILE_OUTPUT_STREAM (stream);
1163 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1165 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1167 return stream->priv->fd;