public static void WriteUnix(string file, SecureWriteCallback callback) { FilePermissions oldUmask = Syscall.umask(FilePermissions.S_IXUSR | FilePermissions.S_IRGRP | FilePermissions.S_IWGRP | FilePermissions.S_IXGRP | FilePermissions.S_IROTH | FilePermissions.S_IWOTH | FilePermissions.S_IXOTH); using (FileStream stream = File.Open(file, FileMode.Create, FileAccess.Write, FileShare.None)) { callback.Invoke(stream); } Syscall.umask(oldUmask); }
public static void WriteWindows(string file, SecureWriteCallback callback) { using (FileStream stream = File.Open(file, FileMode.Create, FileAccess.Write, FileShare.None)) { FileSecurity acl = File.GetAccessControl(file); acl.SetAccessRuleProtection(true, false); foreach (FileSystemAccessRule entry in acl.GetAccessRules(true, true, typeof(NTAccount))) { acl.PurgeAccessRules(entry.IdentityReference); } acl.AddAccessRule(new FileSystemAccessRule(stream.GetAccessControl().GetOwner(typeof(NTAccount)), FileSystemRights.FullControl, AccessControlType.Allow)); File.SetAccessControl(file, acl); callback.Invoke(stream); } }