-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRsyncHost.cs
182 lines (169 loc) · 5.32 KB
/
RsyncHost.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MowerUpdater;
internal class RsyncHost : IDisposable
{
bool disposed = false;
bool _started = false;
Process _rsyncProcess = null;
Process rsyncProcess
{
get => _rsyncProcess;
set
{
if (_rsyncProcess != value)
{
_rsyncProcess?.Dispose();
_rsyncProcess = value;
_started = false;
}
}
}
public bool Active
{
get
{
if (disposed || rsyncProcess == null) return false;
if (rsyncProcess.HasExited) return false;
return _started;
}
}
public int? ExitCode
{
get
{
if (rsyncProcess != null && rsyncProcess.HasExited)
{
return rsyncProcess.ExitCode;
}
return null;
}
}
public event Action<RsyncHost> BeginHostStart;
public event Action<RsyncHost> HostStart;
public event EventHandler HostExited;
public event DataReceivedEventHandler OutputDataReceived;
public event DataReceivedEventHandler ErrorDataReceived;
public RsyncHost() { }
static readonly Regex HttpSchemeRegex = new(@"^https?://", RegexOptions.Compiled);
static char tolower(char ch) => ch <= 'Z' ? (char)(ch + 32) : ch;
static string BuildArguments(JsonNode conf, string version)
{
foreach (var prop in UpdaterConfig.DefaultConfig.AsObject())
{
var confJObject = conf.AsObject();
if (!confJObject.ContainsKey(prop.Key))
{
confJObject[prop.Key] = prop.Value.DeepClone();
}
}
var ignore_patterns = conf["ignores"].AsArray();
var sb = new StringBuilder();
foreach (var param in conf["rsync_parameters"].AsArray())
sb.Append(param.ToString()).Append(' ');
foreach (var pattern in ignore_patterns)
{
sb.Append($"--exclude='")
.Append(pattern)
.Append("' ");
}
var mirror = conf["mirror"].ToString();
mirror = HttpSchemeRegex.Replace(mirror, "rsync://");
sb.Append(' ')
.Append(mirror)
.Append(conf["rsync_base_addr"])
.Append('/')
.Append(version)
.Append('/');
var path = Path.Combine(conf["install_dir"].ToString(), conf["dir_name"].ToString());
if (path.Length >= 2 && path[1] == ':')
sb.Append(" '/cygdrive/")
.Append(tolower(path[0]))
.Append(path.Substring(2).Replace('\\', '/'))
.Append("/'");
else
sb.Append(' ').Append(path.Replace('\\', '/')).Append('/');
return sb.ToString();
}
public bool Start(string configPath, string version)
{
if (disposed) throw new ObjectDisposedException(nameof(RsyncHost));
if (!Active)
{
var conf = JsonNode.Parse(File.ReadAllText(configPath));
BeginHostStart?.Invoke(this);
rsyncProcess = new Process()
{
EnableRaisingEvents = true,
StartInfo = new ProcessStartInfo()
{
FileName = Path.Combine(Environment.CurrentDirectory, "rsync", "rsync.exe"),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
CreateNoWindow = true,
Arguments = BuildArguments(conf, version)
}
};
rsyncProcess.Exited += HostExited;
rsyncProcess.OutputDataReceived += OutputDataReceived;
rsyncProcess.ErrorDataReceived += ErrorDataReceived;
var result = MessageBox.Show($"将要执行的命令:\n\"{rsyncProcess.StartInfo.FileName}\" {rsyncProcess.StartInfo.Arguments}", "确认执行", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
_started = true;
rsyncProcess.Start();
rsyncProcess.BeginOutputReadLine();
rsyncProcess.BeginErrorReadLine();
HostStart?.Invoke(this);
return true;
}
else
{
rsyncProcess = null;
}
}
else // 如果已经启动
{
// pass
}
return false;
}
public async Task<int?> WaitForExit()
{
if (Active)
{
await Task.Run(rsyncProcess.WaitForExit);
}
return ExitCode;
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if (Active)
{
rsyncProcess.Kill();
rsyncProcess = null;
}
}
disposed = true;
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}