Add a GIcon implementation that can add an emblem to another icon.
[glib.git] / gio / glocalfileoutputstream.c
blobcbd99940258c5de26a9a9e7bfc72018512c28a8c
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_WIN32
43 #include <io.h>
44 #ifndef S_ISDIR
45 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
46 #endif
47 #ifndef S_ISREG
48 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
49 #endif
50 #endif
52 #ifndef O_BINARY
53 #define O_BINARY 0
54 #endif
56 #include "gioalias.h"
58 #define g_local_file_output_stream_get_type _g_local_file_output_stream_get_type
59 G_DEFINE_TYPE (GLocalFileOutputStream, g_local_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
61 /* Some of the file replacement code was based on the code from gedit,
62 * relicenced to LGPL with permissions from the authors.
65 #define BACKUP_EXTENSION "~"
67 struct _GLocalFileOutputStreamPrivate {
68 char *tmp_filename;
69 char *original_filename;
70 char *backup_filename;
71 char *etag;
72 int fd;
75 static gssize g_local_file_output_stream_write (GOutputStream *stream,
76 const void *buffer,
77 gsize count,
78 GCancellable *cancellable,
79 GError **error);
80 static gboolean g_local_file_output_stream_close (GOutputStream *stream,
81 GCancellable *cancellable,
82 GError **error);
83 static GFileInfo *g_local_file_output_stream_query_info (GFileOutputStream *stream,
84 char *attributes,
85 GCancellable *cancellable,
86 GError **error);
87 static char * g_local_file_output_stream_get_etag (GFileOutputStream *stream);
88 static goffset g_local_file_output_stream_tell (GFileOutputStream *stream);
89 static gboolean g_local_file_output_stream_can_seek (GFileOutputStream *stream);
90 static gboolean g_local_file_output_stream_seek (GFileOutputStream *stream,
91 goffset offset,
92 GSeekType type,
93 GCancellable *cancellable,
94 GError **error);
95 static gboolean g_local_file_output_stream_can_truncate (GFileOutputStream *stream);
96 static gboolean g_local_file_output_stream_truncate (GFileOutputStream *stream,
97 goffset size,
98 GCancellable *cancellable,
99 GError **error);
101 static void
102 g_local_file_output_stream_finalize (GObject *object)
104 GLocalFileOutputStream *file;
106 file = G_LOCAL_FILE_OUTPUT_STREAM (object);
108 g_free (file->priv->tmp_filename);
109 g_free (file->priv->original_filename);
110 g_free (file->priv->backup_filename);
111 g_free (file->priv->etag);
113 G_OBJECT_CLASS (g_local_file_output_stream_parent_class)->finalize (object);
116 static void
117 g_local_file_output_stream_class_init (GLocalFileOutputStreamClass *klass)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
120 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
121 GFileOutputStreamClass *file_stream_class = G_FILE_OUTPUT_STREAM_CLASS (klass);
123 g_type_class_add_private (klass, sizeof (GLocalFileOutputStreamPrivate));
125 gobject_class->finalize = g_local_file_output_stream_finalize;
127 stream_class->write_fn = g_local_file_output_stream_write;
128 stream_class->close_fn = g_local_file_output_stream_close;
129 file_stream_class->query_info = g_local_file_output_stream_query_info;
130 file_stream_class->get_etag = g_local_file_output_stream_get_etag;
131 file_stream_class->tell = g_local_file_output_stream_tell;
132 file_stream_class->can_seek = g_local_file_output_stream_can_seek;
133 file_stream_class->seek = g_local_file_output_stream_seek;
134 file_stream_class->can_truncate = g_local_file_output_stream_can_truncate;
135 file_stream_class->truncate_fn = g_local_file_output_stream_truncate;
138 static void
139 g_local_file_output_stream_init (GLocalFileOutputStream *stream)
141 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
142 G_TYPE_LOCAL_FILE_OUTPUT_STREAM,
143 GLocalFileOutputStreamPrivate);
146 static gssize
147 g_local_file_output_stream_write (GOutputStream *stream,
148 const void *buffer,
149 gsize count,
150 GCancellable *cancellable,
151 GError **error)
153 GLocalFileOutputStream *file;
154 gssize res;
156 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
158 while (1)
160 if (g_cancellable_set_error_if_cancelled (cancellable, error))
161 return -1;
162 res = write (file->priv->fd, buffer, count);
163 if (res == -1)
165 int errsv = errno;
167 if (errsv == EINTR)
168 continue;
170 g_set_error (error, G_IO_ERROR,
171 g_io_error_from_errno (errsv),
172 _("Error writing to file: %s"),
173 g_strerror (errsv));
176 break;
179 return res;
182 static gboolean
183 g_local_file_output_stream_close (GOutputStream *stream,
184 GCancellable *cancellable,
185 GError **error)
187 GLocalFileOutputStream *file;
188 struct stat final_stat;
189 int res;
191 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
193 if (file->priv->tmp_filename)
195 /* We need to move the temp file to its final place,
196 * and possibly create the backup file
199 if (file->priv->backup_filename)
201 if (g_cancellable_set_error_if_cancelled (cancellable, error))
202 goto err_out;
204 #ifdef HAVE_LINK
205 /* create original -> backup link, the original is then renamed over */
206 if (g_unlink (file->priv->backup_filename) != 0 &&
207 errno != ENOENT)
209 int errsv = errno;
211 g_set_error (error, G_IO_ERROR,
212 G_IO_ERROR_CANT_CREATE_BACKUP,
213 _("Error removing old backup link: %s"),
214 g_strerror (errsv));
215 goto err_out;
218 if (link (file->priv->original_filename, file->priv->backup_filename) != 0)
220 /* link failed or is not supported, try rename */
221 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
223 int errsv = errno;
225 g_set_error (error, G_IO_ERROR,
226 G_IO_ERROR_CANT_CREATE_BACKUP,
227 _("Error creating backup copy: %s"),
228 g_strerror (errsv));
229 goto err_out;
232 #else
233 /* If link not supported, just rename... */
234 if (g_rename (file->priv->original_filename, file->priv->backup_filename) != 0)
236 int errsv = errno;
238 g_set_error (error, G_IO_ERROR,
239 G_IO_ERROR_CANT_CREATE_BACKUP,
240 _("Error creating backup copy: %s"),
241 g_strerror (errsv));
242 goto err_out;
244 #endif
248 if (g_cancellable_set_error_if_cancelled (cancellable, error))
249 goto err_out;
251 /* tmp -> original */
252 if (g_rename (file->priv->tmp_filename, file->priv->original_filename) != 0)
254 int errsv = errno;
256 g_set_error (error, G_IO_ERROR,
257 g_io_error_from_errno (errno),
258 _("Error renaming temporary file: %s"),
259 g_strerror (errsv));
260 goto err_out;
264 if (g_cancellable_set_error_if_cancelled (cancellable, error))
265 goto err_out;
267 if (fstat (file->priv->fd, &final_stat) == 0)
268 file->priv->etag = _g_local_file_info_create_etag (&final_stat);
270 while (1)
272 res = close (file->priv->fd);
273 if (res == -1)
275 int errsv = errno;
277 g_set_error (error, G_IO_ERROR,
278 g_io_error_from_errno (errsv),
279 _("Error closing file: %s"),
280 g_strerror (errsv));
282 break;
285 return res != -1;
287 err_out:
288 /* A simple try to close the fd in case we fail before the actual close */
289 close (file->priv->fd);
290 return FALSE;
293 static char *
294 g_local_file_output_stream_get_etag (GFileOutputStream *stream)
296 GLocalFileOutputStream *file;
298 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
300 return g_strdup (file->priv->etag);
303 static goffset
304 g_local_file_output_stream_tell (GFileOutputStream *stream)
306 GLocalFileOutputStream *file;
307 off_t pos;
309 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
311 pos = lseek (file->priv->fd, 0, SEEK_CUR);
313 if (pos == (off_t)-1)
314 return 0;
316 return pos;
319 static gboolean
320 g_local_file_output_stream_can_seek (GFileOutputStream *stream)
322 GLocalFileOutputStream *file;
323 off_t pos;
325 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
327 pos = lseek (file->priv->fd, 0, SEEK_CUR);
329 if (pos == (off_t)-1 && errno == ESPIPE)
330 return FALSE;
332 return TRUE;
335 static int
336 seek_type_to_lseek (GSeekType type)
338 switch (type)
340 default:
341 case G_SEEK_CUR:
342 return SEEK_CUR;
344 case G_SEEK_SET:
345 return SEEK_SET;
347 case G_SEEK_END:
348 return SEEK_END;
352 static gboolean
353 g_local_file_output_stream_seek (GFileOutputStream *stream,
354 goffset offset,
355 GSeekType type,
356 GCancellable *cancellable,
357 GError **error)
359 GLocalFileOutputStream *file;
360 off_t pos;
362 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
364 pos = lseek (file->priv->fd, offset, seek_type_to_lseek (type));
366 if (pos == (off_t)-1)
368 int errsv = errno;
370 g_set_error (error, G_IO_ERROR,
371 g_io_error_from_errno (errsv),
372 _("Error seeking in file: %s"),
373 g_strerror (errsv));
374 return FALSE;
377 return TRUE;
380 static gboolean
381 g_local_file_output_stream_can_truncate (GFileOutputStream *stream)
383 /* We can't truncate pipes and stuff where we can't seek */
384 return g_local_file_output_stream_can_seek (stream);
387 static gboolean
388 g_local_file_output_stream_truncate (GFileOutputStream *stream,
389 goffset size,
390 GCancellable *cancellable,
391 GError **error)
393 GLocalFileOutputStream *file;
394 int res;
396 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
398 restart:
399 #ifdef G_OS_WIN32
400 res = g_win32_ftruncate (file->priv->fd, size);
401 #else
402 res = ftruncate (file->priv->fd, size);
403 #endif
405 if (res == -1)
407 int errsv = errno;
409 if (errsv == EINTR)
411 if (g_cancellable_set_error_if_cancelled (cancellable, error))
412 return FALSE;
413 goto restart;
416 g_set_error (error, G_IO_ERROR,
417 g_io_error_from_errno (errsv),
418 _("Error truncating file: %s"),
419 g_strerror (errsv));
420 return FALSE;
423 return TRUE;
427 static GFileInfo *
428 g_local_file_output_stream_query_info (GFileOutputStream *stream,
429 char *attributes,
430 GCancellable *cancellable,
431 GError **error)
433 GLocalFileOutputStream *file;
435 file = G_LOCAL_FILE_OUTPUT_STREAM (stream);
437 if (g_cancellable_set_error_if_cancelled (cancellable, error))
438 return NULL;
440 return _g_local_file_info_get_from_fd (file->priv->fd,
441 attributes,
442 error);
445 GFileOutputStream *
446 _g_local_file_output_stream_create (const char *filename,
447 GFileCreateFlags flags,
448 GCancellable *cancellable,
449 GError **error)
451 GLocalFileOutputStream *stream;
452 int mode;
453 int fd;
455 if (g_cancellable_set_error_if_cancelled (cancellable, error))
456 return NULL;
458 if (flags & G_FILE_CREATE_PRIVATE)
459 mode = 0600;
460 else
461 mode = 0666;
463 fd = g_open (filename, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
464 if (fd == -1)
466 int errsv = errno;
468 if (errsv == EINVAL)
469 /* This must be an invalid filename, on e.g. FAT */
470 g_set_error_literal (error, G_IO_ERROR,
471 G_IO_ERROR_INVALID_FILENAME,
472 _("Invalid filename"));
473 else
475 char *display_name = g_filename_display_name (filename);
476 g_set_error (error, G_IO_ERROR,
477 g_io_error_from_errno (errsv),
478 _("Error opening file '%s': %s"),
479 display_name, g_strerror (errsv));
480 g_free (display_name);
482 return NULL;
485 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
486 stream->priv->fd = fd;
487 return G_FILE_OUTPUT_STREAM (stream);
490 GFileOutputStream *
491 _g_local_file_output_stream_append (const char *filename,
492 GFileCreateFlags flags,
493 GCancellable *cancellable,
494 GError **error)
496 GLocalFileOutputStream *stream;
497 int mode;
498 int fd;
500 if (g_cancellable_set_error_if_cancelled (cancellable, error))
501 return NULL;
503 if (flags & G_FILE_CREATE_PRIVATE)
504 mode = 0600;
505 else
506 mode = 0666;
508 fd = g_open (filename, O_CREAT | O_APPEND | O_WRONLY | O_BINARY, mode);
509 if (fd == -1)
511 int errsv = errno;
513 if (errsv == EINVAL)
514 /* This must be an invalid filename, on e.g. FAT */
515 g_set_error_literal (error, G_IO_ERROR,
516 G_IO_ERROR_INVALID_FILENAME,
517 _("Invalid filename"));
518 else
520 char *display_name = g_filename_display_name (filename);
521 g_set_error (error, G_IO_ERROR,
522 g_io_error_from_errno (errsv),
523 _("Error opening file '%s': %s"),
524 display_name, g_strerror (errsv));
525 g_free (display_name);
527 return NULL;
530 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
531 stream->priv->fd = fd;
533 return G_FILE_OUTPUT_STREAM (stream);
536 static char *
537 create_backup_filename (const char *filename)
539 return g_strconcat (filename, BACKUP_EXTENSION, NULL);
542 #define BUFSIZE 8192 /* size of normal write buffer */
544 static gboolean
545 copy_file_data (gint sfd,
546 gint dfd,
547 GError **error)
549 gboolean ret = TRUE;
550 gpointer buffer;
551 const gchar *write_buffer;
552 gssize bytes_read;
553 gssize bytes_to_write;
554 gssize bytes_written;
556 buffer = g_malloc (BUFSIZE);
560 bytes_read = read (sfd, buffer, BUFSIZE);
561 if (bytes_read == -1)
563 int errsv = errno;
565 if (errsv == EINTR)
566 continue;
568 g_set_error (error, G_IO_ERROR,
569 g_io_error_from_errno (errsv),
570 _("Error reading from file: %s"),
571 g_strerror (errsv));
572 ret = FALSE;
573 break;
576 bytes_to_write = bytes_read;
577 write_buffer = buffer;
581 bytes_written = write (dfd, write_buffer, bytes_to_write);
582 if (bytes_written == -1)
584 int errsv = errno;
586 if (errsv == EINTR)
587 continue;
589 g_set_error (error, G_IO_ERROR,
590 g_io_error_from_errno (errsv),
591 _("Error writing to file: %s"),
592 g_strerror (errsv));
593 ret = FALSE;
594 break;
597 bytes_to_write -= bytes_written;
598 write_buffer += bytes_written;
600 while (bytes_to_write > 0);
602 } while ((bytes_read != 0) && (ret == TRUE));
604 g_free (buffer);
606 return ret;
609 static int
610 handle_overwrite_open (const char *filename,
611 const char *etag,
612 gboolean create_backup,
613 char **temp_filename,
614 GCancellable *cancellable,
615 GError **error)
617 int fd = -1;
618 struct stat original_stat;
619 char *current_etag;
620 gboolean is_symlink;
621 int open_flags;
623 /* We only need read access to the original file if we are creating a backup.
624 * We also add O_CREATE to avoid a race if the file was just removed */
625 if (create_backup)
626 open_flags = O_RDWR | O_CREAT | O_BINARY;
627 else
628 open_flags = O_WRONLY | O_CREAT | O_BINARY;
630 /* Some systems have O_NOFOLLOW, which lets us avoid some races
631 * when finding out if the file we opened was a symlink */
632 #ifdef O_NOFOLLOW
633 is_symlink = FALSE;
634 fd = g_open (filename, open_flags | O_NOFOLLOW, 0666);
635 if (fd == -1 && errno == ELOOP)
637 /* Could be a symlink, or it could be a regular ELOOP error,
638 * but then the next open will fail too. */
639 is_symlink = TRUE;
640 fd = g_open (filename, open_flags, 0666);
642 #else
643 fd = g_open (filename, open_flags, 0666);
644 /* This is racy, but we do it as soon as possible to minimize the race */
645 is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
646 #endif
648 if (fd == -1)
650 int errsv = errno;
651 char *display_name = g_filename_display_name (filename);
652 g_set_error (error, G_IO_ERROR,
653 g_io_error_from_errno (errsv),
654 _("Error opening file '%s': %s"),
655 display_name, g_strerror (errsv));
656 g_free (display_name);
657 return -1;
660 if (fstat (fd, &original_stat) != 0)
662 int errsv = errno;
663 char *display_name = g_filename_display_name (filename);
664 g_set_error (error, G_IO_ERROR,
665 g_io_error_from_errno (errsv),
666 _("Error stating file '%s': %s"),
667 display_name, g_strerror (errsv));
668 g_free (display_name);
669 goto err_out;
672 /* not a regular file */
673 if (!S_ISREG (original_stat.st_mode))
675 if (S_ISDIR (original_stat.st_mode))
676 g_set_error_literal (error,
677 G_IO_ERROR,
678 G_IO_ERROR_IS_DIRECTORY,
679 _("Target file is a directory"));
680 else
681 g_set_error_literal (error,
682 G_IO_ERROR,
683 G_IO_ERROR_NOT_REGULAR_FILE,
684 _("Target file is not a regular file"));
685 goto err_out;
688 if (etag != NULL)
690 current_etag = _g_local_file_info_create_etag (&original_stat);
691 if (strcmp (etag, current_etag) != 0)
693 g_set_error_literal (error,
694 G_IO_ERROR,
695 G_IO_ERROR_WRONG_ETAG,
696 _("The file was externally modified"));
697 g_free (current_etag);
698 goto err_out;
700 g_free (current_etag);
703 /* We use two backup strategies.
704 * The first one (which is faster) consist in saving to a
705 * tmp file then rename the original file to the backup and the
706 * tmp file to the original name. This is fast but doesn't work
707 * when the file is a link (hard or symbolic) or when we can't
708 * write to the current dir or can't set the permissions on the
709 * new file.
710 * The second strategy consist simply in copying the old file
711 * to a backup file and rewrite the contents of the file.
714 if (!(original_stat.st_nlink > 1) && !is_symlink)
716 char *dirname, *tmp_filename;
717 int tmpfd;
719 dirname = g_path_get_dirname (filename);
720 tmp_filename = g_build_filename (dirname, ".goutputstream-XXXXXX", NULL);
721 g_free (dirname);
723 tmpfd = g_mkstemp (tmp_filename);
724 if (tmpfd == -1)
726 g_free (tmp_filename);
727 goto fallback_strategy;
730 /* try to keep permissions */
732 if (
733 #ifdef HAVE_FCHOWN
734 fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 ||
735 #endif
736 #ifdef HAVE_FCHMOD
737 fchmod (tmpfd, original_stat.st_mode) == -1 ||
738 #endif
742 struct stat tmp_statbuf;
744 /* Check that we really needed to change something */
745 if (fstat (tmpfd, &tmp_statbuf) != 0 ||
746 original_stat.st_uid != tmp_statbuf.st_uid ||
747 original_stat.st_gid != tmp_statbuf.st_gid ||
748 original_stat.st_mode != tmp_statbuf.st_mode)
750 close (tmpfd);
751 g_unlink (tmp_filename);
752 g_free (tmp_filename);
753 goto fallback_strategy;
757 close (fd);
758 *temp_filename = tmp_filename;
759 return tmpfd;
762 fallback_strategy:
764 if (create_backup)
766 struct stat tmp_statbuf;
767 char *backup_filename;
768 int bfd;
770 backup_filename = create_backup_filename (filename);
772 if (g_unlink (backup_filename) == -1 && errno != ENOENT)
774 g_set_error_literal (error,
775 G_IO_ERROR,
776 G_IO_ERROR_CANT_CREATE_BACKUP,
777 _("Backup file creation failed"));
778 g_free (backup_filename);
779 goto err_out;
782 bfd = g_open (backup_filename,
783 O_WRONLY | O_CREAT | O_EXCL | O_BINARY,
784 original_stat.st_mode & 0777);
786 if (bfd == -1)
788 g_set_error_literal (error,
789 G_IO_ERROR,
790 G_IO_ERROR_CANT_CREATE_BACKUP,
791 _("Backup file creation failed"));
792 g_free (backup_filename);
793 goto err_out;
796 /* If needed, Try to set the group of the backup same as the
797 * original file. If this fails, set the protection
798 * bits for the group same as the protection bits for
799 * others. */
800 #if defined(HAVE_FCHOWN) && defined(HAVE_FCHMOD)
801 if (fstat (bfd, &tmp_statbuf) != 0)
803 g_set_error_literal (error,
804 G_IO_ERROR,
805 G_IO_ERROR_CANT_CREATE_BACKUP,
806 _("Backup file creation failed"));
807 g_unlink (backup_filename);
808 g_free (backup_filename);
809 goto err_out;
812 if ((original_stat.st_gid != tmp_statbuf.st_gid) &&
813 fchown (bfd, (uid_t) -1, original_stat.st_gid) != 0)
815 if (fchmod (bfd,
816 (original_stat.st_mode & 0707) |
817 ((original_stat.st_mode & 07) << 3)) != 0)
819 g_set_error_literal (error,
820 G_IO_ERROR,
821 G_IO_ERROR_CANT_CREATE_BACKUP,
822 _("Backup file creation failed"));
823 g_unlink (backup_filename);
824 close (bfd);
825 g_free (backup_filename);
826 goto err_out;
829 #endif
831 if (!copy_file_data (fd, bfd, NULL))
833 g_set_error_literal (error,
834 G_IO_ERROR,
835 G_IO_ERROR_CANT_CREATE_BACKUP,
836 _("Backup file creation failed"));
837 g_unlink (backup_filename);
838 close (bfd);
839 g_free (backup_filename);
841 goto err_out;
844 close (bfd);
845 g_free (backup_filename);
847 /* Seek back to the start of the file after the backup copy */
848 if (lseek (fd, 0, SEEK_SET) == -1)
850 int errsv = errno;
852 g_set_error (error, G_IO_ERROR,
853 g_io_error_from_errno (errsv),
854 _("Error seeking in file: %s"),
855 g_strerror (errsv));
856 goto err_out;
860 /* Truncate the file at the start */
861 #ifdef G_OS_WIN32
862 if (g_win32_ftruncate (fd, 0) == -1)
863 #else
864 if (ftruncate (fd, 0) == -1)
865 #endif
867 int errsv = errno;
869 g_set_error (error, G_IO_ERROR,
870 g_io_error_from_errno (errsv),
871 _("Error truncating file: %s"),
872 g_strerror (errsv));
873 goto err_out;
876 return fd;
878 err_out:
879 close (fd);
880 return -1;
883 GFileOutputStream *
884 _g_local_file_output_stream_replace (const char *filename,
885 const char *etag,
886 gboolean create_backup,
887 GFileCreateFlags flags,
888 GCancellable *cancellable,
889 GError **error)
891 GLocalFileOutputStream *stream;
892 int mode;
893 int fd;
894 char *temp_file;
896 if (g_cancellable_set_error_if_cancelled (cancellable, error))
897 return NULL;
899 temp_file = NULL;
901 if (flags & G_FILE_CREATE_PRIVATE)
902 mode = 0600;
903 else
904 mode = 0666;
906 /* If the file doesn't exist, create it */
907 fd = g_open (filename, O_CREAT | O_EXCL | O_WRONLY | O_BINARY, mode);
909 if (fd == -1 && errno == EEXIST)
911 /* The file already exists */
912 fd = handle_overwrite_open (filename, etag, create_backup, &temp_file,
913 cancellable, error);
914 if (fd == -1)
915 return NULL;
917 else if (fd == -1)
919 int errsv = errno;
921 if (errsv == EINVAL)
922 /* This must be an invalid filename, on e.g. FAT */
923 g_set_error_literal (error, G_IO_ERROR,
924 G_IO_ERROR_INVALID_FILENAME,
925 _("Invalid filename"));
926 else
928 char *display_name = g_filename_display_name (filename);
929 g_set_error (error, G_IO_ERROR,
930 g_io_error_from_errno (errsv),
931 _("Error opening file '%s': %s"),
932 display_name, g_strerror (errsv));
933 g_free (display_name);
935 return NULL;
939 stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL);
940 stream->priv->fd = fd;
941 stream->priv->tmp_filename = temp_file;
942 if (create_backup)
943 stream->priv->backup_filename = create_backup_filename (filename);
944 stream->priv->original_filename = g_strdup (filename);
946 return G_FILE_OUTPUT_STREAM (stream);