Index: base/test/test_file_util_win.cc |
diff --git a/base/test/test_file_util_win.cc b/base/test/test_file_util_win.cc |
index d546c228455f71cb10bb168ffd68bfda85bc333e..4801e792373a90a8fff60a91ddac8627300c4bf6 100644 |
--- a/base/test/test_file_util_win.cc |
+++ b/base/test/test_file_util_win.cc |
@@ -4,11 +4,13 @@ |
#include "base/test/test_file_util.h" |
-#include <windows.h> |
#include <aclapi.h> |
#include <shlwapi.h> |
#include <stddef.h> |
+#include <wchar.h> |
+#include <windows.h> |
+#include <memory> |
#include <vector> |
#include "base/files/file_path.h" |
@@ -27,42 +29,6 @@ struct PermissionInfo { |
ACL dacl; |
}; |
-// Deny |permission| on the file |path|, for the current user. |
-bool DenyFilePermission(const FilePath& path, DWORD permission) { |
- PACL old_dacl; |
- PSECURITY_DESCRIPTOR security_descriptor; |
- if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
- SE_FILE_OBJECT, |
- DACL_SECURITY_INFORMATION, NULL, NULL, &old_dacl, |
- NULL, &security_descriptor) != ERROR_SUCCESS) { |
- return false; |
- } |
- |
- EXPLICIT_ACCESS change; |
- change.grfAccessPermissions = permission; |
- change.grfAccessMode = DENY_ACCESS; |
- change.grfInheritance = 0; |
- change.Trustee.pMultipleTrustee = NULL; |
- change.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; |
- change.Trustee.TrusteeForm = TRUSTEE_IS_NAME; |
- change.Trustee.TrusteeType = TRUSTEE_IS_USER; |
- change.Trustee.ptstrName = const_cast<wchar_t*>(L"CURRENT_USER"); |
- |
- PACL new_dacl; |
- if (SetEntriesInAcl(1, &change, old_dacl, &new_dacl) != ERROR_SUCCESS) { |
- LocalFree(security_descriptor); |
- return false; |
- } |
- |
- DWORD rc = SetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), |
- SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, |
- NULL, NULL, new_dacl, NULL); |
- LocalFree(security_descriptor); |
- LocalFree(new_dacl); |
- |
- return rc == ERROR_SUCCESS; |
-} |
- |
// Gets a blob indicating the permission information for |path|. |
// |length| is the length of the blob. Zero on failure. |
// Returns the blob pointer, or NULL on failure. |
@@ -149,6 +115,46 @@ bool EvictFileFromSystemCache(const FilePath& file) { |
return true; |
} |
+// Deny |permission| on the file |path|, for the current user. |
+bool DenyFilePermission(const FilePath& path, DWORD permission) { |
+ PACL old_dacl; |
+ PSECURITY_DESCRIPTOR security_descriptor; |
+ |
+ int path_size = path.value().size(); |
Peter Kasting
2016/07/11 02:35:53
Should be size_t.
WC Leung
2016/07/18 09:41:35
Done.
|
+ std::unique_ptr<TCHAR[]> path_ptr(new TCHAR[path_size + 1]); |
Peter Kasting
2016/07/11 02:35:53
Nit: Prefer "= base::MakeUnique" to raw new.
WC Leung
2016/07/18 09:41:35
Done. Thanks for making base::MakeUnique known to
|
+ wcsncpy(path_ptr.get(), path.value().c_str(), path_size + 1); |
+ path_ptr[path_size] = L'\0'; |
Peter Kasting
2016/07/11 02:35:53
I don't see why this line is needed, since the sou
WC Leung
2016/07/18 09:41:35
I'm super-paranoid here because a missing '\0' cau
Peter Kasting
2016/07/18 17:34:30
I'm opposed to adding something to account for (2)
WC Leung
2016/07/19 08:00:12
I see. I do buy in the readability part. So the li
|
+ |
+ if (GetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT, |
+ DACL_SECURITY_INFORMATION, nullptr, nullptr, |
+ &old_dacl, nullptr, |
+ &security_descriptor) != ERROR_SUCCESS) { |
+ return false; |
+ } |
+ |
+ LPTSTR current_user = L"CURRENT_USER"; |
+ EXPLICIT_ACCESS new_access = { |
+ permission, |
+ DENY_ACCESS, |
+ 0, |
+ {nullptr, NO_MULTIPLE_TRUSTEE, TRUSTEE_IS_NAME, TRUSTEE_IS_USER, |
+ current_user}}; |
+ |
+ PACL new_dacl; |
+ if (SetEntriesInAcl(1, &new_access, old_dacl, &new_dacl) != ERROR_SUCCESS) { |
+ LocalFree(security_descriptor); |
+ return false; |
+ } |
+ |
+ DWORD rc = SetNamedSecurityInfo(path_ptr.get(), SE_FILE_OBJECT, |
+ DACL_SECURITY_INFORMATION, nullptr, nullptr, |
+ new_dacl, nullptr); |
+ LocalFree(security_descriptor); |
+ LocalFree(new_dacl); |
+ |
+ return rc == ERROR_SUCCESS; |
+} |
+ |
// Checks if the volume supports Alternate Data Streams. This is required for |
// the Zone Identifier implementation. |
bool VolumeSupportsADS(const FilePath& path) { |