-
Notifications
You must be signed in to change notification settings - Fork 8
/
SynchronizeDirectories.cs
200 lines (181 loc) · 7.15 KB
/
SynchronizeDirectories.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.IO;
using WinSCP;
namespace ValheimSaveShield
{
public class SynchronizeDirectories
{
private static Exception _lasterror;
public static Exception LastError
{
get
{
return _lasterror;
}
}
public static int remoteSync(string hostUrl, string port, string hostDirectory, string localDirectory, string userName, string password, FtpMode ftpMode)
{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = hostUrl,
PortNumber = Int32.Parse(port),
UserName = userName,
Password = password,
FtpMode = ftpMode
};
using (Session session = new Session())
{
// Will continuously report progress of synchronization
session.FileTransferred += FileTransferred;
// Connect
session.Open(sessionOptions);
// Synchronize files
SynchronizationResult synchronizationResult;
synchronizationResult =
session.SynchronizeDirectories(
SynchronizationMode.Local, localDirectory,
hostDirectory, false);
// Throw on any error
synchronizationResult.Check();
}
return 0;
}
catch (Exception e)
{
_lasterror = e;
System.Diagnostics.Debug.WriteLine("Error: {0}", e);
return 1;
}
}
public static int downloadWorlds(string hostUrl, string port, string hostDirectory, string localDirectory, string userName, string password, FtpMode ftpMode)
{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = hostUrl,
PortNumber = Int32.Parse(port),
UserName = userName,
Password = password,
FtpMode = ftpMode
};
using (Session session = new Session())
{
// Will continuously report progress of synchronization
session.FileTransferred += FileTransferred;
// Connect
session.Open(sessionOptions);
//download
//transfer .fwl files first since they don't trigger a backup
var transferResult = session.GetFilesToDirectory(hostDirectory, localDirectory, "*.fwl");
transferResult.Check();
//transfer .db files next, but under a different file extension to delay triggering a backup
transferResult = session.GetFiles($"{hostDirectory}/*.db", $@"{localDirectory}\*.dbftp");
transferResult.Check();
foreach (TransferEventArgs transfer in transferResult.Transfers)
{
//rename finished .db downloads to trigger a backup
var dbName = transfer.Destination.Replace(".dbftp", ".db");
if (File.Exists(dbName)) File.Delete(dbName);
var fi = new FileInfo(transfer.Destination);
fi.MoveTo(fi.FullName.Replace(transfer.Destination, dbName));
}
}
return 0;
}
catch (Exception e)
{
_lasterror = e;
System.Diagnostics.Debug.WriteLine("Error: {0}", e);
return 1;
}
}
private static void FileTransferred(object sender, TransferEventArgs e)
{
if (e.Error == null)
{
System.Diagnostics.Debug.WriteLine("Upload of {0} succeeded", e.FileName);
}
else
{
System.Diagnostics.Debug.WriteLine("Upload of {0} failed: {1}", e.FileName, e.Error);
}
if (e.Chmod != null)
{
if (e.Chmod.Error == null)
{
System.Diagnostics.Debug.WriteLine(
"Permissions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions);
}
else
{
System.Diagnostics.Debug.WriteLine(
"Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error);
}
}
else
{
System.Diagnostics.Debug.WriteLine("Permissions of {0} kept with their defaults", e.Destination);
}
if (e.Touch != null)
{
if (e.Touch.Error == null)
{
System.Diagnostics.Debug.WriteLine(
"Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime);
}
else
{
System.Diagnostics.Debug.WriteLine(
"Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error);
}
}
else
{
// This should never happen during "local to remote" synchronization
System.Diagnostics.Debug.WriteLine(
"Timestamp of {0} kept with its default (current time)", e.Destination);
}
}
public static void uploadFile(string hostUrl, string port, string hostDirectory, string localFile, string userName, string password, FtpMode ftpMode)
{
try
{
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = hostUrl,
PortNumber = Int32.Parse(port),
UserName = userName,
Password = password,
FtpMode = ftpMode
};
using (Session session = new Session())
{
// Will continuously report progress of synchronization
session.FileTransferred += FileTransferred;
// Connect
session.Open(sessionOptions);
// Upload
var uploadResult = session.PutFileToDirectory(localFile, hostDirectory);
// Throw on any error
if (uploadResult.Error != null)
{
throw uploadResult.Error;
}
}
}
catch (Exception e)
{
throw e;
}
}
}
}