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 API to generate unique file names. |
gab
2017/04/06 19:09:37
s/GetTempFileName API/::GetTempFileName()/
chengx
2017/04/06 21:00:30
Done.
| |
345 // "Due to the algorithm used to generate file names, GetTempFileName Windows | |
346 // API can perform poorly when creating a large number of files with the same | |
gab
2017/04/06 19:09:37
Since this is a quote, remove "Windows API" IMO
chengx
2017/04/06 21:00:30
Done.
| |
347 // prefix. In such cases, it is recommended that you construct unique file | |
348 // names based on GUIDs." | |
349 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85). aspx | |
350 FilePath::StringType temp_file_name = | |
351 ASCIIToUTF16(base::GenerateGUID() + ".tmp"); | |
gab
2017/04/06 19:09:37
Add L".tmp" outside ASCIIToUTF16(base::GenerateGUI
chengx
2017/04/06 21:00:30
Done.
| |
344 | 352 |
345 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { | 353 FilePath temp_name = dir.Append(temp_file_name); |
gab
2017/04/06 19:09:37
inline |temp_file_name| in here
chengx
2017/04/06 21:00:30
Done.
| |
354 File file(temp_name, File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE); | |
355 | |
356 if (!file.IsValid()) { | |
346 DPLOG(WARNING) << "Failed to get temporary file name in " | 357 DPLOG(WARNING) << "Failed to get temporary file name in " |
347 << UTF16ToUTF8(dir.value()); | 358 << UTF16ToUTF8(dir.value()); |
348 return false; | 359 return false; |
349 } | 360 } |
361 file.Close(); | |
350 | 362 |
351 wchar_t long_temp_name[MAX_PATH + 1]; | 363 wchar_t long_temp_name[MAX_PATH + 1]; |
352 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); | 364 DWORD long_name_len = |
365 GetLongPathName(temp_name.value().c_str(), long_temp_name, MAX_PATH); | |
353 if (long_name_len > MAX_PATH || long_name_len == 0) { | 366 if (long_name_len > MAX_PATH || long_name_len == 0) { |
354 // GetLongPathName() failed, but we still have a temporary file. | 367 // GetLongPathName() failed, but we still have a temporary file. |
355 *temp_file = FilePath(temp_name); | 368 *temp_file = temp_name; |
gab
2017/04/06 19:09:36
std::move(temp_name)
chengx
2017/04/06 21:00:30
Done.
| |
356 return true; | 369 return true; |
357 } | 370 } |
358 | 371 |
359 FilePath::StringType long_temp_name_str; | 372 FilePath::StringType long_temp_name_str; |
360 long_temp_name_str.assign(long_temp_name, long_name_len); | 373 long_temp_name_str.assign(long_temp_name, long_name_len); |
361 *temp_file = FilePath(long_temp_name_str); | 374 *temp_file = FilePath(long_temp_name_str); |
gab
2017/04/06 19:09:37
std::move(long_temp_name_str)
chengx
2017/04/06 21:00:30
Done.
| |
362 return true; | 375 return true; |
363 } | 376 } |
364 | 377 |
365 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 378 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
366 const FilePath::StringType& prefix, | 379 const FilePath::StringType& prefix, |
367 FilePath* new_dir) { | 380 FilePath* new_dir) { |
368 ThreadRestrictions::AssertIOAllowed(); | 381 ThreadRestrictions::AssertIOAllowed(); |
369 | 382 |
370 FilePath path_to_create; | 383 FilePath path_to_create; |
371 | 384 |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
842 // Like Move, this function is not transactional, so we just | 855 // Like Move, this function is not transactional, so we just |
843 // leave the copied bits behind if deleting from_path fails. | 856 // leave the copied bits behind if deleting from_path fails. |
844 // If to_path exists previously then we have already overwritten | 857 // 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. | 858 // it by now, we don't get better off by deleting the new bits. |
846 } | 859 } |
847 return false; | 860 return false; |
848 } | 861 } |
849 | 862 |
850 } // namespace internal | 863 } // namespace internal |
851 } // namespace base | 864 } // namespace base |
OLD | NEW |