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>
33 #include <glib/gstdio.h>
36 #include "glocalfileoutputstream.h"
37 #include "glocalfileinfo.h"
41 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
42 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
44 /* Some of the file replacement code was based on the code from gedit,
45 * relicenced to LGPL with permissions from the authors.
48 #define BACKUP_EXTENSION "~"
50 struct _GLocalFileOutputStreamPrivate {
52 char *original_filename;
53 char *backup_filename;
58 static gssize g_local_file_output_stream_write (GOutputStream *stream,
61 GCancellable *cancellable,
63 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
64 GCancellable *cancellable,
66 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
68 GCancellable *cancellable,
70 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
71 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
72 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
73 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
76 GCancellable *cancellable,
78 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
79 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
81 GCancellable *cancellable,
85 g_local_file_output_stream_finalize (GObject *object)
87 GLocalFileOutputStream *file;
89 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
91 g_free (file->priv->tmp_filename);
92 g_free (file->priv->original_filename);
93 g_free (file->priv->backup_filename);
94 g_free (file->priv->etag);
96 if (G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize)
97 (*G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize) (object);
101 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
103 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
104 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
105 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
107 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
109 gobject_class->finalize = g_local_file_output_stream_finalize;
111 stream_class->write = g_local_file_output_stream_write;
112 stream_class->close = g_local_file_output_stream_close;
113 file_stream_class->query_info = g_local_file_output_stream_query_info;
114 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
115 file_stream_class->tell = g_local_file_output_stream_tell;
116 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
117 file_stream_class->seek = g_local_file_output_stream_seek;
118 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
119 file_stream_class->truncate = g_local_file_output_stream_truncate;
123 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
125 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
126 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
127 GLocalFileOutputStreamPrivate);
131 g_local_file_output_stream_write (GOutputStream *stream,
134 GCancellable *cancellable,
137 GLocalFileOutputStream *file;
140 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
144 if (g_cancellable_set_error_if_cancelled (cancellable, error))
146 res = write (file->priv->fd, buffer, count);
152 g_set_error (error, G_IO_ERROR,
153 g_io_error_from_errno (errno),
154 _("Error writing to file: %s"),
165 g_local_file_output_stream_close (GOutputStream *stream,
166 GCancellable *cancellable,
169 GLocalFileOutputStream *file;
170 struct stat final_stat;
173 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
175 if (file->priv->tmp_filename)
177 /* We need to move the temp file to its final place,
178 * and possibly create the backup file
181 if (file->priv->backup_filename)
183 if (g_cancellable_set_error_if_cancelled (cancellable, error))
187 /* create original -> backup link, the original is then renamed over */
188 if (unlink (file->priv->backup_filename) != 0 &&
191 g_set_error (error, G_IO_ERROR,
192 G_IO_ERROR_CANT_CREATE_BACKUP,
193 _("Error removing old backup link: %s"),
198 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
200 g_set_error (error, G_IO_ERROR,
201 G_IO_ERROR_CANT_CREATE_BACKUP,
202 _("Error creating backup link: %s"),
207 /* If link not supported, just rename... */
208 if (!rename (file->priv->original_filename, file->priv->backup_filename) != 0)
210 g_set_error (error, G_IO_ERROR,
211 G_IO_ERROR_CANT_CREATE_BACKUP,
212 _("Error creating backup copy: %s"),
220 if (g_cancellable_set_error_if_cancelled (cancellable, error))
223 /* tmp -> original */
224 if (rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
226 g_set_error (error, G_IO_ERROR,
227 g_io_error_from_errno (errno),
228 _("Error renaming temporary file: %s"),
234 if (g_cancellable_set_error_if_cancelled (cancellable, error))
237 if (fstat (file->priv->fd, &final_stat) == 0)
238 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
242 res = close (file->priv->fd);
245 g_set_error (error, G_IO_ERROR,
246 g_io_error_from_errno (errno),
247 _("Error closing file: %s"),
256 /* A simple try to close the fd in case we fail before the actual close */
257 close (file->priv->fd);
262 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
264 GLocalFileOutputStream *file;
266 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
268 return g_strdup (file->priv->etag);
272 g_local_file_output_stream_tell (GFileOutputStream *stream)
274 GLocalFileOutputStream *file;
277 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
279 pos = lseek (file->priv->fd, 0, SEEK_CUR);
281 if (pos == (off_t)-1)
288 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
290 GLocalFileOutputStream *file;
293 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
295 pos = lseek (file->priv->fd, 0, SEEK_CUR);
297 if (pos == (off_t)-1 &&
305 seek_type_to_lseek (GSeekType type)
322 g_local_file_output_stream_seek (GFileOutputStream *stream,
325 GCancellable *cancellable,
328 GLocalFileOutputStream *file;
331 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
333 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
335 if (pos == (off_t)-1)
337 g_set_error (error, G_IO_ERROR,
338 g_io_error_from_errno (errno),
339 _("Error seeking in file: %s"),
348 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
350 /* We can't truncate pipes and stuff where we can't seek */
351 return g_local_file_output_stream_can_seek (stream);
355 g_local_file_output_stream_truncate (GFileOutputStream *stream,
357 GCancellable *cancellable,
360 GLocalFileOutputStream *file;
363 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
366 res = ftruncate (file->priv->fd, size);
372 if (g_cancellable_set_error_if_cancelled (cancellable, error))
377 g_set_error (error, G_IO_ERROR,
378 g_io_error_from_errno (errno),
379 _("Error truncating file: %s"),
389 g_local_file_output_stream_query_info (GFileOutputStream *stream,
391 GCancellable *cancellable,
394 GLocalFileOutputStream *file;
396 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
398 if (g_cancellable_set_error_if_cancelled (cancellable, error))
401 return _g_local_file_info_get_from_fd (file->priv->fd,
407 _g_local_file_output_stream_create (const char *filename,
408 GFileCreateFlags flags,
409 GCancellable *cancellable,
412 GLocalFileOutputStream *stream;
416 if (g_cancellable_set_error_if_cancelled (cancellable, error))
419 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
424 fd = g_open (filename,
425 O_CREAT | O_EXCL | O_WRONLY,
432 /* This must be an invalid filename, on e.g. FAT */
433 g_set_error (error, G_IO_ERROR,
434 G_IO_ERROR_INVALID_FILENAME,
435 _("Invalid filename"));
437 g_set_error (error, G_IO_ERROR,
438 g_io_error_from_errno (errsv),
439 _("Error opening file '%s': %s"),
440 filename, g_strerror (errsv));
444 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
445 stream->priv->fd = fd;
446 return G_FILE_OUTPUT_STREAM (stream);
450 _g_local_file_output_stream_append (const char *filename,
451 GFileCreateFlags flags,
452 GCancellable *cancellable,
455 GLocalFileOutputStream *stream;
459 if (g_cancellable_set_error_if_cancelled (cancellable, error))
462 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
467 fd = g_open (filename,
468 O_CREAT | O_APPEND | O_WRONLY,
475 /* This must be an invalid filename, on e.g. FAT */
476 g_set_error (error, G_IO_ERROR,
477 G_IO_ERROR_INVALID_FILENAME,
478 _("Invalid filename"));
480 g_set_error (error, G_IO_ERROR,
481 g_io_error_from_errno (errsv),
482 _("Error opening file '%s': %s"),
483 filename, g_strerror (errsv));
487 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
488 stream->priv->fd = fd;
490 return G_FILE_OUTPUT_STREAM (stream);
494 create_backup_filename (const char *filename)
496 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
499 #define BUFSIZE 8192 /* size of normal write buffer */
502 copy_file_data (gint sfd,
508 const gchar *write_buffer;
510 ssize_t bytes_to_write;
511 ssize_t bytes_written;
513 buffer = g_malloc (BUFSIZE);
517 bytes_read = read (sfd, buffer, BUFSIZE);
518 if (bytes_read == -1)
523 g_set_error (error, G_IO_ERROR,
524 g_io_error_from_errno (errno),
525 _("Error reading from file: %s"),
531 bytes_to_write = bytes_read;
532 write_buffer = buffer;
536 bytes_written = write (dfd, write_buffer, bytes_to_write);
537 if (bytes_written == -1)
542 g_set_error (error, G_IO_ERROR,
543 g_io_error_from_errno (errno),
544 _("Error writing to file: %s"),
550 bytes_to_write -= bytes_written;
551 write_buffer += bytes_written;
553 while (bytes_to_write > 0);
555 } while ((bytes_read != 0) && (ret == TRUE));
563 handle_overwrite_open (const char *filename,
565 gboolean create_backup,
566 char **temp_filename,
567 GCancellable *cancellable,
571 struct stat original_stat;
576 /* We only need read access to the original file if we are creating a backup.
577 * We also add O_CREATE to avoid a race if the file was just removed */
579 open_flags = O_RDWR | O_CREAT;
581 open_flags = O_WRONLY | O_CREAT;
583 /* Some systems have O_NOFOLLOW, which lets us avoid some races
584 * when finding out if the file we opened was a symlink */
587 fd = g_open (filename, open_flags | O_NOFOLLOW, 0666);
588 if (fd == -1 && errno == ELOOP)
590 /* Could be a symlink, or it could be a regular ELOOP error,
591 * but then the next open will fail too. */
593 fd = g_open (filename, open_flags, 0666);
596 fd = g_open (filename, open_flags, 0666);
597 /* This is racy, but we do it as soon as possible to minimize the race */
598 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
603 g_set_error (error, G_IO_ERROR,
604 g_io_error_from_errno (errno),
605 _("Error opening file '%s': %s"),
606 filename, g_strerror (errno));
610 if (fstat (fd, &original_stat) != 0)
612 g_set_error (error, G_IO_ERROR,
613 g_io_error_from_errno (errno),
614 _("Error stating file '%s': %s"),
615 filename, g_strerror (errno));
619 /* not a regular file */
620 if (!S_ISREG (original_stat.st_mode))
622 if (S_ISDIR (original_stat.st_mode))
625 G_IO_ERROR_IS_DIRECTORY,
626 _("Target file is a directory"));
630 G_IO_ERROR_NOT_REGULAR_FILE,
631 _("Target file is not a regular file"));
637 current_etag = _g_local_file_info_create_etag (&original_stat);
638 if (strcmp (etag, current_etag) != 0)
642 G_IO_ERROR_WRONG_ETAG,
643 _("The file was externally modified"));
644 g_free (current_etag);
647 g_free (current_etag);
650 /* We use two backup strategies.
651 * The first one (which is faster) consist in saving to a
652 * tmp file then rename the original file to the backup and the
653 * tmp file to the original name. This is fast but doesn't work
654 * when the file is a link (hard or symbolic) or when we can't
655 * write to the current dir or can't set the permissions on the
657 * The second strategy consist simply in copying the old file
658 * to a backup file and rewrite the contents of the file.
661 if (!(original_stat.st_nlink > 1) && !is_symlink)
663 char *dirname, *tmp_filename;
666 dirname = g_path_get_dirname (filename);
667 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
670 tmpfd = g_mkstemp (tmp_filename);
673 g_free (tmp_filename);
674 goto fallback_strategy;
677 /* try to keep permissions */
681 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
684 fchmod (tmpfd, original_stat.st_mode) == -1 ||
689 struct stat tmp_statbuf;
691 /* Check that we really needed to change something */
692 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
693 original_stat.st_uid != tmp_statbuf.st_uid ||
694 original_stat.st_gid != tmp_statbuf.st_gid ||
695 original_stat.st_mode != tmp_statbuf.st_mode)
698 unlink (tmp_filename);
699 g_free (tmp_filename);
700 goto fallback_strategy;
705 *temp_filename = tmp_filename;
713 struct stat tmp_statbuf;
714 char *backup_filename;
717 backup_filename = create_backup_filename (filename);
719 if (unlink (backup_filename) == -1 && errno != ENOENT)
723 G_IO_ERROR_CANT_CREATE_BACKUP,
724 _("Backup file creation failed"));
725 g_free (backup_filename);
729 bfd = g_open (backup_filename,
730 O_WRONLY | O_CREAT | O_EXCL,
731 original_stat.st_mode & 0777);
737 G_IO_ERROR_CANT_CREATE_BACKUP,
738 _("Backup file creation failed"));
739 g_free (backup_filename);
743 /* If needed, Try to set the group of the backup same as the
744 * original file. If this fails, set the protection
745 * bits for the group same as the protection bits for
748 if (fstat (bfd, &tmp_statbuf) != 0)
752 G_IO_ERROR_CANT_CREATE_BACKUP,
753 _("Backup file creation failed"));
754 unlink (backup_filename);
755 g_free (backup_filename);
759 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
760 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
763 (original_stat.st_mode & 0707) |
764 ((original_stat.st_mode & 07) << 3)) != 0)
768 G_IO_ERROR_CANT_CREATE_BACKUP,
769 _("Backup file creation failed"));
770 unlink (backup_filename);
772 g_free (backup_filename);
778 if (!copy_file_data (fd, bfd, NULL))
782 G_IO_ERROR_CANT_CREATE_BACKUP,
783 _("Backup file creation failed"));
784 unlink (backup_filename);
786 g_free (backup_filename);
792 g_free (backup_filename);
794 /* Seek back to the start of the file after the backup copy */
795 if (lseek (fd, 0, SEEK_SET) == -1)
797 g_set_error (error, G_IO_ERROR,
798 g_io_error_from_errno (errno),
799 _("Error seeking in file: %s"),
805 /* Truncate the file at the start */
806 if (ftruncate (fd, 0) == -1)
808 g_set_error (error, G_IO_ERROR,
809 g_io_error_from_errno (errno),
810 _("Error truncating file: %s"),
823 _g_local_file_output_stream_replace (const char *filename,
825 gboolean create_backup,
826 GFileCreateFlags flags,
827 GCancellable *cancellable,
830 GLocalFileOutputStream *stream;
835 if (g_cancellable_set_error_if_cancelled (cancellable, error))
840 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
845 /* If the file doesn't exist, create it */
846 fd = g_open (filename,
847 O_CREAT | O_EXCL | O_WRONLY,
850 if (fd == -1 && errno == EEXIST)
852 /* The file already exists */
853 fd = handle_overwrite_open (filename, etag, create_backup, &temp_file,
863 /* This must be an invalid filename, on e.g. FAT */
864 g_set_error (error, G_IO_ERROR,
865 G_IO_ERROR_INVALID_FILENAME,
866 _("Invalid filename"));
868 g_set_error (error, G_IO_ERROR,
869 g_io_error_from_errno (errsv),
870 _("Error opening file '%s': %s"),
871 filename, g_strerror (errsv));
876 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
877 stream->priv->fd = fd;
878 stream->priv->tmp_filename = temp_file;
880 stream->priv->backup_filename = create_backup_filename (filename);
881 stream->priv->original_filename = g_strdup (filename);
883 return G_FILE_OUTPUT_STREAM (stream);