diff options
Diffstat (limited to 'gio/glocalfileoutputstream.c')
-rw-r--r-- | gio/glocalfileoutputstream.c | 102 |
1 files changed, 67 insertions, 35 deletions
diff --git a/gio/glocalfileoutputstream.c b/gio/glocalfileoutputstream.c index 57d2d5dfe..8d7eadd95 100644 --- a/gio/glocalfileoutputstream.c +++ b/gio/glocalfileoutputstream.c @@ -56,6 +56,12 @@ #define O_BINARY 0 #endif +#ifndef O_CLOEXEC +#define O_CLOEXEC 0 +#else +#define HAVE_O_CLOEXEC 1 +#endif + struct _GLocalFileOutputStreamPrivate { char *tmp_filename; char *original_filename; @@ -751,11 +757,12 @@ handle_overwrite_open (const char *filename, int res; int mode; int errsv; + gboolean replace_destination_set = (flags & G_FILE_CREATE_REPLACE_DESTINATION); mode = mode_from_flags_or_info (flags, reference_info); /* We only need read access to the original file if we are creating a backup. - * We also add O_CREATE to avoid a race if the file was just removed */ + * We also add O_CREAT to avoid a race if the file was just removed */ if (create_backup || readable) open_flags = O_RDWR | O_CREAT | O_BINARY; else @@ -778,16 +785,22 @@ handle_overwrite_open (const char *filename, /* Could be a symlink, or it could be a regular ELOOP error, * but then the next open will fail too. */ is_symlink = TRUE; - fd = g_open (filename, open_flags, mode); + if (!replace_destination_set) + fd = g_open (filename, open_flags, mode); } -#else - fd = g_open (filename, open_flags, mode); - errsv = errno; +#else /* if !O_NOFOLLOW */ /* This is racy, but we do it as soon as possible to minimize the race */ is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK); + + if (!is_symlink || !replace_destination_set) + { + fd = g_open (filename, open_flags, mode); + errsv = errno; + } #endif - if (fd == -1) + if (fd == -1 && + (!is_symlink || !replace_destination_set)) { char *display_name = g_filename_display_name (filename); g_set_error (error, G_IO_ERROR, @@ -801,7 +814,10 @@ handle_overwrite_open (const char *filename, #ifdef G_OS_WIN32 res = GLIB_PRIVATE_CALL (g_win32_fstat) (fd, &original_stat); #else - res = fstat (fd, &original_stat); + if (!is_symlink) + res = fstat (fd, &original_stat); + else + res = lstat (filename, &original_stat); #endif errsv = errno; @@ -813,23 +829,34 @@ handle_overwrite_open (const char *filename, _("Error when getting information for file ā%sā: %s"), display_name, g_strerror (errsv)); g_free (display_name); - goto err_out; + goto error; } /* not a regular file */ if (!S_ISREG (original_stat.st_mode)) { if (S_ISDIR (original_stat.st_mode)) - g_set_error_literal (error, - G_IO_ERROR, - G_IO_ERROR_IS_DIRECTORY, - _("Target file is a directory")); - else - g_set_error_literal (error, + { + g_set_error_literal (error, + G_IO_ERROR, + G_IO_ERROR_IS_DIRECTORY, + _("Target file is a directory")); + goto error; + } + else if (!is_symlink || +#ifdef S_ISLNK + !S_ISLNK (original_stat.st_mode) +#else + FALSE +#endif + ) + { + g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_REGULAR_FILE, _("Target file is not a regular file")); - goto err_out; + goto error; + } } if (etag != NULL) @@ -842,7 +869,7 @@ handle_overwrite_open (const char *filename, G_IO_ERROR_WRONG_ETAG, _("The file was externally modified")); g_free (current_etag); - goto err_out; + goto error; } g_free (current_etag); } @@ -858,7 +885,7 @@ handle_overwrite_open (const char *filename, * to a backup file and rewrite the contents of the file. */ - if ((flags & G_FILE_CREATE_REPLACE_DESTINATION) || + if (replace_destination_set || (!(original_stat.st_nlink > 1) && !is_symlink)) { char *dirname, *tmp_filename; @@ -877,7 +904,7 @@ handle_overwrite_open (const char *filename, /* try to keep permissions (unless replacing) */ - if ( ! (flags & G_FILE_CREATE_REPLACE_DESTINATION) && + if (!replace_destination_set && ( #ifdef HAVE_FCHOWN fchown (tmpfd, original_stat.st_uid, original_stat.st_gid) == -1 || @@ -910,7 +937,8 @@ handle_overwrite_open (const char *filename, } } - g_close (fd, NULL); + if (fd >= 0) + g_close (fd, NULL); *temp_filename = tmp_filename; return tmpfd; } @@ -934,7 +962,7 @@ handle_overwrite_open (const char *filename, G_IO_ERROR_CANT_CREATE_BACKUP, _("Backup file creation failed")); g_free (backup_filename); - goto err_out; + goto error; } bfd = g_open (backup_filename, @@ -948,7 +976,7 @@ handle_overwrite_open (const char *filename, G_IO_ERROR_CANT_CREATE_BACKUP, _("Backup file creation failed")); g_free (backup_filename); - goto err_out; + goto error; } /* If needed, Try to set the group of the backup same as the @@ -965,7 +993,7 @@ handle_overwrite_open (const char *filename, g_unlink (backup_filename); g_close (bfd, NULL); g_free (backup_filename); - goto err_out; + goto error; } if ((original_stat.st_gid != tmp_statbuf.st_gid) && @@ -982,7 +1010,7 @@ handle_overwrite_open (const char *filename, g_unlink (backup_filename); g_close (bfd, NULL); g_free (backup_filename); - goto err_out; + goto error; } } #endif @@ -997,7 +1025,7 @@ handle_overwrite_open (const char *filename, g_close (bfd, NULL); g_free (backup_filename); - goto err_out; + goto error; } g_close (bfd, NULL); @@ -1012,11 +1040,11 @@ handle_overwrite_open (const char *filename, g_io_error_from_errno (errsv), _("Error seeking in file: %s"), g_strerror (errsv)); - goto err_out; + goto error; } } - if (flags & G_FILE_CREATE_REPLACE_DESTINATION) + if (replace_destination_set) { g_close (fd, NULL); @@ -1028,7 +1056,7 @@ handle_overwrite_open (const char *filename, g_io_error_from_errno (errsv), _("Error removing old file: %s"), g_strerror (errsv)); - goto err_out2; + goto error; } if (readable) @@ -1045,7 +1073,7 @@ handle_overwrite_open (const char *filename, _("Error opening file ā%sā: %s"), display_name, g_strerror (errsv)); g_free (display_name); - goto err_out2; + goto error; } } else @@ -1063,15 +1091,16 @@ handle_overwrite_open (const char *filename, g_io_error_from_errno (errsv), _("Error truncating file: %s"), g_strerror (errsv)); - goto err_out; + goto error; } } return fd; - err_out: - g_close (fd, NULL); - err_out2: +error: + if (fd >= 0) + g_close (fd, NULL); + return -1; } @@ -1101,7 +1130,7 @@ _g_local_file_output_stream_replace (const char *filename, sync_on_close = FALSE; /* If the file doesn't exist, create it */ - open_flags = O_CREAT | O_EXCL | O_BINARY; + open_flags = O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC; if (readable) open_flags |= O_RDWR; else @@ -1131,8 +1160,11 @@ _g_local_file_output_stream_replace (const char *filename, set_error_from_open_errno (filename, error); return NULL; } - - +#if !defined(HAVE_O_CLOEXEC) && defined(F_SETFD) + else + fcntl (fd, F_SETFD, FD_CLOEXEC); +#endif + stream = g_object_new (G_TYPE_LOCAL_FILE_OUTPUT_STREAM, NULL); stream->priv->fd = fd; stream->priv->sync_on_close = sync_on_close; |