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

Unified Diff: base/files/file_util_win.cc

Issue 2788483005: Use GUID to generate unique temp file names and retire GetTempFileName (Closed)
Patch Set: Address early comments from grt@ 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..0e7b748aa61c731cabde41cf26247bfdc4fe91b8 100644
--- a/base/files/file_util_win.cc
+++ b/base/files/file_util_win.cc
@@ -40,6 +40,31 @@ namespace {
const DWORD kFileShareAll =
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
+// An array consists of digits and alphabet. It is used to generate random
+// prefix of a temp file name which is passed to GetTempFileName Windows API.
+constexpr base::FilePath::CharType kAlphaNumberCharSet[] =
grt (UTC plus 2) 2017/04/04 12:00:43 nit: move these constants into the function as sta
chengx 2017/04/05 05:39:51 This constant has been removed in the new patch se
+ FILE_PATH_LITERAL("0123456789abcdefghijklmnopqrstuvwxyz");
+
+// The length of the temp file name prefix, which is used by GetTempFileName
+// Windows API.
+// GetTempFileName uses up to the first three characters of the input prefix
+// string as the prefix of the file name. Therefore, this constant is set to 3.
+// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).aspx
+constexpr int kPrefixLength = 3;
+
+// Get a random prefix with length kPrefixLength of the temp file name.
+// The characters are obtained from kAlphaNumberCharSet.
+FilePath::StringType GetRandomPrefix() {
+ FilePath::StringType temp_file_name_prefix(kPrefixLength, 0);
+
+ for (size_t i = 0; i < kPrefixLength; i++) {
grt (UTC plus 2) 2017/04/04 12:00:43 nit: prefer pre-increment; see style guide.
chengx 2017/04/05 05:39:51 This for-loop has been removed in the new patch se
+ temp_file_name_prefix[i] = kAlphaNumberCharSet[base::RandGenerator(
+ arraysize(kAlphaNumberCharSet) - 1)];
+ }
+
+ return temp_file_name_prefix;
+}
+
// Deletes all files and directories in a path.
// Returns false on the first failure it encounters.
bool DeleteFileRecursive(const FilePath& path,
@@ -342,7 +367,10 @@ bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
wchar_t temp_name[MAX_PATH + 1];
- if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) {
+ // Supplying a random prefix to GetTempFileName can significantly boost the
+ // chance of successfully creating a new unique name with one shot.
+ if (!GetTempFileName(dir.value().c_str(), GetRandomPrefix().c_str(), 0,
+ temp_name)) {
DPLOG(WARNING) << "Failed to get temporary file name in "
<< UTF16ToUTF8(dir.value());
return false;
« 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