Chromium Code Reviews| Index: base/files/file_util_win.cc |
| diff --git a/base/files/file_util_win.cc b/base/files/file_util_win.cc |
| index 65dc5ce1e2fe7495624cc6d8c16b954395eb11ac..4894b64f7d6c16fd4c8263773706b894cceee4e9 100644 |
| --- a/base/files/file_util_win.cc |
| +++ b/base/files/file_util_win.cc |
| @@ -20,6 +20,7 @@ |
| #include "base/files/file_enumerator.h" |
| #include "base/files/file_path.h" |
| +#include "base/guid.h" |
| #include "base/logging.h" |
| #include "base/macros.h" |
| #include "base/metrics/histogram.h" |
| @@ -340,25 +341,34 @@ FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
| bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { |
| ThreadRestrictions::AssertIOAllowed(); |
| - wchar_t temp_name[MAX_PATH + 1]; |
| + // Use GUID instead of ::GetTempFileName() to generate unique file names. |
| + // "Due to the algorithm used to generate file names, GetTempFileName can |
| + // perform poorly when creating a large number of files with the same prefix. |
| + // In such cases, it is recommended that you construct unique file names based |
| + // on GUIDs." |
| + // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).aspx |
| + FilePath temp_name = dir.Append(ASCIIToUTF16(base::GenerateGUID()) + L".tmp"); |
|
gab
2017/04/06 20:19:12
Actually, we should probably loop this while it ex
chengx
2017/04/06 21:00:31
Acknowledged.
|
| - if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { |
| + File file(temp_name, File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE); |
| + if (!file.IsValid()) { |
| DPLOG(WARNING) << "Failed to get temporary file name in " |
| << UTF16ToUTF8(dir.value()); |
| return false; |
| } |
| + file.Close(); |
| wchar_t long_temp_name[MAX_PATH + 1]; |
| - DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); |
| + DWORD long_name_len = |
| + GetLongPathName(temp_name.value().c_str(), long_temp_name, MAX_PATH); |
| if (long_name_len > MAX_PATH || long_name_len == 0) { |
| // GetLongPathName() failed, but we still have a temporary file. |
| - *temp_file = FilePath(temp_name); |
| + *temp_file = std::move(temp_name); |
| return true; |
| } |
| FilePath::StringType long_temp_name_str; |
| long_temp_name_str.assign(long_temp_name, long_name_len); |
| - *temp_file = FilePath(long_temp_name_str); |
| + *temp_file = FilePath(std::move(long_temp_name_str)); |
| return true; |
| } |