-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
FileInfoCache.cs
492 lines (402 loc) · 19.2 KB
/
FileInfoCache.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//
// Copyright (c) Roland Pihlakas 2019 - 2023
// roland@simplify.ee
//
// Roland Pihlakas licenses this file to you under the GNU Lesser General Public License, ver 2.1.
// See the LICENSE file for more information.
//
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FolderSync
{
internal class FileInfoRef
{
public CachedFileInfo Value;
public CancellationToken Token;
[DebuggerStepThrough]
public FileInfoRef(CachedFileInfo value, CancellationToken token)
{
Value = value;
Token = token;
}
}
internal partial class ConsoleWatch
{
public static async Task<Dictionary<string, CachedFileInfo>> ReadFileInfoCache(string dirName, bool forHistory)
{
Tuple<byte[], long> cacheDataTuple = null;
if (Global.PersistentCacheDestAndHistoryFolders)
{
var cacheDirName = Extensions.GetLongPath(GetCacheDirName(dirName, forHistory));
var cacheFileName = Path.Combine(cacheDirName, "dircache.dat");
if (await Extensions.FSOperation
(
cancellationAndTimeoutToken => File.Exists(cacheFileName),
cacheFileName,
Global.CancellationToken.Token,
timeout: 0, //NB!
suppressLongRunningOperationMessage: true //NB!
))
{
cacheDataTuple = await FileExtensions.ReadAllBytesAsync
(
cacheFileName,
/*allowVSS: */false,
Global.CancellationToken.Token,
timeout: 0, //NB!
suppressLongRunningOperationMessage: true //NB!
);
}
} //if (Global.PersistentCacheDestAndHistoryFolders)
if (cacheDataTuple?.Item1 != null)
{
try
{
var cachedFileInfosDict = Extensions.DeserializeBinary<Dictionary<string, CachedFileInfo>>(cacheDataTuple.Item1);
//TODO: add parent folders to paths if they were removed
var cachedFileInfosDictUpperKeysOnWindows = cachedFileInfosDict
.ToDictionary
(
kvp => kvp.Key.ToUpperInvariantOnWindows(), //backwards compatibility - old cache files might contain non-uppercase keys
kvp => kvp.Value
);
return cachedFileInfosDictUpperKeysOnWindows;
}
catch (SerializationException)
{
//TODO: log error
}
} //if (cacheDataTuple?.Item1 != null)
return null;
} //public static async Task<Dictionary<string, CachedFileInfo>> ReadFileInfoCache(string dirName, bool forHistory)
public static async Task SaveFileInfoCache(Dictionary<string, CachedFileInfo> dirCache, string dirName, bool forHistory)
{
if (Global.PersistentCacheDestAndHistoryFolders)
{
//BackgroundTaskManager.Run(async () =>
//{
var cacheDirName = Extensions.GetLongPath(ConsoleWatch.GetCacheDirName(dirName, forHistory));
var cacheFileName = Path.Combine(cacheDirName, "dircache.dat");
if (!await Extensions.FSOperation
(
cancellationAndTimeoutToken => Directory.Exists(cacheDirName),
cacheDirName,
Global.CancellationToken.Token,
timeout: 0, //NB!
suppressLongRunningOperationMessage: true //NB!
))
{
await Extensions.FSOperation
(
cancellationAndTimeoutToken => Directory.CreateDirectory(cacheDirName),
cacheDirName,
Global.CancellationToken.Token,
timeout: 0, //NB!
suppressLongRunningOperationMessage: true //NB!
);
}
//TODO: remove parent folders from paths
var serialisedData = Extensions.SerializeBinary(dirCache);
await FileExtensions.WriteAllBytesAsync
(
cacheFileName,
serialisedData,
createTempFileFirst: true,
cancellationToken: Global.CancellationToken.Token,
retryCount: 5, //TODO: config
timeout: 0, //NB!
suppressLongRunningOperationMessage: true //NB!
);
//});
}
}
public static async Task RefreshFileInfo(WatcherContext context)
{
var fileInfo = context.Event.FileSystemInfo as FileInfo;
//var fileInfo = context.Event.FileSystemInfo.AsFileInfo(); // as FileInfo;
if (fileInfo != null && !context.FileInfoRefreshed.Value)
//var fileInfo = context.FileInfo;
//if (!context.FileInfoRefreshed.Value)
{
context.FileInfoRefreshed.Value = true;
await Extensions.FSOperation
(
cancellationAndTimeoutToken =>
{
fileInfo.Refresh(); //https://stackoverflow.com/questions/7828132/getting-current-file-length-fileinfo-length-caching-and-stale-information
if (fileInfo.Exists)
{
var dummyAttributes = fileInfo.Attributes;
var dymmyLength = fileInfo.Length;
var dymmyTime = fileInfo.LastWriteTimeUtc;
}
},
fileInfo.FullName,
context.Token
);
context.FileInfo = new CachedFileInfo(fileInfo);
//this method is called only on src files unless bidirectional mirroring is on.
//so actually there should be no need to update file cache here
//but keeping this code here just in case
if (
!context.IsSrcPath
&& Global.CacheDestAndHistoryFolders
&& (!Global.BidirectionalMirror || context.ForHistory)
)
{
//var fullName = Extensions.GetLongPath(context.Event.FullName);
//Global.DestAndHistoryFileInfoCache[fullName.ToUpperInvariantOnWindows()] = context.FileInfo;
#if true
await SaveFileInfo(context.Event.FullName, context.FileInfo, context.ForHistory, fileInfo.DirectoryName, cancellationToken: context.Token);
#else
if (Global.PersistentCacheDestAndHistoryFolders)
{
var dirName = Extensions.GetLongPath(Extensions.GetDirPathWithTrailingSlash(fileInfo.DirectoryName));
using (await Global.PersistentCacheLocks.LockAsync(dirName.ToUpperInvariantOnWindows(), context.Token))
{
var cachedFileInfos = await ReadFileInfoCache(dirName, context.ForHistory);
if (cachedFileInfos != null) //TODO: if cachedFileInfos == null then scan entire folder and create cached file infos file
{
cachedFileInfos[GetNonFullName(fullName).ToUpperInvariantOnWindows()] = new CachedFileInfo(context.FileInfo, useNonFullPath: true);
await SaveFileInfoCache(cachedFileInfos, dirName, context.ForHistory);
}
}
}
#endif
}
} //if (fileInfo != null && !context.FileInfoRefreshed.Value)
} //public static async Task RefreshFileInfo(Context context)
internal static async Task SaveFileInfo(string fileName, CachedFileInfo fileInfo, bool forHistory, string dirName, CancellationToken cancellationToken)
{
//TODO debounce the persistent cache saves and save entire dir cache together, not reading the cache file again each time
//handle fileInfo == null as file deletion
if (fileInfo == null)
{
#if false
var cachedFileInfos = await ReadFileInfoCache(dirName);
CachedFileInfo dummy;
var removed = Global.FileInfoCache.TryRemove(fileName.ToUpperInvariantOnWindows(), out dummy);
if (removed)
{
//ConsoleWatch.SaveFileInfoCache(Dictionary<string, CachedFileInfo> dirCache, string dirName); //TODO!
}
#else
var fullName = Extensions.GetLongPath(fileName);
CachedFileInfo dummy;
var removed1 = Global.DestAndHistoryFileInfoCache.TryRemove(fileName.ToUpperInvariantOnWindows(), out dummy);
if (Global.PersistentCacheDestAndHistoryFolders && removed1)
{
if (dirName == null)
dirName = FolderSyncNetSource.Path.GetDirectoryName(fileName);
dirName = Extensions.GetLongPath(Extensions.GetDirPathWithTrailingSlash(dirName));
using (await Global.PersistentCacheLocks.LockAsync(dirName.ToUpperInvariantOnWindows(), cancellationToken))
{
var cachedFileInfos = await ReadFileInfoCache(dirName, forHistory);
if (cachedFileInfos != null) //TODO: if cachedFileInfos == null then scan entire folder and create cached file infos file
{
var removed2 = cachedFileInfos.Remove(GetNonFullName(fullName).ToUpperInvariantOnWindows());
if (removed2)
{
await SaveFileInfoCache(cachedFileInfos, dirName, forHistory);
}
}
}
}
#endif
}
else //if (fileInfo == null)
{
var fullName = Extensions.GetLongPath(fileName);
Global.DestAndHistoryFileInfoCache[fullName.ToUpperInvariantOnWindows()] = fileInfo;
if (Global.PersistentCacheDestAndHistoryFolders)
{
if (dirName == null)
dirName = FolderSyncNetSource.Path.GetDirectoryName(fileName);
dirName = Extensions.GetLongPath(Extensions.GetDirPathWithTrailingSlash(dirName));
using (await Global.PersistentCacheLocks.LockAsync(dirName.ToUpperInvariantOnWindows(), cancellationToken))
{
var cachedFileInfos = await ReadFileInfoCache(dirName, forHistory);
if (cachedFileInfos != null) //TODO: if cachedFileInfos == null then scan entire folder and create cached file infos file
{
cachedFileInfos[GetNonFullName(fullName).ToUpperInvariantOnWindows()] = new CachedFileInfo(fileInfo, useNonFullPath: true);
await SaveFileInfoCache(cachedFileInfos, dirName, forHistory);
}
}
}
} //if (fileInfo == null)
} //internal static async Task SaveFileInfo(string fileName, CachedFileInfo fileInfo, CancellationToken cancellationToken = default(CancellationToken))
private static Task<CachedFileInfo> GetFileInfo(WatcherContext context)
{
return GetFileInfo(context.Event.FullName, context.Token, context.IsSrcPath, context.ForHistory);
}
private static async Task<CachedFileInfo> GetFileInfo(string fullName, CancellationToken token, bool isSrcFile, bool forHistory)
{
fullName = Extensions.GetLongPath(fullName);
CachedFileInfo result;
bool useCache = false;
if (
!isSrcFile
&& Global.CacheDestAndHistoryFolders
&& (!Global.BidirectionalMirror || forHistory)
)
{
useCache = true;
if (Global.DestAndHistoryFileInfoCache.TryGetValue(fullName.ToUpperInvariantOnWindows(), out result))
return result;
}
var fileInfo = await Extensions.FSOperation
(
cancellationAndTimeoutToken =>
{
var fileInfo1 = new FileInfo(fullName);
//this will cause the actual filesystem call
if (fileInfo1.Exists)
{
var dummyAttributes = fileInfo1.Attributes;
var dymmyLength = fileInfo1.Length;
var dymmyTime = fileInfo1.LastWriteTimeUtc;
}
return fileInfo1;
},
fullName,
token
);
result = new CachedFileInfo(fileInfo);
if (useCache)
{
Global.DestAndHistoryFileInfoCache[fullName.ToUpperInvariantOnWindows()] = result;
if (Global.PersistentCacheDestAndHistoryFolders)
{
var dirName = Extensions.GetLongPath(Extensions.GetDirPathWithTrailingSlash(fileInfo.DirectoryName));
using (await Global.PersistentCacheLocks.LockAsync(dirName.ToUpperInvariantOnWindows(), token))
{
var cachedFileInfos = await ReadFileInfoCache(dirName, forHistory);
if (cachedFileInfos != null) //TODO: if cachedFileInfos == null then scan entire folder and create cached file infos file
{
cachedFileInfos[GetNonFullName(fullName).ToUpperInvariantOnWindows()] = new CachedFileInfo(result, useNonFullPath: true);
await SaveFileInfoCache(cachedFileInfos, dirName, forHistory);
}
}
}
}
return result;
}
private static async Task<bool> GetIsFile(WatcherContext context)
{
if (context.FileInfo == null)
{
context.FileInfo = await GetFileInfo(context);
}
return !context.FileInfo.Attributes.HasFlag(FileAttributes.Directory);
}
private static async Task<bool> GetFileExists(WatcherContext context)
{
if (context.FileInfo == null)
{
context.FileInfo = await GetFileInfo(context);
}
return context.FileInfo.Exists.Value
&& !context.FileInfo.Attributes.HasFlag(FileAttributes.Directory);
}
private static async Task<bool> GetFileExists(FileInfoRef fileInfo, string fullName, bool isSrcFile, bool forHistory)
{
if (fileInfo.Value == null)
{
fileInfo.Value = await GetFileInfo(fullName, fileInfo.Token, isSrcFile, forHistory);
}
return fileInfo.Value.Exists.Value
&& !fileInfo.Value.Attributes.HasFlag(FileAttributes.Directory);
}
private static async Task<DateTime> GetFileTime(WatcherContext context)
{
if (context.FileInfo == null)
{
context.FileInfo = await GetFileInfo(context);
if (!await GetFileExists(context))
{
return DateTime.MinValue;
}
}
return context.FileInfo.LastWriteTimeUtc;
}
private static async Task<DateTime> GetFileTime(FileInfoRef otherFileInfo, string otherFullName, bool isSrcFile, bool forHistory)
{
if (otherFileInfo.Value == null)
{
otherFileInfo.Value = await GetFileInfo(otherFullName, otherFileInfo.Token, isSrcFile, forHistory);
if (!await GetFileExists(otherFileInfo, otherFullName, isSrcFile, forHistory))
{
return DateTime.MinValue;
}
}
return otherFileInfo.Value.LastWriteTimeUtc;
}
private static async Task<long> GetFileSize(WatcherContext context)
{
if (context.FileInfo.Length == null)
{
context.FileInfo = await GetFileInfo(context);
}
#if false
else
{
await RefreshFileInfo(context);
}
#endif
if (!await GetFileExists(context))
{
return -1;
}
else
{
return context.FileInfo.Length.Value;
}
}
private static async Task<long> GetFileSize(FileInfoRef otherFileInfo, string otherFullName, bool isSrcFile, bool forHistory)
{
if (otherFileInfo.Value == null)
{
otherFileInfo.Value = await GetFileInfo(otherFullName, otherFileInfo.Token, isSrcFile, forHistory);
if (!await GetFileExists(otherFileInfo, otherFullName, isSrcFile, forHistory))
{
return -1;
}
}
//NB! no RefreshFileInfo or GetFileExists calls here
if (!otherFileInfo.Value.Exists.Value)
return -1;
else
return otherFileInfo.Value.Length.Value;
}
public static async Task InvalidateFileDataInPersistentCache(WatcherContext context)
{
if (
Global.PersistentCacheDestAndHistoryFolders
&& context.IsSrcPath
&& (!Global.BidirectionalMirror || context.ForHistory)
)
{
var otherFullName = await GetOtherFullName(context);
var longOtherFullName = Extensions.GetLongPath(otherFullName);
var otherDirName = Extensions.GetDirPathWithTrailingSlash(FolderSyncNetSource.Path.GetDirectoryName(otherFullName));
var longOtherDirName = Extensions.GetLongPath(otherDirName);
using (await Global.PersistentCacheLocks.LockAsync(longOtherDirName.ToUpperInvariantOnWindows(), context.Token))
{
var cachedFileInfos = await ReadFileInfoCache(longOtherDirName, context.ForHistory);
if (cachedFileInfos != null) //TODO: if cachedFileInfos == null then scan entire folder and create cached file infos file
{
cachedFileInfos.Remove(GetNonFullName(longOtherFullName).ToUpperInvariantOnWindows());
await SaveFileInfoCache(cachedFileInfos, longOtherDirName, context.ForHistory);
}
}
}
} //public static async Task InvalidateFileDataInPersistentCache(Context context)
}
}