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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
21 /* Needed for the statx() calls in inline functions in glocalfileinfo.h */
28 #include <sys/types.h>
35 #include <glib/gstdio.h>
38 #include "gcancellable.h"
39 #include "glocalfileoutputstream.h"
40 #include "gfileinfo.h"
41 #include "glocalfileinfo.h"
45 #include "gfiledescriptorbased.h"
49 #include "glib-private.h"
50 #include "gioprivate.h"
55 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
58 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
66 struct _GLocalFileOutputStreamPrivate {
68 char *original_filename;
69 char *backup_filename;
71 guint sync_on_close : 1;
77 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
80 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
82 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
83 G_ADD_PRIVATE (GLocalFileOutputStream)
84 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
85 g_file_descriptor_based_iface_init))
87 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
88 G_ADD_PRIVATE (GLocalFileOutputStream))
92 /* Some of the file replacement code was based on the code from gedit,
93 * relicenced to LGPL with permissions from the authors.
96 #define BACKUP_EXTENSION "~"
98 static gssize g_local_file_output_stream_write (GOutputStream *stream,
101 GCancellable *cancellable,
104 static gboolean g_local_file_output_stream_writev (GOutputStream *stream,
105 const GOutputVector *vectors,
107 gsize *bytes_written,
108 GCancellable *cancellable,
111 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
112 GCancellable *cancellable,
114 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
115 const char *attributes,
116 GCancellable *cancellable,
118 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
119 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
120 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
121 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
124 GCancellable *cancellable,
126 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
127 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
129 GCancellable *cancellable,
132 static int g_local_file_output_stream_get_fd (GFileDescriptorBased *stream);
136 g_local_file_output_stream_finalize (GObject *object)
138 GLocalFileOutputStream *file;
140 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
142 g_free (file->priv->tmp_filename);
143 g_free (file->priv->original_filename);
144 g_free (file->priv->backup_filename);
145 g_free (file->priv->etag);
147 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
151 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
153 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
154 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
155 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
157 gobject_class->finalize = g_local_file_output_stream_finalize;
159 stream_class->write_fn = g_local_file_output_stream_write;
161 stream_class->writev_fn = g_local_file_output_stream_writev;
163 stream_class->close_fn = g_local_file_output_stream_close;
164 file_stream_class->query_info = g_local_file_output_stream_query_info;
165 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
166 file_stream_class->tell = g_local_file_output_stream_tell;
167 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
168 file_stream_class->seek = g_local_file_output_stream_seek;
169 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
170 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
175 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
177 iface->get_fd = g_local_file_output_stream_get_fd;
182 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
184 stream->priv = g_local_file_output_stream_get_instance_private (stream);
185 stream->priv->do_close = TRUE;
189 g_local_file_output_stream_write (GOutputStream *stream,
192 GCancellable *cancellable,
195 GLocalFileOutputStream *file;
198 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
202 if (g_cancellable_set_error_if_cancelled (cancellable, error))
204 res = write (file->priv->fd, buffer, count);
212 g_set_error (error, G_IO_ERROR,
213 g_io_error_from_errno (errsv),
214 _("Error writing to file: %s"),
224 /* On Windows there is no equivalent API for files. The closest API to that is
225 * WriteFileGather() but it is useless in general: it requires, among other
226 * things, that each chunk is the size of a whole page and in memory aligned
227 * to a page. We can't possibly guarantee that in GLib.
230 /* Macro to check if struct iovec and GOutputVector have the same ABI */
231 #define G_OUTPUT_VECTOR_IS_IOVEC (sizeof (struct iovec) == sizeof (GOutputVector) && \
232 G_SIZEOF_MEMBER (struct iovec, iov_base) == G_SIZEOF_MEMBER (GOutputVector, buffer) && \
233 G_STRUCT_OFFSET (struct iovec, iov_base) == G_STRUCT_OFFSET (GOutputVector, buffer) && \
234 G_SIZEOF_MEMBER (struct iovec, iov_len) == G_SIZEOF_MEMBER (GOutputVector, size) && \
235 G_STRUCT_OFFSET (struct iovec, iov_len) == G_STRUCT_OFFSET (GOutputVector, size))
238 g_local_file_output_stream_writev (GOutputStream *stream,
239 const GOutputVector *vectors,
241 gsize *bytes_written,
242 GCancellable *cancellable,
245 GLocalFileOutputStream *file;
252 /* Clamp the number of vectors if more given than we can write in one go.
253 * The caller has to handle short writes anyway.
255 if (n_vectors > G_IOV_MAX)
256 n_vectors = G_IOV_MAX;
258 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
260 if (G_OUTPUT_VECTOR_IS_IOVEC)
262 /* ABI is compatible */
263 iov = (struct iovec *) vectors;
269 /* ABI is incompatible */
270 iov = g_newa (struct iovec, n_vectors);
271 for (i = 0; i < n_vectors; i++)
273 iov[i].iov_base = (void *)vectors[i].buffer;
274 iov[i].iov_len = vectors[i].size;
280 if (g_cancellable_set_error_if_cancelled (cancellable, error))
282 res = writev (file->priv->fd, iov, n_vectors);
290 g_set_error (error, G_IO_ERROR,
291 g_io_error_from_errno (errsv),
292 _("Error writing to file: %s"),
295 else if (bytes_written)
297 *bytes_written = res;
308 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
311 out->priv->do_close = do_close;
315 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
316 GCancellable *cancellable,
319 GLocalFileStat final_stat;
321 if (file->priv->sync_on_close &&
322 g_fsync (file->priv->fd) != 0)
326 g_set_error (error, G_IO_ERROR,
327 g_io_error_from_errno (errsv),
328 _("Error writing to file: %s"),
335 /* Must close before renaming on Windows, so just do the close first
336 * in all cases for now.
338 if (GLIB_PRIVATE_CALL (g_win32_fstat) (file->priv->fd, &final_stat) == 0)
339 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
341 if (!g_close (file->priv->fd, NULL))
345 g_set_error (error, G_IO_ERROR,
346 g_io_error_from_errno (errsv),
347 _("Error closing file: %s"),
354 if (file->priv->tmp_filename)
356 /* We need to move the temp file to its final place,
357 * and possibly create the backup file
360 if (file->priv->backup_filename)
362 if (g_cancellable_set_error_if_cancelled (cancellable, error))
366 /* create original -> backup link, the original is then renamed over */
367 if (g_unlink (file->priv->backup_filename) != 0 &&
372 g_set_error (error, G_IO_ERROR,
373 G_IO_ERROR_CANT_CREATE_BACKUP,
374 _("Error removing old backup link: %s"),
379 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
381 /* link failed or is not supported, try rename */
382 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
386 g_set_error (error, G_IO_ERROR,
387 G_IO_ERROR_CANT_CREATE_BACKUP,
388 _("Error creating backup copy: %s"),
394 /* If link not supported, just rename... */
395 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
399 g_set_error (error, G_IO_ERROR,
400 G_IO_ERROR_CANT_CREATE_BACKUP,
401 _("Error creating backup copy: %s"),
409 if (g_cancellable_set_error_if_cancelled (cancellable, error))
412 /* tmp -> original */
413 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
417 g_set_error (error, G_IO_ERROR,
418 g_io_error_from_errno (errsv),
419 _("Error renaming temporary file: %s"),
424 g_clear_pointer (&file->priv->tmp_filename, g_free);
427 if (g_cancellable_set_error_if_cancelled (cancellable, error))
430 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
432 if (g_local_file_fstat (file->priv->fd, G_LOCAL_FILE_STAT_FIELD_MTIME, G_LOCAL_FILE_STAT_FIELD_ALL, &final_stat) == 0)
433 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
435 if (!g_close (file->priv->fd, NULL))
439 g_set_error (error, G_IO_ERROR,
440 g_io_error_from_errno (errsv),
441 _("Error closing file: %s"),
452 /* A simple try to close the fd in case we fail before the actual close */
453 g_close (file->priv->fd, NULL);
455 if (file->priv->tmp_filename)
456 g_unlink (file->priv->tmp_filename);
463 g_local_file_output_stream_close (GOutputStream *stream,
464 GCancellable *cancellable,
467 GLocalFileOutputStream *file;
469 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
471 if (file->priv->do_close)
472 return _g_local_file_output_stream_really_close (file,
479 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
481 GLocalFileOutputStream *file;
483 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
485 return g_strdup (file->priv->etag);
489 g_local_file_output_stream_tell (GFileOutputStream *stream)
491 GLocalFileOutputStream *file;
494 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
496 pos = lseek (file->priv->fd, 0, SEEK_CUR);
498 if (pos == (off_t)-1)
505 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
507 GLocalFileOutputStream *file;
510 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
512 pos = lseek (file->priv->fd, 0, SEEK_CUR);
514 if (pos == (off_t)-1 && errno == ESPIPE)
521 seek_type_to_lseek (GSeekType type)
538 g_local_file_output_stream_seek (GFileOutputStream *stream,
541 GCancellable *cancellable,
544 GLocalFileOutputStream *file;
547 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
549 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
551 if (pos == (off_t)-1)
555 g_set_error (error, G_IO_ERROR,
556 g_io_error_from_errno (errsv),
557 _("Error seeking in file: %s"),
566 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
568 /* We can't truncate pipes and stuff where we can't seek */
569 return g_local_file_output_stream_can_seek (stream);
573 g_local_file_output_stream_truncate (GFileOutputStream *stream,
575 GCancellable *cancellable,
578 GLocalFileOutputStream *file;
581 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
585 res = g_win32_ftruncate (file->priv->fd, size);
587 res = ftruncate (file->priv->fd, size);
596 if (g_cancellable_set_error_if_cancelled (cancellable, error))
601 g_set_error (error, G_IO_ERROR,
602 g_io_error_from_errno (errsv),
603 _("Error truncating file: %s"),
613 g_local_file_output_stream_query_info (GFileOutputStream *stream,
614 const char *attributes,
615 GCancellable *cancellable,
618 GLocalFileOutputStream *file;
620 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
622 if (g_cancellable_set_error_if_cancelled (cancellable, error))
625 return _g_local_file_info_get_from_fd (file->priv->fd,
631 _g_local_file_output_stream_new (int fd)
633 GLocalFileOutputStream *stream;
635 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
636 stream->priv->fd = fd;
637 return G_FILE_OUTPUT_STREAM (stream);
641 set_error_from_open_errno (const char *filename,
647 /* This must be an invalid filename, on e.g. FAT */
648 g_set_error_literal (error, G_IO_ERROR,
649 G_IO_ERROR_INVALID_FILENAME,
650 _("Invalid filename"));
653 char *display_name = g_filename_display_name (filename);
654 g_set_error (error, G_IO_ERROR,
655 g_io_error_from_errno (errsv),
656 _("Error opening file ā%sā: %s"),
657 display_name, g_strerror (errsv));
658 g_free (display_name);
662 static GFileOutputStream *
663 output_stream_open (const char *filename,
666 GCancellable *cancellable,
669 GLocalFileOutputStream *stream;
672 fd = g_open (filename, open_flags, mode);
675 set_error_from_open_errno (filename, error);
679 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
680 stream->priv->fd = fd;
681 return G_FILE_OUTPUT_STREAM (stream);
685 _g_local_file_output_stream_open (const char *filename,
687 GCancellable *cancellable,
692 if (g_cancellable_set_error_if_cancelled (cancellable, error))
695 open_flags = O_BINARY;
697 open_flags |= O_RDWR;
699 open_flags |= O_WRONLY;
701 return output_stream_open (filename, open_flags, 0666, cancellable, error);
705 mode_from_flags_or_info (GFileCreateFlags flags,
706 GFileInfo *reference_info)
708 if (flags & G_FILE_CREATE_PRIVATE)
710 else if (reference_info && g_file_info_has_attribute (reference_info, "unix::mode"))
711 return g_file_info_get_attribute_uint32 (reference_info, "unix::mode") & (~S_IFMT);
717 _g_local_file_output_stream_create (const char *filename,
719 GFileCreateFlags flags,
720 GFileInfo *reference_info,
721 GCancellable *cancellable,
727 if (g_cancellable_set_error_if_cancelled (cancellable, error))
730 mode = mode_from_flags_or_info (flags, reference_info);
732 open_flags = O_CREAT | O_EXCL | O_BINARY;
734 open_flags |= O_RDWR;
736 open_flags |= O_WRONLY;
738 return output_stream_open (filename, open_flags, mode, cancellable, error);
742 _g_local_file_output_stream_append (const char *filename,
743 GFileCreateFlags flags,
744 GCancellable *cancellable,
749 if (g_cancellable_set_error_if_cancelled (cancellable, error))
752 if (flags & G_FILE_CREATE_PRIVATE)
757 return output_stream_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode,
762 create_backup_filename (const char *filename)
764 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
767 #define BUFSIZE 8192 /* size of normal write buffer */
770 copy_file_data (gint sfd,
776 const gchar *write_buffer;
778 gssize bytes_to_write;
779 gssize bytes_written;
781 buffer = g_malloc (BUFSIZE);
785 bytes_read = read (sfd, buffer, BUFSIZE);
786 if (bytes_read == -1)
793 g_set_error (error, G_IO_ERROR,
794 g_io_error_from_errno (errsv),
795 _("Error reading from file: %s"),
801 bytes_to_write = bytes_read;
802 write_buffer = buffer;
806 bytes_written = write (dfd, write_buffer, bytes_to_write);
807 if (bytes_written == -1)
814 g_set_error (error, G_IO_ERROR,
815 g_io_error_from_errno (errsv),
816 _("Error writing to file: %s"),
822 bytes_to_write -= bytes_written;
823 write_buffer += bytes_written;
825 while (bytes_to_write > 0);
827 } while ((bytes_read != 0) && (ret == TRUE));
835 handle_overwrite_open (const char *filename,
838 gboolean create_backup,
839 char **temp_filename,
840 GFileCreateFlags flags,
841 GFileInfo *reference_info,
842 GCancellable *cancellable,
846 GLocalFileStat original_stat;
854 mode = mode_from_flags_or_info (flags, reference_info);
856 /* We only need read access to the original file if we are creating a backup.
857 * We also add O_CREATE to avoid a race if the file was just removed */
858 if (create_backup || readable)
859 open_flags = O_RDWR | O_CREAT | O_BINARY;
861 open_flags = O_WRONLY | O_CREAT | O_BINARY;
863 /* Some systems have O_NOFOLLOW, which lets us avoid some races
864 * when finding out if the file we opened was a symlink */
867 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
869 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
870 if (fd == -1 && errsv == EMLINK)
871 #elif defined(__NetBSD__)
872 if (fd == -1 && errsv == EFTYPE)
874 if (fd == -1 && errsv == ELOOP)
877 /* Could be a symlink, or it could be a regular ELOOP error,
878 * but then the next open will fail too. */
880 fd = g_open (filename, open_flags, mode);
883 fd = g_open (filename, open_flags, mode);
885 /* This is racy, but we do it as soon as possible to minimize the race */
886 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
891 char *display_name = g_filename_display_name (filename);
892 g_set_error (error, G_IO_ERROR,
893 g_io_error_from_errno (errsv),
894 _("Error opening file ā%sā: %s"),
895 display_name, g_strerror (errsv));
896 g_free (display_name);
900 res = g_local_file_fstat (fd,
901 G_LOCAL_FILE_STAT_FIELD_TYPE |
902 G_LOCAL_FILE_STAT_FIELD_MODE |
903 G_LOCAL_FILE_STAT_FIELD_UID |
904 G_LOCAL_FILE_STAT_FIELD_GID |
905 G_LOCAL_FILE_STAT_FIELD_MTIME |
906 G_LOCAL_FILE_STAT_FIELD_NLINK,
907 G_LOCAL_FILE_STAT_FIELD_ALL, &original_stat);
912 char *display_name = g_filename_display_name (filename);
913 g_set_error (error, G_IO_ERROR,
914 g_io_error_from_errno (errsv),
915 _("Error when getting information for file ā%sā: %s"),
916 display_name, g_strerror (errsv));
917 g_free (display_name);
921 /* not a regular file */
922 if (!S_ISREG (_g_stat_mode (&original_stat)))
924 if (S_ISDIR (_g_stat_mode (&original_stat)))
925 g_set_error_literal (error,
927 G_IO_ERROR_IS_DIRECTORY,
928 _("Target file is a directory"));
930 g_set_error_literal (error,
932 G_IO_ERROR_NOT_REGULAR_FILE,
933 _("Target file is not a regular file"));
939 current_etag = _g_local_file_info_create_etag (&original_stat);
940 if (strcmp (etag, current_etag) != 0)
942 g_set_error_literal (error,
944 G_IO_ERROR_WRONG_ETAG,
945 _("The file was externally modified"));
946 g_free (current_etag);
949 g_free (current_etag);
952 /* We use two backup strategies.
953 * The first one (which is faster) consist in saving to a
954 * tmp file then rename the original file to the backup and the
955 * tmp file to the original name. This is fast but doesn't work
956 * when the file is a link (hard or symbolic) or when we can't
957 * write to the current dir or can't set the permissions on the
959 * The second strategy consist simply in copying the old file
960 * to a backup file and rewrite the contents of the file.
963 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
964 (!(_g_stat_nlink (&original_stat) > 1) && !is_symlink))
966 char *dirname, *tmp_filename;
969 dirname = g_path_get_dirname (filename);
970 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
973 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
976 g_free (tmp_filename);
977 goto fallback_strategy;
980 /* try to keep permissions (unless replacing) */
982 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
985 fchown (tmpfd, _g_stat_uid (&original_stat), _g_stat_gid (&original_stat)) == -1 ||
988 fchmod (tmpfd, _g_stat_mode (&original_stat) & ~S_IFMT) == -1 ||
994 GLocalFileStat tmp_statbuf;
997 tres = g_local_file_fstat (tmpfd,
998 G_LOCAL_FILE_STAT_FIELD_TYPE |
999 G_LOCAL_FILE_STAT_FIELD_MODE |
1000 G_LOCAL_FILE_STAT_FIELD_UID |
1001 G_LOCAL_FILE_STAT_FIELD_GID,
1002 G_LOCAL_FILE_STAT_FIELD_ALL, &tmp_statbuf);
1004 /* Check that we really needed to change something */
1006 _g_stat_uid (&original_stat) != _g_stat_uid (&tmp_statbuf) ||
1007 _g_stat_gid (&original_stat) != _g_stat_gid (&tmp_statbuf) ||
1008 _g_stat_mode (&original_stat) != _g_stat_mode (&tmp_statbuf))
1010 g_close (tmpfd, NULL);
1011 g_unlink (tmp_filename);
1012 g_free (tmp_filename);
1013 goto fallback_strategy;
1018 *temp_filename = tmp_filename;
1026 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
1027 GLocalFileStat tmp_statbuf;
1029 char *backup_filename;
1032 backup_filename = create_backup_filename (filename);
1034 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
1036 g_set_error_literal (error,
1038 G_IO_ERROR_CANT_CREATE_BACKUP,
1039 _("Backup file creation failed"));
1040 g_free (backup_filename);
1044 bfd = g_open (backup_filename,
1045 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
1046 _g_stat_mode (&original_stat) & 0777);
1050 g_set_error_literal (error,
1052 G_IO_ERROR_CANT_CREATE_BACKUP,
1053 _("Backup file creation failed"));
1054 g_free (backup_filename);
1058 /* If needed, Try to set the group of the backup same as the
1059 * original file. If this fails, set the protection
1060 * bits for the group same as the protection bits for
1062 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
1063 if (g_local_file_fstat (bfd, G_LOCAL_FILE_STAT_FIELD_GID, G_LOCAL_FILE_STAT_FIELD_ALL, &tmp_statbuf) != 0)
1065 g_set_error_literal (error,
1067 G_IO_ERROR_CANT_CREATE_BACKUP,
1068 _("Backup file creation failed"));
1069 g_unlink (backup_filename);
1070 g_close (bfd, NULL);
1071 g_free (backup_filename);
1075 if ((_g_stat_gid (&original_stat) != _g_stat_gid (&tmp_statbuf)) &&
1076 fchown (bfd, (uid_t) -1, _g_stat_gid (&original_stat)) != 0)
1079 (_g_stat_mode (&original_stat) & 0707) |
1080 ((_g_stat_mode (&original_stat) & 07) << 3)) != 0)
1082 g_set_error_literal (error,
1084 G_IO_ERROR_CANT_CREATE_BACKUP,
1085 _("Backup file creation failed"));
1086 g_unlink (backup_filename);
1087 g_close (bfd, NULL);
1088 g_free (backup_filename);
1094 if (!copy_file_data (fd, bfd, NULL))
1096 g_set_error_literal (error,
1098 G_IO_ERROR_CANT_CREATE_BACKUP,
1099 _("Backup file creation failed"));
1100 g_unlink (backup_filename);
1101 g_close (bfd, NULL);
1102 g_free (backup_filename);
1107 g_close (bfd, NULL);
1108 g_free (backup_filename);
1110 /* Seek back to the start of the file after the backup copy */
1111 if (lseek (fd, 0, SEEK_SET) == -1)
1115 g_set_error (error, G_IO_ERROR,
1116 g_io_error_from_errno (errsv),
1117 _("Error seeking in file: %s"),
1118 g_strerror (errsv));
1123 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1127 if (g_unlink (filename) != 0)
1131 g_set_error (error, G_IO_ERROR,
1132 g_io_error_from_errno (errsv),
1133 _("Error removing old file: %s"),
1134 g_strerror (errsv));
1139 open_flags = O_RDWR | O_CREAT | O_BINARY;
1141 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1142 fd = g_open (filename, open_flags, mode);
1146 char *display_name = g_filename_display_name (filename);
1147 g_set_error (error, G_IO_ERROR,
1148 g_io_error_from_errno (errsv),
1149 _("Error opening file ā%sā: %s"),
1150 display_name, g_strerror (errsv));
1151 g_free (display_name);
1157 /* Truncate the file at the start */
1159 if (g_win32_ftruncate (fd, 0) == -1)
1161 if (ftruncate (fd, 0) == -1)
1166 g_set_error (error, G_IO_ERROR,
1167 g_io_error_from_errno (errsv),
1168 _("Error truncating file: %s"),
1169 g_strerror (errsv));
1183 _g_local_file_output_stream_replace (const char *filename,
1186 gboolean create_backup,
1187 GFileCreateFlags flags,
1188 GFileInfo *reference_info,
1189 GCancellable *cancellable,
1192 GLocalFileOutputStream *stream;
1196 gboolean sync_on_close;
1199 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1204 mode = mode_from_flags_or_info (flags, reference_info);
1205 sync_on_close = FALSE;
1207 /* If the file doesn't exist, create it */
1208 open_flags = O_CREAT | O_EXCL | O_BINARY;
1210 open_flags |= O_RDWR;
1212 open_flags |= O_WRONLY;
1213 fd = g_open (filename, open_flags, mode);
1215 if (fd == -1 && errno == EEXIST)
1217 /* The file already exists */
1218 fd = handle_overwrite_open (filename, readable, etag,
1219 create_backup, &temp_file,
1220 flags, reference_info,
1221 cancellable, error);
1225 /* If the final destination exists, we want to sync the newly written
1226 * file to ensure the data is on disk when we rename over the destination.
1227 * otherwise if we get a system crash we can lose both the new and the
1228 * old file on some filesystems. (I.E. those that don't guarantee the
1229 * data is written to the disk before the metadata.)
1231 sync_on_close = TRUE;
1235 set_error_from_open_errno (filename, error);
1240 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1241 stream->priv->fd = fd;
1242 stream->priv->sync_on_close = sync_on_close;
1243 stream->priv->tmp_filename = temp_file;
1245 stream->priv->backup_filename = create_backup_filename (filename);
1246 stream->priv->original_filename = g_strdup (filename);
1248 return G_FILE_OUTPUT_STREAM (stream);
1252 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1254 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1255 return stream->priv->fd;
1260 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1262 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1263 return _g_local_file_output_stream_get_fd (stream);