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> |
11 #include <shlobj.h> | 11 #include <shlobj.h> |
12 #include <stddef.h> | 12 #include <stddef.h> |
13 #include <stdint.h> | 13 #include <stdint.h> |
14 #include <time.h> | 14 #include <time.h> |
15 #include <winsock2.h> | 15 #include <winsock2.h> |
16 | 16 |
17 #include <algorithm> | 17 #include <algorithm> |
18 #include <limits> | 18 #include <limits> |
19 #include <string> | 19 #include <string> |
20 | 20 |
21 #include "base/files/file_enumerator.h" | 21 #include "base/files/file_enumerator.h" |
22 #include "base/files/file_path.h" | 22 #include "base/files/file_path.h" |
23 #include "base/guid.h" | |
23 #include "base/logging.h" | 24 #include "base/logging.h" |
24 #include "base/macros.h" | 25 #include "base/macros.h" |
25 #include "base/metrics/histogram.h" | 26 #include "base/metrics/histogram.h" |
26 #include "base/process/process_handle.h" | 27 #include "base/process/process_handle.h" |
27 #include "base/rand_util.h" | 28 #include "base/rand_util.h" |
28 #include "base/strings/string_number_conversions.h" | 29 #include "base/strings/string_number_conversions.h" |
29 #include "base/strings/string_util.h" | 30 #include "base/strings/string_util.h" |
30 #include "base/strings/utf_string_conversions.h" | 31 #include "base/strings/utf_string_conversions.h" |
31 #include "base/threading/thread_restrictions.h" | 32 #include "base/threading/thread_restrictions.h" |
32 #include "base/time/time.h" | 33 #include "base/time/time.h" |
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 } | 334 } |
334 // Open file in binary mode, to avoid problems with fwrite. On Windows | 335 // Open file in binary mode, to avoid problems with fwrite. On Windows |
335 // it replaces \n's with \r\n's, which may surprise you. | 336 // 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 | 337 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx |
337 return OpenFile(*path, "wb+"); | 338 return OpenFile(*path, "wb+"); |
338 } | 339 } |
339 | 340 |
340 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { | 341 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
341 ThreadRestrictions::AssertIOAllowed(); | 342 ThreadRestrictions::AssertIOAllowed(); |
342 | 343 |
343 wchar_t temp_name[MAX_PATH + 1]; | 344 // Use GUID instead of ::GetTempFileName() to generate unique file names. |
345 // "Due to the algorithm used to generate file names, GetTempFileName can | |
346 // perform poorly when creating a large number of files with the same prefix. | |
347 // In such cases, it is recommended that you construct unique file names based | |
348 // on GUIDs." | |
349 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85). aspx | |
344 | 350 |
345 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { | 351 FilePath temp_name; |
352 bool create_file_success = false; | |
353 | |
354 // Although it is nearly impossible to get a duplicate name with GUID, we | |
355 // still use a loop here in case it happens. | |
356 for (int i = 0; i < 100; ++i) { | |
357 temp_name = dir.Append(ASCIIToUTF16(base::GenerateGUID()) + L".tmp"); | |
358 File file(temp_name, | |
359 File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE); | |
360 if (file.IsValid()) { | |
361 file.Close(); | |
362 create_file_success = true; | |
363 break; | |
364 } | |
365 } | |
366 | |
367 // Exist early if we can't create an unique name. | |
gab
2017/04/07 17:27:15
Remove this comment, it merely states what the sel
chengx
2017/04/07 18:13:04
Sure, will remove it in another CL.
| |
368 if (!create_file_success) { | |
346 DPLOG(WARNING) << "Failed to get temporary file name in " | 369 DPLOG(WARNING) << "Failed to get temporary file name in " |
347 << UTF16ToUTF8(dir.value()); | 370 << UTF16ToUTF8(dir.value()); |
348 return false; | 371 return false; |
349 } | 372 } |
350 | 373 |
351 wchar_t long_temp_name[MAX_PATH + 1]; | 374 wchar_t long_temp_name[MAX_PATH + 1]; |
352 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); | 375 DWORD long_name_len = |
376 GetLongPathName(temp_name.value().c_str(), long_temp_name, MAX_PATH); | |
353 if (long_name_len > MAX_PATH || long_name_len == 0) { | 377 if (long_name_len > MAX_PATH || long_name_len == 0) { |
354 // GetLongPathName() failed, but we still have a temporary file. | 378 // GetLongPathName() failed, but we still have a temporary file. |
355 *temp_file = FilePath(temp_name); | 379 *temp_file = std::move(temp_name); |
356 return true; | 380 return true; |
357 } | 381 } |
358 | 382 |
359 FilePath::StringType long_temp_name_str; | 383 FilePath::StringType long_temp_name_str; |
360 long_temp_name_str.assign(long_temp_name, long_name_len); | 384 long_temp_name_str.assign(long_temp_name, long_name_len); |
361 *temp_file = FilePath(long_temp_name_str); | 385 *temp_file = FilePath(std::move(long_temp_name_str)); |
362 return true; | 386 return true; |
363 } | 387 } |
364 | 388 |
365 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 389 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
366 const FilePath::StringType& prefix, | 390 const FilePath::StringType& prefix, |
367 FilePath* new_dir) { | 391 FilePath* new_dir) { |
368 ThreadRestrictions::AssertIOAllowed(); | 392 ThreadRestrictions::AssertIOAllowed(); |
369 | 393 |
370 FilePath path_to_create; | 394 FilePath path_to_create; |
371 | 395 |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
842 // Like Move, this function is not transactional, so we just | 866 // Like Move, this function is not transactional, so we just |
843 // leave the copied bits behind if deleting from_path fails. | 867 // leave the copied bits behind if deleting from_path fails. |
844 // If to_path exists previously then we have already overwritten | 868 // 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. | 869 // it by now, we don't get better off by deleting the new bits. |
846 } | 870 } |
847 return false; | 871 return false; |
848 } | 872 } |
849 | 873 |
850 } // namespace internal | 874 } // namespace internal |
851 } // namespace base | 875 } // namespace base |
OLD | NEW |