OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <propvarutil.h> | 8 #include <propvarutil.h> |
9 #include <psapi.h> | 9 #include <psapi.h> |
10 #include <shellapi.h> | 10 #include <shellapi.h> |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 *temp_file = FilePath(temp_file_str); | 554 *temp_file = FilePath(temp_file_str); |
555 return true; | 555 return true; |
556 } | 556 } |
557 | 557 |
558 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 558 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
559 const FilePath::StringType& prefix, | 559 const FilePath::StringType& prefix, |
560 FilePath* new_dir) { | 560 FilePath* new_dir) { |
561 FilePath path_to_create; | 561 FilePath path_to_create; |
562 srand(static_cast<uint32>(time(NULL))); | 562 srand(static_cast<uint32>(time(NULL))); |
563 | 563 |
564 int count = 0; | 564 for (int count = 0; count < 50; ++count) { |
565 while (count < 50) { | |
566 // Try create a new temporary directory with random generated name. If | 565 // Try create a new temporary directory with random generated name. If |
567 // the one exists, keep trying another path name until we reach some limit. | 566 // the one exists, keep trying another path name until we reach some limit. |
568 path_to_create = base_dir; | 567 path_to_create = base_dir; |
569 | 568 |
570 string16 new_dir_name; | 569 string16 new_dir_name; |
571 new_dir_name.assign(prefix); | 570 new_dir_name.assign(prefix); |
572 new_dir_name.append(base::IntToString16(rand() % kint16max)); | 571 new_dir_name.append(base::IntToString16(rand() % kint16max)); |
573 | 572 |
574 path_to_create = path_to_create.Append(new_dir_name); | 573 path_to_create = path_to_create.Append(new_dir_name); |
575 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) | 574 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) { |
576 break; | 575 *new_dir = path_to_create; |
577 count++; | 576 return true; |
| 577 } |
578 } | 578 } |
579 | 579 |
580 if (count == 50) { | 580 return false; |
581 return false; | |
582 } | |
583 | |
584 *new_dir = path_to_create; | |
585 return true; | |
586 } | 581 } |
587 | 582 |
588 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 583 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
589 FilePath* new_temp_path) { | 584 FilePath* new_temp_path) { |
590 FilePath system_temp_dir; | 585 FilePath system_temp_dir; |
591 if (!GetTempDir(&system_temp_dir)) | 586 if (!GetTempDir(&system_temp_dir)) |
592 return false; | 587 return false; |
593 | 588 |
594 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); | 589 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); |
595 } | 590 } |
596 | 591 |
597 bool CreateDirectory(const FilePath& full_path) { | 592 bool CreateDirectory(const FilePath& full_path) { |
598 // If the path exists, we've succeeded if it's a directory, failed otherwise. | 593 // If the path exists, we've succeeded if it's a directory, failed otherwise. |
599 const wchar_t* full_path_str = full_path.value().c_str(); | 594 const wchar_t* full_path_str = full_path.value().c_str(); |
600 DWORD fileattr = ::GetFileAttributes(full_path_str); | 595 DWORD fileattr = ::GetFileAttributes(full_path_str); |
601 if (fileattr != INVALID_FILE_ATTRIBUTES) { | 596 if (fileattr != INVALID_FILE_ATTRIBUTES) { |
602 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 597 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
603 DLOG(INFO) << "CreateDirectory(" << full_path_str << "), " | 598 DVLOG(1) << "CreateDirectory(" << full_path_str << "), " |
604 << "directory already exists."; | 599 << "directory already exists."; |
605 return true; | 600 return true; |
606 } else { | |
607 LOG(WARNING) << "CreateDirectory(" << full_path_str << "), " | |
608 << "conflicts with existing file."; | |
609 return false; | |
610 } | 601 } |
| 602 LOG(WARNING) << "CreateDirectory(" << full_path_str << "), " |
| 603 << "conflicts with existing file."; |
| 604 return false; |
611 } | 605 } |
612 | 606 |
613 // Invariant: Path does not exist as file or directory. | 607 // Invariant: Path does not exist as file or directory. |
614 | 608 |
615 // Attempt to create the parent recursively. This will immediately return | 609 // Attempt to create the parent recursively. This will immediately return |
616 // true if it already exists, otherwise will create all required parent | 610 // true if it already exists, otherwise will create all required parent |
617 // directories starting with the highest-level missing parent. | 611 // directories starting with the highest-level missing parent. |
618 FilePath parent_path(full_path.DirName()); | 612 FilePath parent_path(full_path.DirName()); |
619 if (parent_path.value() == full_path.value()) { | 613 if (parent_path.value() == full_path.value()) { |
620 return false; | 614 return false; |
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1061 uint8 unused = *(touch + offset); | 1055 uint8 unused = *(touch + offset); |
1062 offset += step_size; | 1056 offset += step_size; |
1063 } | 1057 } |
1064 FreeLibrary(dll_module); | 1058 FreeLibrary(dll_module); |
1065 } | 1059 } |
1066 | 1060 |
1067 return true; | 1061 return true; |
1068 } | 1062 } |
1069 | 1063 |
1070 } // namespace file_util | 1064 } // namespace file_util |
OLD | NEW |