internal static void SetSecurityInfo(SafeHandle handle, NativeMethods.SE_OBJECT_TYPE objectType, NativeObjectSecurity fileSecurity) { byte[] managedDescriptor = fileSecurity.GetSecurityDescriptorBinaryForm(); using (SafeGlobalMemoryBufferHandle hDescriptor = new SafeGlobalMemoryBufferHandle(managedDescriptor.Length)) { hDescriptor.CopyFrom(managedDescriptor, 0, managedDescriptor.Length); NativeMethods.SECURITY_DESCRIPTOR_CONTROL control; uint revision; if (!NativeMethods.GetSecurityDescriptorControl(hDescriptor, out control, out revision)) { NativeError.ThrowException(); } IntPtr pDacl; bool daclDefaulted, daclPresent; if (!NativeMethods.GetSecurityDescriptorDacl(hDescriptor, out daclPresent, out pDacl, out daclDefaulted)) { NativeError.ThrowException(); } IntPtr pSacl; bool saclDefaulted, saclPresent; if (!NativeMethods.GetSecurityDescriptorSacl(hDescriptor, out saclPresent, out pSacl, out saclDefaulted)) { NativeError.ThrowException(); } IntPtr pOwner; bool ownerDefaulted; if (!NativeMethods.GetSecurityDescriptorOwner(hDescriptor, out pOwner, out ownerDefaulted)) { NativeError.ThrowException(); } IntPtr pGroup; bool GroupDefaulted; if (!NativeMethods.GetSecurityDescriptorGroup(hDescriptor, out pGroup, out GroupDefaulted)) { NativeError.ThrowException(); } PrivilegeEnabler privilegeEnabler = null; NativeMethods.SECURITY_INFORMATION info = 0; if (daclPresent) { info |= NativeMethods.SECURITY_INFORMATION.DACL_SECURITY_INFORMATION; if ((control & NativeMethods.SECURITY_DESCRIPTOR_CONTROL.SE_DACL_PROTECTED) != 0) { info |= NativeMethods.SECURITY_INFORMATION.PROTECTED_DACL_SECURITY_INFORMATION; } else { info |= NativeMethods.SECURITY_INFORMATION.UNPROTECTED_DACL_SECURITY_INFORMATION; } } if (saclPresent) { info |= NativeMethods.SECURITY_INFORMATION.SACL_SECURITY_INFORMATION; if ((control & NativeMethods.SECURITY_DESCRIPTOR_CONTROL.SE_SACL_PROTECTED) != 0) { info |= NativeMethods.SECURITY_INFORMATION.PROTECTED_SACL_SECURITY_INFORMATION; } else { info |= NativeMethods.SECURITY_INFORMATION.UNPROTECTED_SACL_SECURITY_INFORMATION; } privilegeEnabler = new PrivilegeEnabler(Privilege.Security); } if (pOwner != IntPtr.Zero) { info |= NativeMethods.SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION; } if (pGroup != IntPtr.Zero) { info |= NativeMethods.SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION; } try { uint errorCode = NativeMethods.SetSecurityInfo(handle, objectType, info, pOwner, pGroup, pDacl, pSacl); if (errorCode != 0) { NativeError.ThrowException((int)errorCode); } } finally { if (privilegeEnabler != null) { privilegeEnabler.Dispose(); } } } }
public bool MoveNext() { mInfo = new FileSystemEntryInfo(); if (mHandle == null) { if (mTransaction == null) { mHandle = NativeMethods.FindFirstFileExW(mPath, NativeMethods.FINDEX_INFO_LEVELS.FindExInfoStandard, mInfo, mFindexSearchOptions, IntPtr.Zero, NativeMethods.FINDEX_FLAGS.FIND_FIRST_EX_NONE); } else { mHandle = NativeMethods.FindFirstFileTransactedW(mPath, NativeMethods.FINDEX_INFO_LEVELS.FindExInfoStandard, mInfo, mFindexSearchOptions, IntPtr.Zero, NativeMethods.FINDEX_FLAGS.FIND_FIRST_EX_NONE, mTransaction.SafeHandle); } if (mHandle.IsInvalid) { uint error = (uint)Marshal.GetLastWin32Error(); if (error == Win32Errors.ERROR_FILE_NOT_FOUND) { return(false); } else { NativeError.ThrowException(error, mPath, mPath); } } return(Filter()); } else if (mHandle.IsInvalid) { return(false); } else { if (!NativeMethods.FindNextFileW(mHandle, mInfo)) { uint error = (uint)Marshal.GetLastWin32Error(); if (error == Win32Errors.ERROR_NO_MORE_FILES) { mHandle.Close(); return(false); } else { NativeError.ThrowException(error); } } else { return(Filter()); } } // Actually unreachable code, but seems to be neccessary. return(false); }