Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/download/base_file.h" | 5 #include "content/browser/download/base_file.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "content/public/browser/download_interrupt_reasons.h" | 8 #include "content/public/browser/download_interrupt_reasons.h" |
| 9 | 9 |
| 10 namespace content { | 10 namespace content { |
| 11 | 11 |
| 12 DownloadInterruptReason BaseFile::MoveFileAndAdjustPermissions( | 12 DownloadInterruptReason BaseFile::MoveFileAndAdjustPermissions( |
| 13 const base::FilePath& new_path) { | 13 const base::FilePath& new_path) { |
| 14 // Similarly, on Unix, we're moving a temp file created with permissions 600 | 14 // Move a temporary file created with mode 0600 to |new_path|. If |
| 15 // to |new_path|. Here, we try to fix up the destination file with appropriate | 15 // |new_path| does not already exist, create it. The kernel will apply the |
| 16 // permissions. | 16 // user's umask to the mode 0666. |
| 17 struct stat st; | 17 mode_t mode = 0600; |
| 18 // First check the file existence and create an empty file if it doesn't | |
| 19 // exist. | |
| 20 if (!base::PathExists(new_path)) { | 18 if (!base::PathExists(new_path)) { |
| 21 int write_error = base::WriteFile(new_path, "", 0); | 19 struct stat status; |
| 22 if (write_error < 0) | 20 if (!base::WriteFileWithMode(new_path, "", 0, 0666) || |
|
asanka
2014/10/24 18:15:46
I misspoke in my earlier comment regarding using 0
palmer
2014/10/24 18:35:35
The bug this CL fixes is to respect the user's uma
asanka
2014/10/28 21:24:20
Acknowledged.
| |
| 21 stat(new_path.value().c_str(), &status)) { | |
| 23 return LogSystemError("WriteFile", errno); | 22 return LogSystemError("WriteFile", errno); |
| 23 } | |
| 24 mode = status.st_mode & 0777; | |
|
asanka
2014/10/24 18:15:46
If a file exists at |new_path| we should use the m
palmer
2014/10/24 18:35:35
I think that would surprise people.
asanka
2014/10/28 21:24:20
Would it? Let's say someone is downloading foo.txt
palmer
2014/10/29 01:12:38
Ahh, yes. I see what you mean now. You are right.
| |
| 24 } | 25 } |
| 25 int stat_error = stat(new_path.value().c_str(), &st); | |
| 26 bool stat_succeeded = (stat_error == 0); | |
| 27 if (!stat_succeeded) | |
| 28 LogSystemError("stat", errno); | |
| 29 | 26 |
| 30 if (!base::Move(full_path_, new_path)) | 27 // If rename(2) fails, fall back to base::Move. |
| 31 return LogSystemError("Move", errno); | 28 if (rename(full_path_.value().c_str(), new_path.value().c_str())) { |
| 29 if (!base::Move(full_path_, new_path)) | |
| 30 return LogSystemError("Move", errno); | |
| 31 } | |
| 32 | 32 |
| 33 if (stat_succeeded) { | 33 // If |base::Move| had to copy the file (e.g. because the source is on a |
| 34 // On Windows file systems (FAT, NTFS), chmod fails. This is OK. | 34 // different volume than |new_path|, we must re-set the mode. This is |
| 35 int chmod_error = chmod(new_path.value().c_str(), st.st_mode); | 35 // racy but may be the best we can do. |
| 36 if (chmod_error < 0) | 36 // |
| 37 LogSystemError("chmod", errno); | 37 // On Windows file systems (FAT, NTFS), chmod fails. This is OK. |
| 38 } | 38 if (chmod(new_path.value().c_str(), mode)) |
| 39 (void) LogSystemError("chmod", errno); | |
| 40 | |
| 39 return DOWNLOAD_INTERRUPT_REASON_NONE; | 41 return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 40 } | 42 } |
| 41 | 43 |
| 42 } // namespace content | 44 } // namespace content |
| OLD | NEW |