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

Unified Diff: base/files/file_util_win.cc

Issue 2788483005: Use GUID to generate unique temp file names and retire GetTempFileName (Closed)
Patch Set: Using GUID to generate new temp file names. 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..f741db06e41902d632f4063b0dcea478d23d3fe7 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,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];
-
- if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) {
+ FilePath::StringType temp_name = UTF8ToUTF16(base::GenerateGUID() + ".tmp");
grt (UTC plus 2) 2017/04/05 11:26:34 ASCIIToUTF16 since you know the GUID is ASCII
chengx 2017/04/05 18:22:34 Done.
+ FilePath temp_path_name = dir.Append(temp_name);
+ HANDLE file_handle =
grt (UTC plus 2) 2017/04/05 11:26:34 either use base::win::ScopedHandle or base::File.
chengx 2017/04/05 18:22:34 Done. Changed to base::File.
+ ::CreateFile(temp_path_name.value().c_str(), GENERIC_READ | GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
grt (UTC plus 2) 2017/04/05 11:26:34 regarding CREATE_ALWAYS: we have no guarantee that
grt (UTC plus 2) 2017/04/05 11:26:34 nit: NULL -> nullptr
chengx 2017/04/05 18:22:34 Done. I know nullptr is preferred. However, "NULL
chengx 2017/04/05 18:22:34 I agree. Changed CREATE_ALWAYS to CREATE_NEW.
+ FILE_ATTRIBUTE_NORMAL, NULL);
+ if (file_handle == INVALID_HANDLE_VALUE) {
DPLOG(WARNING) << "Failed to get temporary file name in "
<< UTF16ToUTF8(dir.value());
return false;
}
+ ::CloseHandle(file_handle);
- wchar_t long_temp_name[MAX_PATH + 1];
- DWORD long_name_len = GetLongPathName(temp_name, 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);
+ wchar_t full_path_name[MAX_PATH + 1];
+ DWORD full_path_name_len =
+ GetLongPathName(temp_path_name.value().c_str(), full_path_name, MAX_PATH);
grt (UTC plus 2) 2017/04/05 11:26:34 could you dig through the blamelist to figure out
chengx 2017/04/05 18:22:34 I had the same doubts here too. So I made some cha
grt (UTC plus 2) 2017/04/06 10:35:02 Yes, GetLongPathName will fail if the user doesn't
+ if (full_path_name_len > MAX_PATH || full_path_name_len == 0) {
+ // GetFullPathName() failed, but we still have a temporary file.
+ *temp_file = temp_path_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);
+ FilePath::StringType full_path_name_str;
+ full_path_name_str.assign(full_path_name, full_path_name_len);
+ *temp_file = FilePath(full_path_name_str);
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