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)
69 #define HAVE_O_CLOEXEC 1
72 struct _GLocalFileOutputStreamPrivate {
74 char *original_filename;
75 char *backup_filename;
77 guint sync_on_close : 1;
83 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
86 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
88 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
89 G_ADD_PRIVATE (GLocalFileOutputStream)
90 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
91 g_file_descriptor_based_iface_init))
93 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
94 G_ADD_PRIVATE (GLocalFileOutputStream))
98 /* Some of the file replacement code was based on the code from gedit,
99 * relicenced to LGPL with permissions from the authors.
102 #define BACKUP_EXTENSION "~"
104 static gssize g_local_file_output_stream_write (GOutputStream *stream,
107 GCancellable *cancellable,
110 static gboolean g_local_file_output_stream_writev (GOutputStream *stream,
111 const GOutputVector *vectors,
113 gsize *bytes_written,
114 GCancellable *cancellable,
117 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
118 GCancellable *cancellable,
120 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
121 const char *attributes,
122 GCancellable *cancellable,
124 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
125 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
126 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
127 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
130 GCancellable *cancellable,
132 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
133 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
135 GCancellable *cancellable,
138 static int g_local_file_output_stream_get_fd (GFileDescriptorBased *stream);
142 g_local_file_output_stream_finalize (GObject *object)
144 GLocalFileOutputStream *file;
146 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
148 g_free (file->priv->tmp_filename);
149 g_free (file->priv->original_filename);
150 g_free (file->priv->backup_filename);
151 g_free (file->priv->etag);
153 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
157 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
159 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
160 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
161 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
163 gobject_class->finalize = g_local_file_output_stream_finalize;
165 stream_class->write_fn = g_local_file_output_stream_write;
167 stream_class->writev_fn = g_local_file_output_stream_writev;
169 stream_class->close_fn = g_local_file_output_stream_close;
170 file_stream_class->query_info = g_local_file_output_stream_query_info;
171 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
172 file_stream_class->tell = g_local_file_output_stream_tell;
173 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
174 file_stream_class->seek = g_local_file_output_stream_seek;
175 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
176 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
181 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
183 iface->get_fd = g_local_file_output_stream_get_fd;
188 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
190 stream->priv = g_local_file_output_stream_get_instance_private (stream);
191 stream->priv->do_close = TRUE;
195 g_local_file_output_stream_write (GOutputStream *stream,
198 GCancellable *cancellable,
201 GLocalFileOutputStream *file;
204 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
208 if (g_cancellable_set_error_if_cancelled (cancellable, error))
210 res = write (file->priv->fd, buffer, count);
218 g_set_error (error, G_IO_ERROR,
219 g_io_error_from_errno (errsv),
220 _("Error writing to file: %s"),
230 /* On Windows there is no equivalent API for files. The closest API to that is
231 * WriteFileGather() but it is useless in general: it requires, among other
232 * things, that each chunk is the size of a whole page and in memory aligned
233 * to a page. We can't possibly guarantee that in GLib.
236 /* Macro to check if struct iovec and GOutputVector have the same ABI */
237 #define G_OUTPUT_VECTOR_IS_IOVEC (sizeof (struct iovec) == sizeof (GOutputVector) && \
238 G_SIZEOF_MEMBER (struct iovec, iov_base) == G_SIZEOF_MEMBER (GOutputVector, buffer) && \
239 G_STRUCT_OFFSET (struct iovec, iov_base) == G_STRUCT_OFFSET (GOutputVector, buffer) && \
240 G_SIZEOF_MEMBER (struct iovec, iov_len) == G_SIZEOF_MEMBER (GOutputVector, size) && \
241 G_STRUCT_OFFSET (struct iovec, iov_len) == G_STRUCT_OFFSET (GOutputVector, size))
244 g_local_file_output_stream_writev (GOutputStream *stream,
245 const GOutputVector *vectors,
247 gsize *bytes_written,
248 GCancellable *cancellable,
251 GLocalFileOutputStream *file;
258 /* Clamp the number of vectors if more given than we can write in one go.
259 * The caller has to handle short writes anyway.
261 if (n_vectors > G_IOV_MAX)
262 n_vectors = G_IOV_MAX;
264 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
266 if (G_OUTPUT_VECTOR_IS_IOVEC)
268 /* ABI is compatible */
269 iov = (struct iovec *) vectors;
275 /* ABI is incompatible */
276 iov = g_newa (struct iovec, n_vectors);
277 for (i = 0; i < n_vectors; i++)
279 iov[i].iov_base = (void *)vectors[i].buffer;
280 iov[i].iov_len = vectors[i].size;
286 if (g_cancellable_set_error_if_cancelled (cancellable, error))
288 res = writev (file->priv->fd, iov, n_vectors);
296 g_set_error (error, G_IO_ERROR,
297 g_io_error_from_errno (errsv),
298 _("Error writing to file: %s"),
301 else if (bytes_written)
303 *bytes_written = res;
314 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
317 out->priv->do_close = do_close;
321 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
322 GCancellable *cancellable,
325 GLocalFileStat final_stat;
327 if (file->priv->sync_on_close &&
328 g_fsync (file->priv->fd) != 0)
332 g_set_error (error, G_IO_ERROR,
333 g_io_error_from_errno (errsv),
334 _("Error writing to file: %s"),
341 /* Must close before renaming on Windows, so just do the close first
342 * in all cases for now.
344 if (GLIB_PRIVATE_CALL (g_win32_fstat) (file->priv->fd, &final_stat) == 0)
345 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
347 if (!g_close (file->priv->fd, NULL))
351 g_set_error (error, G_IO_ERROR,
352 g_io_error_from_errno (errsv),
353 _("Error closing file: %s"),
360 if (file->priv->tmp_filename)
362 /* We need to move the temp file to its final place,
363 * and possibly create the backup file
366 if (file->priv->backup_filename)
368 if (g_cancellable_set_error_if_cancelled (cancellable, error))
372 /* create original -> backup link, the original is then renamed over */
373 if (g_unlink (file->priv->backup_filename) != 0 &&
378 g_set_error (error, G_IO_ERROR,
379 G_IO_ERROR_CANT_CREATE_BACKUP,
380 _("Error removing old backup link: %s"),
385 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
387 /* link failed or is not supported, try rename */
388 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
392 g_set_error (error, G_IO_ERROR,
393 G_IO_ERROR_CANT_CREATE_BACKUP,
394 _("Error creating backup copy: %s"),
400 /* If link not supported, just rename... */
401 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
405 g_set_error (error, G_IO_ERROR,
406 G_IO_ERROR_CANT_CREATE_BACKUP,
407 _("Error creating backup copy: %s"),
415 if (g_cancellable_set_error_if_cancelled (cancellable, error))
418 /* tmp -> original */
419 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
423 g_set_error (error, G_IO_ERROR,
424 g_io_error_from_errno (errsv),
425 _("Error renaming temporary file: %s"),
430 g_clear_pointer (&file->priv->tmp_filename, g_free);
433 if (g_cancellable_set_error_if_cancelled (cancellable, error))
436 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
438 if (g_local_file_fstat (file->priv->fd, G_LOCAL_FILE_STAT_FIELD_MTIME, G_LOCAL_FILE_STAT_FIELD_ALL, &final_stat) == 0)
439 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
441 if (!g_close (file->priv->fd, NULL))
445 g_set_error (error, G_IO_ERROR,
446 g_io_error_from_errno (errsv),
447 _("Error closing file: %s"),
458 /* A simple try to close the fd in case we fail before the actual close */
459 g_close (file->priv->fd, NULL);
461 if (file->priv->tmp_filename)
462 g_unlink (file->priv->tmp_filename);
469 g_local_file_output_stream_close (GOutputStream *stream,
470 GCancellable *cancellable,
473 GLocalFileOutputStream *file;
475 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
477 if (file->priv->do_close)
478 return _g_local_file_output_stream_really_close (file,
485 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
487 GLocalFileOutputStream *file;
489 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
491 return g_strdup (file->priv->etag);
495 g_local_file_output_stream_tell (GFileOutputStream *stream)
497 GLocalFileOutputStream *file;
500 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
502 pos = lseek (file->priv->fd, 0, SEEK_CUR);
504 if (pos == (off_t)-1)
511 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
513 GLocalFileOutputStream *file;
516 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
518 pos = lseek (file->priv->fd, 0, SEEK_CUR);
520 if (pos == (off_t)-1 && errno == ESPIPE)
527 seek_type_to_lseek (GSeekType type)
544 g_local_file_output_stream_seek (GFileOutputStream *stream,
547 GCancellable *cancellable,
550 GLocalFileOutputStream *file;
553 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
555 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
557 if (pos == (off_t)-1)
561 g_set_error (error, G_IO_ERROR,
562 g_io_error_from_errno (errsv),
563 _("Error seeking in file: %s"),
572 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
574 /* We can't truncate pipes and stuff where we can't seek */
575 return g_local_file_output_stream_can_seek (stream);
579 g_local_file_output_stream_truncate (GFileOutputStream *stream,
581 GCancellable *cancellable,
584 GLocalFileOutputStream *file;
587 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
591 res = g_win32_ftruncate (file->priv->fd, size);
593 res = ftruncate (file->priv->fd, size);
602 if (g_cancellable_set_error_if_cancelled (cancellable, error))
607 g_set_error (error, G_IO_ERROR,
608 g_io_error_from_errno (errsv),
609 _("Error truncating file: %s"),
619 g_local_file_output_stream_query_info (GFileOutputStream *stream,
620 const char *attributes,
621 GCancellable *cancellable,
624 GLocalFileOutputStream *file;
626 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
628 if (g_cancellable_set_error_if_cancelled (cancellable, error))
631 return _g_local_file_info_get_from_fd (file->priv->fd,
637 _g_local_file_output_stream_new (int fd)
639 GLocalFileOutputStream *stream;
641 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
642 stream->priv->fd = fd;
643 return G_FILE_OUTPUT_STREAM (stream);
647 set_error_from_open_errno (const char *filename,
653 /* This must be an invalid filename, on e.g. FAT */
654 g_set_error_literal (error, G_IO_ERROR,
655 G_IO_ERROR_INVALID_FILENAME,
656 _("Invalid filename"));
659 char *display_name = g_filename_display_name (filename);
660 g_set_error (error, G_IO_ERROR,
661 g_io_error_from_errno (errsv),
662 _("Error opening file ā%sā: %s"),
663 display_name, g_strerror (errsv));
664 g_free (display_name);
668 static GFileOutputStream *
669 output_stream_open (const char *filename,
672 GCancellable *cancellable,
675 GLocalFileOutputStream *stream;
678 fd = g_open (filename, open_flags, mode);
681 set_error_from_open_errno (filename, error);
685 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
686 stream->priv->fd = fd;
687 return G_FILE_OUTPUT_STREAM (stream);
691 _g_local_file_output_stream_open (const char *filename,
693 GCancellable *cancellable,
698 if (g_cancellable_set_error_if_cancelled (cancellable, error))
701 open_flags = O_BINARY;
703 open_flags |= O_RDWR;
705 open_flags |= O_WRONLY;
707 return output_stream_open (filename, open_flags, 0666, cancellable, error);
711 mode_from_flags_or_info (GFileCreateFlags flags,
712 GFileInfo *reference_info)
714 if (flags & G_FILE_CREATE_PRIVATE)
716 else if (reference_info && g_file_info_has_attribute (reference_info, "unix::mode"))
717 return g_file_info_get_attribute_uint32 (reference_info, "unix::mode") & (~S_IFMT);
723 _g_local_file_output_stream_create (const char *filename,
725 GFileCreateFlags flags,
726 GFileInfo *reference_info,
727 GCancellable *cancellable,
733 if (g_cancellable_set_error_if_cancelled (cancellable, error))
736 mode = mode_from_flags_or_info (flags, reference_info);
738 open_flags = O_CREAT | O_EXCL | O_BINARY;
740 open_flags |= O_RDWR;
742 open_flags |= O_WRONLY;
744 return output_stream_open (filename, open_flags, mode, cancellable, error);
748 _g_local_file_output_stream_append (const char *filename,
749 GFileCreateFlags flags,
750 GCancellable *cancellable,
755 if (g_cancellable_set_error_if_cancelled (cancellable, error))
758 if (flags & G_FILE_CREATE_PRIVATE)
763 return output_stream_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode,
768 create_backup_filename (const char *filename)
770 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
773 #define BUFSIZE 8192 /* size of normal write buffer */
776 copy_file_data (gint sfd,
782 const gchar *write_buffer;
784 gssize bytes_to_write;
785 gssize bytes_written;
787 buffer = g_malloc (BUFSIZE);
791 bytes_read = read (sfd, buffer, BUFSIZE);
792 if (bytes_read == -1)
799 g_set_error (error, G_IO_ERROR,
800 g_io_error_from_errno (errsv),
801 _("Error reading from file: %s"),
807 bytes_to_write = bytes_read;
808 write_buffer = buffer;
812 bytes_written = write (dfd, write_buffer, bytes_to_write);
813 if (bytes_written == -1)
820 g_set_error (error, G_IO_ERROR,
821 g_io_error_from_errno (errsv),
822 _("Error writing to file: %s"),
828 bytes_to_write -= bytes_written;
829 write_buffer += bytes_written;
831 while (bytes_to_write > 0);
833 } while ((bytes_read != 0) && (ret == TRUE));
841 handle_overwrite_open (const char *filename,
844 gboolean create_backup,
845 char **temp_filename,
846 GFileCreateFlags flags,
847 GFileInfo *reference_info,
848 GCancellable *cancellable,
852 GLocalFileStat original_stat;
859 gboolean replace_destination_set = (flags & G_FILE_CREATE_REPLACE_DESTINATION);
861 mode = mode_from_flags_or_info (flags, reference_info);
863 /* We only need read access to the original file if we are creating a backup.
864 * We also add O_CREAT to avoid a race if the file was just removed */
865 if (create_backup || readable)
866 open_flags = O_RDWR | O_CREAT | O_BINARY;
868 open_flags = O_WRONLY | O_CREAT | O_BINARY;
870 /* Some systems have O_NOFOLLOW, which lets us avoid some races
871 * when finding out if the file we opened was a symlink */
874 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
876 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
877 if (fd == -1 && errsv == EMLINK)
878 #elif defined(__NetBSD__)
879 if (fd == -1 && errsv == EFTYPE)
881 if (fd == -1 && errsv == ELOOP)
884 /* Could be a symlink, or it could be a regular ELOOP error,
885 * but then the next open will fail too. */
887 if (!replace_destination_set)
888 fd = g_open (filename, open_flags, mode);
890 #else /* if !O_NOFOLLOW */
891 /* This is racy, but we do it as soon as possible to minimize the race */
892 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
894 if (!is_symlink || !replace_destination_set)
896 fd = g_open (filename, open_flags, mode);
902 (!is_symlink || !replace_destination_set))
904 char *display_name = g_filename_display_name (filename);
905 g_set_error (error, G_IO_ERROR,
906 g_io_error_from_errno (errsv),
907 _("Error opening file ā%sā: %s"),
908 display_name, g_strerror (errsv));
909 g_free (display_name);
915 res = g_local_file_fstat (fd,
916 G_LOCAL_FILE_STAT_FIELD_TYPE |
917 G_LOCAL_FILE_STAT_FIELD_MODE |
918 G_LOCAL_FILE_STAT_FIELD_UID |
919 G_LOCAL_FILE_STAT_FIELD_GID |
920 G_LOCAL_FILE_STAT_FIELD_MTIME |
921 G_LOCAL_FILE_STAT_FIELD_NLINK,
922 G_LOCAL_FILE_STAT_FIELD_ALL, &original_stat);
927 res = g_local_file_lstat (filename,
928 G_LOCAL_FILE_STAT_FIELD_TYPE |
929 G_LOCAL_FILE_STAT_FIELD_MODE |
930 G_LOCAL_FILE_STAT_FIELD_UID |
931 G_LOCAL_FILE_STAT_FIELD_GID |
932 G_LOCAL_FILE_STAT_FIELD_MTIME |
933 G_LOCAL_FILE_STAT_FIELD_NLINK,
934 G_LOCAL_FILE_STAT_FIELD_ALL, &original_stat);
940 char *display_name = g_filename_display_name (filename);
941 g_set_error (error, G_IO_ERROR,
942 g_io_error_from_errno (errsv),
943 _("Error when getting information for file ā%sā: %s"),
944 display_name, g_strerror (errsv));
945 g_free (display_name);
949 /* not a regular file */
950 if (!S_ISREG (_g_stat_mode (&original_stat)))
952 if (S_ISDIR (_g_stat_mode (&original_stat)))
954 g_set_error_literal (error,
956 G_IO_ERROR_IS_DIRECTORY,
957 _("Target file is a directory"));
960 else if (!is_symlink ||
962 !S_ISLNK (_g_stat_mode (&original_stat))
968 g_set_error_literal (error,
970 G_IO_ERROR_NOT_REGULAR_FILE,
971 _("Target file is not a regular file"));
978 GLocalFileStat etag_stat;
979 GLocalFileStat *etag_stat_pointer;
981 /* The ETag is calculated on the details of the target file, for symlinks,
982 * so we might need to stat() again. */
985 res = g_local_file_stat (filename,
986 G_LOCAL_FILE_STAT_FIELD_MTIME,
987 G_LOCAL_FILE_STAT_FIELD_ALL, &etag_stat);
992 char *display_name = g_filename_display_name (filename);
993 g_set_error (error, G_IO_ERROR,
994 g_io_error_from_errno (errsv),
995 _("Error when getting information for file ā%sā: %s"),
996 display_name, g_strerror (errsv));
997 g_free (display_name);
1001 etag_stat_pointer = &etag_stat;
1004 etag_stat_pointer = &original_stat;
1006 /* Compare the ETags */
1007 current_etag = _g_local_file_info_create_etag (etag_stat_pointer);
1008 if (strcmp (etag, current_etag) != 0)
1010 g_set_error_literal (error,
1012 G_IO_ERROR_WRONG_ETAG,
1013 _("The file was externally modified"));
1014 g_free (current_etag);
1017 g_free (current_etag);
1020 /* We use two backup strategies.
1021 * The first one (which is faster) consist in saving to a
1022 * tmp file then rename the original file to the backup and the
1023 * tmp file to the original name. This is fast but doesn't work
1024 * when the file is a link (hard or symbolic) or when we can't
1025 * write to the current dir or can't set the permissions on the
1027 * The second strategy consist simply in copying the old file
1028 * to a backup file and rewrite the contents of the file.
1031 if (replace_destination_set ||
1032 (!(_g_stat_nlink (&original_stat) > 1) && !is_symlink))
1034 char *dirname, *tmp_filename;
1037 dirname = g_path_get_dirname (filename);
1038 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
1041 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
1044 g_free (tmp_filename);
1045 goto fallback_strategy;
1048 /* try to keep permissions (unless replacing) */
1050 if (!replace_destination_set &&
1053 fchown (tmpfd, _g_stat_uid (&original_stat), _g_stat_gid (&original_stat)) == -1 ||
1056 fchmod (tmpfd, _g_stat_mode (&original_stat) & ~S_IFMT) == -1 ||
1062 GLocalFileStat tmp_statbuf;
1065 tres = g_local_file_fstat (tmpfd,
1066 G_LOCAL_FILE_STAT_FIELD_TYPE |
1067 G_LOCAL_FILE_STAT_FIELD_MODE |
1068 G_LOCAL_FILE_STAT_FIELD_UID |
1069 G_LOCAL_FILE_STAT_FIELD_GID,
1070 G_LOCAL_FILE_STAT_FIELD_ALL, &tmp_statbuf);
1072 /* Check that we really needed to change something */
1074 _g_stat_uid (&original_stat) != _g_stat_uid (&tmp_statbuf) ||
1075 _g_stat_gid (&original_stat) != _g_stat_gid (&tmp_statbuf) ||
1076 _g_stat_mode (&original_stat) != _g_stat_mode (&tmp_statbuf))
1078 g_close (tmpfd, NULL);
1079 g_unlink (tmp_filename);
1080 g_free (tmp_filename);
1081 goto fallback_strategy;
1087 *temp_filename = tmp_filename;
1095 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
1096 GLocalFileStat tmp_statbuf;
1098 char *backup_filename;
1101 backup_filename = create_backup_filename (filename);
1103 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
1105 g_set_error_literal (error,
1107 G_IO_ERROR_CANT_CREATE_BACKUP,
1108 _("Backup file creation failed"));
1109 g_free (backup_filename);
1113 bfd = g_open (backup_filename,
1114 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
1115 _g_stat_mode (&original_stat) & 0777);
1119 g_set_error_literal (error,
1121 G_IO_ERROR_CANT_CREATE_BACKUP,
1122 _("Backup file creation failed"));
1123 g_free (backup_filename);
1127 /* If needed, Try to set the group of the backup same as the
1128 * original file. If this fails, set the protection
1129 * bits for the group same as the protection bits for
1131 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
1132 if (g_local_file_fstat (bfd, G_LOCAL_FILE_STAT_FIELD_GID, G_LOCAL_FILE_STAT_FIELD_ALL, &tmp_statbuf) != 0)
1134 g_set_error_literal (error,
1136 G_IO_ERROR_CANT_CREATE_BACKUP,
1137 _("Backup file creation failed"));
1138 g_unlink (backup_filename);
1139 g_close (bfd, NULL);
1140 g_free (backup_filename);
1144 if ((_g_stat_gid (&original_stat) != _g_stat_gid (&tmp_statbuf)) &&
1145 fchown (bfd, (uid_t) -1, _g_stat_gid (&original_stat)) != 0)
1148 (_g_stat_mode (&original_stat) & 0707) |
1149 ((_g_stat_mode (&original_stat) & 07) << 3)) != 0)
1151 g_set_error_literal (error,
1153 G_IO_ERROR_CANT_CREATE_BACKUP,
1154 _("Backup file creation failed"));
1155 g_unlink (backup_filename);
1156 g_close (bfd, NULL);
1157 g_free (backup_filename);
1163 if (!copy_file_data (fd, bfd, NULL))
1165 g_set_error_literal (error,
1167 G_IO_ERROR_CANT_CREATE_BACKUP,
1168 _("Backup file creation failed"));
1169 g_unlink (backup_filename);
1170 g_close (bfd, NULL);
1171 g_free (backup_filename);
1176 g_close (bfd, NULL);
1177 g_free (backup_filename);
1179 /* Seek back to the start of the file after the backup copy */
1180 if (lseek (fd, 0, SEEK_SET) == -1)
1184 g_set_error (error, G_IO_ERROR,
1185 g_io_error_from_errno (errsv),
1186 _("Error seeking in file: %s"),
1187 g_strerror (errsv));
1192 if (replace_destination_set)
1196 if (g_unlink (filename) != 0)
1200 g_set_error (error, G_IO_ERROR,
1201 g_io_error_from_errno (errsv),
1202 _("Error removing old file: %s"),
1203 g_strerror (errsv));
1208 open_flags = O_RDWR | O_CREAT | O_BINARY;
1210 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1211 fd = g_open (filename, open_flags, mode);
1215 char *display_name = g_filename_display_name (filename);
1216 g_set_error (error, G_IO_ERROR,
1217 g_io_error_from_errno (errsv),
1218 _("Error opening file ā%sā: %s"),
1219 display_name, g_strerror (errsv));
1220 g_free (display_name);
1226 /* Truncate the file at the start */
1228 if (g_win32_ftruncate (fd, 0) == -1)
1230 if (ftruncate (fd, 0) == -1)
1235 g_set_error (error, G_IO_ERROR,
1236 g_io_error_from_errno (errsv),
1237 _("Error truncating file: %s"),
1238 g_strerror (errsv));
1253 _g_local_file_output_stream_replace (const char *filename,
1256 gboolean create_backup,
1257 GFileCreateFlags flags,
1258 GFileInfo *reference_info,
1259 GCancellable *cancellable,
1262 GLocalFileOutputStream *stream;
1266 gboolean sync_on_close;
1269 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1274 mode = mode_from_flags_or_info (flags, reference_info);
1275 sync_on_close = FALSE;
1277 /* If the file doesn't exist, create it */
1278 open_flags = O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC;
1280 open_flags |= O_RDWR;
1282 open_flags |= O_WRONLY;
1283 fd = g_open (filename, open_flags, mode);
1285 if (fd == -1 && errno == EEXIST)
1287 /* The file already exists */
1288 fd = handle_overwrite_open (filename, readable, etag,
1289 create_backup, &temp_file,
1290 flags, reference_info,
1291 cancellable, error);
1295 /* If the final destination exists, we want to sync the newly written
1296 * file to ensure the data is on disk when we rename over the destination.
1297 * otherwise if we get a system crash we can lose both the new and the
1298 * old file on some filesystems. (I.E. those that don't guarantee the
1299 * data is written to the disk before the metadata.)
1301 sync_on_close = TRUE;
1305 set_error_from_open_errno (filename, error);
1308 #if !defined(HAVE_O_CLOEXEC) && defined(F_SETFD)
1310 fcntl (fd, F_SETFD, FD_CLOEXEC);
1313 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1314 stream->priv->fd = fd;
1315 stream->priv->sync_on_close = sync_on_close;
1316 stream->priv->tmp_filename = temp_file;
1318 stream->priv->backup_filename = create_backup_filename (filename);
1319 stream->priv->original_filename = g_strdup (filename);
1321 return G_FILE_OUTPUT_STREAM (stream);
1325 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1327 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1328 return stream->priv->fd;
1333 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1335 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1336 return _g_local_file_output_stream_get_fd (stream);