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"
43 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
44 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
46 /* Some of the file replacement code was based on the code from gedit,
47 * relicenced to LGPL with permissions from the authors.
50 #define BACKUP_EXTENSION "~"
52 struct _GLocalFileOutputStreamPrivate {
54 char *original_filename;
55 char *backup_filename;
60 static gssize g_local_file_output_stream_write (GOutputStream *stream,
63 GCancellable *cancellable,
65 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
66 GCancellable *cancellable,
68 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
70 GCancellable *cancellable,
72 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
73 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
74 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
75 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
78 GCancellable *cancellable,
80 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
81 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
83 GCancellable *cancellable,
87 g_local_file_output_stream_finalize (GObject *object)
89 GLocalFileOutputStream *file;
91 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
93 g_free (file->priv->tmp_filename);
94 g_free (file->priv->original_filename);
95 g_free (file->priv->backup_filename);
96 g_free (file->priv->etag);
98 if (G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize)
99 (*G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize) (object);
103 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
105 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
106 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
107 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
109 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
111 gobject_class->finalize = g_local_file_output_stream_finalize;
113 stream_class->write_fn = g_local_file_output_stream_write;
114 stream_class->close_fn = g_local_file_output_stream_close;
115 file_stream_class->query_info = g_local_file_output_stream_query_info;
116 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
117 file_stream_class->tell = g_local_file_output_stream_tell;
118 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
119 file_stream_class->seek = g_local_file_output_stream_seek;
120 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
121 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
125 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
127 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
128 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
129 GLocalFileOutputStreamPrivate);
133 g_local_file_output_stream_write (GOutputStream *stream,
136 GCancellable *cancellable,
139 GLocalFileOutputStream *file;
142 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
146 if (g_cancellable_set_error_if_cancelled (cancellable, error))
148 res = write (file->priv->fd, buffer, count);
154 g_set_error (error, G_IO_ERROR,
155 g_io_error_from_errno (errno),
156 _("Error writing to file: %s"),
167 g_local_file_output_stream_close (GOutputStream *stream,
168 GCancellable *cancellable,
171 GLocalFileOutputStream *file;
172 struct stat final_stat;
175 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
177 if (file->priv->tmp_filename)
179 /* We need to move the temp file to its final place,
180 * and possibly create the backup file
183 if (file->priv->backup_filename)
185 if (g_cancellable_set_error_if_cancelled (cancellable, error))
189 /* create original -> backup link, the original is then renamed over */
190 if (unlink (file->priv->backup_filename) != 0 &&
193 g_set_error (error, G_IO_ERROR,
194 G_IO_ERROR_CANT_CREATE_BACKUP,
195 _("Error removing old backup link: %s"),
200 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
202 g_set_error (error, G_IO_ERROR,
203 G_IO_ERROR_CANT_CREATE_BACKUP,
204 _("Error creating backup link: %s"),
209 /* If link not supported, just rename... */
210 if (!rename (file->priv->original_filename, file->priv->backup_filename) != 0)
212 g_set_error (error, G_IO_ERROR,
213 G_IO_ERROR_CANT_CREATE_BACKUP,
214 _("Error creating backup copy: %s"),
222 if (g_cancellable_set_error_if_cancelled (cancellable, error))
225 /* tmp -> original */
226 if (rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
228 g_set_error (error, G_IO_ERROR,
229 g_io_error_from_errno (errno),
230 _("Error renaming temporary file: %s"),
236 if (g_cancellable_set_error_if_cancelled (cancellable, error))
239 if (fstat (file->priv->fd, &final_stat) == 0)
240 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
244 res = close (file->priv->fd);
247 g_set_error (error, G_IO_ERROR,
248 g_io_error_from_errno (errno),
249 _("Error closing file: %s"),
258 /* A simple try to close the fd in case we fail before the actual close */
259 close (file->priv->fd);
264 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
266 GLocalFileOutputStream *file;
268 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
270 return g_strdup (file->priv->etag);
274 g_local_file_output_stream_tell (GFileOutputStream *stream)
276 GLocalFileOutputStream *file;
279 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
281 pos = lseek (file->priv->fd, 0, SEEK_CUR);
283 if (pos == (off_t)-1)
290 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
292 GLocalFileOutputStream *file;
295 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
297 pos = lseek (file->priv->fd, 0, SEEK_CUR);
299 if (pos == (off_t)-1 &&
307 seek_type_to_lseek (GSeekType type)
324 g_local_file_output_stream_seek (GFileOutputStream *stream,
327 GCancellable *cancellable,
330 GLocalFileOutputStream *file;
333 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
335 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
337 if (pos == (off_t)-1)
339 g_set_error (error, G_IO_ERROR,
340 g_io_error_from_errno (errno),
341 _("Error seeking in file: %s"),
350 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
352 /* We can't truncate pipes and stuff where we can't seek */
353 return g_local_file_output_stream_can_seek (stream);
357 g_local_file_output_stream_truncate (GFileOutputStream *stream,
359 GCancellable *cancellable,
362 GLocalFileOutputStream *file;
365 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
368 res = ftruncate (file->priv->fd, size);
374 if (g_cancellable_set_error_if_cancelled (cancellable, error))
379 g_set_error (error, G_IO_ERROR,
380 g_io_error_from_errno (errno),
381 _("Error truncating file: %s"),
391 g_local_file_output_stream_query_info (GFileOutputStream *stream,
393 GCancellable *cancellable,
396 GLocalFileOutputStream *file;
398 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
400 if (g_cancellable_set_error_if_cancelled (cancellable, error))
403 return _g_local_file_info_get_from_fd (file->priv->fd,
409 _g_local_file_output_stream_create (const char *filename,
410 GFileCreateFlags flags,
411 GCancellable *cancellable,
414 GLocalFileOutputStream *stream;
418 if (g_cancellable_set_error_if_cancelled (cancellable, error))
421 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
426 fd = g_open (filename,
427 O_CREAT | O_EXCL | O_WRONLY,
434 /* This must be an invalid filename, on e.g. FAT */
435 g_set_error (error, G_IO_ERROR,
436 G_IO_ERROR_INVALID_FILENAME,
437 _("Invalid filename"));
439 g_set_error (error, G_IO_ERROR,
440 g_io_error_from_errno (errsv),
441 _("Error opening file '%s': %s"),
442 filename, g_strerror (errsv));
446 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
447 stream->priv->fd = fd;
448 return G_FILE_OUTPUT_STREAM (stream);
452 _g_local_file_output_stream_append (const char *filename,
453 GFileCreateFlags flags,
454 GCancellable *cancellable,
457 GLocalFileOutputStream *stream;
461 if (g_cancellable_set_error_if_cancelled (cancellable, error))
464 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
469 fd = g_open (filename,
470 O_CREAT | O_APPEND | O_WRONLY,
477 /* This must be an invalid filename, on e.g. FAT */
478 g_set_error (error, G_IO_ERROR,
479 G_IO_ERROR_INVALID_FILENAME,
480 _("Invalid filename"));
482 g_set_error (error, G_IO_ERROR,
483 g_io_error_from_errno (errsv),
484 _("Error opening file '%s': %s"),
485 filename, g_strerror (errsv));
489 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
490 stream->priv->fd = fd;
492 return G_FILE_OUTPUT_STREAM (stream);
496 create_backup_filename (const char *filename)
498 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
501 #define BUFSIZE 8192 /* size of normal write buffer */
504 copy_file_data (gint sfd,
510 const gchar *write_buffer;
512 gssize bytes_to_write;
513 gssize bytes_written;
515 buffer = g_malloc (BUFSIZE);
519 bytes_read = read (sfd, buffer, BUFSIZE);
520 if (bytes_read == -1)
525 g_set_error (error, G_IO_ERROR,
526 g_io_error_from_errno (errno),
527 _("Error reading from file: %s"),
533 bytes_to_write = bytes_read;
534 write_buffer = buffer;
538 bytes_written = write (dfd, write_buffer, bytes_to_write);
539 if (bytes_written == -1)
544 g_set_error (error, G_IO_ERROR,
545 g_io_error_from_errno (errno),
546 _("Error writing to file: %s"),
552 bytes_to_write -= bytes_written;
553 write_buffer += bytes_written;
555 while (bytes_to_write > 0);
557 } while ((bytes_read != 0) && (ret == TRUE));
565 handle_overwrite_open (const char *filename,
567 gboolean create_backup,
568 char **temp_filename,
569 GCancellable *cancellable,
573 struct stat original_stat;
578 /* We only need read access to the original file if we are creating a backup.
579 * We also add O_CREATE to avoid a race if the file was just removed */
581 open_flags = O_RDWR | O_CREAT;
583 open_flags = O_WRONLY | O_CREAT;
585 /* Some systems have O_NOFOLLOW, which lets us avoid some races
586 * when finding out if the file we opened was a symlink */
589 fd = g_open (filename, open_flags | O_NOFOLLOW, 0666);
590 if (fd == -1 && errno == ELOOP)
592 /* Could be a symlink, or it could be a regular ELOOP error,
593 * but then the next open will fail too. */
595 fd = g_open (filename, open_flags, 0666);
598 fd = g_open (filename, open_flags, 0666);
599 /* This is racy, but we do it as soon as possible to minimize the race */
600 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
605 g_set_error (error, G_IO_ERROR,
606 g_io_error_from_errno (errno),
607 _("Error opening file '%s': %s"),
608 filename, g_strerror (errno));
612 if (fstat (fd, &original_stat) != 0)
614 g_set_error (error, G_IO_ERROR,
615 g_io_error_from_errno (errno),
616 _("Error stating file '%s': %s"),
617 filename, g_strerror (errno));
621 /* not a regular file */
622 if (!S_ISREG (original_stat.st_mode))
624 if (S_ISDIR (original_stat.st_mode))
627 G_IO_ERROR_IS_DIRECTORY,
628 _("Target file is a directory"));
632 G_IO_ERROR_NOT_REGULAR_FILE,
633 _("Target file is not a regular file"));
639 current_etag = _g_local_file_info_create_etag (&original_stat);
640 if (strcmp (etag, current_etag) != 0)
644 G_IO_ERROR_WRONG_ETAG,
645 _("The file was externally modified"));
646 g_free (current_etag);
649 g_free (current_etag);
652 /* We use two backup strategies.
653 * The first one (which is faster) consist in saving to a
654 * tmp file then rename the original file to the backup and the
655 * tmp file to the original name. This is fast but doesn't work
656 * when the file is a link (hard or symbolic) or when we can't
657 * write to the current dir or can't set the permissions on the
659 * The second strategy consist simply in copying the old file
660 * to a backup file and rewrite the contents of the file.
663 if (!(original_stat.st_nlink > 1) && !is_symlink)
665 char *dirname, *tmp_filename;
668 dirname = g_path_get_dirname (filename);
669 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
672 tmpfd = g_mkstemp (tmp_filename);
675 g_free (tmp_filename);
676 goto fallback_strategy;
679 /* try to keep permissions */
683 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
686 fchmod (tmpfd, original_stat.st_mode) == -1 ||
691 struct stat tmp_statbuf;
693 /* Check that we really needed to change something */
694 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
695 original_stat.st_uid != tmp_statbuf.st_uid ||
696 original_stat.st_gid != tmp_statbuf.st_gid ||
697 original_stat.st_mode != tmp_statbuf.st_mode)
700 unlink (tmp_filename);
701 g_free (tmp_filename);
702 goto fallback_strategy;
707 *temp_filename = tmp_filename;
715 struct stat tmp_statbuf;
716 char *backup_filename;
719 backup_filename = create_backup_filename (filename);
721 if (unlink (backup_filename) == -1 && errno != ENOENT)
725 G_IO_ERROR_CANT_CREATE_BACKUP,
726 _("Backup file creation failed"));
727 g_free (backup_filename);
731 bfd = g_open (backup_filename,
732 O_WRONLY | O_CREAT | O_EXCL,
733 original_stat.st_mode & 0777);
739 G_IO_ERROR_CANT_CREATE_BACKUP,
740 _("Backup file creation failed"));
741 g_free (backup_filename);
745 /* If needed, Try to set the group of the backup same as the
746 * original file. If this fails, set the protection
747 * bits for the group same as the protection bits for
750 if (fstat (bfd, &tmp_statbuf) != 0)
754 G_IO_ERROR_CANT_CREATE_BACKUP,
755 _("Backup file creation failed"));
756 unlink (backup_filename);
757 g_free (backup_filename);
761 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
762 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
765 (original_stat.st_mode & 0707) |
766 ((original_stat.st_mode & 07) << 3)) != 0)
770 G_IO_ERROR_CANT_CREATE_BACKUP,
771 _("Backup file creation failed"));
772 unlink (backup_filename);
774 g_free (backup_filename);
780 if (!copy_file_data (fd, bfd, NULL))
784 G_IO_ERROR_CANT_CREATE_BACKUP,
785 _("Backup file creation failed"));
786 unlink (backup_filename);
788 g_free (backup_filename);
794 g_free (backup_filename);
796 /* Seek back to the start of the file after the backup copy */
797 if (lseek (fd, 0, SEEK_SET) == -1)
799 g_set_error (error, G_IO_ERROR,
800 g_io_error_from_errno (errno),
801 _("Error seeking in file: %s"),
807 /* Truncate the file at the start */
808 if (ftruncate (fd, 0) == -1)
810 g_set_error (error, G_IO_ERROR,
811 g_io_error_from_errno (errno),
812 _("Error truncating file: %s"),
825 _g_local_file_output_stream_replace (const char *filename,
827 gboolean create_backup,
828 GFileCreateFlags flags,
829 GCancellable *cancellable,
832 GLocalFileOutputStream *stream;
837 if (g_cancellable_set_error_if_cancelled (cancellable, error))
842 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
847 /* If the file doesn't exist, create it */
848 fd = g_open (filename,
849 O_CREAT | O_EXCL | O_WRONLY,
852 if (fd == -1 && errno == EEXIST)
854 /* The file already exists */
855 fd = handle_overwrite_open (filename, etag, create_backup, &temp_file,
865 /* This must be an invalid filename, on e.g. FAT */
866 g_set_error (error, G_IO_ERROR,
867 G_IO_ERROR_INVALID_FILENAME,
868 _("Invalid filename"));
870 g_set_error (error, G_IO_ERROR,
871 g_io_error_from_errno (errsv),
872 _("Error opening file '%s': %s"),
873 filename, g_strerror (errsv));
878 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
879 stream->priv->fd = fd;
880 stream->priv->tmp_filename = temp_file;
882 stream->priv->backup_filename = create_backup_filename (filename);
883 stream->priv->original_filename = g_strdup (filename);
885 return G_FILE_OUTPUT_STREAM (stream);