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 "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <io.h> | 8 #include <io.h> |
| 9 #include <psapi.h> | 9 #include <psapi.h> |
| 10 #include <shellapi.h> | 10 #include <shellapi.h> |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 #include "base/win/scoped_handle.h" | 33 #include "base/win/scoped_handle.h" |
| 34 #include "base/win/windows_version.h" | 34 #include "base/win/windows_version.h" |
| 35 | 35 |
| 36 namespace base { | 36 namespace base { |
| 37 | 37 |
| 38 namespace { | 38 namespace { |
| 39 | 39 |
| 40 const DWORD kFileShareAll = | 40 const DWORD kFileShareAll = |
| 41 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 41 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
| 42 | 42 |
| 43 // An array consists of digits and alphabet. It is used to generate random | |
| 44 // prefix of a temp file name which is passed to GetTempFileName Windows API. | |
| 45 constexpr base::FilePath::CharType kAlphaNumberCharSet[] = | |
|
grt (UTC plus 2)
2017/04/04 12:00:43
nit: move these constants into the function as sta
chengx
2017/04/05 05:39:51
This constant has been removed in the new patch se
| |
| 46 FILE_PATH_LITERAL("0123456789abcdefghijklmnopqrstuvwxyz"); | |
| 47 | |
| 48 // The length of the temp file name prefix, which is used by GetTempFileName | |
| 49 // Windows API. | |
| 50 // GetTempFileName uses up to the first three characters of the input prefix | |
| 51 // string as the prefix of the file name. Therefore, this constant is set to 3. | |
| 52 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).as px | |
| 53 constexpr int kPrefixLength = 3; | |
| 54 | |
| 55 // Get a random prefix with length kPrefixLength of the temp file name. | |
| 56 // The characters are obtained from kAlphaNumberCharSet. | |
| 57 FilePath::StringType GetRandomPrefix() { | |
| 58 FilePath::StringType temp_file_name_prefix(kPrefixLength, 0); | |
| 59 | |
| 60 for (size_t i = 0; i < kPrefixLength; i++) { | |
|
grt (UTC plus 2)
2017/04/04 12:00:43
nit: prefer pre-increment; see style guide.
chengx
2017/04/05 05:39:51
This for-loop has been removed in the new patch se
| |
| 61 temp_file_name_prefix[i] = kAlphaNumberCharSet[base::RandGenerator( | |
| 62 arraysize(kAlphaNumberCharSet) - 1)]; | |
| 63 } | |
| 64 | |
| 65 return temp_file_name_prefix; | |
| 66 } | |
| 67 | |
| 43 // Deletes all files and directories in a path. | 68 // Deletes all files and directories in a path. |
| 44 // Returns false on the first failure it encounters. | 69 // Returns false on the first failure it encounters. |
| 45 bool DeleteFileRecursive(const FilePath& path, | 70 bool DeleteFileRecursive(const FilePath& path, |
| 46 const FilePath::StringType& pattern, | 71 const FilePath::StringType& pattern, |
| 47 bool recursive) { | 72 bool recursive) { |
| 48 FileEnumerator traversal(path, false, | 73 FileEnumerator traversal(path, false, |
| 49 FileEnumerator::FILES | FileEnumerator::DIRECTORIES, | 74 FileEnumerator::FILES | FileEnumerator::DIRECTORIES, |
| 50 pattern); | 75 pattern); |
| 51 for (FilePath current = traversal.Next(); !current.empty(); | 76 for (FilePath current = traversal.Next(); !current.empty(); |
| 52 current = traversal.Next()) { | 77 current = traversal.Next()) { |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 // it replaces \n's with \r\n's, which may surprise you. | 360 // it replaces \n's with \r\n's, which may surprise you. |
| 336 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx | 361 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx |
| 337 return OpenFile(*path, "wb+"); | 362 return OpenFile(*path, "wb+"); |
| 338 } | 363 } |
| 339 | 364 |
| 340 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 365 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
| 341 ThreadRestrictions::AssertIOAllowed(); | 366 ThreadRestrictions::AssertIOAllowed(); |
| 342 | 367 |
| 343 wchar_t temp_name[MAX_PATH + 1]; | 368 wchar_t temp_name[MAX_PATH + 1]; |
| 344 | 369 |
| 345 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { | 370 // Supplying a random prefix to GetTempFileName can significantly boost the |
| 371 // chance of successfully creating a new unique name with one shot. | |
| 372 if (!GetTempFileName(dir.value().c_str(), GetRandomPrefix().c_str(), 0, | |
| 373 temp_name)) { | |
| 346 DPLOG(WARNING) << "Failed to get temporary file name in " | 374 DPLOG(WARNING) << "Failed to get temporary file name in " |
| 347 << UTF16ToUTF8(dir.value()); | 375 << UTF16ToUTF8(dir.value()); |
| 348 return false; | 376 return false; |
| 349 } | 377 } |
| 350 | 378 |
| 351 wchar_t long_temp_name[MAX_PATH + 1]; | 379 wchar_t long_temp_name[MAX_PATH + 1]; |
| 352 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); | 380 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); |
| 353 if (long_name_len > MAX_PATH || long_name_len == 0) { | 381 if (long_name_len > MAX_PATH || long_name_len == 0) { |
| 354 // GetLongPathName() failed, but we still have a temporary file. | 382 // GetLongPathName() failed, but we still have a temporary file. |
| 355 *temp_file = FilePath(temp_name); | 383 *temp_file = FilePath(temp_name); |
| (...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 842 // Like Move, this function is not transactional, so we just | 870 // Like Move, this function is not transactional, so we just |
| 843 // leave the copied bits behind if deleting from_path fails. | 871 // leave the copied bits behind if deleting from_path fails. |
| 844 // If to_path exists previously then we have already overwritten | 872 // If to_path exists previously then we have already overwritten |
| 845 // it by now, we don't get better off by deleting the new bits. | 873 // it by now, we don't get better off by deleting the new bits. |
| 846 } | 874 } |
| 847 return false; | 875 return false; |
| 848 } | 876 } |
| 849 | 877 |
| 850 } // namespace internal | 878 } // namespace internal |
| 851 } // namespace base | 879 } // namespace base |
| OLD | NEW |