-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileTools.cs
235 lines (210 loc) · 8.87 KB
/
FileTools.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Threading;
namespace TCSynchronize
{
class FileTools
{
public bool isPathDirectory(string path)
{
FileAttributes fileAttributes = File.GetAttributes(path);
return ((fileAttributes & FileAttributes.Directory) == FileAttributes.Directory);
}
public void rename(string oldPath, string newPath)
{
try
{
// Directory.Move works for directories AND files.
// Check if rename is just a case letters change
if (string.Compare(oldPath, newPath, true) != 0)
{
Directory.Move(oldPath, newPath);
}
else
{
// Rename in two phasis because Directory.Move doesn't handle rename when it's only a case letters change
Directory.Move(oldPath, oldPath + ".tmp");
Directory.Move(oldPath + ".tmp", newPath);
}
}
catch (DirectoryNotFoundException e)
{
// Ignored exceptions
// If the old path does not exist, do nothing
_ = e;
Logger.log(Logger.Level.Debug, $"Ignore exception rename {oldPath} -> {newPath}");
}
}
public void remove(string path)
{
try
{
bool isDirectory = isPathDirectory(path);
if (isDirectory)
{
Directory.Delete(path, true);
}
else
{
File.Delete(path);
}
}
catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException)
{
// Ignored exceptions
// If the path does not exist, do nothing
Logger.log(Logger.Level.Debug, $"Ignore exception remove {path}");
}
}
public void copy(string srcPath, string destPath, bool handleDirectory, Filter filter)
{
try
{
bool isDirectory = isPathDirectory(srcPath);
if (isDirectory)
{
if (handleDirectory)
{
copyDirectory(srcPath, destPath, filter, false);
}
}
else
{
copyFile(srcPath, destPath);
}
}
catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException)
{
// Ignored exceptions
// If the source no longer exists, do nothing
Logger.log(Logger.Level.Debug, $"Ignore exception copy {srcPath} -> {destPath}");
}
}
private void createDirectory(string path)
{
DirectoryInfo destDirectoryInfo = new DirectoryInfo(path);
destDirectoryInfo.Create();
}
public void copyFile(string srcPath, string destPath)
{
FileInfo destFileInfo = new FileInfo(destPath);
if (destFileInfo.Exists)
{
FileAttributes attributes = File.GetAttributes(destPath);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// Make the file RW
attributes &= ~FileAttributes.ReadOnly;
File.SetAttributes(destPath, attributes);
}
}
File.Copy(srcPath, destPath, true);
}
public void copyDirectory(string srcPath, string destPath, Filter filter, bool logsOuput)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
long nScannedEntries = 0;
long nScannedDirectories = 0;
long nUpdatedDirectories = 0;
long nScannedFiles = 0;
long nUpdatedFiles = 0;
createDirectory(destPath);
EnumerationOptions enumerationOptions = new EnumerationOptions();
enumerationOptions.RecurseSubdirectories = true;
IEnumerable<string> srcEntries = Directory.EnumerateFileSystemEntries(srcPath, "*", enumerationOptions);
foreach (string srcEntry in srcEntries)
{
if (!filter.isPathFiltered(srcEntry))
{
int nTries = 0;
while (true)
{
try
{
string destEntry = srcEntry.Replace(srcPath, destPath);
if (isPathDirectory(srcEntry))
{
nScannedDirectories++;
DirectoryInfo destDirectoryInfo = new DirectoryInfo(destEntry);
if (!destDirectoryInfo.Exists)
{
nUpdatedDirectories++;
//Logger.log(Logger.Level.Info, srcEntry);
destDirectoryInfo.Create();
}
else
{
// Check if the directory name is the same (case sensitive)
string name = destDirectoryInfo.Name;
string realName = destDirectoryInfo.Parent.GetFileSystemInfos(destDirectoryInfo.Name)[0].Name;
if (string.Compare(name, realName) != 0)
{
nUpdatedDirectories++;
rename(destEntry, destEntry);
}
}
}
else
{
nScannedFiles++;
FileInfo srcFileInfo = new FileInfo(srcEntry);
FileInfo destFileInfo = new FileInfo(destEntry);
if (!destFileInfo.Exists
|| (srcFileInfo.LastWriteTimeUtc.CompareTo(destFileInfo.LastWriteTimeUtc) == 1)
|| (srcFileInfo.Length != destFileInfo.Length))
{
nUpdatedFiles++;
//Logger.log(Logger.Level.Info, srcEntry);
copyFile(srcEntry, destEntry);
}
else
{
// Check if the directory name is the same (case sensitive)
string name = destFileInfo.Name;
string realName = destFileInfo.Directory.GetFileSystemInfos(destFileInfo.Name)[0].Name;
if (string.Compare(name, realName) != 0)
{
nUpdatedFiles++;
rename(destEntry, destEntry);
}
}
}
break;
}
catch (Exception e)
{
nTries++;
if (nTries >= 10)
{
Logger.log(Logger.Level.Info, $"Synchronization in error on {srcEntry} after 10 attemps.");
throw e;
}
else
{
Thread.Sleep(500);
}
}
}
}
nScannedEntries++;
if (logsOuput)
{
if (nScannedEntries >= 1 && (nScannedEntries % 5000) == 0)
{
Logger.log(Logger.Level.Info, $"Number of scanned entries: {nScannedEntries}");
}
}
}
stopwatch.Stop();
if (logsOuput)
{
Logger.log(Logger.Level.Info, $"Synchronization completed in {stopwatch.ElapsedMilliseconds}ms");
Logger.log(Logger.Level.Info, $"Directories: {nScannedDirectories} scanned, {nUpdatedDirectories} updated");
Logger.log(Logger.Level.Info, $"Files: {nScannedFiles} scanned, {nUpdatedFiles} updated");
}
}
}
}