Skip to content

Commit 27d7971

Browse files
committed
Make sure that torrent-client is executable before running it
1 parent ca38741 commit 27d7971

File tree

5 files changed

+65
-24
lines changed

5 files changed

+65
-24
lines changed

Assets/PatchKit Patcher/Scripts/AppData/Remote/Downloaders/TorrentClient.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace PatchKit.Unity.Patcher.AppData.Remote.Downloaders
1515
/// <seealso cref="System.IDisposable" />
1616
public class TorrentClient : IDisposable
1717
{
18+
private const string TorrentClientWinPath = "torrent-client/win/torrent-client.exe";
19+
private const string TorrentClientOsx64Path = "torrent-client/osx64/torrent-client";
20+
private const string TorrentClientLinux64Path = "torrent-client/linux64/torrent-client";
1821
private static readonly DebugLogger DebugLogger = new DebugLogger(typeof(TorrentClient));
1922

2023
private string _streamingAssetsPath;
@@ -116,7 +119,7 @@ private ProcessStartInfo GetProcessStartInfo()
116119
{
117120
var processStartInfo = new ProcessStartInfo
118121
{
119-
FileName = _streamingAssetsPath.PathCombine("torrent-client/win/torrent-client.exe"),
122+
FileName = _streamingAssetsPath.PathCombine(TorrentClientWinPath),
120123
RedirectStandardInput = true,
121124
RedirectStandardOutput = true,
122125
UseShellExecute = false,
@@ -130,13 +133,16 @@ private ProcessStartInfo GetProcessStartInfo()
130133
{
131134
var processStartInfo = new ProcessStartInfo
132135
{
133-
FileName = _streamingAssetsPath.PathCombine("torrent-client/osx64/torrent-client"),
136+
FileName = _streamingAssetsPath.PathCombine(TorrentClientOsx64Path),
134137
RedirectStandardInput = true,
135138
RedirectStandardOutput = true,
136139
UseShellExecute = false,
137140
CreateNoWindow = true
138141
};
139142

143+
// make sure that binary can be executed
144+
Chmod.SetExecutableFlag(processStartInfo.FileName);
145+
140146
processStartInfo.EnvironmentVariables["DYLD_LIBRARY_PATH"] = Path.Combine(_streamingAssetsPath, "torrent-client/osx64");
141147

142148
return processStartInfo;
@@ -146,13 +152,16 @@ private ProcessStartInfo GetProcessStartInfo()
146152
{
147153
var processStartInfo = new ProcessStartInfo
148154
{
149-
FileName = _streamingAssetsPath.PathCombine("torrent-client/linux64/torrent-client"),
155+
FileName = _streamingAssetsPath.PathCombine(TorrentClientLinux64Path),
150156
RedirectStandardInput = true,
151157
RedirectStandardOutput = true,
152158
UseShellExecute = false,
153159
CreateNoWindow = true
154160
};
155161

162+
// make sure that binary can be executed
163+
Chmod.SetExecutableFlag(processStartInfo.FileName);
164+
156165
processStartInfo.EnvironmentVariables["LD_LIBRARY_PATH"] = Path.Combine(_streamingAssetsPath, "torrent-client/linux64");
157166

158167
return processStartInfo;

Assets/PatchKit Patcher/Scripts/AppStarter.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void Start()
4646
string filePath = _app.LocalDirectory.Path.PathCombine(fileName);
4747
if (IsExecutable(filePath, platformType))
4848
{
49-
Chmod(filePath, "+x");
49+
Chmod.SetExecutableFlag(filePath);
5050
}
5151
}
5252
}
@@ -108,26 +108,6 @@ private ProcessStartInfo GetProcessStartInfo(string executablePath, PlatformType
108108
}
109109
}
110110

111-
private void Chmod(string filePath, string permissions)
112-
{
113-
var process = new Process
114-
{
115-
StartInfo =
116-
{
117-
FileName = "/bin/chmod",
118-
Arguments = string.Format("{0} \"{1}\"", permissions, filePath)
119-
}
120-
};
121-
122-
DebugLogger.Log(string.Format("Executing {0} {1}...", process.StartInfo.FileName,
123-
process.StartInfo.Arguments));
124-
125-
process.Start();
126-
process.WaitForExit();
127-
128-
DebugLogger.Log("Done");
129-
}
130-
131111
private void StartAppProcess(ProcessStartInfo processStartInfo)
132112
{
133113
DebugLogger.Log(string.Format("Starting process '{0}' with arguments '{1}'", processStartInfo.FileName,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Diagnostics;
2+
using System.IO;
3+
using System.Net;
4+
using PatchKit.Unity.Patcher.Debug;
5+
6+
namespace PatchKit.Unity.Utilities
7+
{
8+
9+
public class Chmod
10+
{
11+
private const string ChmodPath = "/bin/chmod";
12+
13+
private static readonly DebugLogger DebugLogger = new DebugLogger(typeof(Chmod));
14+
15+
public static void SetExecutableFlag(string path, bool val = true)
16+
{
17+
Execute(val ? "+x" : "-x", path);
18+
}
19+
20+
private static void Execute(string param, string path)
21+
{
22+
Assert.IsTrue(Platform.IsPosix(), "Chmod can be run only on POSIX platforms");
23+
Assert.IsTrue(File.Exists(ChmodPath), "/bin/chmod should exist");
24+
25+
var process = new Process
26+
{
27+
StartInfo =
28+
{
29+
FileName = ChmodPath,
30+
Arguments = string.Format("{0} \"{1}\"", param, path)
31+
}
32+
};
33+
34+
DebugLogger.Log(string.Format("Executing {0} {1}...", process.StartInfo.FileName,
35+
process.StartInfo.Arguments));
36+
37+
process.Start();
38+
process.WaitForExit();
39+
40+
DebugLogger.Log("Done");
41+
}
42+
}
43+
44+
} // namespace

Assets/PatchKit Patcher/Scripts/Utilities/Chmod.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/PatchKit Patcher/Scripts/Utilities/Platform.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public static bool IsLinux()
4949
return IsOneOf(RuntimePlatform.LinuxPlayer, RuntimePlatform.LinuxEditor);
5050
}
5151

52+
public static bool IsPosix()
53+
{
54+
return IsLinux() || IsOSX();
55+
}
56+
5257
public static bool IsOneOf(params RuntimePlatform[] platforms)
5358
{
5459
var runtimePlatform = GetRuntimePlatform();

0 commit comments

Comments
 (0)