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 // This file contains utility functions for dealing with the local | 5 // This file contains utility functions for dealing with the local |
6 // filesystem. | 6 // filesystem. |
7 | 7 |
8 #ifndef BASE_FILE_UTIL_H_ | 8 #ifndef BASE_FILE_UTIL_H_ |
9 #define BASE_FILE_UTIL_H_ | 9 #define BASE_FILE_UTIL_H_ |
10 | 10 |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 | 12 |
13 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
14 #include <windows.h> | 14 #include <windows.h> |
| 15 #if defined(UNIT_TEST) |
| 16 #include <aclapi.h> |
| 17 #endif |
15 #elif defined(OS_POSIX) | 18 #elif defined(OS_POSIX) |
16 #include <sys/stat.h> | 19 #include <sys/stat.h> |
17 #endif | 20 #endif |
18 | 21 |
19 #include <stdio.h> | 22 #include <stdio.h> |
20 | 23 |
21 #include <stack> | 24 #include <stack> |
22 #include <string> | 25 #include <string> |
23 #include <vector> | 26 #include <vector> |
24 | 27 |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 // Renames a file using the SHFileOperation API to ensure that the target file | 498 // Renames a file using the SHFileOperation API to ensure that the target file |
496 // gets the correct default security descriptor in the new path. | 499 // gets the correct default security descriptor in the new path. |
497 bool RenameFileAndResetSecurityDescriptor( | 500 bool RenameFileAndResetSecurityDescriptor( |
498 const FilePath& source_file_path, | 501 const FilePath& source_file_path, |
499 const FilePath& target_file_path); | 502 const FilePath& target_file_path); |
500 | 503 |
501 // Returns whether the file has been modified since a particular date. | 504 // Returns whether the file has been modified since a particular date. |
502 bool HasFileBeenModifiedSince(const FileEnumerator::FindInfo& find_info, | 505 bool HasFileBeenModifiedSince(const FileEnumerator::FindInfo& find_info, |
503 const base::Time& cutoff_time); | 506 const base::Time& cutoff_time); |
504 | 507 |
| 508 #ifdef UNIT_TEST |
| 509 |
| 510 inline bool MakeFileUnreadable(const FilePath& path) { |
| 511 #if defined(OS_POSIX) |
| 512 struct stat stat_buf; |
| 513 if (stat(path.value().c_str(), &stat_buf) != 0) |
| 514 return false; |
| 515 stat_buf.st_mode &= ~(S_IRUSR | S_IRGRP | S_IROTH); |
| 516 |
| 517 return chmod(path.value().c_str(), stat_buf.st_mode) == 0; |
| 518 |
| 519 #elif defined(OS_WIN) |
| 520 PACL old_dacl; |
| 521 PSECURITY_DESCRIPTOR security_descriptor; |
| 522 if (GetNamedSecurityInfo(path.value().c_str(), SE_FILE_OBJECT, |
| 523 DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl, |
| 524 NULL, &security_descriptor) != ERROR_SUCCESS) |
| 525 return false; |
| 526 |
| 527 // Deny Read access for the current user. |
| 528 EXPLICIT_ACCESS change; |
| 529 change.grfAccessPermissions = GENERIC_READ; |
| 530 change.grfAccessMode = DENY_ACCESS; |
| 531 change.grfInheritance = 0; |
| 532 change.Trustee.pMultipleTrustee = NULL; |
| 533 change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; |
| 534 change.Trustee.TrusteeForm = TRUSTEE_IS_NAME; |
| 535 change.Trustee.TrusteeType = TRUSTEE_IS_USER; |
| 536 change.Trustee.ptstrName = L"CURRENT_USER"; |
| 537 |
| 538 PACL new_dacl; |
| 539 if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) { |
| 540 LocalFree(security_descriptor); |
| 541 return false; |
| 542 } |
| 543 |
| 544 DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
| 545 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, |
| 546 NULL, NULL, new_dacl, NULL); |
| 547 LocalFree(security_descriptor); |
| 548 LocalFree(new_dacl); |
| 549 |
| 550 return rc == ERROR_SUCCESS; |
| 551 #else |
| 552 NOTIMPLEMENTED(); |
| 553 return false; |
| 554 #endif |
| 555 } |
| 556 |
| 557 #endif // UNIT_TEST |
| 558 |
505 } // namespace file_util | 559 } // namespace file_util |
506 | 560 |
507 // Deprecated functions have been moved to this separate header file, | 561 // Deprecated functions have been moved to this separate header file, |
508 // which must be included last after all the above definitions. | 562 // which must be included last after all the above definitions. |
509 #include "base/file_util_deprecated.h" | 563 #include "base/file_util_deprecated.h" |
510 | 564 |
511 #endif // BASE_FILE_UTIL_H_ | 565 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |