我有一个问题,我希望有人能给我build议! 基本上在我的程序中,我将采取一个Windows文件夹,更改所有权,设置访问控制规则等,为此它漂亮的香草Java使用:
Files.getFileAttributeView(target, AclFileAttributeView.class);
当我想忘记一个目录时出现问题,为此我想重新启用父目录的inheritance,并删除所有的ACL规则。 我可以删除规则,并使用Java设置所有者,但它看起来像我必须去原生设置inheritance标志…
我想我可以得到一个指向这个安全描述符的指针:
Advapi32 advapi32 = Advapi32.INSTANCE; PointerByReference ppsidOwner = new PointerByReference(); PointerByReference ppsidGroup = new PointerByReference(); PointerByReference ppDacl = new PointerByReference(); PointerByReference ppSacl = new PointerByReference(); PointerByReference ppSecurityDescriptor = new PointerByReference(); int reqSecurityInfo = Advapi32.OWNER_SECURITY_INFORMATION | Advapi32.DACL_SECURITY_INFORMATION | Advapi32.SACL_SECURITY_INFORMATION | Advapi32.GROUP_SECURITY_INFORMATION; int ret = advapi32.GetNamedSecurityInfo("c:\\\\testpaths", Advapi32.SE_FILE_OBJECT, reqSecurityInfo, ppsidOwner, ppsidGroup, ppDacl, ppSacl, ppSecurityDescriptor); if(ret != 0){ throw new Win32Exception(ret); }
Advapi32.java包含以下内容: Advapi32 INSTANCE = (Advapi32) Native.loadLibrary("Advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);
但是我真的在这里挣扎了一下,我是在正确的轨道上,还是我是一个白痴? 对我来说这是陌生的,请帮忙!
我决定最简单的方法是在C#中实现代码,并将简单的DLL导入到Java中,并将其作为一个例子:
[RGiesecke.DllExport.DllExport] static void ReEnableInheretance(String dirPath) { DirectorySecurity dirSecurity = new DirectorySecurity(); dirSecurity.SetAccessRuleProtection(false, false); Directory.SetAccessControl(dirPath, dirSecurity); }
在Java中:
public interface MyNewLib extends Library { public void ReEnableInheretance(String dirPath); } MyNewLib myNewLib = (MyNewLib )Native.loadLibrary("MyNewLib ",MyNewLib.class); myNewLib .ReEnableInheretance(path.toString());
把依赖关系放在.net上,但这对我来说不是问题。