Chromium Code Reviews| Index: content/browser/download/base_file_posix.cc |
| diff --git a/content/browser/download/base_file_posix.cc b/content/browser/download/base_file_posix.cc |
| index b5d8e01442244b0f0a4fb45b60d3060618bb4786..d024ac7ff9247c68260e015593c89a6618d40ef9 100644 |
| --- a/content/browser/download/base_file_posix.cc |
| +++ b/content/browser/download/base_file_posix.cc |
| @@ -11,31 +11,32 @@ namespace content { |
| DownloadInterruptReason BaseFile::MoveFileAndAdjustPermissions( |
| const base::FilePath& new_path) { |
| - // Similarly, on Unix, we're moving a temp file created with permissions 600 |
| - // to |new_path|. Here, we try to fix up the destination file with appropriate |
| - // permissions. |
| - struct stat st; |
| - // First check the file existence and create an empty file if it doesn't |
| - // exist. |
| + // Move a temporary file created with mode 0600 to |new_path|. If |
| + // |new_path| does not already exist, set the correct mode on the new file |
| + // (honoring the user's umask). |
| + mode_t mask = umask(0); |
| + (void) umask(mask); |
| + mode_t mode = 0666 & ~mask; |
| + |
| if (!base::PathExists(new_path)) { |
| - int write_error = base::WriteFile(new_path, "", 0); |
| - if (write_error < 0) |
| + if (base::WriteFileMode(new_path, "", 0, mode) != 0) |
|
rvargas (doing something else)
2014/10/21 01:05:53
The signature changed
palmer
2014/10/21 01:26:42
Done.
|
| return LogSystemError("WriteFile", errno); |
| } |
| - int stat_error = stat(new_path.value().c_str(), &st); |
| - bool stat_succeeded = (stat_error == 0); |
| - if (!stat_succeeded) |
| - LogSystemError("stat", errno); |
| - |
| - if (!base::Move(full_path_, new_path)) |
| - return LogSystemError("Move", errno); |
| - |
| - if (stat_succeeded) { |
| - // On Windows file systems (FAT, NTFS), chmod fails. This is OK. |
| - int chmod_error = chmod(new_path.value().c_str(), st.st_mode); |
| - if (chmod_error < 0) |
| - LogSystemError("chmod", errno); |
| + |
| + // If rename(2) fails, fall back to base::Move. |
| + if (rename(full_path_.value().c_str(), new_path.value().c_str())) { |
| + if (!base::Move(full_path_, new_path)) |
| + return LogSystemError("Move", errno); |
| } |
| + |
| + // If |base::Move| had to copy the file (e.g. because the source is on a |
| + // different volume than |new_path|, we must re-set the mode. This is |
| + // racy but may be the best we can do. |
| + // |
| + // On Windows file systems (FAT, NTFS), chmod fails. This is OK. |
| + if (chmod(new_path.value().c_str(), mode)) |
| + (void) LogSystemError("chmod", errno); |
| + |
| return DOWNLOAD_INTERRUPT_REASON_NONE; |
| } |