Simplify the FilePermissions into one set of flags

pull/243/head
Ryan Kistner 4 years ago
parent 08eb74a7cc
commit 7b89e0c1c4

@ -5,33 +5,21 @@ namespace DepotDownloader
{ {
public static class PlatformUtilities public static class PlatformUtilities
{ {
[Flags] private const int ModeExecuteOwner = 0x0040;
private enum FilePermissions : uint private const int ModeExecuteGroup = 0x0008;
{ private const int ModeExecuteOther = 0x0001;
S_IXUSR = 0x0040, // Execute by owner private const int ModeExecute = ModeExecuteOwner | ModeExecuteGroup | ModeExecuteOther;
S_IXGRP = 0x0008, // Execute by group
S_IXOTH = 0x0001, // Execute by other
EXECUTE = S_IXGRP | S_IXUSR | S_IXOTH
}
private enum FilePermissionsShort : ushort
{
S_IXUSR = 0x0040, // Execute by owner
S_IXGRP = 0x0008, // Execute by group
S_IXOTH = 0x0001, // Execute by other
EXECUTE = S_IXGRP | S_IXUSR | S_IXOTH
}
[StructLayout(LayoutKind.Explicit, Size = 144)] [StructLayout(LayoutKind.Explicit, Size = 144)]
private readonly struct StatLinux private readonly struct StatLinux
{ {
[FieldOffset(24)] public readonly FilePermissions st_mode; [FieldOffset(24)] public readonly uint st_mode;
} }
[StructLayout(LayoutKind.Explicit, Size = 144)] [StructLayout(LayoutKind.Explicit, Size = 144)]
private readonly struct StatOSX private readonly struct StatOSX
{ {
[FieldOffset(4)] public readonly FilePermissionsShort st_mode; [FieldOffset(4)] public readonly ushort st_mode;
} }
[DllImport("libc", EntryPoint = "__xstat", SetLastError = true)] [DllImport("libc", EntryPoint = "__xstat", SetLastError = true)]
@ -41,10 +29,10 @@ namespace DepotDownloader
private static extern int stat(string path, out StatOSX stat); private static extern int stat(string path, out StatOSX stat);
[DllImport("libc", SetLastError = true)] [DllImport("libc", SetLastError = true)]
private static extern int chmod(string path, FilePermissions mode); private static extern int chmod(string path, uint mode);
[DllImport("libc", SetLastError = true)] [DllImport("libc", SetLastError = true)]
private static extern int chmod(string path, FilePermissionsShort mode); private static extern int chmod(string path, ushort mode);
[DllImport("libc", CallingConvention = CallingConvention.Cdecl, SetLastError = true)] [DllImport("libc", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
private static extern IntPtr strerror(int errno); private static extern IntPtr strerror(int errno);
@ -64,22 +52,24 @@ namespace DepotDownloader
{ {
ThrowIf(PlatformUtilities.stat(1, path, out var stat)); ThrowIf(PlatformUtilities.stat(1, path, out var stat));
if (stat.st_mode.HasFlag(FilePermissions.EXECUTE) != value) var hasExecuteMask = (stat.st_mode & ModeExecute) == ModeExecute;
if (hasExecuteMask != value)
{ {
ThrowIf(chmod(path, value ThrowIf(chmod(path, (uint)(value
? stat.st_mode | FilePermissions.EXECUTE ? stat.st_mode | ModeExecute
: stat.st_mode & ~FilePermissions.EXECUTE)); : stat.st_mode & ~ModeExecute)));
} }
} }
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{ {
ThrowIf(PlatformUtilities.stat(path, out var stat)); ThrowIf(PlatformUtilities.stat(path, out var stat));
if (stat.st_mode.HasFlag(FilePermissionsShort.EXECUTE) != value) var hasExecuteMask = (stat.st_mode & ModeExecute) == ModeExecute;
if (hasExecuteMask != value)
{ {
ThrowIf(chmod(path, value ThrowIf(chmod(path, (ushort)(value
? stat.st_mode | FilePermissionsShort.EXECUTE ? stat.st_mode | ModeExecute
: stat.st_mode & ~FilePermissionsShort.EXECUTE)); : stat.st_mode & ~ModeExecute)));
} }
} }
} }

Loading…
Cancel
Save