| 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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 } | 276 } |
| 272 | 277 |
| 273 bool MakeFileUnreadable(const FilePath& path) { | 278 bool MakeFileUnreadable(const FilePath& path) { |
| 274 return DenyFilePermission(path, GENERIC_READ); | 279 return DenyFilePermission(path, GENERIC_READ); |
| 275 } | 280 } |
| 276 | 281 |
| 277 bool MakeFileUnwritable(const FilePath& path) { | 282 bool MakeFileUnwritable(const FilePath& path) { |
| 278 return DenyFilePermission(path, GENERIC_WRITE); | 283 return DenyFilePermission(path, GENERIC_WRITE); |
| 279 } | 284 } |
| 280 | 285 |
| 286 void* GetPermissionInfo(const FilePath& path, size_t* length) { |
| 287 DCHECK(length != NULL); |
| 288 *length = 0; |
| 289 PACL dacl = NULL; |
| 290 PSECURITY_DESCRIPTOR security_descriptor; |
| 291 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
| 292 SE_FILE_OBJECT, |
| 293 DACL_SECURITY_INFORMATION, NULL, NULL, &dacl, |
| 294 NULL, &security_descriptor) != ERROR_SUCCESS) { |
| 295 return NULL; |
| 296 } |
| 297 DCHECK(dacl != NULL); |
| 298 |
| 299 *length = sizeof(PSECURITY_DESCRIPTOR) + dacl->AclSize; |
| 300 PermissionInfo* info = reinterpret_cast<PermissionInfo*>(new char[*length]); |
| 301 info->security_descriptor = security_descriptor; |
| 302 memcpy(&info->dacl, dacl, dacl->AclSize); |
| 303 |
| 304 return info; |
| 305 } |
| 306 |
| 307 bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) { |
| 308 if (!info || !length) |
| 309 return false; |
| 310 |
| 311 PermissionInfo* perm = reinterpret_cast<PermissionInfo*>(info); |
| 312 |
| 313 DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
| 314 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, |
| 315 NULL, NULL, &perm->dacl, NULL); |
| 316 LocalFree(perm->security_descriptor); |
| 317 |
| 318 char* char_array = reinterpret_cast<char*>(info); |
| 319 delete [] char_array; |
| 320 |
| 321 return rc == ERROR_SUCCESS; |
| 322 } |
| 323 |
| 281 } // namespace file_util | 324 } // namespace file_util |
| OLD | NEW |