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
67 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)
75 /* Some of the file replacement code was based on the code from gedit,
76 * relicenced to LGPL with permissions from the authors.
79 #define BACKUP_EXTENSION "~"
81 struct _GLocalFileOutputStreamPrivate {
83 char *original_filename;
84 char *backup_filename;
86 guint sync_on_close : 1;
91 static gssize g_local_file_output_stream_write (GOutputStream *stream,
94 GCancellable *cancellable,
96 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
97 GCancellable *cancellable,
99 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
100 const char *attributes,
101 GCancellable *cancellable,
103 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
104 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
105 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
106 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
109 GCancellable *cancellable,
111 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
112 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
114 GCancellable *cancellable,
117 static int g_local_file_output_stream_get_fd (GFileDescriptorBased *stream);
121 g_local_file_output_stream_finalize (GObject *object)
123 GLocalFileOutputStream *file;
125 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
127 g_free (file->priv->tmp_filename);
128 g_free (file->priv->original_filename);
129 g_free (file->priv->backup_filename);
130 g_free (file->priv->etag);
132 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
137 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
139 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
141 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
143 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
145 gobject_class->finalize = g_local_file_output_stream_finalize;
147 stream_class->write_fn = g_local_file_output_stream_write;
148 stream_class->close_fn = g_local_file_output_stream_close;
149 file_stream_class->query_info = g_local_file_output_stream_query_info;
150 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
151 file_stream_class->tell = g_local_file_output_stream_tell;
152 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
153 file_stream_class->seek = g_local_file_output_stream_seek;
154 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
155 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
160 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
162 iface->get_fd = g_local_file_output_stream_get_fd;
167 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
169 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
170 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
171 GLocalFileOutputStreamPrivate);
172 stream->priv->do_close = TRUE;
176 g_local_file_output_stream_write (GOutputStream *stream,
179 GCancellable *cancellable,
182 GLocalFileOutputStream *file;
185 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
189 if (g_cancellable_set_error_if_cancelled (cancellable, error))
191 res = write (file->priv->fd, buffer, count);
199 g_set_error (error, G_IO_ERROR,
200 g_io_error_from_errno (errsv),
201 _("Error writing to file: %s"),
212 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
215 out->priv->do_close = do_close;
219 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
220 GCancellable *cancellable,
223 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 res = close (file->priv->fd);
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"),
333 if (g_cancellable_set_error_if_cancelled (cancellable, error))
336 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
338 if (fstat (file->priv->fd, &final_stat) == 0)
339 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
343 res = close (file->priv->fd);
348 g_set_error (error, G_IO_ERROR,
349 g_io_error_from_errno (errsv),
350 _("Error closing file: %s"),
367 /* A simple try to close the fd in case we fail before the actual close */
368 close (file->priv->fd);
375 g_local_file_output_stream_close (GOutputStream *stream,
376 GCancellable *cancellable,
379 GLocalFileOutputStream *file;
381 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
383 if (file->priv->do_close)
384 return _g_local_file_output_stream_really_close (file,
391 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
393 GLocalFileOutputStream *file;
395 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
397 return g_strdup (file->priv->etag);
401 g_local_file_output_stream_tell (GFileOutputStream *stream)
403 GLocalFileOutputStream *file;
406 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
408 pos = lseek (file->priv->fd, 0, SEEK_CUR);
410 if (pos == (off_t)-1)
417 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
419 GLocalFileOutputStream *file;
422 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
424 pos = lseek (file->priv->fd, 0, SEEK_CUR);
426 if (pos == (off_t)-1 && errno == ESPIPE)
433 seek_type_to_lseek (GSeekType type)
450 g_local_file_output_stream_seek (GFileOutputStream *stream,
453 GCancellable *cancellable,
456 GLocalFileOutputStream *file;
459 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
461 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
463 if (pos == (off_t)-1)
467 g_set_error (error, G_IO_ERROR,
468 g_io_error_from_errno (errsv),
469 _("Error seeking in file: %s"),
478 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
480 /* We can't truncate pipes and stuff where we can't seek */
481 return g_local_file_output_stream_can_seek (stream);
485 g_local_file_output_stream_truncate (GFileOutputStream *stream,
487 GCancellable *cancellable,
490 GLocalFileOutputStream *file;
493 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
497 res = g_win32_ftruncate (file->priv->fd, size);
499 res = ftruncate (file->priv->fd, size);
508 if (g_cancellable_set_error_if_cancelled (cancellable, error))
513 g_set_error (error, G_IO_ERROR,
514 g_io_error_from_errno (errsv),
515 _("Error truncating file: %s"),
525 g_local_file_output_stream_query_info (GFileOutputStream *stream,
526 const char *attributes,
527 GCancellable *cancellable,
530 GLocalFileOutputStream *file;
532 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
534 if (g_cancellable_set_error_if_cancelled (cancellable, error))
537 return _g_local_file_info_get_from_fd (file->priv->fd,
543 _g_local_file_output_stream_open (const char *filename,
545 GCancellable *cancellable,
548 GLocalFileOutputStream *stream;
552 if (g_cancellable_set_error_if_cancelled (cancellable, error))
555 open_flags = O_BINARY;
557 open_flags |= O_RDWR;
559 open_flags |= O_WRONLY;
561 fd = g_open (filename, open_flags, 0666);
567 /* This must be an invalid filename, on e.g. FAT */
568 g_set_error_literal (error, G_IO_ERROR,
569 G_IO_ERROR_INVALID_FILENAME,
570 _("Invalid filename"));
573 char *display_name = g_filename_display_name (filename);
574 g_set_error (error, G_IO_ERROR,
575 g_io_error_from_errno (errsv),
576 _("Error opening file '%s': %s"),
577 display_name, g_strerror (errsv));
578 g_free (display_name);
583 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
584 stream->priv->fd = fd;
585 return G_FILE_OUTPUT_STREAM (stream);
589 _g_local_file_output_stream_create (const char *filename,
591 GFileCreateFlags flags,
592 GCancellable *cancellable,
595 GLocalFileOutputStream *stream;
600 if (g_cancellable_set_error_if_cancelled (cancellable, error))
603 if (flags & G_FILE_CREATE_PRIVATE)
608 open_flags = O_CREAT | O_EXCL | O_BINARY;
610 open_flags |= O_RDWR;
612 open_flags |= O_WRONLY;
614 fd = g_open (filename, open_flags, mode);
620 /* This must be an invalid filename, on e.g. FAT */
621 g_set_error_literal (error, G_IO_ERROR,
622 G_IO_ERROR_INVALID_FILENAME,
623 _("Invalid filename"));
626 char *display_name = g_filename_display_name (filename);
627 g_set_error (error, G_IO_ERROR,
628 g_io_error_from_errno (errsv),
629 _("Error opening file '%s': %s"),
630 display_name, g_strerror (errsv));
631 g_free (display_name);
636 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
637 stream->priv->fd = fd;
638 return G_FILE_OUTPUT_STREAM (stream);
642 _g_local_file_output_stream_append (const char *filename,
643 GFileCreateFlags flags,
644 GCancellable *cancellable,
647 GLocalFileOutputStream *stream;
651 if (g_cancellable_set_error_if_cancelled (cancellable, error))
654 if (flags & G_FILE_CREATE_PRIVATE)
659 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
665 /* This must be an invalid filename, on e.g. FAT */
666 g_set_error_literal (error, G_IO_ERROR,
667 G_IO_ERROR_INVALID_FILENAME,
668 _("Invalid filename"));
671 char *display_name = g_filename_display_name (filename);
672 g_set_error (error, G_IO_ERROR,
673 g_io_error_from_errno (errsv),
674 _("Error opening file '%s': %s"),
675 display_name, g_strerror (errsv));
676 g_free (display_name);
681 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
682 stream->priv->fd = fd;
684 return G_FILE_OUTPUT_STREAM (stream);
688 create_backup_filename (const char *filename)
690 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
693 #define BUFSIZE 8192 /* size of normal write buffer */
696 copy_file_data (gint sfd,
702 const gchar *write_buffer;
704 gssize bytes_to_write;
705 gssize bytes_written;
707 buffer = g_malloc (BUFSIZE);
711 bytes_read = read (sfd, buffer, BUFSIZE);
712 if (bytes_read == -1)
719 g_set_error (error, G_IO_ERROR,
720 g_io_error_from_errno (errsv),
721 _("Error reading from file: %s"),
727 bytes_to_write = bytes_read;
728 write_buffer = buffer;
732 bytes_written = write (dfd, write_buffer, bytes_to_write);
733 if (bytes_written == -1)
740 g_set_error (error, G_IO_ERROR,
741 g_io_error_from_errno (errsv),
742 _("Error writing to file: %s"),
748 bytes_to_write -= bytes_written;
749 write_buffer += bytes_written;
751 while (bytes_to_write > 0);
753 } while ((bytes_read != 0) && (ret == TRUE));
761 handle_overwrite_open (const char *filename,
764 gboolean create_backup,
765 char **temp_filename,
766 GFileCreateFlags flags,
767 GCancellable *cancellable,
771 GLocalFileStat original_stat;
778 if (flags & G_FILE_CREATE_PRIVATE)
783 /* We only need read access to the original file if we are creating a backup.
784 * We also add O_CREATE to avoid a race if the file was just removed */
785 if (create_backup || readable)
786 open_flags = O_RDWR | O_CREAT | O_BINARY;
788 open_flags = O_WRONLY | O_CREAT | O_BINARY;
790 /* Some systems have O_NOFOLLOW, which lets us avoid some races
791 * when finding out if the file we opened was a symlink */
794 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
795 if (fd == -1 && errno == ELOOP)
797 /* Could be a symlink, or it could be a regular ELOOP error,
798 * but then the next open will fail too. */
800 fd = g_open (filename, open_flags, mode);
803 fd = g_open (filename, open_flags, mode);
804 /* This is racy, but we do it as soon as possible to minimize the race */
805 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
811 char *display_name = g_filename_display_name (filename);
812 g_set_error (error, G_IO_ERROR,
813 g_io_error_from_errno (errsv),
814 _("Error opening file '%s': %s"),
815 display_name, g_strerror (errsv));
816 g_free (display_name);
821 res = _fstati64 (fd, &original_stat);
823 res = fstat (fd, &original_stat);
829 char *display_name = g_filename_display_name (filename);
830 g_set_error (error, G_IO_ERROR,
831 g_io_error_from_errno (errsv),
832 _("Error stating file '%s': %s"),
833 display_name, g_strerror (errsv));
834 g_free (display_name);
838 /* not a regular file */
839 if (!S_ISREG (original_stat.st_mode))
841 if (S_ISDIR (original_stat.st_mode))
842 g_set_error_literal (error,
844 G_IO_ERROR_IS_DIRECTORY,
845 _("Target file is a directory"));
847 g_set_error_literal (error,
849 G_IO_ERROR_NOT_REGULAR_FILE,
850 _("Target file is not a regular file"));
856 current_etag = _g_local_file_info_create_etag (&original_stat);
857 if (strcmp (etag, current_etag) != 0)
859 g_set_error_literal (error,
861 G_IO_ERROR_WRONG_ETAG,
862 _("The file was externally modified"));
863 g_free (current_etag);
866 g_free (current_etag);
869 /* We use two backup strategies.
870 * The first one (which is faster) consist in saving to a
871 * tmp file then rename the original file to the backup and the
872 * tmp file to the original name. This is fast but doesn't work
873 * when the file is a link (hard or symbolic) or when we can't
874 * write to the current dir or can't set the permissions on the
876 * The second strategy consist simply in copying the old file
877 * to a backup file and rewrite the contents of the file.
880 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
881 (!(original_stat.st_nlink > 1) && !is_symlink))
883 char *dirname, *tmp_filename;
886 dirname = g_path_get_dirname (filename);
887 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
890 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
893 g_free (tmp_filename);
894 goto fallback_strategy;
897 /* try to keep permissions (unless replacing) */
899 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
902 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
905 fchmod (tmpfd, original_stat.st_mode) == -1 ||
911 struct stat tmp_statbuf;
913 /* Check that we really needed to change something */
914 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
915 original_stat.st_uid != tmp_statbuf.st_uid ||
916 original_stat.st_gid != tmp_statbuf.st_gid ||
917 original_stat.st_mode != tmp_statbuf.st_mode)
920 g_unlink (tmp_filename);
921 g_free (tmp_filename);
922 goto fallback_strategy;
927 *temp_filename = tmp_filename;
935 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
936 struct stat tmp_statbuf;
938 char *backup_filename;
941 backup_filename = create_backup_filename (filename);
943 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
945 g_set_error_literal (error,
947 G_IO_ERROR_CANT_CREATE_BACKUP,
948 _("Backup file creation failed"));
949 g_free (backup_filename);
953 bfd = g_open (backup_filename,
954 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
955 original_stat.st_mode & 0777);
959 g_set_error_literal (error,
961 G_IO_ERROR_CANT_CREATE_BACKUP,
962 _("Backup file creation failed"));
963 g_free (backup_filename);
967 /* If needed, Try to set the group of the backup same as the
968 * original file. If this fails, set the protection
969 * bits for the group same as the protection bits for
971 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
972 if (fstat (bfd, &tmp_statbuf) != 0)
974 g_set_error_literal (error,
976 G_IO_ERROR_CANT_CREATE_BACKUP,
977 _("Backup file creation failed"));
978 g_unlink (backup_filename);
979 g_free (backup_filename);
983 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
984 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
987 (original_stat.st_mode & 0707) |
988 ((original_stat.st_mode & 07) << 3)) != 0)
990 g_set_error_literal (error,
992 G_IO_ERROR_CANT_CREATE_BACKUP,
993 _("Backup file creation failed"));
994 g_unlink (backup_filename);
996 g_free (backup_filename);
1002 if (!copy_file_data (fd, bfd, NULL))
1004 g_set_error_literal (error,
1006 G_IO_ERROR_CANT_CREATE_BACKUP,
1007 _("Backup file creation failed"));
1008 g_unlink (backup_filename);
1010 g_free (backup_filename);
1016 g_free (backup_filename);
1018 /* Seek back to the start of the file after the backup copy */
1019 if (lseek (fd, 0, SEEK_SET) == -1)
1023 g_set_error (error, G_IO_ERROR,
1024 g_io_error_from_errno (errsv),
1025 _("Error seeking in file: %s"),
1026 g_strerror (errsv));
1031 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1035 if (g_unlink (filename) != 0)
1039 g_set_error (error, G_IO_ERROR,
1040 g_io_error_from_errno (errsv),
1041 _("Error removing old file: %s"),
1042 g_strerror (errsv));
1047 open_flags = O_RDWR | O_CREAT | O_BINARY;
1049 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1050 fd = g_open (filename, open_flags, mode);
1054 char *display_name = g_filename_display_name (filename);
1055 g_set_error (error, G_IO_ERROR,
1056 g_io_error_from_errno (errsv),
1057 _("Error opening file '%s': %s"),
1058 display_name, g_strerror (errsv));
1059 g_free (display_name);
1065 /* Truncate the file at the start */
1067 if (g_win32_ftruncate (fd, 0) == -1)
1069 if (ftruncate (fd, 0) == -1)
1074 g_set_error (error, G_IO_ERROR,
1075 g_io_error_from_errno (errsv),
1076 _("Error truncating file: %s"),
1077 g_strerror (errsv));
1091 _g_local_file_output_stream_replace (const char *filename,
1094 gboolean create_backup,
1095 GFileCreateFlags flags,
1096 GCancellable *cancellable,
1099 GLocalFileOutputStream *stream;
1103 gboolean sync_on_close;
1106 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1111 if (flags & G_FILE_CREATE_PRIVATE)
1115 sync_on_close = FALSE;
1117 /* If the file doesn't exist, create it */
1118 open_flags = O_CREAT | O_EXCL | O_BINARY;
1120 open_flags |= O_RDWR;
1122 open_flags |= O_WRONLY;
1123 fd = g_open (filename, open_flags, mode);
1125 if (fd == -1 && errno == EEXIST)
1127 /* The file already exists */
1128 fd = handle_overwrite_open (filename, readable, etag,
1129 create_backup, &temp_file,
1130 flags, cancellable, error);
1134 /* If the final destination exists, we want to sync the newly written
1135 * file to ensure the data is on disk when we rename over the destination.
1136 * otherwise if we get a system crash we can lose both the new and the
1137 * old file on some filesystems. (I.E. those that don't guarantee the
1138 * data is written to the disk before the metadata.)
1140 sync_on_close = TRUE;
1146 if (errsv == EINVAL)
1147 /* This must be an invalid filename, on e.g. FAT */
1148 g_set_error_literal (error, G_IO_ERROR,
1149 G_IO_ERROR_INVALID_FILENAME,
1150 _("Invalid filename"));
1153 char *display_name = g_filename_display_name (filename);
1154 g_set_error (error, G_IO_ERROR,
1155 g_io_error_from_errno (errsv),
1156 _("Error opening file '%s': %s"),
1157 display_name, g_strerror (errsv));
1158 g_free (display_name);
1164 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1165 stream->priv->fd = fd;
1166 stream->priv->sync_on_close = sync_on_close;
1167 stream->priv->tmp_filename = temp_file;
1169 stream->priv->backup_filename = create_backup_filename (filename);
1170 stream->priv->original_filename = g_strdup (filename);
1172 return G_FILE_OUTPUT_STREAM (stream);
1176 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1178 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1179 return stream->priv->fd;
1184 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1186 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1187 return _g_local_file_output_stream_get_fd (stream);