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

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. 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>
(...skipping 22 matching lines...) Expand all
33 #include "base/win/scoped_handle.h" 33 #include "base/win/scoped_handle.h"
34 #include "base/win/windows_version.h" 34 #include "base/win/windows_version.h"
35 35
36 namespace base { 36 namespace base {
37 37
38 namespace { 38 namespace {
39 39
40 const DWORD kFileShareAll = 40 const DWORD kFileShareAll =
41 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; 41 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
42 42
43 // An array consists of digits and alphabet. It is used to generate random
44 // prefix of a temp file name which is passed to GetTempFileName Windows API.
45 constexpr base::FilePath::CharType kAlphaNumberCharSet[] =
46 FILE_PATH_LITERAL("0123456789abcdefghijklmnopqrstuvwxyz");
47
48 // The length of the temp file name prefix, which is used by GetTempFileName
49 // Windows API.
50 // GetTempFileName uses up to the first three characters of the input
51 // prefix string as the prefix of the file name. Therefore, this constant is
52 // set to three.
53 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85).as px
54 constexpr int kPrefixLength = 3;
55
56 // Get a random prefix with length kPrefixLength of the temp file name.
57 // The characters are obtained from kAlphaNumberCharSet.
58 FilePath::StringType GetRandomPrefix() {
59 FilePath::StringType temp_file_name_prefix;
grt (UTC plus 2) 2017/04/03 21:11:15 always prefer constructing to the desired size ove
chengx 2017/04/04 01:25:54 Done.
60 temp_file_name_prefix.resize(kPrefixLength);
61
62 for (size_t i = 0; i < kPrefixLength; i++)
grt (UTC plus 2) 2017/04/03 21:11:15 nit: add braces. alternatively, use std::generate
chengx 2017/04/04 01:25:54 Done.
63 temp_file_name_prefix[i] = kAlphaNumberCharSet[base::RandGenerator(
64 arraysize(kAlphaNumberCharSet))];
grt (UTC plus 2) 2017/04/03 21:11:15 arraysize - 1 to omit the trailing string terminat
chengx 2017/04/04 01:25:54 Done.
65
66 return temp_file_name_prefix;
67 }
68
43 // Deletes all files and directories in a path. 69 // Deletes all files and directories in a path.
44 // Returns false on the first failure it encounters. 70 // Returns false on the first failure it encounters.
45 bool DeleteFileRecursive(const FilePath& path, 71 bool DeleteFileRecursive(const FilePath& path,
46 const FilePath::StringType& pattern, 72 const FilePath::StringType& pattern,
47 bool recursive) { 73 bool recursive) {
48 FileEnumerator traversal(path, false, 74 FileEnumerator traversal(path, false,
49 FileEnumerator::FILES | FileEnumerator::DIRECTORIES, 75 FileEnumerator::FILES | FileEnumerator::DIRECTORIES,
50 pattern); 76 pattern);
51 for (FilePath current = traversal.Next(); !current.empty(); 77 for (FilePath current = traversal.Next(); !current.empty();
52 current = traversal.Next()) { 78 current = traversal.Next()) {
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 // it replaces \n's with \r\n's, which may surprise you. 361 // 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 362 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx
337 return OpenFile(*path, "wb+"); 363 return OpenFile(*path, "wb+");
338 } 364 }
339 365
340 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) { 366 bool CreateTemporaryFileInDir(const FilePath& dir, FilePath* temp_file) {
341 ThreadRestrictions::AssertIOAllowed(); 367 ThreadRestrictions::AssertIOAllowed();
342 368
343 wchar_t temp_name[MAX_PATH + 1]; 369 wchar_t temp_name[MAX_PATH + 1];
344 370
345 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { 371 // Due to the algorithm used to generate file names, GetTempFileName Windows
372 // API can perform poorly when creating a large number of files with the same
373 // prefix. Therefore, making the prefix random should boost its performance.
grt (UTC plus 2) 2017/04/03 21:11:15 re: "should": have you verified that filling a dir
chengx 2017/04/04 01:25:54 I should remove "should" probably. The main issue
374 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364991(v=vs.85). aspx
375
376 if (!GetTempFileName(dir.value().c_str(), GetRandomPrefix().c_str(), 0,
377 temp_name)) {
346 DPLOG(WARNING) << "Failed to get temporary file name in " 378 DPLOG(WARNING) << "Failed to get temporary file name in "
347 << UTF16ToUTF8(dir.value()); 379 << UTF16ToUTF8(dir.value());
348 return false; 380 return false;
349 } 381 }
350 382
351 wchar_t long_temp_name[MAX_PATH + 1]; 383 wchar_t long_temp_name[MAX_PATH + 1];
352 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH); 384 DWORD long_name_len = GetLongPathName(temp_name, long_temp_name, MAX_PATH);
353 if (long_name_len > MAX_PATH || long_name_len == 0) { 385 if (long_name_len > MAX_PATH || long_name_len == 0) {
354 // GetLongPathName() failed, but we still have a temporary file. 386 // GetLongPathName() failed, but we still have a temporary file.
355 *temp_file = FilePath(temp_name); 387 *temp_file = FilePath(temp_name);
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
842 // Like Move, this function is not transactional, so we just 874 // Like Move, this function is not transactional, so we just
843 // leave the copied bits behind if deleting from_path fails. 875 // leave the copied bits behind if deleting from_path fails.
844 // If to_path exists previously then we have already overwritten 876 // 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. 877 // it by now, we don't get better off by deleting the new bits.
846 } 878 }
847 return false; 879 return false;
848 } 880 }
849 881
850 } // namespace internal 882 } // namespace internal
851 } // namespace base 883 } // 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