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"
45 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
48 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
58 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
59 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
61 /* Some of the file replacement code was based on the code from gedit,
62 * relicenced to LGPL with permissions from the authors.
65 #define BACKUP_EXTENSION "~"
67 struct _GLocalFileOutputStreamPrivate {
69 char *original_filename;
70 char *backup_filename;
72 gboolean sync_on_close;
76 static gssize g_local_file_output_stream_write (GOutputStream *stream,
79 GCancellable *cancellable,
81 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
82 GCancellable *cancellable,
84 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
85 const char *attributes,
86 GCancellable *cancellable,
88 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
89 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
90 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
91 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
94 GCancellable *cancellable,
96 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
97 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
99 GCancellable *cancellable,
103 g_local_file_output_stream_finalize (GObject *object)
105 GLocalFileOutputStream *file;
107 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
109 g_free (file->priv->tmp_filename);
110 g_free (file->priv->original_filename);
111 g_free (file->priv->backup_filename);
112 g_free (file->priv->etag);
114 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
118 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
120 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
121 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
122 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
124 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
126 gobject_class->finalize = g_local_file_output_stream_finalize;
128 stream_class->write_fn = g_local_file_output_stream_write;
129 stream_class->close_fn = g_local_file_output_stream_close;
130 file_stream_class->query_info = g_local_file_output_stream_query_info;
131 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
132 file_stream_class->tell = g_local_file_output_stream_tell;
133 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
134 file_stream_class->seek = g_local_file_output_stream_seek;
135 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
136 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
140 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
142 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
143 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
144 GLocalFileOutputStreamPrivate);
148 g_local_file_output_stream_write (GOutputStream *stream,
151 GCancellable *cancellable,
154 GLocalFileOutputStream *file;
157 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
161 if (g_cancellable_set_error_if_cancelled (cancellable, error))
163 res = write (file->priv->fd, buffer, count);
171 g_set_error (error, G_IO_ERROR,
172 g_io_error_from_errno (errsv),
173 _("Error writing to file: %s"),
184 g_local_file_output_stream_close (GOutputStream *stream,
185 GCancellable *cancellable,
188 GLocalFileOutputStream *file;
189 GLocalFileStat final_stat;
192 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
195 if (file->priv->sync_on_close &&
196 fsync (file->priv->fd) != 0)
200 g_set_error (error, G_IO_ERROR,
201 g_io_error_from_errno (errno),
202 _("Error writing to file: %s"),
210 /* Must close before renaming on Windows, so just do the close first
211 * in all cases for now.
213 if (_fstati64 (file->priv->fd, &final_stat) == 0)
214 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
216 res = close (file->priv->fd);
221 g_set_error (error, G_IO_ERROR,
222 g_io_error_from_errno (errsv),
223 _("Error closing file: %s"),
230 if (file->priv->tmp_filename)
232 /* We need to move the temp file to its final place,
233 * and possibly create the backup file
236 if (file->priv->backup_filename)
238 if (g_cancellable_set_error_if_cancelled (cancellable, error))
242 /* create original -> backup link, the original is then renamed over */
243 if (g_unlink (file->priv->backup_filename) != 0 &&
248 g_set_error (error, G_IO_ERROR,
249 G_IO_ERROR_CANT_CREATE_BACKUP,
250 _("Error removing old backup link: %s"),
255 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
257 /* link failed or is not supported, try rename */
258 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
262 g_set_error (error, G_IO_ERROR,
263 G_IO_ERROR_CANT_CREATE_BACKUP,
264 _("Error creating backup copy: %s"),
270 /* If link not supported, just rename... */
271 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
275 g_set_error (error, G_IO_ERROR,
276 G_IO_ERROR_CANT_CREATE_BACKUP,
277 _("Error creating backup copy: %s"),
285 if (g_cancellable_set_error_if_cancelled (cancellable, error))
288 /* tmp -> original */
289 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
293 g_set_error (error, G_IO_ERROR,
294 g_io_error_from_errno (errno),
295 _("Error renaming temporary file: %s"),
301 if (g_cancellable_set_error_if_cancelled (cancellable, error))
304 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
306 if (fstat (file->priv->fd, &final_stat) == 0)
307 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
311 res = close (file->priv->fd);
316 g_set_error (error, G_IO_ERROR,
317 g_io_error_from_errno (errsv),
318 _("Error closing file: %s"),
335 /* A simple try to close the fd in case we fail before the actual close */
336 close (file->priv->fd);
342 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
344 GLocalFileOutputStream *file;
346 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
348 return g_strdup (file->priv->etag);
352 g_local_file_output_stream_tell (GFileOutputStream *stream)
354 GLocalFileOutputStream *file;
357 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
359 pos = lseek (file->priv->fd, 0, SEEK_CUR);
361 if (pos == (off_t)-1)
368 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
370 GLocalFileOutputStream *file;
373 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
375 pos = lseek (file->priv->fd, 0, SEEK_CUR);
377 if (pos == (off_t)-1 && errno == ESPIPE)
384 seek_type_to_lseek (GSeekType type)
401 g_local_file_output_stream_seek (GFileOutputStream *stream,
404 GCancellable *cancellable,
407 GLocalFileOutputStream *file;
410 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
412 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
414 if (pos == (off_t)-1)
418 g_set_error (error, G_IO_ERROR,
419 g_io_error_from_errno (errsv),
420 _("Error seeking in file: %s"),
429 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
431 /* We can't truncate pipes and stuff where we can't seek */
432 return g_local_file_output_stream_can_seek (stream);
436 g_local_file_output_stream_truncate (GFileOutputStream *stream,
438 GCancellable *cancellable,
441 GLocalFileOutputStream *file;
444 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
448 res = g_win32_ftruncate (file->priv->fd, size);
450 res = ftruncate (file->priv->fd, size);
459 if (g_cancellable_set_error_if_cancelled (cancellable, error))
464 g_set_error (error, G_IO_ERROR,
465 g_io_error_from_errno (errsv),
466 _("Error truncating file: %s"),
476 g_local_file_output_stream_query_info (GFileOutputStream *stream,
477 const char *attributes,
478 GCancellable *cancellable,
481 GLocalFileOutputStream *file;
483 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
485 if (g_cancellable_set_error_if_cancelled (cancellable, error))
488 return _g_local_file_info_get_from_fd (file->priv->fd,
494 _g_local_file_output_stream_create (const char *filename,
495 GFileCreateFlags flags,
496 GCancellable *cancellable,
499 GLocalFileOutputStream *stream;
503 if (g_cancellable_set_error_if_cancelled (cancellable, error))
506 if (flags & G_FILE_CREATE_PRIVATE)
511 fd = g_open (filename, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
517 /* This must be an invalid filename, on e.g. FAT */
518 g_set_error_literal (error, G_IO_ERROR,
519 G_IO_ERROR_INVALID_FILENAME,
520 _("Invalid filename"));
523 char *display_name = g_filename_display_name (filename);
524 g_set_error (error, G_IO_ERROR,
525 g_io_error_from_errno (errsv),
526 _("Error opening file '%s': %s"),
527 display_name, g_strerror (errsv));
528 g_free (display_name);
533 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
534 stream->priv->fd = fd;
535 return G_FILE_OUTPUT_STREAM (stream);
539 _g_local_file_output_stream_append (const char *filename,
540 GFileCreateFlags flags,
541 GCancellable *cancellable,
544 GLocalFileOutputStream *stream;
548 if (g_cancellable_set_error_if_cancelled (cancellable, error))
551 if (flags & G_FILE_CREATE_PRIVATE)
556 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
562 /* This must be an invalid filename, on e.g. FAT */
563 g_set_error_literal (error, G_IO_ERROR,
564 G_IO_ERROR_INVALID_FILENAME,
565 _("Invalid filename"));
568 char *display_name = g_filename_display_name (filename);
569 g_set_error (error, G_IO_ERROR,
570 g_io_error_from_errno (errsv),
571 _("Error opening file '%s': %s"),
572 display_name, g_strerror (errsv));
573 g_free (display_name);
578 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
579 stream->priv->fd = fd;
581 return G_FILE_OUTPUT_STREAM (stream);
585 create_backup_filename (const char *filename)
587 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
590 #define BUFSIZE 8192 /* size of normal write buffer */
593 copy_file_data (gint sfd,
599 const gchar *write_buffer;
601 gssize bytes_to_write;
602 gssize bytes_written;
604 buffer = g_malloc (BUFSIZE);
608 bytes_read = read (sfd, buffer, BUFSIZE);
609 if (bytes_read == -1)
616 g_set_error (error, G_IO_ERROR,
617 g_io_error_from_errno (errsv),
618 _("Error reading from file: %s"),
624 bytes_to_write = bytes_read;
625 write_buffer = buffer;
629 bytes_written = write (dfd, write_buffer, bytes_to_write);
630 if (bytes_written == -1)
637 g_set_error (error, G_IO_ERROR,
638 g_io_error_from_errno (errsv),
639 _("Error writing to file: %s"),
645 bytes_to_write -= bytes_written;
646 write_buffer += bytes_written;
648 while (bytes_to_write > 0);
650 } while ((bytes_read != 0) && (ret == TRUE));
658 handle_overwrite_open (const char *filename,
660 gboolean create_backup,
661 char **temp_filename,
662 GFileCreateFlags flags,
663 GCancellable *cancellable,
667 GLocalFileStat original_stat;
674 if (flags & G_FILE_CREATE_PRIVATE)
679 /* We only need read access to the original file if we are creating a backup.
680 * We also add O_CREATE to avoid a race if the file was just removed */
682 open_flags = O_RDWR | O_CREAT | O_BINARY;
684 open_flags = O_WRONLY | O_CREAT | O_BINARY;
686 /* Some systems have O_NOFOLLOW, which lets us avoid some races
687 * when finding out if the file we opened was a symlink */
690 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
691 if (fd == -1 && errno == ELOOP)
693 /* Could be a symlink, or it could be a regular ELOOP error,
694 * but then the next open will fail too. */
696 fd = g_open (filename, open_flags, mode);
699 fd = g_open (filename, open_flags, mode);
700 /* This is racy, but we do it as soon as possible to minimize the race */
701 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
707 char *display_name = g_filename_display_name (filename);
708 g_set_error (error, G_IO_ERROR,
709 g_io_error_from_errno (errsv),
710 _("Error opening file '%s': %s"),
711 display_name, g_strerror (errsv));
712 g_free (display_name);
717 res = _fstati64 (fd, &original_stat);
719 res = fstat (fd, &original_stat);
725 char *display_name = g_filename_display_name (filename);
726 g_set_error (error, G_IO_ERROR,
727 g_io_error_from_errno (errsv),
728 _("Error stating file '%s': %s"),
729 display_name, g_strerror (errsv));
730 g_free (display_name);
734 /* not a regular file */
735 if (!S_ISREG (original_stat.st_mode))
737 if (S_ISDIR (original_stat.st_mode))
738 g_set_error_literal (error,
740 G_IO_ERROR_IS_DIRECTORY,
741 _("Target file is a directory"));
743 g_set_error_literal (error,
745 G_IO_ERROR_NOT_REGULAR_FILE,
746 _("Target file is not a regular file"));
752 current_etag = _g_local_file_info_create_etag (&original_stat);
753 if (strcmp (etag, current_etag) != 0)
755 g_set_error_literal (error,
757 G_IO_ERROR_WRONG_ETAG,
758 _("The file was externally modified"));
759 g_free (current_etag);
762 g_free (current_etag);
765 /* We use two backup strategies.
766 * The first one (which is faster) consist in saving to a
767 * tmp file then rename the original file to the backup and the
768 * tmp file to the original name. This is fast but doesn't work
769 * when the file is a link (hard or symbolic) or when we can't
770 * write to the current dir or can't set the permissions on the
772 * The second strategy consist simply in copying the old file
773 * to a backup file and rewrite the contents of the file.
776 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
777 (!(original_stat.st_nlink > 1) && !is_symlink))
779 char *dirname, *tmp_filename;
782 dirname = g_path_get_dirname (filename);
783 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
786 tmpfd = g_mkstemp (tmp_filename);
789 g_free (tmp_filename);
790 goto fallback_strategy;
793 /* try to keep permissions (unless replacing) */
795 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
798 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
801 fchmod (tmpfd, original_stat.st_mode) == -1 ||
807 struct stat tmp_statbuf;
809 /* Check that we really needed to change something */
810 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
811 original_stat.st_uid != tmp_statbuf.st_uid ||
812 original_stat.st_gid != tmp_statbuf.st_gid ||
813 original_stat.st_mode != tmp_statbuf.st_mode)
816 g_unlink (tmp_filename);
817 g_free (tmp_filename);
818 goto fallback_strategy;
823 *temp_filename = tmp_filename;
831 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
832 struct stat tmp_statbuf;
834 char *backup_filename;
837 backup_filename = create_backup_filename (filename);
839 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
841 g_set_error_literal (error,
843 G_IO_ERROR_CANT_CREATE_BACKUP,
844 _("Backup file creation failed"));
845 g_free (backup_filename);
849 bfd = g_open (backup_filename,
850 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
851 original_stat.st_mode & 0777);
855 g_set_error_literal (error,
857 G_IO_ERROR_CANT_CREATE_BACKUP,
858 _("Backup file creation failed"));
859 g_free (backup_filename);
863 /* If needed, Try to set the group of the backup same as the
864 * original file. If this fails, set the protection
865 * bits for the group same as the protection bits for
867 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
868 if (fstat (bfd, &tmp_statbuf) != 0)
870 g_set_error_literal (error,
872 G_IO_ERROR_CANT_CREATE_BACKUP,
873 _("Backup file creation failed"));
874 g_unlink (backup_filename);
875 g_free (backup_filename);
879 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
880 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
883 (original_stat.st_mode & 0707) |
884 ((original_stat.st_mode & 07) << 3)) != 0)
886 g_set_error_literal (error,
888 G_IO_ERROR_CANT_CREATE_BACKUP,
889 _("Backup file creation failed"));
890 g_unlink (backup_filename);
892 g_free (backup_filename);
898 if (!copy_file_data (fd, bfd, NULL))
900 g_set_error_literal (error,
902 G_IO_ERROR_CANT_CREATE_BACKUP,
903 _("Backup file creation failed"));
904 g_unlink (backup_filename);
906 g_free (backup_filename);
912 g_free (backup_filename);
914 /* Seek back to the start of the file after the backup copy */
915 if (lseek (fd, 0, SEEK_SET) == -1)
919 g_set_error (error, G_IO_ERROR,
920 g_io_error_from_errno (errsv),
921 _("Error seeking in file: %s"),
927 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
931 if (g_unlink (filename) != 0)
935 g_set_error (error, G_IO_ERROR,
936 g_io_error_from_errno (errsv),
937 _("Error removing old file: %s"),
942 fd = g_open (filename, O_WRONLY | O_CREAT | O_BINARY, mode);
946 char *display_name = g_filename_display_name (filename);
947 g_set_error (error, G_IO_ERROR,
948 g_io_error_from_errno (errsv),
949 _("Error opening file '%s': %s"),
950 display_name, g_strerror (errsv));
951 g_free (display_name);
957 /* Truncate the file at the start */
959 if (g_win32_ftruncate (fd, 0) == -1)
961 if (ftruncate (fd, 0) == -1)
966 g_set_error (error, G_IO_ERROR,
967 g_io_error_from_errno (errsv),
968 _("Error truncating file: %s"),
983 _g_local_file_output_stream_replace (const char *filename,
985 gboolean create_backup,
986 GFileCreateFlags flags,
987 GCancellable *cancellable,
990 GLocalFileOutputStream *stream;
994 gboolean sync_on_close;
996 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1001 if (flags & G_FILE_CREATE_PRIVATE)
1005 sync_on_close = FALSE;
1007 /* If the file doesn't exist, create it */
1008 fd = g_open (filename, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
1010 if (fd == -1 && errno == EEXIST)
1012 /* The file already exists */
1013 fd = handle_overwrite_open (filename, etag, create_backup, &temp_file,
1014 flags, cancellable, error);
1018 /* If the final destination exists, we want to sync the newly written
1019 * file to ensure the data is on disk when we rename over the destination.
1020 * otherwise if we get a system crash we can lose both the new and the
1021 * old file on some filesystems. (I.E. those that don't guarantee the
1022 * data is written to the disk before the metadata.)
1024 sync_on_close = TRUE;
1030 if (errsv == EINVAL)
1031 /* This must be an invalid filename, on e.g. FAT */
1032 g_set_error_literal (error, G_IO_ERROR,
1033 G_IO_ERROR_INVALID_FILENAME,
1034 _("Invalid filename"));
1037 char *display_name = g_filename_display_name (filename);
1038 g_set_error (error, G_IO_ERROR,
1039 g_io_error_from_errno (errsv),
1040 _("Error opening file '%s': %s"),
1041 display_name, g_strerror (errsv));
1042 g_free (display_name);
1048 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1049 stream->priv->fd = fd;
1050 stream->priv->sync_on_close = sync_on_close;
1051 stream->priv->tmp_filename = temp_file;
1053 stream->priv->backup_filename = create_backup_filename (filename);
1054 stream->priv->original_filename = g_strdup (filename);
1056 return G_FILE_OUTPUT_STREAM (stream);