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 "glocalfileoutputstream.h"
39 #include "glocalfileinfo.h"
44 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
47 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
57 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
58 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
60 /* Some of the file replacement code was based on the code from gedit,
61 * relicenced to LGPL with permissions from the authors.
64 #define BACKUP_EXTENSION "~"
66 struct _GLocalFileOutputStreamPrivate {
68 char *original_filename;
69 char *backup_filename;
74 static gssize g_local_file_output_stream_write (GOutputStream *stream,
77 GCancellable *cancellable,
79 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
80 GCancellable *cancellable,
82 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
84 GCancellable *cancellable,
86 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
87 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
88 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
89 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
92 GCancellable *cancellable,
94 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
95 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
97 GCancellable *cancellable,
101 g_local_file_output_stream_finalize (GObject *object)
103 GLocalFileOutputStream *file;
105 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
107 g_free (file->priv->tmp_filename);
108 g_free (file->priv->original_filename);
109 g_free (file->priv->backup_filename);
110 g_free (file->priv->etag);
112 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
116 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
118 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
120 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
122 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
124 gobject_class->finalize = g_local_file_output_stream_finalize;
126 stream_class->write_fn = g_local_file_output_stream_write;
127 stream_class->close_fn = g_local_file_output_stream_close;
128 file_stream_class->query_info = g_local_file_output_stream_query_info;
129 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
130 file_stream_class->tell = g_local_file_output_stream_tell;
131 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
132 file_stream_class->seek = g_local_file_output_stream_seek;
133 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
134 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
138 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
140 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
141 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
142 GLocalFileOutputStreamPrivate);
146 g_local_file_output_stream_write (GOutputStream *stream,
149 GCancellable *cancellable,
152 GLocalFileOutputStream *file;
155 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
159 if (g_cancellable_set_error_if_cancelled (cancellable, error))
161 res = write (file->priv->fd, buffer, count);
169 g_set_error (error, G_IO_ERROR,
170 g_io_error_from_errno (errsv),
171 _("Error writing to file: %s"),
182 g_local_file_output_stream_close (GOutputStream *stream,
183 GCancellable *cancellable,
186 GLocalFileOutputStream *file;
187 struct stat final_stat;
190 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
192 if (file->priv->tmp_filename)
194 /* We need to move the temp file to its final place,
195 * and possibly create the backup file
198 if (file->priv->backup_filename)
200 if (g_cancellable_set_error_if_cancelled (cancellable, error))
204 /* create original -> backup link, the original is then renamed over */
205 if (g_unlink (file->priv->backup_filename) != 0 &&
210 g_set_error (error, G_IO_ERROR,
211 G_IO_ERROR_CANT_CREATE_BACKUP,
212 _("Error removing old backup link: %s"),
217 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
219 /* link failed or is not supported, try rename */
220 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
224 g_set_error (error, G_IO_ERROR,
225 G_IO_ERROR_CANT_CREATE_BACKUP,
226 _("Error creating backup copy: %s"),
232 /* If link not supported, just rename... */
233 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
237 g_set_error (error, G_IO_ERROR,
238 G_IO_ERROR_CANT_CREATE_BACKUP,
239 _("Error creating backup copy: %s"),
247 if (g_cancellable_set_error_if_cancelled (cancellable, error))
250 /* tmp -> original */
251 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
255 g_set_error (error, G_IO_ERROR,
256 g_io_error_from_errno (errno),
257 _("Error renaming temporary file: %s"),
263 if (g_cancellable_set_error_if_cancelled (cancellable, error))
266 if (fstat (file->priv->fd, &final_stat) == 0)
267 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
271 res = close (file->priv->fd);
276 g_set_error (error, G_IO_ERROR,
277 g_io_error_from_errno (errsv),
278 _("Error closing file: %s"),
287 /* A simple try to close the fd in case we fail before the actual close */
288 close (file->priv->fd);
293 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
295 GLocalFileOutputStream *file;
297 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
299 return g_strdup (file->priv->etag);
303 g_local_file_output_stream_tell (GFileOutputStream *stream)
305 GLocalFileOutputStream *file;
308 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
310 pos = lseek (file->priv->fd, 0, SEEK_CUR);
312 if (pos == (off_t)-1)
319 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
321 GLocalFileOutputStream *file;
324 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
326 pos = lseek (file->priv->fd, 0, SEEK_CUR);
328 if (pos == (off_t)-1 && errno == ESPIPE)
335 seek_type_to_lseek (GSeekType type)
352 g_local_file_output_stream_seek (GFileOutputStream *stream,
355 GCancellable *cancellable,
358 GLocalFileOutputStream *file;
361 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
363 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
365 if (pos == (off_t)-1)
369 g_set_error (error, G_IO_ERROR,
370 g_io_error_from_errno (errsv),
371 _("Error seeking in file: %s"),
380 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
382 /* We can't truncate pipes and stuff where we can't seek */
383 return g_local_file_output_stream_can_seek (stream);
387 g_local_file_output_stream_truncate (GFileOutputStream *stream,
389 GCancellable *cancellable,
392 GLocalFileOutputStream *file;
395 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
399 res = g_win32_ftruncate (file->priv->fd, size);
401 res = ftruncate (file->priv->fd, size);
410 if (g_cancellable_set_error_if_cancelled (cancellable, error))
415 g_set_error (error, G_IO_ERROR,
416 g_io_error_from_errno (errsv),
417 _("Error truncating file: %s"),
427 g_local_file_output_stream_query_info (GFileOutputStream *stream,
429 GCancellable *cancellable,
432 GLocalFileOutputStream *file;
434 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
436 if (g_cancellable_set_error_if_cancelled (cancellable, error))
439 return _g_local_file_info_get_from_fd (file->priv->fd,
445 _g_local_file_output_stream_create (const char *filename,
446 GFileCreateFlags flags,
447 GCancellable *cancellable,
450 GLocalFileOutputStream *stream;
454 if (g_cancellable_set_error_if_cancelled (cancellable, error))
457 if (flags & G_FILE_CREATE_PRIVATE)
462 fd = g_open (filename, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
468 /* This must be an invalid filename, on e.g. FAT */
469 g_set_error_literal (error, G_IO_ERROR,
470 G_IO_ERROR_INVALID_FILENAME,
471 _("Invalid filename"));
474 char *display_name = g_filename_display_name (filename);
475 g_set_error (error, G_IO_ERROR,
476 g_io_error_from_errno (errsv),
477 _("Error opening file '%s': %s"),
478 display_name, g_strerror (errsv));
479 g_free (display_name);
484 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
485 stream->priv->fd = fd;
486 return G_FILE_OUTPUT_STREAM (stream);
490 _g_local_file_output_stream_append (const char *filename,
491 GFileCreateFlags flags,
492 GCancellable *cancellable,
495 GLocalFileOutputStream *stream;
499 if (g_cancellable_set_error_if_cancelled (cancellable, error))
502 if (flags & G_FILE_CREATE_PRIVATE)
507 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
513 /* This must be an invalid filename, on e.g. FAT */
514 g_set_error_literal (error, G_IO_ERROR,
515 G_IO_ERROR_INVALID_FILENAME,
516 _("Invalid filename"));
519 char *display_name = g_filename_display_name (filename);
520 g_set_error (error, G_IO_ERROR,
521 g_io_error_from_errno (errsv),
522 _("Error opening file '%s': %s"),
523 display_name, g_strerror (errsv));
524 g_free (display_name);
529 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
530 stream->priv->fd = fd;
532 return G_FILE_OUTPUT_STREAM (stream);
536 create_backup_filename (const char *filename)
538 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
541 #define BUFSIZE 8192 /* size of normal write buffer */
544 copy_file_data (gint sfd,
550 const gchar *write_buffer;
552 gssize bytes_to_write;
553 gssize bytes_written;
555 buffer = g_malloc (BUFSIZE);
559 bytes_read = read (sfd, buffer, BUFSIZE);
560 if (bytes_read == -1)
567 g_set_error (error, G_IO_ERROR,
568 g_io_error_from_errno (errsv),
569 _("Error reading from file: %s"),
575 bytes_to_write = bytes_read;
576 write_buffer = buffer;
580 bytes_written = write (dfd, write_buffer, bytes_to_write);
581 if (bytes_written == -1)
588 g_set_error (error, G_IO_ERROR,
589 g_io_error_from_errno (errsv),
590 _("Error writing to file: %s"),
596 bytes_to_write -= bytes_written;
597 write_buffer += bytes_written;
599 while (bytes_to_write > 0);
601 } while ((bytes_read != 0) && (ret == TRUE));
609 handle_overwrite_open (const char *filename,
611 gboolean create_backup,
612 char **temp_filename,
613 GCancellable *cancellable,
617 struct stat original_stat;
622 /* We only need read access to the original file if we are creating a backup.
623 * We also add O_CREATE to avoid a race if the file was just removed */
625 open_flags = O_RDWR | O_CREAT | O_BINARY;
627 open_flags = O_WRONLY | O_CREAT | O_BINARY;
629 /* Some systems have O_NOFOLLOW, which lets us avoid some races
630 * when finding out if the file we opened was a symlink */
633 fd = g_open (filename, open_flags | O_NOFOLLOW, 0666);
634 if (fd == -1 && errno == ELOOP)
636 /* Could be a symlink, or it could be a regular ELOOP error,
637 * but then the next open will fail too. */
639 fd = g_open (filename, open_flags, 0666);
642 fd = g_open (filename, open_flags, 0666);
643 /* This is racy, but we do it as soon as possible to minimize the race */
644 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
650 char *display_name = g_filename_display_name (filename);
651 g_set_error (error, G_IO_ERROR,
652 g_io_error_from_errno (errsv),
653 _("Error opening file '%s': %s"),
654 display_name, g_strerror (errsv));
655 g_free (display_name);
659 if (fstat (fd, &original_stat) != 0)
662 char *display_name = g_filename_display_name (filename);
663 g_set_error (error, G_IO_ERROR,
664 g_io_error_from_errno (errsv),
665 _("Error stating file '%s': %s"),
666 display_name, g_strerror (errsv));
667 g_free (display_name);
671 /* not a regular file */
672 if (!S_ISREG (original_stat.st_mode))
674 if (S_ISDIR (original_stat.st_mode))
675 g_set_error_literal (error,
677 G_IO_ERROR_IS_DIRECTORY,
678 _("Target file is a directory"));
680 g_set_error_literal (error,
682 G_IO_ERROR_NOT_REGULAR_FILE,
683 _("Target file is not a regular file"));
689 current_etag = _g_local_file_info_create_etag (&original_stat);
690 if (strcmp (etag, current_etag) != 0)
692 g_set_error_literal (error,
694 G_IO_ERROR_WRONG_ETAG,
695 _("The file was externally modified"));
696 g_free (current_etag);
699 g_free (current_etag);
702 /* We use two backup strategies.
703 * The first one (which is faster) consist in saving to a
704 * tmp file then rename the original file to the backup and the
705 * tmp file to the original name. This is fast but doesn't work
706 * when the file is a link (hard or symbolic) or when we can't
707 * write to the current dir or can't set the permissions on the
709 * The second strategy consist simply in copying the old file
710 * to a backup file and rewrite the contents of the file.
713 if (!(original_stat.st_nlink > 1) && !is_symlink)
715 char *dirname, *tmp_filename;
718 dirname = g_path_get_dirname (filename);
719 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
722 tmpfd = g_mkstemp (tmp_filename);
725 g_free (tmp_filename);
726 goto fallback_strategy;
729 /* try to keep permissions */
733 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
736 fchmod (tmpfd, original_stat.st_mode) == -1 ||
741 struct stat tmp_statbuf;
743 /* Check that we really needed to change something */
744 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
745 original_stat.st_uid != tmp_statbuf.st_uid ||
746 original_stat.st_gid != tmp_statbuf.st_gid ||
747 original_stat.st_mode != tmp_statbuf.st_mode)
750 g_unlink (tmp_filename);
751 g_free (tmp_filename);
752 goto fallback_strategy;
757 *temp_filename = tmp_filename;
765 struct stat tmp_statbuf;
766 char *backup_filename;
769 backup_filename = create_backup_filename (filename);
771 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
773 g_set_error_literal (error,
775 G_IO_ERROR_CANT_CREATE_BACKUP,
776 _("Backup file creation failed"));
777 g_free (backup_filename);
781 bfd = g_open (backup_filename,
782 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
783 original_stat.st_mode & 0777);
787 g_set_error_literal (error,
789 G_IO_ERROR_CANT_CREATE_BACKUP,
790 _("Backup file creation failed"));
791 g_free (backup_filename);
795 /* If needed, Try to set the group of the backup same as the
796 * original file. If this fails, set the protection
797 * bits for the group same as the protection bits for
799 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
800 if (fstat (bfd, &tmp_statbuf) != 0)
802 g_set_error_literal (error,
804 G_IO_ERROR_CANT_CREATE_BACKUP,
805 _("Backup file creation failed"));
806 g_unlink (backup_filename);
807 g_free (backup_filename);
811 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
812 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
815 (original_stat.st_mode & 0707) |
816 ((original_stat.st_mode & 07) << 3)) != 0)
818 g_set_error_literal (error,
820 G_IO_ERROR_CANT_CREATE_BACKUP,
821 _("Backup file creation failed"));
822 g_unlink (backup_filename);
824 g_free (backup_filename);
830 if (!copy_file_data (fd, bfd, NULL))
832 g_set_error_literal (error,
834 G_IO_ERROR_CANT_CREATE_BACKUP,
835 _("Backup file creation failed"));
836 g_unlink (backup_filename);
838 g_free (backup_filename);
844 g_free (backup_filename);
846 /* Seek back to the start of the file after the backup copy */
847 if (lseek (fd, 0, SEEK_SET) == -1)
851 g_set_error (error, G_IO_ERROR,
852 g_io_error_from_errno (errsv),
853 _("Error seeking in file: %s"),
859 /* Truncate the file at the start */
861 if (g_win32_ftruncate (fd, 0) == -1)
863 if (ftruncate (fd, 0) == -1)
868 g_set_error (error, G_IO_ERROR,
869 g_io_error_from_errno (errsv),
870 _("Error truncating file: %s"),
883 _g_local_file_output_stream_replace (const char *filename,
885 gboolean create_backup,
886 GFileCreateFlags flags,
887 GCancellable *cancellable,
890 GLocalFileOutputStream *stream;
895 if (g_cancellable_set_error_if_cancelled (cancellable, error))
900 if (flags & G_FILE_CREATE_PRIVATE)
905 /* If the file doesn't exist, create it */
906 fd = g_open (filename, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
908 if (fd == -1 && errno == EEXIST)
910 /* The file already exists */
911 fd = handle_overwrite_open (filename, etag, create_backup, &temp_file,
921 /* This must be an invalid filename, on e.g. FAT */
922 g_set_error_literal (error, G_IO_ERROR,
923 G_IO_ERROR_INVALID_FILENAME,
924 _("Invalid filename"));
927 char *display_name = g_filename_display_name (filename);
928 g_set_error (error, G_IO_ERROR,
929 g_io_error_from_errno (errsv),
930 _("Error opening file '%s': %s"),
931 display_name, g_strerror (errsv));
932 g_free (display_name);
938 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
939 stream->priv->fd = fd;
940 stream->priv->tmp_filename = temp_file;
942 stream->priv->backup_filename = create_backup_filename (filename);
943 stream->priv->original_filename = g_strdup (filename);
945 return G_FILE_OUTPUT_STREAM (stream);