| 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/files/file_path_watcher.h" | 5 #include "base/files/file_path_watcher.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <aclapi.h> | 9 #include <aclapi.h> |
| 10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
| (...skipping 750 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 761 } | 761 } |
| 762 | 762 |
| 763 #endif // OS_LINUX | 763 #endif // OS_LINUX |
| 764 | 764 |
| 765 enum Permission { | 765 enum Permission { |
| 766 Read, | 766 Read, |
| 767 Write, | 767 Write, |
| 768 Execute | 768 Execute |
| 769 }; | 769 }; |
| 770 | 770 |
| 771 #if defined(OS_MACOSX) |
| 771 bool ChangeFilePermissions(const FilePath& path, Permission perm, bool allow) { | 772 bool ChangeFilePermissions(const FilePath& path, Permission perm, bool allow) { |
| 772 #if defined(OS_POSIX) | |
| 773 struct stat stat_buf; | 773 struct stat stat_buf; |
| 774 | 774 |
| 775 if (stat(path.value().c_str(), &stat_buf) != 0) | 775 if (stat(path.value().c_str(), &stat_buf) != 0) |
| 776 return false; | 776 return false; |
| 777 | 777 |
| 778 mode_t mode = 0; | 778 mode_t mode = 0; |
| 779 switch (perm) { | 779 switch (perm) { |
| 780 case Read: | 780 case Read: |
| 781 mode = S_IRUSR | S_IRGRP | S_IROTH; | 781 mode = S_IRUSR | S_IRGRP | S_IROTH; |
| 782 break; | 782 break; |
| 783 case Write: | 783 case Write: |
| 784 mode = S_IWUSR | S_IWGRP | S_IWOTH; | 784 mode = S_IWUSR | S_IWGRP | S_IWOTH; |
| 785 break; | 785 break; |
| 786 case Execute: | 786 case Execute: |
| 787 mode = S_IXUSR | S_IXGRP | S_IXOTH; | 787 mode = S_IXUSR | S_IXGRP | S_IXOTH; |
| 788 break; | 788 break; |
| 789 default: | 789 default: |
| 790 ADD_FAILURE() << "unknown perm " << perm; | 790 ADD_FAILURE() << "unknown perm " << perm; |
| 791 return false; | 791 return false; |
| 792 } | 792 } |
| 793 if (allow) { | 793 if (allow) { |
| 794 stat_buf.st_mode |= mode; | 794 stat_buf.st_mode |= mode; |
| 795 } else { | 795 } else { |
| 796 stat_buf.st_mode &= ~mode; | 796 stat_buf.st_mode &= ~mode; |
| 797 } | 797 } |
| 798 return chmod(path.value().c_str(), stat_buf.st_mode) == 0; | 798 return chmod(path.value().c_str(), stat_buf.st_mode) == 0; |
| 799 | |
| 800 #elif defined(OS_WIN) | |
| 801 PACL old_dacl; | |
| 802 PSECURITY_DESCRIPTOR security_descriptor; | |
| 803 if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), | |
| 804 SE_FILE_OBJECT, | |
| 805 DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl, | |
| 806 NULL, &security_descriptor) != ERROR_SUCCESS) | |
| 807 return false; | |
| 808 | |
| 809 DWORD mode = 0; | |
| 810 switch (perm) { | |
| 811 case Read: | |
| 812 mode = GENERIC_READ; | |
| 813 break; | |
| 814 case Write: | |
| 815 mode = GENERIC_WRITE; | |
| 816 break; | |
| 817 case Execute: | |
| 818 mode = GENERIC_EXECUTE; | |
| 819 break; | |
| 820 default: | |
| 821 ADD_FAILURE() << "unknown perm " << perm; | |
| 822 return false; | |
| 823 } | |
| 824 | |
| 825 // Deny Read access for the current user. | |
| 826 EXPLICIT_ACCESS change; | |
| 827 change.grfAccessPermissions = mode; | |
| 828 change.grfAccessMode = allow ? GRANT_ACCESS : DENY_ACCESS; | |
| 829 change.grfInheritance = 0; | |
| 830 change.Trustee.pMultipleTrustee = NULL; | |
| 831 change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; | |
| 832 change.Trustee.TrusteeForm = TRUSTEE_IS_NAME; | |
| 833 change.Trustee.TrusteeType = TRUSTEE_IS_USER; | |
| 834 change.Trustee.ptstrName = L"CURRENT_USER"; | |
| 835 | |
| 836 PACL new_dacl; | |
| 837 if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) { | |
| 838 LocalFree(security_descriptor); | |
| 839 return false; | |
| 840 } | |
| 841 | |
| 842 DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), | |
| 843 SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, | |
| 844 NULL, NULL, new_dacl, NULL); | |
| 845 LocalFree(security_descriptor); | |
| 846 LocalFree(new_dacl); | |
| 847 | |
| 848 return rc == ERROR_SUCCESS; | |
| 849 #else | |
| 850 NOTIMPLEMENTED(); | |
| 851 return false; | |
| 852 #endif | |
| 853 } | 799 } |
| 800 #endif // defined(OS_MACOSX) |
| 854 | 801 |
| 855 #if defined(OS_MACOSX) | 802 #if defined(OS_MACOSX) |
| 856 // Linux implementation of FilePathWatcher doesn't catch attribute changes. | 803 // Linux implementation of FilePathWatcher doesn't catch attribute changes. |
| 857 // http://crbug.com/78043 | 804 // http://crbug.com/78043 |
| 858 // Windows implementation of FilePathWatcher catches attribute changes that | 805 // Windows implementation of FilePathWatcher catches attribute changes that |
| 859 // don't affect the path being watched. | 806 // don't affect the path being watched. |
| 860 // http://crbug.com/78045 | 807 // http://crbug.com/78045 |
| 861 | 808 |
| 862 // Verify that changing attributes on a directory works. | 809 // Verify that changing attributes on a directory works. |
| 863 TEST_F(FilePathWatcherTest, DirAttributesChanged) { | 810 TEST_F(FilePathWatcherTest, DirAttributesChanged) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 887 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 834 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
| 888 ASSERT_TRUE(WaitForEvents()); | 835 ASSERT_TRUE(WaitForEvents()); |
| 889 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 836 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
| 890 DeleteDelegateOnFileThread(delegate.release()); | 837 DeleteDelegateOnFileThread(delegate.release()); |
| 891 } | 838 } |
| 892 | 839 |
| 893 #endif // OS_MACOSX | 840 #endif // OS_MACOSX |
| 894 } // namespace | 841 } // namespace |
| 895 | 842 |
| 896 } // namespace base | 843 } // namespace base |
| OLD | NEW |