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)
53 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
54 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
56 /* Some of the file replacement code was based on the code from gedit,
57 * relicenced to LGPL with permissions from the authors.
60 #define BACKUP_EXTENSION "~"
62 struct _GLocalFileOutputStreamPrivate {
64 char *original_filename;
65 char *backup_filename;
70 static gssize g_local_file_output_stream_write (GOutputStream *stream,
73 GCancellable *cancellable,
75 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
76 GCancellable *cancellable,
78 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
80 GCancellable *cancellable,
82 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
83 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
84 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
85 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
88 GCancellable *cancellable,
90 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
91 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
93 GCancellable *cancellable,
97 g_local_file_output_stream_finalize (GObject *object)
99 GLocalFileOutputStream *file;
101 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
103 g_free (file->priv->tmp_filename);
104 g_free (file->priv->original_filename);
105 g_free (file->priv->backup_filename);
106 g_free (file->priv->etag);
108 if (G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize)
109 (*G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize) (object);
113 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
117 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
119 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
121 gobject_class->finalize = g_local_file_output_stream_finalize;
123 stream_class->write_fn = g_local_file_output_stream_write;
124 stream_class->close_fn = g_local_file_output_stream_close;
125 file_stream_class->query_info = g_local_file_output_stream_query_info;
126 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
127 file_stream_class->tell = g_local_file_output_stream_tell;
128 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
129 file_stream_class->seek = g_local_file_output_stream_seek;
130 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
131 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
135 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
137 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
138 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
139 GLocalFileOutputStreamPrivate);
143 g_local_file_output_stream_write (GOutputStream *stream,
146 GCancellable *cancellable,
149 GLocalFileOutputStream *file;
152 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
156 if (g_cancellable_set_error_if_cancelled (cancellable, error))
158 res = write (file->priv->fd, buffer, count);
164 g_set_error (error, G_IO_ERROR,
165 g_io_error_from_errno (errno),
166 _("Error writing to file: %s"),
177 g_local_file_output_stream_close (GOutputStream *stream,
178 GCancellable *cancellable,
181 GLocalFileOutputStream *file;
182 struct stat final_stat;
185 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
187 if (file->priv->tmp_filename)
189 /* We need to move the temp file to its final place,
190 * and possibly create the backup file
193 if (file->priv->backup_filename)
195 if (g_cancellable_set_error_if_cancelled (cancellable, error))
199 /* create original -> backup link, the original is then renamed over */
200 if (unlink (file->priv->backup_filename) != 0 &&
203 g_set_error (error, G_IO_ERROR,
204 G_IO_ERROR_CANT_CREATE_BACKUP,
205 _("Error removing old backup link: %s"),
210 if (link (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 link: %s"),
219 /* If link not supported, just rename... */
220 if (!rename (file->priv->original_filename, file->priv->backup_filename) != 0)
222 g_set_error (error, G_IO_ERROR,
223 G_IO_ERROR_CANT_CREATE_BACKUP,
224 _("Error creating backup copy: %s"),
232 if (g_cancellable_set_error_if_cancelled (cancellable, error))
235 /* tmp -> original */
236 if (rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
238 g_set_error (error, G_IO_ERROR,
239 g_io_error_from_errno (errno),
240 _("Error renaming temporary file: %s"),
246 if (g_cancellable_set_error_if_cancelled (cancellable, error))
249 if (fstat (file->priv->fd, &final_stat) == 0)
250 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
254 res = close (file->priv->fd);
257 g_set_error (error, G_IO_ERROR,
258 g_io_error_from_errno (errno),
259 _("Error closing file: %s"),
268 /* A simple try to close the fd in case we fail before the actual close */
269 close (file->priv->fd);
274 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
276 GLocalFileOutputStream *file;
278 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
280 return g_strdup (file->priv->etag);
284 g_local_file_output_stream_tell (GFileOutputStream *stream)
286 GLocalFileOutputStream *file;
289 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
291 pos = lseek (file->priv->fd, 0, SEEK_CUR);
293 if (pos == (off_t)-1)
300 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
302 GLocalFileOutputStream *file;
305 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
307 pos = lseek (file->priv->fd, 0, SEEK_CUR);
309 if (pos == (off_t)-1 && errno == ESPIPE)
316 seek_type_to_lseek (GSeekType type)
333 g_local_file_output_stream_seek (GFileOutputStream *stream,
336 GCancellable *cancellable,
339 GLocalFileOutputStream *file;
342 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
344 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
346 if (pos == (off_t)-1)
348 g_set_error (error, G_IO_ERROR,
349 g_io_error_from_errno (errno),
350 _("Error seeking in file: %s"),
359 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
361 /* We can't truncate pipes and stuff where we can't seek */
362 return g_local_file_output_stream_can_seek (stream);
366 g_local_file_output_stream_truncate (GFileOutputStream *stream,
368 GCancellable *cancellable,
371 GLocalFileOutputStream *file;
374 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
378 res = g_win32_ftruncate (file->priv->fd, size);
380 res = ftruncate (file->priv->fd, size);
387 if (g_cancellable_set_error_if_cancelled (cancellable, error))
392 g_set_error (error, G_IO_ERROR,
393 g_io_error_from_errno (errno),
394 _("Error truncating file: %s"),
404 g_local_file_output_stream_query_info (GFileOutputStream *stream,
406 GCancellable *cancellable,
409 GLocalFileOutputStream *file;
411 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
413 if (g_cancellable_set_error_if_cancelled (cancellable, error))
416 return _g_local_file_info_get_from_fd (file->priv->fd,
422 _g_local_file_output_stream_create (const char *filename,
423 GFileCreateFlags flags,
424 GCancellable *cancellable,
427 GLocalFileOutputStream *stream;
431 if (g_cancellable_set_error_if_cancelled (cancellable, error))
434 if (flags & G_FILE_CREATE_PRIVATE)
439 fd = g_open (filename, O_CREAT | O_EXCL | O_WRONLY, mode);
445 /* This must be an invalid filename, on e.g. FAT */
446 g_set_error (error, G_IO_ERROR,
447 G_IO_ERROR_INVALID_FILENAME,
448 _("Invalid filename"));
450 g_set_error (error, G_IO_ERROR,
451 g_io_error_from_errno (errsv),
452 _("Error opening file '%s': %s"),
453 filename, g_strerror (errsv));
457 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
458 stream->priv->fd = fd;
459 return G_FILE_OUTPUT_STREAM (stream);
463 _g_local_file_output_stream_append (const char *filename,
464 GFileCreateFlags flags,
465 GCancellable *cancellable,
468 GLocalFileOutputStream *stream;
472 if (g_cancellable_set_error_if_cancelled (cancellable, error))
475 if (flags & G_FILE_CREATE_PRIVATE)
480 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY, mode);
486 /* This must be an invalid filename, on e.g. FAT */
487 g_set_error (error, G_IO_ERROR,
488 G_IO_ERROR_INVALID_FILENAME,
489 _("Invalid filename"));
491 g_set_error (error, G_IO_ERROR,
492 g_io_error_from_errno (errsv),
493 _("Error opening file '%s': %s"),
494 filename, g_strerror (errsv));
498 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
499 stream->priv->fd = fd;
501 return G_FILE_OUTPUT_STREAM (stream);
505 create_backup_filename (const char *filename)
507 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
510 #define BUFSIZE 8192 /* size of normal write buffer */
513 copy_file_data (gint sfd,
519 const gchar *write_buffer;
521 gssize bytes_to_write;
522 gssize bytes_written;
524 buffer = g_malloc (BUFSIZE);
528 bytes_read = read (sfd, buffer, BUFSIZE);
529 if (bytes_read == -1)
534 g_set_error (error, G_IO_ERROR,
535 g_io_error_from_errno (errno),
536 _("Error reading from file: %s"),
542 bytes_to_write = bytes_read;
543 write_buffer = buffer;
547 bytes_written = write (dfd, write_buffer, bytes_to_write);
548 if (bytes_written == -1)
553 g_set_error (error, G_IO_ERROR,
554 g_io_error_from_errno (errno),
555 _("Error writing to file: %s"),
561 bytes_to_write -= bytes_written;
562 write_buffer += bytes_written;
564 while (bytes_to_write > 0);
566 } while ((bytes_read != 0) && (ret == TRUE));
574 handle_overwrite_open (const char *filename,
576 gboolean create_backup,
577 char **temp_filename,
578 GCancellable *cancellable,
582 struct stat original_stat;
587 /* We only need read access to the original file if we are creating a backup.
588 * We also add O_CREATE to avoid a race if the file was just removed */
590 open_flags = O_RDWR | O_CREAT;
592 open_flags = O_WRONLY | O_CREAT;
594 /* Some systems have O_NOFOLLOW, which lets us avoid some races
595 * when finding out if the file we opened was a symlink */
598 fd = g_open (filename, open_flags | O_NOFOLLOW, 0666);
599 if (fd == -1 && errno == ELOOP)
601 /* Could be a symlink, or it could be a regular ELOOP error,
602 * but then the next open will fail too. */
604 fd = g_open (filename, open_flags, 0666);
607 fd = g_open (filename, open_flags, 0666);
608 /* This is racy, but we do it as soon as possible to minimize the race */
609 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
614 g_set_error (error, G_IO_ERROR,
615 g_io_error_from_errno (errno),
616 _("Error opening file '%s': %s"),
617 filename, g_strerror (errno));
621 if (fstat (fd, &original_stat) != 0)
623 g_set_error (error, G_IO_ERROR,
624 g_io_error_from_errno (errno),
625 _("Error stating file '%s': %s"),
626 filename, g_strerror (errno));
630 /* not a regular file */
631 if (!S_ISREG (original_stat.st_mode))
633 if (S_ISDIR (original_stat.st_mode))
636 G_IO_ERROR_IS_DIRECTORY,
637 _("Target file is a directory"));
641 G_IO_ERROR_NOT_REGULAR_FILE,
642 _("Target file is not a regular file"));
648 current_etag = _g_local_file_info_create_etag (&original_stat);
649 if (strcmp (etag, current_etag) != 0)
653 G_IO_ERROR_WRONG_ETAG,
654 _("The file was externally modified"));
655 g_free (current_etag);
658 g_free (current_etag);
661 /* We use two backup strategies.
662 * The first one (which is faster) consist in saving to a
663 * tmp file then rename the original file to the backup and the
664 * tmp file to the original name. This is fast but doesn't work
665 * when the file is a link (hard or symbolic) or when we can't
666 * write to the current dir or can't set the permissions on the
668 * The second strategy consist simply in copying the old file
669 * to a backup file and rewrite the contents of the file.
672 if (!(original_stat.st_nlink > 1) && !is_symlink)
674 char *dirname, *tmp_filename;
677 dirname = g_path_get_dirname (filename);
678 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
681 tmpfd = g_mkstemp (tmp_filename);
684 g_free (tmp_filename);
685 goto fallback_strategy;
688 /* try to keep permissions */
692 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
695 fchmod (tmpfd, original_stat.st_mode) == -1 ||
700 struct stat tmp_statbuf;
702 /* Check that we really needed to change something */
703 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
704 original_stat.st_uid != tmp_statbuf.st_uid ||
705 original_stat.st_gid != tmp_statbuf.st_gid ||
706 original_stat.st_mode != tmp_statbuf.st_mode)
709 unlink (tmp_filename);
710 g_free (tmp_filename);
711 goto fallback_strategy;
716 *temp_filename = tmp_filename;
724 struct stat tmp_statbuf;
725 char *backup_filename;
728 backup_filename = create_backup_filename (filename);
730 if (unlink (backup_filename) == -1 && errno != ENOENT)
734 G_IO_ERROR_CANT_CREATE_BACKUP,
735 _("Backup file creation failed"));
736 g_free (backup_filename);
740 bfd = g_open (backup_filename,
741 O_WRONLY | O_CREAT | O_EXCL,
742 original_stat.st_mode & 0777);
748 G_IO_ERROR_CANT_CREATE_BACKUP,
749 _("Backup file creation failed"));
750 g_free (backup_filename);
754 /* If needed, Try to set the group of the backup same as the
755 * original file. If this fails, set the protection
756 * bits for the group same as the protection bits for
759 if (fstat (bfd, &tmp_statbuf) != 0)
763 G_IO_ERROR_CANT_CREATE_BACKUP,
764 _("Backup file creation failed"));
765 unlink (backup_filename);
766 g_free (backup_filename);
770 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
771 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
774 (original_stat.st_mode & 0707) |
775 ((original_stat.st_mode & 07) << 3)) != 0)
779 G_IO_ERROR_CANT_CREATE_BACKUP,
780 _("Backup file creation failed"));
781 unlink (backup_filename);
783 g_free (backup_filename);
789 if (!copy_file_data (fd, bfd, NULL))
793 G_IO_ERROR_CANT_CREATE_BACKUP,
794 _("Backup file creation failed"));
795 unlink (backup_filename);
797 g_free (backup_filename);
803 g_free (backup_filename);
805 /* Seek back to the start of the file after the backup copy */
806 if (lseek (fd, 0, SEEK_SET) == -1)
808 g_set_error (error, G_IO_ERROR,
809 g_io_error_from_errno (errno),
810 _("Error seeking in file: %s"),
816 /* Truncate the file at the start */
818 if (g_win32_ftruncate (fd, 0) == -1)
820 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 (const char *filename,
840 gboolean create_backup,
841 GFileCreateFlags flags,
842 GCancellable *cancellable,
845 GLocalFileOutputStream *stream;
850 if (g_cancellable_set_error_if_cancelled (cancellable, error))
855 if (flags & G_FILE_CREATE_PRIVATE)
860 /* If the file doesn't exist, create it */
861 fd = g_open (filename, O_CREAT | O_EXCL | O_WRONLY, mode);
863 if (fd == -1 && errno == EEXIST)
865 /* The file already exists */
866 fd = handle_overwrite_open (filename, etag, create_backup, &temp_file,
876 /* This must be an invalid filename, on e.g. FAT */
877 g_set_error (error, G_IO_ERROR,
878 G_IO_ERROR_INVALID_FILENAME,
879 _("Invalid filename"));
881 g_set_error (error, G_IO_ERROR,
882 g_io_error_from_errno (errsv),
883 _("Error opening file '%s': %s"),
884 filename, g_strerror (errsv));
889 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
890 stream->priv->fd = fd;
891 stream->priv->tmp_filename = temp_file;
893 stream->priv->backup_filename = create_backup_filename (filename);
894 stream->priv->original_filename = g_strdup (filename);
896 return G_FILE_OUTPUT_STREAM (stream);