forked from doublecmd/doublecmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathufilesourcecopyoperation.pas
314 lines (257 loc) · 9.36 KB
/
ufilesourcecopyoperation.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
unit uFileSourceCopyOperation;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, syncobjs,
DCOSUtils,
uFileSourceOperation,
uFileSourceOperationTypes,
uFileSourceOperationOptions,
uFileSource,
uFile;
type
// Statistics are the same for CopyIn and CopyOut operations.
PFileSourceCopyOperationStatistics = ^TFileSourceCopyOperationStatistics;
TFileSourceCopyOperationStatistics = record
CurrentFileFrom: String;
CurrentFileTo: String;
CurrentFileTotalBytes: Int64;
CurrentFileDoneBytes: Int64;
TotalFiles: Int64;
DoneFiles: Int64;
TotalBytes: Int64;
DoneBytes: Int64;
BytesPerSecond: Int64;
RemainingTime: TDateTime;
end;
{en
Base class for CopyIn and CopyOut operations.
}
{ TFileSourceCopyOperation }
TFileSourceCopyOperation = class(TFileSourceOperation)
private
FStatistics: TFileSourceCopyOperationStatistics;
FStatisticsAtStartTime: TFileSourceCopyOperationStatistics;
FStatisticsLock: TCriticalSection; //<en For synchronizing statistics.
FSourceFileSource: IFileSource;
FTargetFileSource: IFileSource;
FSourceFiles: TFiles;
FRenameMask: String;
protected
FTargetPath: String;
FCopyAttributesOptions: TCopyAttributesOptions;
FSymLinkOption: TFileSourceOperationOptionSymLink;
FFileExistsOption: TFileSourceOperationOptionFileExists;
FDirExistsOption: TFileSourceOperationOptionDirectoryExists;
protected
function GetID: TFileSourceOperationType; override;
procedure DoReloadFileSources; override;
procedure UpdateStatistics(var NewStatistics: TFileSourceCopyOperationStatistics);
procedure UpdateStatisticsAtStartTime; override;
procedure ShowCompareFilesUI(SourceFile: TFile; const TargetFilePath: String);
procedure ShowCompareFilesUIByFileObject(SourceFile: TFile; TargetFile: TFile);
property TargetPath: String read FTargetPath;
public
{en
@param(aSourceFileSource
File source from which the files will be copied.)
@param(aTargetFileSource
File source to which the files will be copied.)
@param(theSourceFiles
Files which are to be copied.
Class takes ownership of the pointer.)
@param(aTargetPath
Path in the target file source where the files should be copied to.)
}
constructor Create(aSourceFileSource: IFileSource;
aTargetFileSource: IFileSource;
var theSourceFiles: TFiles;
aTargetPath: String); virtual reintroduce;
destructor Destroy; override;
function GetDescription(Details: TFileSourceOperationDescriptionDetails): String; override;
function RetrieveStatistics: TFileSourceCopyOperationStatistics;
property SourceFiles: TFiles read FSourceFiles;
property SourceFileSource: IFileSource read FSourceFileSource;
property TargetFileSource: IFileSource read FTargetFileSource;
property RenameMask: String read FRenameMask write FRenameMask;
property SymLinkOption: TFileSourceOperationOptionSymLink read FSymLinkOption write FSymLinkOption;
property FileExistsOption: TFileSourceOperationOptionFileExists read FFileExistsOption write FFileExistsOption;
property CopyAttributesOptions: TCopyAttributesOptions read FCopyAttributesOptions write FCopyAttributesOptions;
property DirExistsOption: TFileSourceOperationOptionDirectoryExists read FDirExistsOption write FDirExistsOption;
end;
{en
Operation that copies files from another file source into a file source of specific type
(to file system for TFileSystemCopyInOperation,
to network for TNetworkCopyInOperation, etc.).
Source file source must be a file system file source.
(Or is it enough if it's a file source with directly accessible files ? (DirectAccess flag))
Target file source should match the class type.
Example meaning of this operation:
- archive: pack
- network: upload
}
TFileSourceCopyInOperation = class(TFileSourceCopyOperation)
protected
function GetID: TFileSourceOperationType; override;
end;
{en
Operation that copies files into another file source from a file source of specific type
(from file system for TFileSystemCopyOutOperation,
from network for TNetworkCopyOutOperation, etc.).
Source file source should match the class type.
Target file source must be a file system file source.
(Or is it enough if it's a file source with directly accessible files ? (DirectAccess flag))
Example meaning of this operation:
- archive: unpack
- network: download
}
TFileSourceCopyOutOperation = class(TFileSourceCopyOperation)
protected
function GetID: TFileSourceOperationType; override;
end;
implementation
uses
uDCUtils, uLng, uGlobs, uShowForm;
// -- TFileSourceCopyOperation ------------------------------------------------
constructor TFileSourceCopyOperation.Create(aSourceFileSource: IFileSource;
aTargetFileSource: IFileSource;
var theSourceFiles: TFiles;
aTargetPath: String);
begin
with FStatistics do
begin
CurrentFileFrom := '';
CurrentFileTo := '';
TotalFiles := 0;
DoneFiles := 0;
TotalBytes := 0;
DoneBytes := 0;
CurrentFileTotalBytes := 0;
CurrentFileDoneBytes := 0;
BytesPerSecond := 0;
RemainingTime := 0;
end;
FStatisticsLock := TCriticalSection.Create;
case GetID of
fsoCopy,
fsoCopyIn:
// Copy into target - run on target.
inherited Create(aTargetFileSource);
fsoCopyOut:
// Copy out from source - run on source.
inherited Create(aSourceFileSource);
else
raise Exception.Create('Invalid file source type');
end;
FSourceFileSource := aSourceFileSource;
FTargetFileSource := aTargetFileSource;
FSourceFiles := theSourceFiles;
theSourceFiles := nil;
FTargetPath := IncludeTrailingPathDelimiter(aTargetPath);
FRenameMask := '';
if gOperationOptionCopyTime then
FCopyAttributesOptions := FCopyAttributesOptions + [caoCopyTime];
end;
destructor TFileSourceCopyOperation.Destroy;
begin
inherited Destroy;
if Assigned(FStatisticsLock) then
FreeAndNil(FStatisticsLock);
if Assigned(FSourceFiles) then
FreeAndNil(FSourceFiles);
end;
function TFileSourceCopyOperation.GetID: TFileSourceOperationType;
begin
Result:= fsoCopy;
end;
procedure TFileSourceCopyOperation.DoReloadFileSources;
begin
FTargetFileSource.Reload(FTargetPath);
end;
function TFileSourceCopyOperation.GetDescription(Details: TFileSourceOperationDescriptionDetails): String;
begin
case Details of
fsoddJobAndTarget:
begin
if SourceFiles.Count = 1 then
Result := Format(rsOperCopyingSomethingTo, [SourceFiles[0].Name, TargetPath])
else
Result := Format(rsOperCopyingFromTo, [SourceFiles.Path, TargetPath]);
end;
else
Result := rsOperCopying;
end;
end;
procedure TFileSourceCopyOperation.UpdateStatistics(var NewStatistics: TFileSourceCopyOperationStatistics);
begin
FStatisticsLock.Acquire;
try
// Check if the value by which we calculate progress and remaining time has changed.
if FStatistics.DoneBytes <> NewStatistics.DoneBytes then
begin
with NewStatistics do
begin
RemainingTime :=
EstimateRemainingTime(FStatisticsAtStartTime.DoneBytes,
DoneBytes,
TotalBytes,
StartTime,
SysUtils.Now,
BytesPerSecond);
// Update overall progress.
if TotalBytes <> 0 then
UpdateProgress(DoneBytes / TotalBytes);
end;
end;
FStatistics := NewStatistics;
finally
FStatisticsLock.Release;
end;
end;
procedure TFileSourceCopyOperation.UpdateStatisticsAtStartTime;
begin
FStatisticsLock.Acquire;
try
Self.FStatisticsAtStartTime := Self.FStatistics;
finally
FStatisticsLock.Release;
end;
end;
function TFileSourceCopyOperation.RetrieveStatistics: TFileSourceCopyOperationStatistics;
begin
// Statistics have to be synchronized because there are multiple values
// and they all have to be consistent at every moment.
FStatisticsLock.Acquire;
try
Result := Self.FStatistics;
finally
FStatisticsLock.Release;
end;
end;
procedure TFileSourceCopyOperation.ShowCompareFilesUIByFileObject(SourceFile: TFile; TargetFile: TFile);
begin
PrepareToolData(SourceFileSource, SourceFile, TargetFileSource, TargetFile, @ShowDifferByGlobList, True);
end;
procedure TFileSourceCopyOperation.ShowCompareFilesUI(SourceFile: TFile; const TargetFilePath: String);
var
TargetFile: TFile = nil;
begin
TargetFile := TargetFileSource.CreateFileObject(ExtractFilePath(TargetFilePath));
TargetFile.Name := ExtractFileName(TargetFilePath);
try
PrepareToolData(SourceFileSource, SourceFile, TargetFileSource, TargetFile, @ShowDifferByGlobList, True);
finally
TargetFile.Free;
end;
end;
// -- TFileSourceCopyInOperation ----------------------------------------------
function TFileSourceCopyInOperation.GetID: TFileSourceOperationType;
begin
Result := fsoCopyIn;
end;
// -- TFileSourceCopyOutOperation ---------------------------------------------
function TFileSourceCopyOutOperation.GetID: TFileSourceOperationType;
begin
Result := fsoCopyOut;
end;
end.