OLD | NEW |
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/file_util.h" | 5 #include "base/file_util.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <psapi.h> | 8 #include <psapi.h> |
9 #include <shellapi.h> | 9 #include <shellapi.h> |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
11 #include <time.h> | 11 #include <time.h> |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <limits> | 14 #include <limits> |
15 #include <string> | 15 #include <string> |
16 | 16 |
17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
20 #include "base/process_util.h" | 20 #include "base/process_util.h" |
| 21 #include "base/rand_util.h" |
21 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
22 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
23 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
24 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
25 #include "base/time.h" | 26 #include "base/time.h" |
26 #include "base/win/scoped_handle.h" | 27 #include "base/win/scoped_handle.h" |
27 #include "base/win/windows_version.h" | 28 #include "base/win/windows_version.h" |
28 | 29 |
29 using base::FilePath; | 30 using base::FilePath; |
30 | 31 |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 *temp_file = FilePath(long_temp_name_str); | 365 *temp_file = FilePath(long_temp_name_str); |
365 return true; | 366 return true; |
366 } | 367 } |
367 | 368 |
368 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 369 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
369 const FilePath::StringType& prefix, | 370 const FilePath::StringType& prefix, |
370 FilePath* new_dir) { | 371 FilePath* new_dir) { |
371 base::ThreadRestrictions::AssertIOAllowed(); | 372 base::ThreadRestrictions::AssertIOAllowed(); |
372 | 373 |
373 FilePath path_to_create; | 374 FilePath path_to_create; |
374 srand(static_cast<uint32>(time(NULL))); | |
375 | 375 |
376 for (int count = 0; count < 50; ++count) { | 376 for (int count = 0; count < 50; ++count) { |
377 // Try create a new temporary directory with random generated name. If | 377 // Try create a new temporary directory with random generated name. If |
378 // the one exists, keep trying another path name until we reach some limit. | 378 // the one exists, keep trying another path name until we reach some limit. |
379 string16 new_dir_name; | 379 string16 new_dir_name; |
380 new_dir_name.assign(prefix); | 380 new_dir_name.assign(prefix); |
381 new_dir_name.append(base::IntToString16(::base::GetCurrentProcId())); | 381 new_dir_name.append(base::IntToString16(::base::GetCurrentProcId())); |
382 new_dir_name.push_back('_'); | 382 new_dir_name.push_back('_'); |
383 new_dir_name.append(base::IntToString16(rand() % kint16max)); | 383 new_dir_name.append(base::IntToString16(base::RandInt(0, kint16max))); |
384 | 384 |
385 path_to_create = base_dir.Append(new_dir_name); | 385 path_to_create = base_dir.Append(new_dir_name); |
386 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) { | 386 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) { |
387 *new_dir = path_to_create; | 387 *new_dir = path_to_create; |
388 return true; | 388 return true; |
389 } | 389 } |
390 } | 390 } |
391 | 391 |
392 return false; | 392 return false; |
393 } | 393 } |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 | 731 |
732 // Length of |path| with path separator appended. | 732 // Length of |path| with path separator appended. |
733 size_t prefix = path.StripTrailingSeparators().value().size() + 1; | 733 size_t prefix = path.StripTrailingSeparators().value().size() + 1; |
734 // The whole path string must be shorter than MAX_PATH. That is, it must be | 734 // The whole path string must be shorter than MAX_PATH. That is, it must be |
735 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). | 735 // prefix + component_length < MAX_PATH (or equivalently, <= MAX_PATH - 1). |
736 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); | 736 int whole_path_limit = std::max(0, MAX_PATH - 1 - static_cast<int>(prefix)); |
737 return std::min(whole_path_limit, static_cast<int>(max_length)); | 737 return std::min(whole_path_limit, static_cast<int>(max_length)); |
738 } | 738 } |
739 | 739 |
740 } // namespace file_util | 740 } // namespace file_util |
OLD | NEW |