OLD | NEW |
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 <aclapi.h> | 7 #include <aclapi.h> |
8 #include <shlwapi.h> | 8 #include <shlwapi.h> |
9 #include <windows.h> | 9 #include <windows.h> |
10 | 10 |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/file_path.h" | 13 #include "base/file_path.h" |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/string_split.h" | 16 #include "base/string_split.h" |
17 #include "base/win/scoped_handle.h" | 17 #include "base/win/scoped_handle.h" |
18 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
19 | 19 |
20 namespace file_util { | 20 namespace file_util { |
21 | 21 |
22 static const ptrdiff_t kOneMB = 1024 * 1024; | 22 static const ptrdiff_t kOneMB = 1024 * 1024; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
| 26 struct PermissionInfo { |
| 27 PSECURITY_DESCRIPTOR security_descriptor; |
| 28 ACL dacl; |
| 29 }; |
| 30 |
26 // Deny |permission| on the file |path|, for the current user. | 31 // Deny |permission| on the file |path|, for the current user. |
27 bool DenyFilePermission(const FilePath& path, DWORD permission) { | 32 bool DenyFilePermission(const FilePath& path, DWORD permission) { |
28 PACL old_dacl; | 33 PACL old_dacl; |
29 PSECURITY_DESCRIPTOR security_descriptor; | 34 PSECURITY_DESCRIPTOR security_descriptor; |
30 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), | 35 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
31 SE_FILE_OBJECT, | 36 SE_FILE_OBJECT, |
32 DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl, | 37 DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl, |
33 NULL, &security_descriptor) != ERROR_SUCCESS) { | 38 NULL, &security_descriptor) != ERROR_SUCCESS) { |
34 return false; | 39 return false; |
35 } | 40 } |
(...skipping 16 matching lines...) Expand all Loading... |
52 | 57 |
53 DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), | 58 DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
54 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, | 59 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, |
55 NULL, NULL, new_dacl, NULL); | 60 NULL, NULL, new_dacl, NULL); |
56 LocalFree(security_descriptor); | 61 LocalFree(security_descriptor); |
57 LocalFree(new_dacl); | 62 LocalFree(new_dacl); |
58 | 63 |
59 return rc == ERROR_SUCCESS; | 64 return rc == ERROR_SUCCESS; |
60 } | 65 } |
61 | 66 |
| 67 // Gets a blob indicating the permission information for |path|. |
| 68 // |length| is the length of the blob. Zero on failure. |
| 69 // Returns the blob pointer, or NULL on failure. |
| 70 void* GetPermissionInfo(const FilePath& path, size_t* length) { |
| 71 DCHECK(length != NULL); |
| 72 *length = 0; |
| 73 PACL dacl = NULL; |
| 74 PSECURITY_DESCRIPTOR security_descriptor; |
| 75 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
| 76 SE_FILE_OBJECT, |
| 77 DACL_SECURITY_INFORMATION, NULL, NULL, &dacl, |
| 78 NULL, &security_descriptor) != ERROR_SUCCESS) { |
| 79 return NULL; |
| 80 } |
| 81 DCHECK(dacl != NULL); |
| 82 |
| 83 *length = sizeof(PSECURITY_DESCRIPTOR) + dacl->AclSize; |
| 84 PermissionInfo* info = reinterpret_cast<PermissionInfo*>(new char[*length]); |
| 85 info->security_descriptor = security_descriptor; |
| 86 memcpy(&info->dacl, dacl, dacl->AclSize); |
| 87 |
| 88 return info; |
| 89 } |
| 90 |
| 91 // Restores the permission information for |path|, given the blob retrieved |
| 92 // using |GetPermissionInfo()|. |
| 93 // |info| is the pointer to the blob. |
| 94 // |length| is the length of the blob. |
| 95 // Either |info| or |length| may be NULL/0, in which case nothing happens. |
| 96 bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) { |
| 97 if (!info || !length) |
| 98 return false; |
| 99 |
| 100 PermissionInfo* perm = reinterpret_cast<PermissionInfo*>(info); |
| 101 |
| 102 DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
| 103 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, |
| 104 NULL, NULL, &perm->dacl, NULL); |
| 105 LocalFree(perm->security_descriptor); |
| 106 |
| 107 char* char_array = reinterpret_cast<char*>(info); |
| 108 delete [] char_array; |
| 109 |
| 110 return rc == ERROR_SUCCESS; |
| 111 } |
| 112 |
62 } // namespace | 113 } // namespace |
63 | 114 |
64 bool DieFileDie(const FilePath& file, bool recurse) { | 115 bool DieFileDie(const FilePath& file, bool recurse) { |
65 // It turns out that to not induce flakiness a long timeout is needed. | 116 // It turns out that to not induce flakiness a long timeout is needed. |
66 const int kIterations = 25; | 117 const int kIterations = 25; |
67 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(10) / | 118 const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(10) / |
68 kIterations; | 119 kIterations; |
69 | 120 |
70 if (!file_util::PathExists(file)) | 121 if (!file_util::PathExists(file)) |
71 return true; | 122 return true; |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 } | 321 } |
271 | 322 |
272 bool MakeFileUnreadable(const FilePath& path) { | 323 bool MakeFileUnreadable(const FilePath& path) { |
273 return DenyFilePermission(path, GENERIC_READ); | 324 return DenyFilePermission(path, GENERIC_READ); |
274 } | 325 } |
275 | 326 |
276 bool MakeFileUnwritable(const FilePath& path) { | 327 bool MakeFileUnwritable(const FilePath& path) { |
277 return DenyFilePermission(path, GENERIC_WRITE); | 328 return DenyFilePermission(path, GENERIC_WRITE); |
278 } | 329 } |
279 | 330 |
| 331 PermissionRestorer::PermissionRestorer(const FilePath& path) |
| 332 : path_(path), info_(NULL), length_(0) { |
| 333 info_ = GetPermissionInfo(path_, &length_); |
| 334 DCHECK(info_ != NULL); |
| 335 DCHECK_NE(0u, length_); |
| 336 } |
| 337 |
| 338 PermissionRestorer::~PermissionRestorer() { |
| 339 if (!RestorePermissionInfo(path_, info_, length_)) |
| 340 NOTREACHED(); |
| 341 } |
| 342 |
280 } // namespace file_util | 343 } // namespace file_util |
OLD | NEW |