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"
39 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
41 /* Some of the file replacement code was based on the code from gedit,
42 * relicenced to LGPL with permissions from the authors.
45 #define BACKUP_EXTENSION "~"
47 struct _GLocalFileOutputStreamPrivate {
49 char *original_filename;
50 char *backup_filename;
55 static gssize g_local_file_output_stream_write (GOutputStream *stream,
58 GCancellable *cancellable,
60 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
61 GCancellable *cancellable,
63 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
65 GCancellable *cancellable,
67 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
68 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
69 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
70 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
73 GCancellable *cancellable,
75 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
76 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
78 GCancellable *cancellable,
82 g_local_file_output_stream_finalize (GObject *object)
84 GLocalFileOutputStream *file;
86 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
88 g_free (file->priv->tmp_filename);
89 g_free (file->priv->original_filename);
90 g_free (file->priv->backup_filename);
91 g_free (file->priv->etag);
93 if (G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize)
94 (*G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize) (object);
98 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
100 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
102 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
104 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
106 gobject_class->finalize = g_local_file_output_stream_finalize;
108 stream_class->write = g_local_file_output_stream_write;
109 stream_class->close = g_local_file_output_stream_close;
110 file_stream_class->query_info = g_local_file_output_stream_query_info;
111 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
112 file_stream_class->tell = g_local_file_output_stream_tell;
113 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
114 file_stream_class->seek = g_local_file_output_stream_seek;
115 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
116 file_stream_class->truncate = g_local_file_output_stream_truncate;
120 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
122 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
123 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
124 GLocalFileOutputStreamPrivate);
128 g_local_file_output_stream_write (GOutputStream *stream,
131 GCancellable *cancellable,
134 GLocalFileOutputStream *file;
137 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
141 if (g_cancellable_set_error_if_cancelled (cancellable, error))
143 res = write (file->priv->fd, buffer, count);
149 g_set_error (error, G_IO_ERROR,
150 g_io_error_from_errno (errno),
151 _("Error writing to file: %s"),
162 g_local_file_output_stream_close (GOutputStream *stream,
163 GCancellable *cancellable,
166 GLocalFileOutputStream *file;
167 struct stat final_stat;
170 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
172 if (file->priv->tmp_filename)
174 /* We need to move the temp file to its final place,
175 * and possibly create the backup file
178 if (file->priv->backup_filename)
180 if (g_cancellable_set_error_if_cancelled (cancellable, error))
184 /* create original -> backup link, the original is then renamed over */
185 if (unlink (file->priv->backup_filename) != 0 &&
188 g_set_error (error, G_IO_ERROR,
189 G_IO_ERROR_CANT_CREATE_BACKUP,
190 _("Error removing old backup link: %s"),
195 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
197 g_set_error (error, G_IO_ERROR,
198 G_IO_ERROR_CANT_CREATE_BACKUP,
199 _("Error creating backup link: %s"),
204 /* If link not supported, just rename... */
205 if (!rename (file->priv->original_filename, file->priv->backup_filename) != 0)
207 g_set_error (error, G_IO_ERROR,
208 G_IO_ERROR_CANT_CREATE_BACKUP,
209 _("Error creating backup copy: %s"),
217 if (g_cancellable_set_error_if_cancelled (cancellable, error))
220 /* tmp -> original */
221 if (rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
223 g_set_error (error, G_IO_ERROR,
224 g_io_error_from_errno (errno),
225 _("Error renamining temporary file: %s"),
231 if (g_cancellable_set_error_if_cancelled (cancellable, error))
234 if (fstat (file->priv->fd, &final_stat) == 0)
235 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
239 res = close (file->priv->fd);
242 g_set_error (error, G_IO_ERROR,
243 g_io_error_from_errno (errno),
244 _("Error closing file: %s"),
253 /* A simple try to close the fd in case we fail before the actual close */
254 close (file->priv->fd);
259 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
261 GLocalFileOutputStream *file;
263 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
265 return g_strdup (file->priv->etag);
269 g_local_file_output_stream_tell (GFileOutputStream *stream)
271 GLocalFileOutputStream *file;
274 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
276 pos = lseek (file->priv->fd, 0, SEEK_CUR);
278 if (pos == (off_t)-1)
285 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
287 GLocalFileOutputStream *file;
290 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
292 pos = lseek (file->priv->fd, 0, SEEK_CUR);
294 if (pos == (off_t)-1 &&
302 seek_type_to_lseek (GSeekType type)
319 g_local_file_output_stream_seek (GFileOutputStream *stream,
322 GCancellable *cancellable,
325 GLocalFileOutputStream *file;
328 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
330 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
332 if (pos == (off_t)-1)
334 g_set_error (error, G_IO_ERROR,
335 g_io_error_from_errno (errno),
336 _("Error seeking in file: %s"),
345 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
347 /* We can't truncate pipes and stuff where we can't seek */
348 return g_local_file_output_stream_can_seek (stream);
352 g_local_file_output_stream_truncate (GFileOutputStream *stream,
354 GCancellable *cancellable,
357 GLocalFileOutputStream *file;
360 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
363 res = ftruncate(file->priv->fd, size);
369 if (g_cancellable_set_error_if_cancelled (cancellable, error))
374 g_set_error (error, G_IO_ERROR,
375 g_io_error_from_errno (errno),
376 _("Error truncating file: %s"),
386 g_local_file_output_stream_query_info (GFileOutputStream *stream,
388 GCancellable *cancellable,
391 GLocalFileOutputStream *file;
393 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
395 if (g_cancellable_set_error_if_cancelled (cancellable, error))
398 return _g_local_file_info_get_from_fd (file->priv->fd,
404 * g_local_file_output_stream_create:
407 * @cancellable: optional #GCancellable object, %NULL to ignore.
408 * @error: a #GError location to store the error occuring, or %NULL to
410 * Returns: #GFileOutputStream.
413 g_local_file_output_stream_create (const char *filename,
414 GFileCreateFlags flags,
415 GCancellable *cancellable,
418 GLocalFileOutputStream *stream;
422 if (g_cancellable_set_error_if_cancelled (cancellable, error))
425 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
430 fd = g_open (filename,
431 O_CREAT | O_EXCL | O_WRONLY,
438 /* This must be an invalid filename, on e.g. FAT */
439 g_set_error (error, G_IO_ERROR,
440 G_IO_ERROR_INVALID_FILENAME,
441 _("Invalid filename"));
443 g_set_error (error, G_IO_ERROR,
444 g_io_error_from_errno (errsv),
445 _("Error opening file '%s': %s"),
446 filename, g_strerror (errsv));
450 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
451 stream->priv->fd = fd;
452 return G_FILE_OUTPUT_STREAM (stream);
456 * g_local_file_output_stream_append:
459 * @cancellable: optional #GCancellable object, %NULL to ignore.
460 * @error: a #GError location to store the error occuring, or %NULL to
465 g_local_file_output_stream_append (const char *filename,
466 GFileCreateFlags flags,
467 GCancellable *cancellable,
470 GLocalFileOutputStream *stream;
474 if (g_cancellable_set_error_if_cancelled (cancellable, error))
477 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
482 fd = g_open (filename,
483 O_CREAT | O_APPEND | O_WRONLY,
490 /* This must be an invalid filename, on e.g. FAT */
491 g_set_error (error, G_IO_ERROR,
492 G_IO_ERROR_INVALID_FILENAME,
493 _("Invalid filename"));
495 g_set_error (error, G_IO_ERROR,
496 g_io_error_from_errno (errsv),
497 _("Error opening file '%s': %s"),
498 filename, g_strerror (errsv));
502 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
503 stream->priv->fd = fd;
505 return G_FILE_OUTPUT_STREAM (stream);
509 create_backup_filename (const char *filename)
511 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
514 #define BUFSIZE 8192 /* size of normal write buffer */
517 copy_file_data (gint sfd,
523 const gchar *write_buffer;
525 ssize_t bytes_to_write;
526 ssize_t bytes_written;
528 buffer = g_malloc (BUFSIZE);
532 bytes_read = read (sfd, buffer, BUFSIZE);
533 if (bytes_read == -1)
538 g_set_error (error, G_IO_ERROR,
539 g_io_error_from_errno (errno),
540 _("Error reading from file: %s"),
546 bytes_to_write = bytes_read;
547 write_buffer = buffer;
551 bytes_written = write (dfd, write_buffer, bytes_to_write);
552 if (bytes_written == -1)
557 g_set_error (error, G_IO_ERROR,
558 g_io_error_from_errno (errno),
559 _("Error writing to file: %s"),
565 bytes_to_write -= bytes_written;
566 write_buffer += bytes_written;
568 while (bytes_to_write > 0);
570 } while ((bytes_read != 0) && (ret == TRUE));
578 handle_overwrite_open (const char *filename,
580 gboolean create_backup,
581 char **temp_filename,
582 GCancellable *cancellable,
586 struct stat original_stat;
591 /* We only need read access to the original file if we are creating a backup.
592 * We also add O_CREATE to avoid a race if the file was just removed */
594 open_flags = O_RDWR | O_CREAT;
596 open_flags = O_WRONLY | O_CREAT;
598 /* Some systems have O_NOFOLLOW, which lets us avoid some races
599 * when finding out if the file we opened was a symlink */
602 fd = g_open (filename, open_flags | O_NOFOLLOW, 0666);
603 if (fd == -1 && errno == ELOOP)
605 /* Could be a symlink, or it could be a regular ELOOP error,
606 * but then the next open will fail too. */
608 fd = g_open (filename, open_flags, 0666);
611 fd = g_open (filename, open_flags, 0666);
612 /* This is racy, but we do it as soon as possible to minimize the race */
613 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
618 g_set_error (error, G_IO_ERROR,
619 g_io_error_from_errno (errno),
620 _("Error opening file '%s': %s"),
621 filename, g_strerror (errno));
625 if (fstat (fd, &original_stat) != 0)
627 g_set_error (error, G_IO_ERROR,
628 g_io_error_from_errno (errno),
629 _("Error stating file '%s': %s"),
630 filename, g_strerror (errno));
634 /* not a regular file */
635 if (!S_ISREG (original_stat.st_mode))
637 if (S_ISDIR (original_stat.st_mode))
640 G_IO_ERROR_IS_DIRECTORY,
641 _("Target file is a directory"));
645 G_IO_ERROR_NOT_REGULAR_FILE,
646 _("Target file is not a regular file"));
652 current_etag = _g_local_file_info_create_etag (&original_stat);
653 if (strcmp (etag, current_etag) != 0)
657 G_IO_ERROR_WRONG_ETAG,
658 _("The file was externally modified"));
659 g_free (current_etag);
662 g_free (current_etag);
665 /* We use two backup strategies.
666 * The first one (which is faster) consist in saving to a
667 * tmp file then rename the original file to the backup and the
668 * tmp file to the original name. This is fast but doesn't work
669 * when the file is a link (hard or symbolic) or when we can't
670 * write to the current dir or can't set the permissions on the
672 * The second strategy consist simply in copying the old file
673 * to a backup file and rewrite the contents of the file.
676 if (!(original_stat.st_nlink > 1) && !is_symlink)
678 char *dirname, *tmp_filename;
681 dirname = g_path_get_dirname (filename);
682 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
685 tmpfd = g_mkstemp (tmp_filename);
688 g_free (tmp_filename);
689 goto fallback_strategy;
692 /* try to keep permissions */
696 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
699 fchmod (tmpfd, original_stat.st_mode) == -1 ||
704 struct stat tmp_statbuf;
706 /* Check that we really needed to change something */
707 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
708 original_stat.st_uid != tmp_statbuf.st_uid ||
709 original_stat.st_gid != tmp_statbuf.st_gid ||
710 original_stat.st_mode != tmp_statbuf.st_mode)
713 unlink (tmp_filename);
714 g_free (tmp_filename);
715 goto fallback_strategy;
720 *temp_filename = tmp_filename;
728 struct stat tmp_statbuf;
729 char *backup_filename;
732 backup_filename = create_backup_filename (filename);
734 if (unlink (backup_filename) == -1 && errno != ENOENT)
738 G_IO_ERROR_CANT_CREATE_BACKUP,
739 _("Backup file creation failed"));
740 g_free (backup_filename);
744 bfd = g_open (backup_filename,
745 O_WRONLY | O_CREAT | O_EXCL,
746 original_stat.st_mode & 0777);
752 G_IO_ERROR_CANT_CREATE_BACKUP,
753 _("Backup file creation failed"));
754 g_free (backup_filename);
758 /* If needed, Try to set the group of the backup same as the
759 * original file. If this fails, set the protection
760 * bits for the group same as the protection bits for
763 if (fstat (bfd, &tmp_statbuf) != 0)
767 G_IO_ERROR_CANT_CREATE_BACKUP,
768 _("Backup file creation failed"));
769 unlink (backup_filename);
770 g_free (backup_filename);
774 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
775 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
778 (original_stat.st_mode & 0707) |
779 ((original_stat.st_mode & 07) << 3)) != 0)
783 G_IO_ERROR_CANT_CREATE_BACKUP,
784 _("Backup file creation failed"));
785 unlink (backup_filename);
787 g_free (backup_filename);
793 if (!copy_file_data (fd, bfd, NULL))
797 G_IO_ERROR_CANT_CREATE_BACKUP,
798 _("Backup file creation failed"));
799 unlink (backup_filename);
801 g_free (backup_filename);
807 g_free (backup_filename);
809 /* Seek back to the start of the file after the backup copy */
810 if (lseek (fd, 0, SEEK_SET) == -1)
812 g_set_error (error, G_IO_ERROR,
813 g_io_error_from_errno (errno),
814 _("Error seeking in file: %s"),
820 /* Truncate the file at the start */
821 if (ftruncate (fd, 0) == -1)
823 g_set_error (error, G_IO_ERROR,
824 g_io_error_from_errno (errno),
825 _("Error truncating file: %s"),
838 * g_local_file_output_stream_replace:
839 * @filename: the file name.
841 * @create_backup: if set, create a backup of the file.
843 * @cancellable: optional #GCancellable object, %NULL to ignore.
844 * @error: a #GError location to store the error occuring, or %NULL to
846 * Returns: #GFileOutputStream
849 g_local_file_output_stream_replace (const char *filename,
851 gboolean create_backup,
852 GFileCreateFlags flags,
853 GCancellable *cancellable,
856 GLocalFileOutputStream *stream;
861 if (g_cancellable_set_error_if_cancelled (cancellable, error))
866 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
871 /* If the file doesn't exist, create it */
872 fd = g_open (filename,
873 O_CREAT | O_EXCL | O_WRONLY,
876 if (fd == -1 && errno == EEXIST)
878 /* The file already exists */
879 fd = handle_overwrite_open (filename, etag, create_backup, &temp_file,
889 /* This must be an invalid filename, on e.g. FAT */
890 g_set_error (error, G_IO_ERROR,
891 G_IO_ERROR_INVALID_FILENAME,
892 _("Invalid filename"));
894 g_set_error (error, G_IO_ERROR,
895 g_io_error_from_errno (errsv),
896 _("Error opening file '%s': %s"),
897 filename, g_strerror (errsv));
902 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
903 stream->priv->fd = fd;
904 stream->priv->tmp_filename = temp_file;
906 stream->priv->backup_filename = create_backup_filename (filename);
907 stream->priv->original_filename = g_strdup (filename);
909 return G_FILE_OUTPUT_STREAM (stream);