| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 // Move to the next drive letter string, which starts one | 57 // Move to the next drive letter string, which starts one |
| 58 // increment after the '\0' that terminates the current string. | 58 // increment after the '\0' that terminates the current string. |
| 59 while(*drive_map_ptr++); | 59 while(*drive_map_ptr++); |
| 60 } | 60 } |
| 61 | 61 |
| 62 // No drive matched. The path does not start with a device junction. | 62 // No drive matched. The path does not start with a device junction. |
| 63 *drive_letter_path = device_path; | 63 *drive_letter_path = device_path; |
| 64 return true; | 64 return true; |
| 65 } | 65 } |
| 66 | 66 |
| 67 // Build a security descriptor with the weakest possible file permissions. | |
| 68 bool InitLooseSecurityDescriptor(SECURITY_ATTRIBUTES *sa, | |
| 69 SECURITY_DESCRIPTOR *sd) { | |
| 70 DWORD last_error; | |
| 71 | |
| 72 if (!InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION)) { | |
| 73 last_error = GetLastError(); | |
| 74 LOG(ERROR) << "InitializeSecurityDescriptor failed: GetLastError() = " | |
| 75 << last_error; | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 if (!SetSecurityDescriptorDacl(sd, | |
| 80 TRUE, // bDaclPresent: Add one to |sd|. | |
| 81 NULL, // pDacl: NULL means allow all access. | |
| 82 FALSE // bDaclDefaulted: Not defaulted. | |
| 83 )) { | |
| 84 last_error = GetLastError(); | |
| 85 LOG(ERROR) << "SetSecurityDescriptorDacl() failed: GetLastError() = " | |
| 86 << last_error; | |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 if (!SetSecurityDescriptorGroup(sd, | |
| 91 NULL, // pGroup: No no primary group. | |
| 92 FALSE // bGroupDefaulted: Not defaulted. | |
| 93 )) { | |
| 94 last_error = GetLastError(); | |
| 95 LOG(ERROR) << "SetSecurityDescriptorGroup() failed: GetLastError() = " | |
| 96 << last_error; | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 if (!SetSecurityDescriptorSacl(sd, | |
| 101 FALSE, // bSaclPresent: No SACL. | |
| 102 NULL, | |
| 103 FALSE | |
| 104 )) { | |
| 105 last_error = GetLastError(); | |
| 106 LOG(ERROR) << "SetSecurityDescriptorSacl() failed: GetLastError() = " | |
| 107 << last_error; | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 sa->nLength = sizeof(SECURITY_ATTRIBUTES); | |
| 112 sa->lpSecurityDescriptor = sd; | |
| 113 sa->bInheritHandle = TRUE; | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 } // namespace | 67 } // namespace |
| 118 | 68 |
| 119 std::wstring GetDirectoryFromPath(const std::wstring& path) { | 69 std::wstring GetDirectoryFromPath(const std::wstring& path) { |
| 120 wchar_t path_buffer[MAX_PATH]; | 70 wchar_t path_buffer[MAX_PATH]; |
| 121 wchar_t* file_ptr = NULL; | 71 wchar_t* file_ptr = NULL; |
| 122 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) | 72 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) |
| 123 return L""; | 73 return L""; |
| 124 | 74 |
| 125 std::wstring::size_type length = | 75 std::wstring::size_type length = |
| 126 file_ptr ? file_ptr - path_buffer : path.length(); | 76 file_ptr ? file_ptr - path_buffer : path.length(); |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 } | 544 } |
| 595 | 545 |
| 596 std::wstring temp_file_str; | 546 std::wstring temp_file_str; |
| 597 temp_file_str.assign(temp_name, path_len); | 547 temp_file_str.assign(temp_name, path_len); |
| 598 *temp_file = FilePath(temp_file_str); | 548 *temp_file = FilePath(temp_file_str); |
| 599 return true; | 549 return true; |
| 600 } | 550 } |
| 601 | 551 |
| 602 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 552 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
| 603 const FilePath::StringType& prefix, | 553 const FilePath::StringType& prefix, |
| 604 bool loosen_permissions, | |
| 605 FilePath* new_dir) { | 554 FilePath* new_dir) { |
| 606 SECURITY_ATTRIBUTES sa; | |
| 607 SECURITY_DESCRIPTOR sd; | |
| 608 | |
| 609 LPSECURITY_ATTRIBUTES directory_security_attributes = NULL; | |
| 610 if (loosen_permissions) { | |
| 611 if (InitLooseSecurityDescriptor(&sa, &sd)) | |
| 612 directory_security_attributes = &sa; | |
| 613 else | |
| 614 LOG(ERROR) << "Failed to init security attributes, fall back to NULL."; | |
| 615 } | |
| 616 | |
| 617 FilePath path_to_create; | 555 FilePath path_to_create; |
| 618 srand(static_cast<uint32>(time(NULL))); | 556 srand(static_cast<uint32>(time(NULL))); |
| 619 | 557 |
| 620 int count = 0; | 558 int count = 0; |
| 621 while (count < 50) { | 559 while (count < 50) { |
| 622 // Try create a new temporary directory with random generated name. If | 560 // Try create a new temporary directory with random generated name. If |
| 623 // the one exists, keep trying another path name until we reach some limit. | 561 // the one exists, keep trying another path name until we reach some limit. |
| 624 path_to_create = base_dir; | 562 path_to_create = base_dir; |
| 625 | 563 |
| 626 std::wstring new_dir_name; | 564 std::wstring new_dir_name; |
| 627 new_dir_name.assign(prefix); | 565 new_dir_name.assign(prefix); |
| 628 new_dir_name.append(IntToWString(rand() % kint16max)); | 566 new_dir_name.append(IntToWString(rand() % kint16max)); |
| 629 | 567 |
| 630 path_to_create = path_to_create.Append(new_dir_name); | 568 path_to_create = path_to_create.Append(new_dir_name); |
| 631 if (::CreateDirectory(path_to_create.value().c_str(), | 569 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) |
| 632 directory_security_attributes)) | |
| 633 break; | 570 break; |
| 634 count++; | 571 count++; |
| 635 } | 572 } |
| 636 | 573 |
| 637 if (count == 50) { | 574 if (count == 50) { |
| 638 return false; | 575 return false; |
| 639 } | 576 } |
| 640 | 577 |
| 641 *new_dir = path_to_create; | 578 *new_dir = path_to_create; |
| 642 | |
| 643 return true; | 579 return true; |
| 644 } | 580 } |
| 645 | 581 |
| 646 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 582 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
| 647 FilePath* new_temp_path) { | 583 FilePath* new_temp_path) { |
| 648 FilePath system_temp_dir; | 584 FilePath system_temp_dir; |
| 649 if (!GetTempDir(&system_temp_dir)) | 585 if (!GetTempDir(&system_temp_dir)) |
| 650 return false; | 586 return false; |
| 651 | 587 |
| 652 return CreateTemporaryDirInDir(system_temp_dir, | 588 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); |
| 653 prefix, | |
| 654 false, | |
| 655 new_temp_path); | |
| 656 } | 589 } |
| 657 | 590 |
| 658 bool CreateDirectory(const FilePath& full_path) { | 591 bool CreateDirectory(const FilePath& full_path) { |
| 659 return file_util::CreateDirectoryExtraLogging(full_path, LOG(INFO)); | 592 return file_util::CreateDirectoryExtraLogging(full_path, LOG(INFO)); |
| 660 } | 593 } |
| 661 | 594 |
| 662 // TODO(skerner): Extra logging has been added to understand crbug/35198 . | 595 // TODO(skerner): Extra logging has been added to understand crbug/35198 . |
| 663 // Remove it once we get a log from a user who can reproduce the issue. | 596 // Remove it once we get a log from a user who can reproduce the issue. |
| 664 bool CreateDirectoryExtraLogging(const FilePath& full_path, | 597 bool CreateDirectoryExtraLogging(const FilePath& full_path, |
| 665 std::ostream& log) { | 598 std::ostream& log) { |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1119 size_t offset = 0; | 1052 size_t offset = 0; |
| 1120 while (offset < actual_size_to_read) { | 1053 while (offset < actual_size_to_read) { |
| 1121 uint8 unused = *(touch + offset); | 1054 uint8 unused = *(touch + offset); |
| 1122 offset += step_size; | 1055 offset += step_size; |
| 1123 } | 1056 } |
| 1124 FreeLibrary(dll_module); | 1057 FreeLibrary(dll_module); |
| 1125 return true; | 1058 return true; |
| 1126 } | 1059 } |
| 1127 | 1060 |
| 1128 } // namespace file_util | 1061 } // namespace file_util |
| OLD | NEW |