-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUEngine.pas
448 lines (369 loc) · 10.9 KB
/
UEngine.pas
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
unit UEngine;
interface
uses System.Classes, UConfig, System.Generics.Collections, DzDirSeek;
type
TFileOperation = (foAppend, foUpdate, foDelete);
TFileInfo = class
RelativePath: string;
Operation: TFileOperation;
IsDir: Boolean;
Size: Int64;
constructor Create(Item: TDSResultItem; xOperation: TFileOperation);
end;
TLstFileInfo = class(TObjectList<TFileInfo>);
TEngine = class(TThread)
protected
procedure Execute; override;
private
Queue: record
LastTick: Cardinal; //GPU update controller
Log: TStringList;
Status: string;
TotalSize, CurrentSize: Int64;
end;
procedure Log(const Prefix: Char; const Text: string; ForceUpdate: Boolean = True);
procedure Status(const Text: string; ForceUpdate: Boolean = True);
procedure DoDefinition(Def: TDefinition);
procedure CheckForQueueFlush(ForceUpdate: Boolean);
procedure DoScan(Def: TDefinition; LCopy, LDel: TLstFileInfo);
procedure CopyFile(Def: TDefinition; FI: TFileInfo);
procedure DeleteFile(Def: TDefinition; FI: TFileInfo);
procedure CopyStream(Source, Destination: TStream);
public
constructor Create;
destructor Destroy; override;
end;
implementation
uses UFrmMain, System.SysUtils, System.IOUtils, UMasks, UVars,
System.Diagnostics;
constructor TEngine.Create;
begin
inherited Create(True);
FreeOnTerminate := True;
Queue.Log := TStringList.Create;
end;
destructor TEngine.Destroy;
begin
Queue.Log.Free;
inherited;
Synchronize(
procedure
begin
FrmMain.SetControlsState(True); //this will force definitions list invalidate
end);
end;
procedure TEngine.Execute;
var
D: TDefinition;
SW: TStopWatch;
begin
try
for D in Config.Definitions do
begin
if D.Checked then
begin
SW := TStopwatch.StartNew;
DoDefinition(D);
if not Config.SecureMode then
D.LastUpdate := Now; //update definition timestamp
if Queue.TotalSize>0 then
Log(':', 'Total copy size: '+BytesToMB(Queue.TotalSize));
SW.Stop;
Log(':', 'Elapsed time: '+SW.Elapsed.ToString);
end;
end;
except
on E: Exception do
Log('#', 'ERROR: '+E.Message);
end;
CheckForQueueFlush(True); //remaining log
end;
procedure TEngine.Log(const Prefix: Char; const Text: string; ForceUpdate: Boolean);
var
LogFilePrefix: string;
begin
if Config.WriteLogFile then
begin
if CharInSet(Prefix, ['+', '~', '-']) then
LogFilePrefix := Format('(%s)', [Prefix]);
TFile.AppendAllText(pubLogFile, DateTimeToStr(Now)+' - '+LogFilePrefix+Text + #13#10);
end;
Queue.Log.Add(Prefix+Text);
CheckForQueueFlush(ForceUpdate);
end;
procedure TEngine.Status(const Text: string; ForceUpdate: Boolean);
begin
Queue.Status := Text;
CheckForQueueFlush(ForceUpdate);
end;
procedure TEngine.CheckForQueueFlush(ForceUpdate: Boolean);
var
Percent: Byte;
begin
if ForceUpdate or (GetTickCount > Queue.LastTick+1000) then
begin
Synchronize(
procedure
var
A: string;
AtEnd: Boolean;
begin
if Queue.Log.Count>0 then
begin
AtEnd := FrmMain.LLogs.ItemIndex = FrmMain.LLogs.Count-1;
FrmMain.LLogs.Items.BeginUpdate;
try
for A in Queue.Log do
FrmMain.LLogs.Items.Add(A);
if AtEnd then FrmMain.LLogs.ItemIndex := FrmMain.LLogs.Count-1;
finally
FrmMain.LLogs.Items.EndUpdate;
end;
Queue.Log.Clear;
end;
FrmMain.LbStatus.Caption := Queue.Status;
if Queue.TotalSize>0 then
begin
Percent := Trunc(Queue.CurrentSize / Queue.TotalSize * 100);
FrmMain.ProgressBar.Position := Percent;
FrmMain.LbSize.Caption := Format('%s/%s (%d%%)',
[BytesToMB(Queue.CurrentSize), BytesToMB(Queue.TotalSize), Percent]);
end else
begin
FrmMain.ProgressBar.Position := 0;
FrmMain.LbSize.Caption := string.Empty;
end;
if not FrmMain.BtnStop.Enabled then
raise Exception.Create('Process aborted by user');
end);
//
Queue.LastTick := GetTickCount;
end;
end;
constructor TFileInfo.Create(Item: TDSResultItem; xOperation: TFileOperation);
begin
RelativePath := Item.RelativePath;
Size := Item.Size;
IsDir := Item.IsDir;
Operation := xOperation;
end;
procedure TEngine.DoScan(Def: TDefinition; LCopy, LDel: TLstFileInfo);
procedure PrepareDirSeek(DS: TDzDirSeek; const Dir: string; SubDir: Boolean;
const Inclusions, Exclusions: string; HiddenFiles: Boolean);
begin
DS.Dir := Dir;
DS.SubDir := SubDir;
DS.Sorted := True;
DS.UseMask := True;
DS.Inclusions.Text := TMasks.GetMasks(Inclusions);
DS.Exclusions.Text := TMasks.GetMasks(Exclusions);
DS.SearchHiddenFiles := HiddenFiles;
DS.SearchSystemFiles := False;
DS.IncludeDirItem := True;
end;
var
DS_Src, DS_Dest: TDzDirSeek;
Index: Integer;
Item: TDSResultItem;
A: string;
xAdd, xMod, xDel: Integer;
begin
xAdd := 0;
xMod := 0;
xDel := 0;
DS_Src := TDzDirSeek.Create(nil);
DS_Dest := TDzDirSeek.Create(nil);
try
PrepareDirSeek(DS_Src, Def.Source, Def.Recursive, Def.Inclusions, Def.Exclusions, Def.HiddenFiles);
PrepareDirSeek(DS_Dest, Def.Destination, True, string.Empty, string.Empty, False);
Status('Scanning source...');
DS_Src.Seek;
Status('Scanning destination...');
DS_Dest.Seek;
Status('Comparing...');
for Item in DS_Src.ResultList do
begin
Index := DS_Dest.ResultList.IndexOfRelativePath(Item.RelativePath, True);
if Index = -1 then
begin
//new file or folder
if not Item.IsDir then
begin
LCopy.Add(TFileInfo.Create(Item, foAppend));
Inc(xAdd);
end;
end else
begin
//existing file or folder
if not Item.IsDir then
begin
if Item.Timestamp <> DS_Dest.ResultList[Index].Timestamp then
begin
LCopy.Add(TFileInfo.Create(Item, foUpdate));
Inc(xMod);
end;
end;
DS_Dest.ResultList.Delete(Index);
end;
end;
if Def.Delete then
begin
//remaining files in destination list represents removed files
for Item in DS_Dest.ResultList do
begin
//removed file or folder
LDel.Insert(0, TFileInfo.Create(Item, foDelete)); //descending list
Inc(xDel);
end;
end;
finally
DS_Src.Free;
DS_Dest.Free;
end;
A := string.Empty;
if xAdd>0 then A := A + Format(', New: %d', [xAdd]);
if xMod>0 then A := A + Format(', Modified: %d', [xMod]);
if xDel>0 then A := A + Format(', Deleted: %d', [xDel]);
Delete(A, 1, 2);
if A<>string.Empty then
Log(':', A);
//security check for deleting on first execution
if (xDel>0) and (Def.LastUpdate=0) and not Config.SecureMode then
raise Exception.Create(
'For security reasons, synchronization has been canceled,'+
' as it is the first execution of this definition and files and/or folders'+
' to be removed at the destination have been detected.'+
' If you are sure of the definition settings, you will need to disable'+
' the exclusion files option in the definition to proceed with this operation.'
);
end;
procedure TEngine.DoDefinition(Def: TDefinition);
procedure LogAndStatusOperation(FI: TFileInfo; const LogFlag: Char; const StatusPrefix: string);
begin
Log(LogFlag, FI.RelativePath, False);
if not Config.SecureMode then
Status(StatusPrefix+' '+FI.RelativePath, False);
end;
var
LCopy, LDel: TLstFileInfo;
FI: TFileInfo;
begin
Queue.TotalSize := 0;
Queue.CurrentSize := 0;
Log('@', '['+Def.Name+']');
Status(string.Empty);
if Config.SecureMode then
Log('*', '*** SECURE MODE - No changes will be made ***');
if not TDirectory.Exists(Def.Source) then
raise Exception.Create('Source not found');
if not Config.SecureMode then
if not TDirectory.Exists(Def.Destination) then
if not ForceDirectories(Def.Destination) then
raise Exception.Create('Cannot create root destination folder');
LCopy := TLstFileInfo.Create;
LDel := TLstFileInfo.Create;
try
DoScan(Def, LCopy, LDel);
for FI in LCopy do
begin
Queue.TotalSize := Queue.TotalSize + FI.Size;
end;
for FI in LCopy do
begin
case FI.Operation of //Operation foAppend and foUpdate are always file
foAppend:
begin
LogAndStatusOperation(FI, '+', 'Appending');
CopyFile(Def, FI);
end;
foUpdate:
begin
LogAndStatusOperation(FI, '~', 'Updating');
CopyFile(Def, FI);
end;
else raise Exception.Create('Invalid operation');
end;
end;
for FI in LDel do
begin
case FI.Operation of
foDelete:
begin
LogAndStatusOperation(FI, '-', 'Deleting');
DeleteFile(Def, FI);
end;
else raise Exception.Create('Invalid operation');
end;
end;
if (LCopy.Count=0) and (LDel.Count=0) then Log(':', 'Nothing changed');
finally
LCopy.Free;
LDel.Free;
end;
end;
procedure TEngine.CopyFile(Def: TDefinition; FI: TFileInfo);
var
SourceFile, DestFile, DestDirectory: string;
SourceStm, DestStm: TFileStream;
begin
if Config.SecureMode then Exit;
SourceFile := TPath.Combine(Def.Source, FI.RelativePath);
DestFile := TPath.Combine(Def.Destination, FI.RelativePath);
DestDirectory := ExtractFilePath(DestFile);
if not TDirectory.Exists(DestDirectory) then
begin
if not ForceDirectories(DestDirectory) then
raise Exception.Create('Cannot create destination folder');
end;
SourceStm := TFileStream.Create(SourceFile, fmOpenRead or fmShareDenyNone);
try
//if file size changed during process, adjust total size
Queue.TotalSize := Queue.TotalSize + (SourceStm.Size - FI.Size);
DestStm := TFileStream.Create(DestFile, fmCreate);
try
CopyStream(SourceStm, DestStm);
finally
DestStm.Free;
end;
finally
SourceStm.Free;
end;
TFile.SetLastWriteTime(DestFile, TFile.GetLastWriteTime(SourceFile));
end;
procedure TEngine.CopyStream(Source, Destination: TStream);
const
MaxBufSize = $F000;
var
BufSize, N: Integer;
Buffer: TBytes;
Count: Int64;
begin
Count := Source.Size;
if Count > MaxBufSize then BufSize := MaxBufSize else BufSize := Count;
SetLength(Buffer, BufSize);
try
while Count <> 0 do
begin
if Count > BufSize then N := BufSize else N := Count;
Source.ReadBuffer(Buffer, N);
Destination.WriteBuffer(Buffer, N);
Dec(Count, N);
Queue.CurrentSize := Queue.CurrentSize + N;
CheckForQueueFlush(False);
end;
finally
SetLength(Buffer, 0);
end;
end;
procedure TEngine.DeleteFile(Def: TDefinition; FI: TFileInfo);
var
Path: string;
begin
if Config.SecureMode then Exit;
Path := TPath.Combine(Def.Destination, FI.RelativePath);
if FI.IsDir then
TDirectory.Delete(Path)
else
TFile.Delete(Path);
end;
end.