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

Side by Side Diff: base/files/file_util_win.cc

Issue 2788483005: Use GUID to generate unique temp file names and retire GetTempFileName (Closed)
Patch Set: Address comments from gab@ 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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() to generate unique file names.
345 // "Due to the algorithm used to generate file names, GetTempFileName can
346 // perform poorly when creating a large number of files with the same prefix.
347 // In such cases, it is recommended that you construct unique file names based
348 // on GUIDs."
349 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85). aspx
350 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.
344 351
345 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { 352 File file(temp_name, File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE);
353 if (!file.IsValid()) {
346 DPLOG(WARNING) << "Failed to get temporary file name in " 354 DPLOG(WARNING) << "Failed to get temporary file name in "
347 << UTF16ToUTF8(dir.value()); 355 << UTF16ToUTF8(dir.value());
348 return false; 356 return false;
349 } 357 }
358 file.Close();
350 359
351 wchar_t long_temp_name[MAX_PATH + 1]; 360 wchar_t long_temp_name[MAX_PATH + 1];
352 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); 361 DWORD long_name_len =
362 GetLongPathName(temp_name.value().c_str(), long_temp_name, MAX_PATH);
353 if (long_name_len > MAX_PATH || long_name_len == 0) { 363 if (long_name_len > MAX_PATH || long_name_len == 0) {
354 // GetLongPathName() failed, but we still have a temporary file. 364 // GetLongPathName() failed, but we still have a temporary file.
355 *temp_file = FilePath(temp_name); 365 *temp_file = std::move(temp_name);
356 return true; 366 return true;
357 } 367 }
358 368
359 FilePath::StringType long_temp_name_str; 369 FilePath::StringType long_temp_name_str;
360 long_temp_name_str.assign(long_temp_name, long_name_len); 370 long_temp_name_str.assign(long_temp_name, long_name_len);
361 *temp_file = FilePath(long_temp_name_str); 371 *temp_file = FilePath(std::move(long_temp_name_str));
362 return true; 372 return true;
363 } 373 }
364 374
365 bool CreateTemporaryDirInDir(const FilePath& base_dir, 375 bool CreateTemporaryDirInDir(const FilePath& base_dir,
366 const FilePath::StringType& prefix, 376 const FilePath::StringType& prefix,
367 FilePath* new_dir) { 377 FilePath* new_dir) {
368 ThreadRestrictions::AssertIOAllowed(); 378 ThreadRestrictions::AssertIOAllowed();
369 379
370 FilePath path_to_create; 380 FilePath path_to_create;
371 381
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 // Like Move, this function is not transactional, so we just 852 // Like Move, this function is not transactional, so we just
843 // leave the copied bits behind if deleting from_path fails. 853 // leave the copied bits behind if deleting from_path fails.
844 // If to_path exists previously then we have already overwritten 854 // 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. 855 // it by now, we don't get better off by deleting the new bits.
846 } 856 }
847 return false; 857 return false;
848 } 858 }
849 859
850 } // namespace internal 860 } // namespace internal
851 } // namespace base 861 } // namespace base
OLDNEW
« 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