GMenuModel exporter: remove workaround
[glib.git] / gio / glocalfileoutputstream.c
blobaff270c0cdd15ba30cd952a0393a956871e6a0b9
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include "config.h"
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31 #include <errno.h>
32 #include <string.h>
34 #include <glib.h>
35 #include <glib/gstdio.h>
36 #include "glibintl.h"
37 #include "gioerror.h"
38 #include "gcancellable.h"
39 #include "glocalfileoutputstream.h"
40 #include "glocalfileinfo.h"
42 #ifdef G_OS_UNIX
43 #include "gfiledescriptorbased.h"
44 #endif
46 #ifdef G_OS_WIN32
47 #include <io.h>
48 #ifndef S_ISDIR
49 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
50 #endif
51 #ifndef S_ISREG
52 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
53 #endif
54 #endif
56 #ifndef O_BINARY
57 #define O_BINARY 0
58 #endif
61 #ifdef G_OS_UNIX
62 static void g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
63 #endif
65 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
66 #ifdef G_OS_UNIX
67 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,
68 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
69 g_file_descriptor_based_iface_init)
71 #else
72 G_DEFINE_TYPE_WITH_CODE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM,);
73 #endif
76 /* Some of the file replacement code was based on the code from gedit,
77 * relicenced to LGPL with permissions from the authors.
80 #define BACKUP_EXTENSION "~"
82 struct _GLocalFileOutputStreamPrivate {
83 char *tmp_filename;
84 char *original_filename;
85 char *backup_filename;
86 char *etag;
87 guint sync_on_close : 1;
88 guint do_close : 1;
89 int fd;
92 static gssize g_local_file_output_stream_write (GOutputStream *stream,
93 const void *buffer,
94 gsize count,
95 GCancellable *cancellable,
96 GError **error);
97 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
98 GCancellable *cancellable,
99 GError **error);
100 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
101 const char *attributes,
102 GCancellable *cancellable,
103 GError **error);
104 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
105 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
106 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
107 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
108 goffset offset,
109 GSeekType type,
110 GCancellable *cancellable,
111 GError **error);
112 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
113 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
114 goffset size,
115 GCancellable *cancellable,
116 GError **error);
117 #ifdef G_OS_UNIX
118 static int g_local_file_output_stream_get_fd (GFileDescriptorBased *stream);
119 #endif
121 static void
122 g_local_file_output_stream_finalize (GObject *object)
124 GLocalFileOutputStream *file;
126 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
128 g_free (file->priv->tmp_filename);
129 g_free (file->priv->original_filename);
130 g_free (file->priv->backup_filename);
131 g_free (file->priv->etag);
133 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
137 static void
138 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
140 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
141 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
142 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
144 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
146 gobject_class->finalize = g_local_file_output_stream_finalize;
148 stream_class->write_fn = g_local_file_output_stream_write;
149 stream_class->close_fn = g_local_file_output_stream_close;
150 file_stream_class->query_info = g_local_file_output_stream_query_info;
151 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
152 file_stream_class->tell = g_local_file_output_stream_tell;
153 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
154 file_stream_class->seek = g_local_file_output_stream_seek;
155 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
156 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
159 #ifdef G_OS_UNIX
160 static void
161 g_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
163 iface->get_fd = g_local_file_output_stream_get_fd;
165 #endif
167 static void
168 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
170 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
171 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
172 GLocalFileOutputStreamPrivate);
173 stream->priv->do_close = TRUE;
176 static gssize
177 g_local_file_output_stream_write (GOutputStream *stream,
178 const void *buffer,
179 gsize count,
180 GCancellable *cancellable,
181 GError **error)
183 GLocalFileOutputStream *file;
184 gssize res;
186 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
188 while (1)
190 if (g_cancellable_set_error_if_cancelled (cancellable, error))
191 return -1;
192 res = write (file->priv->fd, buffer, count);
193 if (res == -1)
195 int errsv = errno;
197 if (errsv == EINTR)
198 continue;
200 g_set_error (error, G_IO_ERROR,
201 g_io_error_from_errno (errsv),
202 _("Error writing to file: %s"),
203 g_strerror (errsv));
206 break;
209 return res;
212 void
213 _g_local_file_output_stream_set_do_close (GLocalFileOutputStream *out,
214 gboolean do_close)
216 out->priv->do_close = do_close;
219 gboolean
220 _g_local_file_output_stream_really_close (GLocalFileOutputStream *file,
221 GCancellable *cancellable,
222 GError **error)
224 GLocalFileStat final_stat;
225 int res;
227 #ifdef HAVE_FSYNC
228 if (file->priv->sync_on_close &&
229 fsync (file->priv->fd) != 0)
231 int errsv = errno;
233 g_set_error (error, G_IO_ERROR,
234 g_io_error_from_errno (errsv),
235 _("Error writing to file: %s"),
236 g_strerror (errsv));
237 goto err_out;
239 #endif
241 #ifdef G_OS_WIN32
243 /* Must close before renaming on Windows, so just do the close first
244 * in all cases for now.
246 if (_fstati64 (file->priv->fd, &final_stat) == 0)
247 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
249 res = close (file->priv->fd);
250 if (res == -1)
252 int errsv = errno;
254 g_set_error (error, G_IO_ERROR,
255 g_io_error_from_errno (errsv),
256 _("Error closing file: %s"),
257 g_strerror (errsv));
258 return FALSE;
261 #endif
263 if (file->priv->tmp_filename)
265 /* We need to move the temp file to its final place,
266 * and possibly create the backup file
269 if (file->priv->backup_filename)
271 if (g_cancellable_set_error_if_cancelled (cancellable, error))
272 goto err_out;
274 #ifdef HAVE_LINK
275 /* create original -> backup link, the original is then renamed over */
276 if (g_unlink (file->priv->backup_filename) != 0 &&
277 errno != ENOENT)
279 int errsv = errno;
281 g_set_error (error, G_IO_ERROR,
282 G_IO_ERROR_CANT_CREATE_BACKUP,
283 _("Error removing old backup link: %s"),
284 g_strerror (errsv));
285 goto err_out;
288 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
290 /* link failed or is not supported, try rename */
291 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
293 int errsv = errno;
295 g_set_error (error, G_IO_ERROR,
296 G_IO_ERROR_CANT_CREATE_BACKUP,
297 _("Error creating backup copy: %s"),
298 g_strerror (errsv));
299 goto err_out;
302 #else
303 /* If link not supported, just rename... */
304 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
306 int errsv = errno;
308 g_set_error (error, G_IO_ERROR,
309 G_IO_ERROR_CANT_CREATE_BACKUP,
310 _("Error creating backup copy: %s"),
311 g_strerror (errsv));
312 goto err_out;
314 #endif
318 if (g_cancellable_set_error_if_cancelled (cancellable, error))
319 goto err_out;
321 /* tmp -> original */
322 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
324 int errsv = errno;
326 g_set_error (error, G_IO_ERROR,
327 g_io_error_from_errno (errsv),
328 _("Error renaming temporary file: %s"),
329 g_strerror (errsv));
330 goto err_out;
334 if (g_cancellable_set_error_if_cancelled (cancellable, error))
335 goto err_out;
337 #ifndef G_OS_WIN32 /* Already did the fstat() and close() above on Win32 */
339 if (fstat (file->priv->fd, &final_stat) == 0)
340 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
342 while (1)
344 res = close (file->priv->fd);
345 if (res == -1)
347 int errsv = errno;
349 g_set_error (error, G_IO_ERROR,
350 g_io_error_from_errno (errsv),
351 _("Error closing file: %s"),
352 g_strerror (errsv));
354 break;
357 return res != -1;
359 #else
361 return TRUE;
363 #endif
365 err_out:
367 #ifndef G_OS_WIN32
368 /* A simple try to close the fd in case we fail before the actual close */
369 close (file->priv->fd);
370 #endif
371 return FALSE;
375 static gboolean
376 g_local_file_output_stream_close (GOutputStream *stream,
377 GCancellable *cancellable,
378 GError **error)
380 GLocalFileOutputStream *file;
382 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
384 if (file->priv->do_close)
385 return _g_local_file_output_stream_really_close (file,
386 cancellable,
387 error);
388 return TRUE;
391 static char *
392 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
394 GLocalFileOutputStream *file;
396 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
398 return g_strdup (file->priv->etag);
401 static goffset
402 g_local_file_output_stream_tell (GFileOutputStream *stream)
404 GLocalFileOutputStream *file;
405 off_t pos;
407 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
409 pos = lseek (file->priv->fd, 0, SEEK_CUR);
411 if (pos == (off_t)-1)
412 return 0;
414 return pos;
417 static gboolean
418 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
420 GLocalFileOutputStream *file;
421 off_t pos;
423 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
425 pos = lseek (file->priv->fd, 0, SEEK_CUR);
427 if (pos == (off_t)-1 && errno == ESPIPE)
428 return FALSE;
430 return TRUE;
433 static int
434 seek_type_to_lseek (GSeekType type)
436 switch (type)
438 default:
439 case G_SEEK_CUR:
440 return SEEK_CUR;
442 case G_SEEK_SET:
443 return SEEK_SET;
445 case G_SEEK_END:
446 return SEEK_END;
450 static gboolean
451 g_local_file_output_stream_seek (GFileOutputStream *stream,
452 goffset offset,
453 GSeekType type,
454 GCancellable *cancellable,
455 GError **error)
457 GLocalFileOutputStream *file;
458 off_t pos;
460 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
462 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
464 if (pos == (off_t)-1)
466 int errsv = errno;
468 g_set_error (error, G_IO_ERROR,
469 g_io_error_from_errno (errsv),
470 _("Error seeking in file: %s"),
471 g_strerror (errsv));
472 return FALSE;
475 return TRUE;
478 static gboolean
479 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
481 /* We can't truncate pipes and stuff where we can't seek */
482 return g_local_file_output_stream_can_seek (stream);
485 static gboolean
486 g_local_file_output_stream_truncate (GFileOutputStream *stream,
487 goffset size,
488 GCancellable *cancellable,
489 GError **error)
491 GLocalFileOutputStream *file;
492 int res;
494 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
496 restart:
497 #ifdef G_OS_WIN32
498 res = g_win32_ftruncate (file->priv->fd, size);
499 #else
500 res = ftruncate (file->priv->fd, size);
501 #endif
503 if (res == -1)
505 int errsv = errno;
507 if (errsv == EINTR)
509 if (g_cancellable_set_error_if_cancelled (cancellable, error))
510 return FALSE;
511 goto restart;
514 g_set_error (error, G_IO_ERROR,
515 g_io_error_from_errno (errsv),
516 _("Error truncating file: %s"),
517 g_strerror (errsv));
518 return FALSE;
521 return TRUE;
525 static GFileInfo *
526 g_local_file_output_stream_query_info (GFileOutputStream *stream,
527 const char *attributes,
528 GCancellable *cancellable,
529 GError **error)
531 GLocalFileOutputStream *file;
533 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
535 if (g_cancellable_set_error_if_cancelled (cancellable, error))
536 return NULL;
538 return _g_local_file_info_get_from_fd (file->priv->fd,
539 attributes,
540 error);
543 GFileOutputStream *
544 _g_local_file_output_stream_open (const char *filename,
545 gboolean readable,
546 GCancellable *cancellable,
547 GError **error)
549 GLocalFileOutputStream *stream;
550 int fd;
551 int open_flags;
553 if (g_cancellable_set_error_if_cancelled (cancellable, error))
554 return NULL;
556 open_flags = O_BINARY;
557 if (readable)
558 open_flags |= O_RDWR;
559 else
560 open_flags |= O_WRONLY;
562 fd = g_open (filename, open_flags, 0666);
563 if (fd == -1)
565 int errsv = errno;
567 if (errsv == EINVAL)
568 /* This must be an invalid filename, on e.g. FAT */
569 g_set_error_literal (error, G_IO_ERROR,
570 G_IO_ERROR_INVALID_FILENAME,
571 _("Invalid filename"));
572 else
574 char *display_name = g_filename_display_name (filename);
575 g_set_error (error, G_IO_ERROR,
576 g_io_error_from_errno (errsv),
577 _("Error opening file '%s': %s"),
578 display_name, g_strerror (errsv));
579 g_free (display_name);
581 return NULL;
584 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
585 stream->priv->fd = fd;
586 return G_FILE_OUTPUT_STREAM (stream);
589 GFileOutputStream *
590 _g_local_file_output_stream_create (const char *filename,
591 gboolean readable,
592 GFileCreateFlags flags,
593 GCancellable *cancellable,
594 GError **error)
596 GLocalFileOutputStream *stream;
597 int mode;
598 int fd;
599 int open_flags;
601 if (g_cancellable_set_error_if_cancelled (cancellable, error))
602 return NULL;
604 if (flags & G_FILE_CREATE_PRIVATE)
605 mode = 0600;
606 else
607 mode = 0666;
609 open_flags = O_CREAT | O_EXCL | O_BINARY;
610 if (readable)
611 open_flags |= O_RDWR;
612 else
613 open_flags |= O_WRONLY;
615 fd = g_open (filename, open_flags, mode);
616 if (fd == -1)
618 int errsv = errno;
620 if (errsv == EINVAL)
621 /* This must be an invalid filename, on e.g. FAT */
622 g_set_error_literal (error, G_IO_ERROR,
623 G_IO_ERROR_INVALID_FILENAME,
624 _("Invalid filename"));
625 else
627 char *display_name = g_filename_display_name (filename);
628 g_set_error (error, G_IO_ERROR,
629 g_io_error_from_errno (errsv),
630 _("Error opening file '%s': %s"),
631 display_name, g_strerror (errsv));
632 g_free (display_name);
634 return NULL;
637 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
638 stream->priv->fd = fd;
639 return G_FILE_OUTPUT_STREAM (stream);
642 GFileOutputStream *
643 _g_local_file_output_stream_append (const char *filename,
644 GFileCreateFlags flags,
645 GCancellable *cancellable,
646 GError **error)
648 GLocalFileOutputStream *stream;
649 int mode;
650 int fd;
652 if (g_cancellable_set_error_if_cancelled (cancellable, error))
653 return NULL;
655 if (flags & G_FILE_CREATE_PRIVATE)
656 mode = 0600;
657 else
658 mode = 0666;
660 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
661 if (fd == -1)
663 int errsv = errno;
665 if (errsv == EINVAL)
666 /* This must be an invalid filename, on e.g. FAT */
667 g_set_error_literal (error, G_IO_ERROR,
668 G_IO_ERROR_INVALID_FILENAME,
669 _("Invalid filename"));
670 else
672 char *display_name = g_filename_display_name (filename);
673 g_set_error (error, G_IO_ERROR,
674 g_io_error_from_errno (errsv),
675 _("Error opening file '%s': %s"),
676 display_name, g_strerror (errsv));
677 g_free (display_name);
679 return NULL;
682 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
683 stream->priv->fd = fd;
685 return G_FILE_OUTPUT_STREAM (stream);
688 static char *
689 create_backup_filename (const char *filename)
691 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
694 #define BUFSIZE 8192 /* size of normal write buffer */
696 static gboolean
697 copy_file_data (gint sfd,
698 gint dfd,
699 GError **error)
701 gboolean ret = TRUE;
702 gpointer buffer;
703 const gchar *write_buffer;
704 gssize bytes_read;
705 gssize bytes_to_write;
706 gssize bytes_written;
708 buffer = g_malloc (BUFSIZE);
712 bytes_read = read (sfd, buffer, BUFSIZE);
713 if (bytes_read == -1)
715 int errsv = errno;
717 if (errsv == EINTR)
718 continue;
720 g_set_error (error, G_IO_ERROR,
721 g_io_error_from_errno (errsv),
722 _("Error reading from file: %s"),
723 g_strerror (errsv));
724 ret = FALSE;
725 break;
728 bytes_to_write = bytes_read;
729 write_buffer = buffer;
733 bytes_written = write (dfd, write_buffer, bytes_to_write);
734 if (bytes_written == -1)
736 int errsv = errno;
738 if (errsv == EINTR)
739 continue;
741 g_set_error (error, G_IO_ERROR,
742 g_io_error_from_errno (errsv),
743 _("Error writing to file: %s"),
744 g_strerror (errsv));
745 ret = FALSE;
746 break;
749 bytes_to_write -= bytes_written;
750 write_buffer += bytes_written;
752 while (bytes_to_write > 0);
754 } while ((bytes_read != 0) && (ret == TRUE));
756 g_free (buffer);
758 return ret;
761 static int
762 handle_overwrite_open (const char *filename,
763 gboolean readable,
764 const char *etag,
765 gboolean create_backup,
766 char **temp_filename,
767 GFileCreateFlags flags,
768 GCancellable *cancellable,
769 GError **error)
771 int fd = -1;
772 GLocalFileStat original_stat;
773 char *current_etag;
774 gboolean is_symlink;
775 int open_flags;
776 int res;
777 int mode;
779 if (flags & G_FILE_CREATE_PRIVATE)
780 mode = 0600;
781 else
782 mode = 0666;
784 /* We only need read access to the original file if we are creating a backup.
785 * We also add O_CREATE to avoid a race if the file was just removed */
786 if (create_backup || readable)
787 open_flags = O_RDWR | O_CREAT | O_BINARY;
788 else
789 open_flags = O_WRONLY | O_CREAT | O_BINARY;
791 /* Some systems have O_NOFOLLOW, which lets us avoid some races
792 * when finding out if the file we opened was a symlink */
793 #ifdef O_NOFOLLOW
794 is_symlink = FALSE;
795 fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
796 if (fd == -1 && errno == ELOOP)
798 /* Could be a symlink, or it could be a regular ELOOP error,
799 * but then the next open will fail too. */
800 is_symlink = TRUE;
801 fd = g_open (filename, open_flags, mode);
803 #else
804 fd = g_open (filename, open_flags, mode);
805 /* This is racy, but we do it as soon as possible to minimize the race */
806 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
807 #endif
809 if (fd == -1)
811 int errsv = errno;
812 char *display_name = g_filename_display_name (filename);
813 g_set_error (error, G_IO_ERROR,
814 g_io_error_from_errno (errsv),
815 _("Error opening file '%s': %s"),
816 display_name, g_strerror (errsv));
817 g_free (display_name);
818 return -1;
821 #ifdef G_OS_WIN32
822 res = _fstati64 (fd, &original_stat);
823 #else
824 res = fstat (fd, &original_stat);
825 #endif
827 if (res != 0)
829 int errsv = errno;
830 char *display_name = g_filename_display_name (filename);
831 g_set_error (error, G_IO_ERROR,
832 g_io_error_from_errno (errsv),
833 _("Error when getting information for file '%s': %s"),
834 display_name, g_strerror (errsv));
835 g_free (display_name);
836 goto err_out;
839 /* not a regular file */
840 if (!S_ISREG (original_stat.st_mode))
842 if (S_ISDIR (original_stat.st_mode))
843 g_set_error_literal (error,
844 G_IO_ERROR,
845 G_IO_ERROR_IS_DIRECTORY,
846 _("Target file is a directory"));
847 else
848 g_set_error_literal (error,
849 G_IO_ERROR,
850 G_IO_ERROR_NOT_REGULAR_FILE,
851 _("Target file is not a regular file"));
852 goto err_out;
855 if (etag != NULL)
857 current_etag = _g_local_file_info_create_etag (&original_stat);
858 if (strcmp (etag, current_etag) != 0)
860 g_set_error_literal (error,
861 G_IO_ERROR,
862 G_IO_ERROR_WRONG_ETAG,
863 _("The file was externally modified"));
864 g_free (current_etag);
865 goto err_out;
867 g_free (current_etag);
870 /* We use two backup strategies.
871 * The first one (which is faster) consist in saving to a
872 * tmp file then rename the original file to the backup and the
873 * tmp file to the original name. This is fast but doesn't work
874 * when the file is a link (hard or symbolic) or when we can't
875 * write to the current dir or can't set the permissions on the
876 * new file.
877 * The second strategy consist simply in copying the old file
878 * to a backup file and rewrite the contents of the file.
881 if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) ||
882 (!(original_stat.st_nlink > 1) && !is_symlink))
884 char *dirname, *tmp_filename;
885 int tmpfd;
887 dirname = g_path_get_dirname (filename);
888 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
889 g_free (dirname);
891 tmpfd = g_mkstemp_full (tmp_filename, (readable ? O_RDWR : O_WRONLY) | O_BINARY, mode);
892 if (tmpfd == -1)
894 g_free (tmp_filename);
895 goto fallback_strategy;
898 /* try to keep permissions (unless replacing) */
900 if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) &&
902 #ifdef HAVE_FCHOWN
903 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
904 #endif
905 #ifdef HAVE_FCHMOD
906 fchmod (tmpfd, original_stat.st_mode) == -1 ||
907 #endif
912 GLocalFileStat tmp_statbuf;
913 int tres;
915 #ifdef G_OS_WIN32
916 tres = _fstati64 (tmpfd, &tmp_statbuf);
917 #else
918 tres = fstat (tmpfd, &tmp_statbuf);
919 #endif
920 /* Check that we really needed to change something */
921 if (tres != 0 ||
922 original_stat.st_uid != tmp_statbuf.st_uid ||
923 original_stat.st_gid != tmp_statbuf.st_gid ||
924 original_stat.st_mode != tmp_statbuf.st_mode)
926 close (tmpfd);
927 g_unlink (tmp_filename);
928 g_free (tmp_filename);
929 goto fallback_strategy;
933 close (fd);
934 *temp_filename = tmp_filename;
935 return tmpfd;
938 fallback_strategy:
940 if (create_backup)
942 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
943 struct stat tmp_statbuf;
944 #endif
945 char *backup_filename;
946 int bfd;
948 backup_filename = create_backup_filename (filename);
950 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
952 g_set_error_literal (error,
953 G_IO_ERROR,
954 G_IO_ERROR_CANT_CREATE_BACKUP,
955 _("Backup file creation failed"));
956 g_free (backup_filename);
957 goto err_out;
960 bfd = g_open (backup_filename,
961 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
962 original_stat.st_mode & 0777);
964 if (bfd == -1)
966 g_set_error_literal (error,
967 G_IO_ERROR,
968 G_IO_ERROR_CANT_CREATE_BACKUP,
969 _("Backup file creation failed"));
970 g_free (backup_filename);
971 goto err_out;
974 /* If needed, Try to set the group of the backup same as the
975 * original file. If this fails, set the protection
976 * bits for the group same as the protection bits for
977 * others. */
978 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
979 if (fstat (bfd, &tmp_statbuf) != 0)
981 g_set_error_literal (error,
982 G_IO_ERROR,
983 G_IO_ERROR_CANT_CREATE_BACKUP,
984 _("Backup file creation failed"));
985 g_unlink (backup_filename);
986 g_free (backup_filename);
987 goto err_out;
990 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
991 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
993 if (fchmod (bfd,
994 (original_stat.st_mode & 0707) |
995 ((original_stat.st_mode & 07) << 3)) != 0)
997 g_set_error_literal (error,
998 G_IO_ERROR,
999 G_IO_ERROR_CANT_CREATE_BACKUP,
1000 _("Backup file creation failed"));
1001 g_unlink (backup_filename);
1002 close (bfd);
1003 g_free (backup_filename);
1004 goto err_out;
1007 #endif
1009 if (!copy_file_data (fd, bfd, NULL))
1011 g_set_error_literal (error,
1012 G_IO_ERROR,
1013 G_IO_ERROR_CANT_CREATE_BACKUP,
1014 _("Backup file creation failed"));
1015 g_unlink (backup_filename);
1016 close (bfd);
1017 g_free (backup_filename);
1019 goto err_out;
1022 close (bfd);
1023 g_free (backup_filename);
1025 /* Seek back to the start of the file after the backup copy */
1026 if (lseek (fd, 0, SEEK_SET) == -1)
1028 int errsv = errno;
1030 g_set_error (error, G_IO_ERROR,
1031 g_io_error_from_errno (errsv),
1032 _("Error seeking in file: %s"),
1033 g_strerror (errsv));
1034 goto err_out;
1038 if (flags & G_FILE_CREATE_REPLACE_DESTINATION)
1040 close (fd);
1042 if (g_unlink (filename) != 0)
1044 int errsv = errno;
1046 g_set_error (error, G_IO_ERROR,
1047 g_io_error_from_errno (errsv),
1048 _("Error removing old file: %s"),
1049 g_strerror (errsv));
1050 goto err_out2;
1053 if (readable)
1054 open_flags = O_RDWR | O_CREAT | O_BINARY;
1055 else
1056 open_flags = O_WRONLY | O_CREAT | O_BINARY;
1057 fd = g_open (filename, open_flags, mode);
1058 if (fd == -1)
1060 int errsv = errno;
1061 char *display_name = g_filename_display_name (filename);
1062 g_set_error (error, G_IO_ERROR,
1063 g_io_error_from_errno (errsv),
1064 _("Error opening file '%s': %s"),
1065 display_name, g_strerror (errsv));
1066 g_free (display_name);
1067 goto err_out2;
1070 else
1072 /* Truncate the file at the start */
1073 #ifdef G_OS_WIN32
1074 if (g_win32_ftruncate (fd, 0) == -1)
1075 #else
1076 if (ftruncate (fd, 0) == -1)
1077 #endif
1079 int errsv = errno;
1081 g_set_error (error, G_IO_ERROR,
1082 g_io_error_from_errno (errsv),
1083 _("Error truncating file: %s"),
1084 g_strerror (errsv));
1085 goto err_out;
1089 return fd;
1091 err_out:
1092 close (fd);
1093 err_out2:
1094 return -1;
1097 GFileOutputStream *
1098 _g_local_file_output_stream_replace (const char *filename,
1099 gboolean readable,
1100 const char *etag,
1101 gboolean create_backup,
1102 GFileCreateFlags flags,
1103 GCancellable *cancellable,
1104 GError **error)
1106 GLocalFileOutputStream *stream;
1107 int mode;
1108 int fd;
1109 char *temp_file;
1110 gboolean sync_on_close;
1111 int open_flags;
1113 if (g_cancellable_set_error_if_cancelled (cancellable, error))
1114 return NULL;
1116 temp_file = NULL;
1118 if (flags & G_FILE_CREATE_PRIVATE)
1119 mode = 0600;
1120 else
1121 mode = 0666;
1122 sync_on_close = FALSE;
1124 /* If the file doesn't exist, create it */
1125 open_flags = O_CREAT | O_EXCL | O_BINARY;
1126 if (readable)
1127 open_flags |= O_RDWR;
1128 else
1129 open_flags |= O_WRONLY;
1130 fd = g_open (filename, open_flags, mode);
1132 if (fd == -1 && errno == EEXIST)
1134 /* The file already exists */
1135 fd = handle_overwrite_open (filename, readable, etag,
1136 create_backup, &temp_file,
1137 flags, cancellable, error);
1138 if (fd == -1)
1139 return NULL;
1141 /* If the final destination exists, we want to sync the newly written
1142 * file to ensure the data is on disk when we rename over the destination.
1143 * otherwise if we get a system crash we can lose both the new and the
1144 * old file on some filesystems. (I.E. those that don't guarantee the
1145 * data is written to the disk before the metadata.)
1147 sync_on_close = TRUE;
1149 else if (fd == -1)
1151 int errsv = errno;
1153 if (errsv == EINVAL)
1154 /* This must be an invalid filename, on e.g. FAT */
1155 g_set_error_literal (error, G_IO_ERROR,
1156 G_IO_ERROR_INVALID_FILENAME,
1157 _("Invalid filename"));
1158 else
1160 char *display_name = g_filename_display_name (filename);
1161 g_set_error (error, G_IO_ERROR,
1162 g_io_error_from_errno (errsv),
1163 _("Error opening file '%s': %s"),
1164 display_name, g_strerror (errsv));
1165 g_free (display_name);
1167 return NULL;
1171 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
1172 stream->priv->fd = fd;
1173 stream->priv->sync_on_close = sync_on_close;
1174 stream->priv->tmp_filename = temp_file;
1175 if (create_backup)
1176 stream->priv->backup_filename = create_backup_filename (filename);
1177 stream->priv->original_filename = g_strdup (filename);
1179 return G_FILE_OUTPUT_STREAM (stream);
1182 gint
1183 _g_local_file_output_stream_get_fd (GLocalFileOutputStream *stream)
1185 g_return_val_if_fail (G_IS_LOCAL_FILE_OUTPUT_STREAM (stream), -1);
1186 return stream->priv->fd;
1189 #ifdef G_OS_UNIX
1190 static int
1191 g_local_file_output_stream_get_fd (GFileDescriptorBased *fd_based)
1193 GLocalFileOutputStream *stream = G_LOCAL_FILE_OUTPUT_STREAM (fd_based);
1194 return _g_local_file_output_stream_get_fd (stream);
1196 #endif