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 const char kAlphaNumberCharSet[] = | |
gab
2017/04/03 16:40:49
I think you'll need to use FILE_PATH_LITERAL to ge
gab
2017/04/03 16:40:49
constexpr
chengx
2017/04/03 18:35:10
Done.
chengx
2017/04/03 18:35:11
Done.
| |
46 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
gab
2017/04/03 16:40:49
Filenames on Windows are case insensitive so don't
chengx
2017/04/03 18:35:10
Done.
| |
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 | |
51 // prefix string as the prefix of the file name. Therefore, this constant is | |
52 // set to three. | |
53 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).as px | |
54 const int kPrefixLength = 3; | |
gab
2017/04/03 16:40:50
constexpr
chengx
2017/04/03 18:35:11
Done.
| |
55 | |
56 // Get a random prefix with length kPrefixLength of the temp file name. | |
57 // The characters are obtained from kAlphaNumberCharSet. | |
58 FilePath::StringType GetRandomPrefix() { | |
59 FilePath::StringType temp_file_name_prefix; | |
60 temp_file_name_prefix.resize(kPrefixLength); | |
61 | |
62 size_t char_set_length = strlen(kAlphaNumberCharSet); | |
gab
2017/04/03 16:40:50
inline arraysize(kAlphaNumberCharSet) instead (it'
chengx
2017/04/03 18:35:10
Done.
| |
63 | |
64 srand(time(NULL)); | |
65 for (size_t i = 0; i < kPrefixLength; i++) | |
66 temp_file_name_prefix[i] = kAlphaNumberCharSet[rand() % char_set_length]; | |
gab
2017/04/03 16:40:49
Use base::RandGenerator(arraysize(kAlphaNumberChar
chengx
2017/04/03 18:35:10
Yes, no need to seed here if using base::RandGener
| |
67 | |
68 return temp_file_name_prefix; | |
69 } | |
70 | |
43 // Deletes all files and directories in a path. | 71 // Deletes all files and directories in a path. |
44 // Returns false on the first failure it encounters. | 72 // Returns false on the first failure it encounters. |
45 bool DeleteFileRecursive(const FilePath& path, | 73 bool DeleteFileRecursive(const FilePath& path, |
46 const FilePath::StringType& pattern, | 74 const FilePath::StringType& pattern, |
47 bool recursive) { | 75 bool recursive) { |
48 FileEnumerator traversal(path, false, | 76 FileEnumerator traversal(path, false, |
49 FileEnumerator::FILES | FileEnumerator::DIRECTORIES, | 77 FileEnumerator::FILES | FileEnumerator::DIRECTORIES, |
50 pattern); | 78 pattern); |
51 for (FilePath current = traversal.Next(); !current.empty(); | 79 for (FilePath current = traversal.Next(); !current.empty(); |
52 current = traversal.Next()) { | 80 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. | 363 // 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 | 364 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx |
337 return OpenFile(*path, "wb+"); | 365 return OpenFile(*path, "wb+"); |
338 } | 366 } |
339 | 367 |
340 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 368 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
341 ThreadRestrictions::AssertIOAllowed(); | 369 ThreadRestrictions::AssertIOAllowed(); |
342 | 370 |
343 wchar_t temp_name[MAX_PATH + 1]; | 371 wchar_t temp_name[MAX_PATH + 1]; |
344 | 372 |
345 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { | 373 // Due to the algorithm used to generate file names, GetTempFileName Windows |
374 // API can perform poorly when creating a large number of files with the same | |
375 // prefix. Therefore, making the prefix random should boost its performance. | |
376 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85). aspx | |
377 | |
378 if (!GetTempFileName(dir.value().c_str(), GetRandomPrefix().c_str(), 0, | |
379 temp_name)) { | |
346 DPLOG(WARNING) << "Failed to get temporary file name in " | 380 DPLOG(WARNING) << "Failed to get temporary file name in " |
347 << UTF16ToUTF8(dir.value()); | 381 << UTF16ToUTF8(dir.value()); |
348 return false; | 382 return false; |
349 } | 383 } |
350 | 384 |
351 wchar_t long_temp_name[MAX_PATH + 1]; | 385 wchar_t long_temp_name[MAX_PATH + 1]; |
352 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); | 386 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); |
353 if (long_name_len > MAX_PATH || long_name_len == 0) { | 387 if (long_name_len > MAX_PATH || long_name_len == 0) { |
354 // GetLongPathName() failed, but we still have a temporary file. | 388 // GetLongPathName() failed, but we still have a temporary file. |
355 *temp_file = FilePath(temp_name); | 389 *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 | 876 // Like Move, this function is not transactional, so we just |
843 // leave the copied bits behind if deleting from_path fails. | 877 // leave the copied bits behind if deleting from_path fails. |
844 // If to_path exists previously then we have already overwritten | 878 // 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. | 879 // it by now, we don't get better off by deleting the new bits. |
846 } | 880 } |
847 return false; | 881 return false; |
848 } | 882 } |
849 | 883 |
850 } // namespace internal | 884 } // namespace internal |
851 } // namespace base | 885 } // namespace base |
OLD | NEW |