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 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
43 /* Some of the file replacement code was based on the code from gedit,
44 * relicenced to LGPL with permissions from the authors.
47 #define BACKUP_EXTENSION "~"
49 struct _GLocalFileOutputStreamPrivate {
51 char *original_filename;
52 char *backup_filename;
57 static gssize g_local_file_output_stream_write (GOutputStream *stream,
60 GCancellable *cancellable,
62 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
63 GCancellable *cancellable,
65 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
67 GCancellable *cancellable,
69 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
70 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
71 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
72 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
75 GCancellable *cancellable,
77 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
78 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
80 GCancellable *cancellable,
84 g_local_file_output_stream_finalize (GObject *object)
86 GLocalFileOutputStream *file;
88 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
90 g_free (file->priv->tmp_filename);
91 g_free (file->priv->original_filename);
92 g_free (file->priv->backup_filename);
93 g_free (file->priv->etag);
95 if (G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize)
96 (*G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize) (object);
100 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
102 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
103 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
104 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
106 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
108 gobject_class->finalize = g_local_file_output_stream_finalize;
110 stream_class->write = g_local_file_output_stream_write;
111 stream_class->close = g_local_file_output_stream_close;
112 file_stream_class->query_info = g_local_file_output_stream_query_info;
113 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
114 file_stream_class->tell = g_local_file_output_stream_tell;
115 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
116 file_stream_class->seek = g_local_file_output_stream_seek;
117 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
118 file_stream_class->truncate = g_local_file_output_stream_truncate;
122 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
124 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
125 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
126 GLocalFileOutputStreamPrivate);
130 g_local_file_output_stream_write (GOutputStream *stream,
133 GCancellable *cancellable,
136 GLocalFileOutputStream *file;
139 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
143 if (g_cancellable_set_error_if_cancelled (cancellable, error))
145 res = write (file->priv->fd, buffer, count);
151 g_set_error (error, G_IO_ERROR,
152 g_io_error_from_errno (errno),
153 _("Error writing to file: %s"),
164 g_local_file_output_stream_close (GOutputStream *stream,
165 GCancellable *cancellable,
168 GLocalFileOutputStream *file;
169 struct stat final_stat;
172 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
174 if (file->priv->tmp_filename)
176 /* We need to move the temp file to its final place,
177 * and possibly create the backup file
180 if (file->priv->backup_filename)
182 if (g_cancellable_set_error_if_cancelled (cancellable, error))
186 /* create original -> backup link, the original is then renamed over */
187 if (unlink (file->priv->backup_filename) != 0 &&
190 g_set_error (error, G_IO_ERROR,
191 G_IO_ERROR_CANT_CREATE_BACKUP,
192 _("Error removing old backup link: %s"),
197 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
199 g_set_error (error, G_IO_ERROR,
200 G_IO_ERROR_CANT_CREATE_BACKUP,
201 _("Error creating backup link: %s"),
206 /* If link not supported, just rename... */
207 if (!rename (file->priv->original_filename, file->priv->backup_filename) != 0)
209 g_set_error (error, G_IO_ERROR,
210 G_IO_ERROR_CANT_CREATE_BACKUP,
211 _("Error creating backup copy: %s"),
219 if (g_cancellable_set_error_if_cancelled (cancellable, error))
222 /* tmp -> original */
223 if (rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
225 g_set_error (error, G_IO_ERROR,
226 g_io_error_from_errno (errno),
227 _("Error renaming temporary file: %s"),
233 if (g_cancellable_set_error_if_cancelled (cancellable, error))
236 if (fstat (file->priv->fd, &final_stat) == 0)
237 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
241 res = close (file->priv->fd);
244 g_set_error (error, G_IO_ERROR,
245 g_io_error_from_errno (errno),
246 _("Error closing file: %s"),
255 /* A simple try to close the fd in case we fail before the actual close */
256 close (file->priv->fd);
261 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
263 GLocalFileOutputStream *file;
265 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
267 return g_strdup (file->priv->etag);
271 g_local_file_output_stream_tell (GFileOutputStream *stream)
273 GLocalFileOutputStream *file;
276 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
278 pos = lseek (file->priv->fd, 0, SEEK_CUR);
280 if (pos == (off_t)-1)
287 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
289 GLocalFileOutputStream *file;
292 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
294 pos = lseek (file->priv->fd, 0, SEEK_CUR);
296 if (pos == (off_t)-1 &&
304 seek_type_to_lseek (GSeekType type)
321 g_local_file_output_stream_seek (GFileOutputStream *stream,
324 GCancellable *cancellable,
327 GLocalFileOutputStream *file;
330 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
332 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
334 if (pos == (off_t)-1)
336 g_set_error (error, G_IO_ERROR,
337 g_io_error_from_errno (errno),
338 _("Error seeking in file: %s"),
347 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
349 /* We can't truncate pipes and stuff where we can't seek */
350 return g_local_file_output_stream_can_seek (stream);
354 g_local_file_output_stream_truncate (GFileOutputStream *stream,
356 GCancellable *cancellable,
359 GLocalFileOutputStream *file;
362 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
365 res = ftruncate(file->priv->fd, size);
371 if (g_cancellable_set_error_if_cancelled (cancellable, error))
376 g_set_error (error, G_IO_ERROR,
377 g_io_error_from_errno (errno),
378 _("Error truncating file: %s"),
388 g_local_file_output_stream_query_info (GFileOutputStream *stream,
390 GCancellable *cancellable,
393 GLocalFileOutputStream *file;
395 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
397 if (g_cancellable_set_error_if_cancelled (cancellable, error))
400 return _g_local_file_info_get_from_fd (file->priv->fd,
406 g_local_file_output_stream_create (const char *filename,
407 GFileCreateFlags flags,
408 GCancellable *cancellable,
411 GLocalFileOutputStream *stream;
415 if (g_cancellable_set_error_if_cancelled (cancellable, error))
418 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
423 fd = g_open (filename,
424 O_CREAT | O_EXCL | O_WRONLY,
431 /* This must be an invalid filename, on e.g. FAT */
432 g_set_error (error, G_IO_ERROR,
433 G_IO_ERROR_INVALID_FILENAME,
434 _("Invalid filename"));
436 g_set_error (error, G_IO_ERROR,
437 g_io_error_from_errno (errsv),
438 _("Error opening file '%s': %s"),
439 filename, g_strerror (errsv));
443 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
444 stream->priv->fd = fd;
445 return G_FILE_OUTPUT_STREAM (stream);
449 g_local_file_output_stream_append (const char *filename,
450 GFileCreateFlags flags,
451 GCancellable *cancellable,
454 GLocalFileOutputStream *stream;
458 if (g_cancellable_set_error_if_cancelled (cancellable, error))
461 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
466 fd = g_open (filename,
467 O_CREAT | O_APPEND | O_WRONLY,
474 /* This must be an invalid filename, on e.g. FAT */
475 g_set_error (error, G_IO_ERROR,
476 G_IO_ERROR_INVALID_FILENAME,
477 _("Invalid filename"));
479 g_set_error (error, G_IO_ERROR,
480 g_io_error_from_errno (errsv),
481 _("Error opening file '%s': %s"),
482 filename, g_strerror (errsv));
486 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
487 stream->priv->fd = fd;
489 return G_FILE_OUTPUT_STREAM (stream);
493 create_backup_filename (const char *filename)
495 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
498 #define BUFSIZE 8192 /* size of normal write buffer */
501 copy_file_data (gint sfd,
507 const gchar *write_buffer;
509 ssize_t bytes_to_write;
510 ssize_t bytes_written;
512 buffer = g_malloc (BUFSIZE);
516 bytes_read = read (sfd, buffer, BUFSIZE);
517 if (bytes_read == -1)
522 g_set_error (error, G_IO_ERROR,
523 g_io_error_from_errno (errno),
524 _("Error reading from file: %s"),
530 bytes_to_write = bytes_read;
531 write_buffer = buffer;
535 bytes_written = write (dfd, write_buffer, bytes_to_write);
536 if (bytes_written == -1)
541 g_set_error (error, G_IO_ERROR,
542 g_io_error_from_errno (errno),
543 _("Error writing to file: %s"),
549 bytes_to_write -= bytes_written;
550 write_buffer += bytes_written;
552 while (bytes_to_write > 0);
554 } while ((bytes_read != 0) && (ret == TRUE));
562 handle_overwrite_open (const char *filename,
564 gboolean create_backup,
565 char **temp_filename,
566 GCancellable *cancellable,
570 struct stat original_stat;
575 /* We only need read access to the original file if we are creating a backup.
576 * We also add O_CREATE to avoid a race if the file was just removed */
578 open_flags = O_RDWR | O_CREAT;
580 open_flags = O_WRONLY | O_CREAT;
582 /* Some systems have O_NOFOLLOW, which lets us avoid some races
583 * when finding out if the file we opened was a symlink */
586 fd = g_open (filename, open_flags | O_NOFOLLOW, 0666);
587 if (fd == -1 && errno == ELOOP)
589 /* Could be a symlink, or it could be a regular ELOOP error,
590 * but then the next open will fail too. */
592 fd = g_open (filename, open_flags, 0666);
595 fd = g_open (filename, open_flags, 0666);
596 /* This is racy, but we do it as soon as possible to minimize the race */
597 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
602 g_set_error (error, G_IO_ERROR,
603 g_io_error_from_errno (errno),
604 _("Error opening file '%s': %s"),
605 filename, g_strerror (errno));
609 if (fstat (fd, &original_stat) != 0)
611 g_set_error (error, G_IO_ERROR,
612 g_io_error_from_errno (errno),
613 _("Error stating file '%s': %s"),
614 filename, g_strerror (errno));
618 /* not a regular file */
619 if (!S_ISREG (original_stat.st_mode))
621 if (S_ISDIR (original_stat.st_mode))
624 G_IO_ERROR_IS_DIRECTORY,
625 _("Target file is a directory"));
629 G_IO_ERROR_NOT_REGULAR_FILE,
630 _("Target file is not a regular file"));
636 current_etag = _g_local_file_info_create_etag (&original_stat);
637 if (strcmp (etag, current_etag) != 0)
641 G_IO_ERROR_WRONG_ETAG,
642 _("The file was externally modified"));
643 g_free (current_etag);
646 g_free (current_etag);
649 /* We use two backup strategies.
650 * The first one (which is faster) consist in saving to a
651 * tmp file then rename the original file to the backup and the
652 * tmp file to the original name. This is fast but doesn't work
653 * when the file is a link (hard or symbolic) or when we can't
654 * write to the current dir or can't set the permissions on the
656 * The second strategy consist simply in copying the old file
657 * to a backup file and rewrite the contents of the file.
660 if (!(original_stat.st_nlink > 1) && !is_symlink)
662 char *dirname, *tmp_filename;
665 dirname = g_path_get_dirname (filename);
666 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
669 tmpfd = g_mkstemp (tmp_filename);
672 g_free (tmp_filename);
673 goto fallback_strategy;
676 /* try to keep permissions */
680 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
683 fchmod (tmpfd, original_stat.st_mode) == -1 ||
688 struct stat tmp_statbuf;
690 /* Check that we really needed to change something */
691 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
692 original_stat.st_uid != tmp_statbuf.st_uid ||
693 original_stat.st_gid != tmp_statbuf.st_gid ||
694 original_stat.st_mode != tmp_statbuf.st_mode)
697 unlink (tmp_filename);
698 g_free (tmp_filename);
699 goto fallback_strategy;
704 *temp_filename = tmp_filename;
712 struct stat tmp_statbuf;
713 char *backup_filename;
716 backup_filename = create_backup_filename (filename);
718 if (unlink (backup_filename) == -1 && errno != ENOENT)
722 G_IO_ERROR_CANT_CREATE_BACKUP,
723 _("Backup file creation failed"));
724 g_free (backup_filename);
728 bfd = g_open (backup_filename,
729 O_WRONLY | O_CREAT | O_EXCL,
730 original_stat.st_mode & 0777);
736 G_IO_ERROR_CANT_CREATE_BACKUP,
737 _("Backup file creation failed"));
738 g_free (backup_filename);
742 /* If needed, Try to set the group of the backup same as the
743 * original file. If this fails, set the protection
744 * bits for the group same as the protection bits for
747 if (fstat (bfd, &tmp_statbuf) != 0)
751 G_IO_ERROR_CANT_CREATE_BACKUP,
752 _("Backup file creation failed"));
753 unlink (backup_filename);
754 g_free (backup_filename);
758 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
759 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
762 (original_stat.st_mode & 0707) |
763 ((original_stat.st_mode & 07) << 3)) != 0)
767 G_IO_ERROR_CANT_CREATE_BACKUP,
768 _("Backup file creation failed"));
769 unlink (backup_filename);
771 g_free (backup_filename);
777 if (!copy_file_data (fd, bfd, NULL))
781 G_IO_ERROR_CANT_CREATE_BACKUP,
782 _("Backup file creation failed"));
783 unlink (backup_filename);
785 g_free (backup_filename);
791 g_free (backup_filename);
793 /* Seek back to the start of the file after the backup copy */
794 if (lseek (fd, 0, SEEK_SET) == -1)
796 g_set_error (error, G_IO_ERROR,
797 g_io_error_from_errno (errno),
798 _("Error seeking in file: %s"),
804 /* Truncate the file at the start */
805 if (ftruncate (fd, 0) == -1)
807 g_set_error (error, G_IO_ERROR,
808 g_io_error_from_errno (errno),
809 _("Error truncating file: %s"),
822 g_local_file_output_stream_replace (const char *filename,
824 gboolean create_backup,
825 GFileCreateFlags flags,
826 GCancellable *cancellable,
829 GLocalFileOutputStream *stream;
834 if (g_cancellable_set_error_if_cancelled (cancellable, error))
839 if (flags & G_FILE_CREATE_FLAGS_PRIVATE)
844 /* If the file doesn't exist, create it */
845 fd = g_open (filename,
846 O_CREAT | O_EXCL | O_WRONLY,
849 if (fd == -1 && errno == EEXIST)
851 /* The file already exists */
852 fd = handle_overwrite_open (filename, etag, create_backup, &temp_file,
862 /* This must be an invalid filename, on e.g. FAT */
863 g_set_error (error, G_IO_ERROR,
864 G_IO_ERROR_INVALID_FILENAME,
865 _("Invalid filename"));
867 g_set_error (error, G_IO_ERROR,
868 g_io_error_from_errno (errsv),
869 _("Error opening file '%s': %s"),
870 filename, g_strerror (errsv));
875 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
876 stream->priv->fd = fd;
877 stream->priv->tmp_filename = temp_file;
879 stream->priv->backup_filename = create_backup_filename (filename);
880 stream->priv->original_filename = g_strdup (filename);
882 return G_FILE_OUTPUT_STREAM (stream);