Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: base/test/test_file_util_win.cc

Issue 2047483003: Add fallback behavior if the last used profile cannot initialize (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bug-614753-fix
Patch Set: Respond to comments, copy base/test/* from CL 2061593002 again. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/test/test_file_util.h" 5 #include "base/test/test_file_util.h"
6 6
7 #include <windows.h>
8 #include <aclapi.h> 7 #include <aclapi.h>
9 #include <shlwapi.h> 8 #include <shlwapi.h>
10 #include <stddef.h> 9 #include <stddef.h>
10 #include <wchar.h>
11 #include <windows.h>
11 12
13 #include <memory>
12 #include <vector> 14 #include <vector>
13 15
14 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
15 #include "base/files/file_util.h" 17 #include "base/files/file_util.h"
16 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/memory/ptr_util.h"
17 #include "base/strings/string_split.h" 20 #include "base/strings/string_split.h"
18 #include "base/threading/platform_thread.h" 21 #include "base/threading/platform_thread.h"
19 #include "base/win/scoped_handle.h" 22 #include "base/win/scoped_handle.h"
20 23
21 namespace base { 24 namespace base {
22 25
23 namespace { 26 namespace {
24 27
25 struct PermissionInfo { 28 struct PermissionInfo {
26 PSECURITY_DESCRIPTOR security_descriptor; 29 PSECURITY_DESCRIPTOR security_descriptor;
27 ACL dacl; 30 ACL dacl;
28 }; 31 };
29 32
30 // Deny |permission| on the file |path|, for the current user.
31 bool DenyFilePermission(const FilePath& path, DWORD permission) {
32 PACL old_dacl;
33 PSECURITY_DESCRIPTOR security_descriptor;
34 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()),
35 SE_FILE_OBJECT,
36 DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl,
37 NULL, &security_descriptor) != ERROR_SUCCESS) {
38 return false;
39 }
40
41 EXPLICIT_ACCESS change;
42 change.grfAccessPermissions = permission;
43 change.grfAccessMode = DENY_ACCESS;
44 change.grfInheritance = 0;
45 change.Trustee.pMultipleTrustee = NULL;
46 change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE;
47 change.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
48 change.Trustee.TrusteeType = TRUSTEE_IS_USER;
49 change.Trustee.ptstrName = const_cast<wchar_t*>(L"CURRENT_USER");
50
51 PACL new_dacl;
52 if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) {
53 LocalFree(security_descriptor);
54 return false;
55 }
56
57 DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()),
58 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION,
59 NULL, NULL, new_dacl, NULL);
60 LocalFree(security_descriptor);
61 LocalFree(new_dacl);
62
63 return rc == ERROR_SUCCESS;
64 }
65
66 // Gets a blob indicating the permission information for |path|. 33 // Gets a blob indicating the permission information for |path|.
67 // |length| is the length of the blob. Zero on failure. 34 // |length| is the length of the blob. Zero on failure.
68 // Returns the blob pointer, or NULL on failure. 35 // Returns the blob pointer, or NULL on failure.
69 void* GetPermissionInfo(const FilePath& path, size_t* length) { 36 void* GetPermissionInfo(const FilePath& path, size_t* length) {
70 DCHECK(length != NULL); 37 DCHECK(length != NULL);
71 *length = 0; 38 *length = 0;
72 PACL dacl = NULL; 39 PACL dacl = NULL;
73 PSECURITY_DESCRIPTOR security_descriptor; 40 PSECURITY_DESCRIPTOR security_descriptor;
74 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), 41 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()),
75 SE_FILE_OBJECT, 42 SE_FILE_OBJECT,
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 // local experimentation validates this simplified and *much* faster approach: 109 // local experimentation validates this simplified and *much* faster approach:
143 // [1] Sysinternals RamMap no longer lists these files as cached afterwards. 110 // [1] Sysinternals RamMap no longer lists these files as cached afterwards.
144 // [2] Telemetry performance test startup.cold.blank_page reports sane values. 111 // [2] Telemetry performance test startup.cold.blank_page reports sane values.
145 BY_HANDLE_FILE_INFORMATION bhi = {0}; 112 BY_HANDLE_FILE_INFORMATION bhi = {0};
146 CHECK(::GetFileInformationByHandle(file_handle.Get(), &bhi)); 113 CHECK(::GetFileInformationByHandle(file_handle.Get(), &bhi));
147 CHECK(::SetFileTime(file_handle.Get(), &bhi.ftCreationTime, 114 CHECK(::SetFileTime(file_handle.Get(), &bhi.ftCreationTime,
148 &bhi.ftLastAccessTime, &bhi.ftLastWriteTime)); 115 &bhi.ftLastAccessTime, &bhi.ftLastWriteTime));
149 return true; 116 return true;
150 } 117 }
151 118
119 // Deny |permission| on the file |path|, for the current user.
120 bool DenyFilePermission(const FilePath& path, DWORD permission) {
121 PACL old_dacl;
122 PSECURITY_DESCRIPTOR security_descriptor;
123
124 size_t path_size = path.value().size() + 1;
125 std::unique_ptr<TCHAR[]> path_ptr = base::MakeUnique<TCHAR[]>(path_size);
126 wcsncpy(path_ptr.get(), path.value().c_str(), path_size);
127
128 if (GetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT,
129 DACL_SECURITY_INFORMATION, nullptr, nullptr,
130 &old_dacl, nullptr,
131 &security_descriptor) != ERROR_SUCCESS) {
132 return false;
133 }
134
135 LPTSTR current_user = L"CURRENT_USER";
136 EXPLICIT_ACCESS new_access = {
137 permission,
138 DENY_ACCESS,
139 0,
140 {nullptr, NO_MULTIPLE_TRUSTEE, TRUSTEE_IS_NAME, TRUSTEE_IS_USER,
141 current_user}};
142
143 PACL new_dacl;
144 if (SetEntriesInAcl(1, &new_access, old_dacl, &new_dacl) != ERROR_SUCCESS) {
145 LocalFree(security_descriptor);
146 return false;
147 }
148
149 DWORD rc = SetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT,
150 DACL_SECURITY_INFORMATION, nullptr, nullptr,
151 new_dacl, nullptr);
152 LocalFree(security_descriptor);
153 LocalFree(new_dacl);
154
155 return rc == ERROR_SUCCESS;
156 }
157
152 // Checks if the volume supports Alternate Data Streams. This is required for 158 // Checks if the volume supports Alternate Data Streams. This is required for
153 // the Zone Identifier implementation. 159 // the Zone Identifier implementation.
154 bool VolumeSupportsADS(const FilePath& path) { 160 bool VolumeSupportsADS(const FilePath& path) {
155 wchar_t drive[MAX_PATH] = {0}; 161 wchar_t drive[MAX_PATH] = {0};
156 wcscpy_s(drive, MAX_PATH, path.value().c_str()); 162 wcscpy_s(drive, MAX_PATH, path.value().c_str());
157 163
158 if (!PathStripToRootW(drive)) 164 if (!PathStripToRootW(drive))
159 return false; 165 return false;
160 166
161 DWORD fs_flags = 0; 167 DWORD fs_flags = 0;
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 DCHECK(info_ != NULL); 212 DCHECK(info_ != NULL);
207 DCHECK_NE(0u, length_); 213 DCHECK_NE(0u, length_);
208 } 214 }
209 215
210 FilePermissionRestorer::~FilePermissionRestorer() { 216 FilePermissionRestorer::~FilePermissionRestorer() {
211 if (!RestorePermissionInfo(path_, info_, length_)) 217 if (!RestorePermissionInfo(path_, info_, length_))
212 NOTREACHED(); 218 NOTREACHED();
213 } 219 }
214 220
215 } // namespace base 221 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698