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 | |
116 } // namespace | 66 } // namespace |
117 | 67 |
118 std::wstring GetDirectoryFromPath(const std::wstring& path) { | 68 std::wstring GetDirectoryFromPath(const std::wstring& path) { |
119 wchar_t path_buffer[MAX_PATH]; | 69 wchar_t path_buffer[MAX_PATH]; |
120 wchar_t* file_ptr = NULL; | 70 wchar_t* file_ptr = NULL; |
121 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) | 71 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) |
122 return L""; | 72 return L""; |
123 | 73 |
124 std::wstring::size_type length = | 74 std::wstring::size_type length = |
125 file_ptr ? file_ptr - path_buffer : path.length(); | 75 file_ptr ? file_ptr - path_buffer : path.length(); |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 } | 543 } |
594 | 544 |
595 std::wstring temp_file_str; | 545 std::wstring temp_file_str; |
596 temp_file_str.assign(temp_name, path_len); | 546 temp_file_str.assign(temp_name, path_len); |
597 *temp_file = FilePath(temp_file_str); | 547 *temp_file = FilePath(temp_file_str); |
598 return true; | 548 return true; |
599 } | 549 } |
600 | 550 |
601 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 551 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
602 const FilePath::StringType& prefix, | 552 const FilePath::StringType& prefix, |
603 FilePath* new_dir, | 553 FilePath* new_dir) { |
604 bool loosen_permissions) { | |
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 | |
616 FilePath path_to_create; | 554 FilePath path_to_create; |
617 srand(static_cast<uint32>(time(NULL))); | 555 srand(static_cast<uint32>(time(NULL))); |
618 | 556 |
619 int count = 0; | 557 int count = 0; |
620 while (count < 50) { | 558 while (count < 50) { |
621 // Try create a new temporary directory with random generated name. If | 559 // Try create a new temporary directory with random generated name. If |
622 // the one exists, keep trying another path name until we reach some limit. | 560 // the one exists, keep trying another path name until we reach some limit. |
623 path_to_create = base_dir; | 561 path_to_create = base_dir; |
624 | 562 |
625 std::wstring new_dir_name; | 563 std::wstring new_dir_name; |
626 new_dir_name.assign(prefix); | 564 new_dir_name.assign(prefix); |
627 new_dir_name.append(IntToWString(rand() % kint16max)); | 565 new_dir_name.append(IntToWString(rand() % kint16max)); |
628 | 566 |
629 path_to_create = path_to_create.Append(new_dir_name); | 567 path_to_create = path_to_create.Append(new_dir_name); |
630 if (::CreateDirectory(path_to_create.value().c_str(), | 568 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) |
631 directory_security_attributes)) | |
632 break; | 569 break; |
633 count++; | 570 count++; |
634 } | 571 } |
635 | 572 |
636 if (count == 50) { | 573 if (count == 50) { |
637 return false; | 574 return false; |
638 } | 575 } |
639 | 576 |
640 *new_dir = path_to_create; | 577 *new_dir = path_to_create; |
641 | |
642 return true; | 578 return true; |
643 } | 579 } |
644 | 580 |
645 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 581 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
646 FilePath* new_temp_path) { | 582 FilePath* new_temp_path) { |
647 FilePath system_temp_dir; | 583 FilePath system_temp_dir; |
648 if (!GetTempDir(&system_temp_dir)) | 584 if (!GetTempDir(&system_temp_dir)) |
649 return false; | 585 return false; |
650 | 586 |
651 return CreateTemporaryDirInDir(system_temp_dir, | 587 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); |
652 prefix, | |
653 new_temp_path, | |
654 false); | |
655 } | 588 } |
656 | 589 |
657 bool CreateDirectory(const FilePath& full_path) { | 590 bool CreateDirectory(const FilePath& full_path) { |
658 return file_util::CreateDirectoryExtraLogging(full_path, LOG(INFO)); | 591 return file_util::CreateDirectoryExtraLogging(full_path, LOG(INFO)); |
659 } | 592 } |
660 | 593 |
661 // TODO(skerner): Extra logging has been added to understand crbug/35198 . | 594 // TODO(skerner): Extra logging has been added to understand crbug/35198 . |
662 // Remove it once we get a log from a user who can reproduce the issue. | 595 // Remove it once we get a log from a user who can reproduce the issue. |
663 bool CreateDirectoryExtraLogging(const FilePath& full_path, | 596 bool CreateDirectoryExtraLogging(const FilePath& full_path, |
664 std::ostream& log) { | 597 std::ostream& log) { |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1094 // will find a drive letter which maps to the path's device, so | 1027 // will find a drive letter which maps to the path's device, so |
1095 // that we return a path starting with a drive letter. | 1028 // that we return a path starting with a drive letter. |
1096 FilePath mapped_file(mapped_file_path); | 1029 FilePath mapped_file(mapped_file_path); |
1097 success = DevicePathToDriveLetterPath(mapped_file, real_path); | 1030 success = DevicePathToDriveLetterPath(mapped_file, real_path); |
1098 } | 1031 } |
1099 UnmapViewOfFile(file_view); | 1032 UnmapViewOfFile(file_view); |
1100 return success; | 1033 return success; |
1101 } | 1034 } |
1102 | 1035 |
1103 } // namespace file_util | 1036 } // namespace file_util |
OLD | NEW |