Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(690)

Unified Diff: base/files/file_util_win.cc

Issue 2788483005: Use GUID to generate unique temp file names and retire GetTempFileName (Closed)
Patch Set: Add comments to the code, fix nits. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..43b0c65c2d4a86374436ad5545b76760b3f51384 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,19 +341,31 @@ 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 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.
+ // "Due to the algorithm used to generate file names, GetTempFileName Windows
+ // 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.
+ // 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::StringType temp_file_name =
+ 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.
- if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) {
+ 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.
+ 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 = temp_name;
gab 2017/04/06 19:09:36 std::move(temp_name)
chengx 2017/04/06 21:00:30 Done.
return true;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698