forked from bozaro/UE4GitDepsPacker
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathStatHelper.cs
67 lines (61 loc) · 1.84 KB
/
StatHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.IO;
using System.Reflection;
namespace GitDepsPacker
{
static class StatFileHelper
{
const uint ExecutableBits = (1 << 0) | (1 << 3) | (1 << 6);
private static MethodInfo StatMethod;
private static FieldInfo StatModeField;
static StatFileHelper()
{
StatMethod = null;
// Try to load the Mono Posix assembly. If it doesn't exist, we're on Windows.
Assembly MonoPosix;
try
{
MonoPosix = Assembly.Load("Mono.Posix, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756");
}
catch (FileNotFoundException)
{
return;
}
Type SyscallType = MonoPosix.GetType("Mono.Unix.Native.Syscall");
if(SyscallType == null)
{
throw new InvalidOperationException("Couldn't find Syscall type");
}
StatMethod = SyscallType.GetMethod ("stat");
if (StatMethod == null)
{
throw new InvalidOperationException("Couldn't find Mono.Unix.Native.Syscall.stat method");
}
Type StatType = MonoPosix.GetType("Mono.Unix.Native.Stat");
if(StatType == null)
{
throw new InvalidOperationException("Couldn't find Mono.Unix.Native.Stat type");
}
StatModeField = StatType.GetField("st_mode");
if(StatModeField == null)
{
throw new InvalidOperationException("Couldn't find Mono.Unix.Native.Stat.st_mode field");
}
}
public static bool IsExecutalbe(string FileName)
{
if (StatMethod == null) {
return false;
}
object[] StatArgs = new object[] { FileName, null };
int StatResult = (int)StatMethod.Invoke(null, StatArgs);
if (StatResult != 0)
{
throw new InvalidOperationException(String.Format("Stat() call for {0} failed with error {1}", FileName, StatResult));
}
// Get the current permissions
uint CurrentPermissions = (uint)StatModeField.GetValue(StatArgs[1]);
return (CurrentPermissions & ExecutableBits) != 0;
}
}
}