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