forked from gligli/SuperCopier2
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSCAPI.pas
More file actions
416 lines (343 loc) · 11.5 KB
/
Copy pathSCAPI.pas
File metadata and controls
416 lines (343 loc) · 11.5 KB
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
{
This file is part of SuperCopier2.
SuperCopier2 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
SuperCopier2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
}
unit SCAPI;
interface
uses Windows,Classes,Contnrs,SysUtils,TntSysUtils,
SCCommon,SCWin32,SCLocStrings,SCAPICommon,SCBaseList,SCWorkThreadList,
SCWorkThread,SCCopyThread,SCLocEngine;
type
EAPIError=class(Exception);
EAPIAlreadyRunning=class(EAPIError);
TAPI=class(TThread)
private
FSemaphore:THandle;
FMutex:THandle;
FFileMapping:THandle;
FClientEvent:THandle;
FAPIEvent:THandle;
FFileMappingStream:TFileMappingStream;
FShutdown:Boolean;
FHandleList:TObjectList; //TODO: le système actuel où le handle est l'index d'item leake un peu de mémoire
// en effet, on ne peut pas faire de Delete() sur la liste sinon les handles se décalent ...
// (on mets l'item à nil, ce qui leake 4 octets par handle libéré)
FLastError:TApiError;
FEnabled:Boolean;
function CheckHandle(AHandle:THandle;AClass:TClass):Boolean;
// Fonctions de l'API
procedure APIObjectFree(AHandle:Integer);
function APIGetLastError:TApiError;
function APIErrorMessage(AError:TApiError):WideString;
function APIObjectExists(AHandle:Integer):Boolean;
function APIGetLocString(ALocStringID:Integer):WideString;
function APINewBaseList:Integer;
procedure APIBaselistAddItem(ABaseListHandle:Integer;AItemName:WideString);
function APIIsEnabled:Boolean;
function APIProcessBaseList(ABaseListHandle:Integer;AOperation:Integer;ADestDir:WideString):Integer;
function APIIsSameVolumeMove(ABaseListHandle:Integer;ADestDir:WideString):Boolean;
function APINewCopy(AIsMove:Boolean):Integer;
procedure APICopyAddBaseList(ACopyHandle,ABaseListHandle:Integer;AMode:TBaselistAddMode;ADestDir:WideString);
protected
procedure Execute;override;
public
constructor Create;
destructor Destroy;override;
procedure RemoveHandle(AObject:TObject);
property Enabled:Boolean read FEnabled write FEnabled;
end;
var
API:TAPI=nil;
implementation
uses Math;
{ TAPI }
procedure TAPI.APIBaselistAddItem(ABaseListHandle: Integer;
AItemName: WideString);
var Item:TBaseItem;
begin
if not CheckHandle(ABaseListHandle,TBaseList) then Exit;
Item:=TBaseItem.Create;
Item.SrcName:=AItemName;
Item.IsDirectory:=WideDirectoryExists(AItemName);
(FHandleList[ABaseListHandle] as TBaseList).Add(Item);
FLastError:=aeNone;
end;
procedure TAPI.APICopyAddBaseList(ACopyHandle, ABaseListHandle: Integer;
AMode: TBaselistAddMode; ADestDir: WideString);
begin
if not CheckHandle(ACopyHandle,TCopyThread) or not CheckHandle(ABaseListHandle,TBaseList) then Exit;
(FHandleList[ACopyHandle] as TCopyThread).AddBaseList(FHandleList[ABaseListHandle] as TBaseList,AMode,ADestDir);
FLastError:=aeNone;
end;
function TAPI.APIErrorMessage(AError: TApiError): WideString;
begin
Result:='';
if (AError>=Low(TApiError)) and (AError<=High(TApiError)) then
Result:=SC2_API_ERRORS_NAMES[AError];
end;
function TAPI.APIGetLastError: TApiError;
begin
Result:=FLastError;
end;
function TAPI.APIGetLocString(ALocStringID: Integer): WideString;
begin
Result:='';
FLastError:=aeBadLocStringId;
if (ALocStringID>=Low(LOC_STRINGS_ARRAY)) and (ALocStringID<=High(LOC_STRINGS_ARRAY)) then
begin
Result:=LOC_STRINGS_ARRAY[ALocStringID]^;
FLastError:=aeNone;
end;
end;
function TAPI.APIIsEnabled: Boolean;
begin
Result:=FEnabled;
end;
function TAPI.APIIsSameVolumeMove(ABaseListHandle: Integer;
ADestDir: WideString): Boolean;
var GuessedSrcDir:WideString;
BaseList:TBaseList;
begin
Result:=False;
if not CheckHandle(ABaseListHandle,TBaseList) then Exit;
BaseList:=FHandleList[ABaseListHandle] as TBaseList;
if BaseList.Count=0 then
begin
FLastError:=aeEmptyBaseList;
Exit;
end;
GuessedSrcDir:=WideExtractFilePath(BaseList[0].SrcName);
Result:=SameVolume(GuessedSrcDir,ADestDir);
FLastError:=aeNone;
end;
function TAPI.APINewBaseList: Integer;
begin
Result:=FHandleList.Add(TBaseList.Create);
FLastError:=aeNone;
end;
function TAPI.APINewCopy(AIsMove: Boolean): Integer;
begin
Result:=FHandleList.Add(TCopyThread.Create(AIsMove));
FLastError:=aeNone;
end;
function TAPI.APIObjectExists(AHandle: Integer): Boolean;
begin
Result:=CheckHandle(AHandle,TObject);
FLastError:=aeNone;
end;
procedure TAPI.APIObjectFree(AHandle: Integer);
begin
if not CheckHandle(AHandle,TObject) then Exit;
FHandleList[AHandle].Free;
FHandleList[AHandle]:=nil;
FLastError:=aeNone;
end;
function TAPI.APIProcessBaseList(ABaseListHandle: Integer; AOperation: Integer;
ADestDir: WideString): Integer;
var WorkThread:TWorkThread;
begin
Result:=SC2_API_INVALID_HANDLE;
if not CheckHandle(ABaseListHandle,TBaseList) then Exit;
WorkThread:=WorkThreadList.ProcessBaseList(FHandleList[ABaseListHandle] as TBaseList,AOperation,ADestDir);
if WorkThread=nil then
Result:=SC2_API_INVALID_HANDLE
else
begin
Result:=FHandleList.IndexOf(WorkThread);
if Result=SC2_API_INVALID_HANDLE then
Result:=FHandleList.Add(WorkThread);
end;
FLastError:=aeNone;
end;
function TAPI.CheckHandle(AHandle: THandle;AClass:TClass): Boolean;
begin
Result:=InRange(AHandle,0,FHandleList.Count-1) and (FHandleList[AHandle]<>nil);
if not Result then
FLastError:=aeBadHandle
else
begin
Result:=FHandleList[AHandle] is AClass;
if not Result then
FLastError:=aeWrongHandleType;
end;
end;
constructor TAPI.Create;
var Error:Integer;
begin
inherited Create(True);
FSemaphore:=0;
FMutex:=0;
FFileMapping:=0;
FClientEvent:=0;
FAPIEvent:=0;
FFileMappingStream:=nil;
// Sémaphore
FSemaphore:=SCWin32.CreateSemaphore(nil,0,MaxInt,PWideChar(SessionUniqueAPIIdentifier(SC2_API_SEMAPHORE_ID)));
Error:=GetLastError;
if (Error=ERROR_ALREADY_EXISTS) or (Error=ERROR_ACCESS_DENIED) then
raise EAPIAlreadyRunning.Create(lsAPIAlreadyRunning);
if FSemaphore=0 then
raise EAPIError.Create(lsAPINoSemaphore);
// Mutex
FMutex:=SCWin32.CreateMutex(nil,False,PWideChar(SessionUniqueAPIIdentifier(SC2_API_MUTEX_ID)));
if FMutex=0 then
raise EAPIError.Create(lsAPINoMutex);
// File mapping
FFileMapping:=SCWin32.CreateFileMapping(INVALID_HANDLE_VALUE,nil,PAGE_READWRITE,0,SC2_API_FILEMAPPING_SIZE,PWideChar(SessionUniqueAPIIdentifier(SC2_API_FILEMAPPING_ID)));
if FFileMapping=0 then
raise EAPIError.Create(lsAPINoFileMapping);
// Client event
FClientEvent:=SCWin32.CreateEvent(nil,False,False,PWideChar(SessionUniqueAPIIdentifier(SC2_API_CLIENTEVENT_ID)));
if FClientEvent=0 then
raise EAPIError.Create(lsAPINoEvent);
// API event
FAPIEvent:=SCWin32.CreateEvent(nil,False,False,PWideChar(SessionUniqueAPIIdentifier(SC2_API_APIEVENT_ID)));
if FAPIEvent=0 then
raise EAPIError.Create(lsAPINoEvent);
FFileMappingStream:=TFileMappingStream.Create(FFileMapping,SC2_API_FILEMAPPING_SIZE);
FShutdown:=False;
FreeOnTerminate:=False;
FHandleList:=TObjectList.Create(False);
FLastError:=aeNone;
FEnabled:=False;
Resume;
end;
destructor TAPI.Destroy;
begin
if FMutex<>0 then
begin
// simuler un évènement d'un client pour pouvoir fermer la thread
WaitForSingleObject(FMutex,INFINITE);
try
FFileMappingStream.Seek(0,soFromBeginning);
FFileMappingStream.WriteInteger(Integer(afNone));
FShutdown:=True;
SetEvent(FClientEvent);
WaitForSingleObject(FAPIEvent,INFINITE);
WaitFor;
finally
ReleaseMutex(FMutex);
end;
end;
FHandleList.Free;
FFileMappingStream.Free;
CloseHandle(FAPIEvent);
CloseHandle(FClientEvent);
CloseHandle(FFileMapping);
CloseHandle(FMutex);
CloseHandle(FSemaphore);
inherited;
end;
procedure TAPI.Execute;
var ApiFunction:TApiFunction;
s:WideString;
i,j,k:Integer;
b:boolean;
begin
dbgln('API thread starting');
while not FShutdown do
begin
WaitForSingleObject(FClientEvent,INFINITE);
try
try
with FFileMappingStream do
begin
Seek(0,soBeginning);
ApiFunction:=TApiFunction(ReadInteger);
dbgln('Received API call #'+IntToStr(Ord(ApiFunction)));
// /!\ ne pas appeler plusieurs fois Read... dans les paramètres d'une fonction, car Delphi evalue les params de droite à gauche !!!
case ApiFunction of
afObjectFree:
APIObjectFree(ReadInteger);
afGetLastError:
begin
Seek(0,soBeginning);
WriteInteger(Ord(APIGetLastError));
end;
afErrorMessage:
begin
s:=APIErrorMessage(TApiError(ReadInteger));
Seek(0,soBeginning);
WriteWideString(s);
end;
afObjectExists:
begin
b:=APIObjectExists(ReadInteger);
Seek(0,soBeginning);
WriteInteger(Ord(b));
end;
afGetLocString:
begin
s:=APIGetLocString(ReadInteger);
Seek(0,soBeginning);
WriteWideString(s);
end;
afNewBaseList:
begin
Seek(0,soBeginning);
WriteInteger(APINewBaseList);
end;
afBaselistAddItem:
begin
i:=ReadInteger;
APIBaselistAddItem(i,ReadWideString);
end;
afIsEnabled:
begin
Seek(0,soBeginning);
WriteInteger(Ord(APIIsEnabled));
end;
afProcessBaseList:
begin
i:=ReadInteger;
j:=ReadInteger;
i:=APIProcessBaseList(i,j,ReadWideString);
Seek(0,soBeginning);
WriteInteger(i);
end;
afIsSameVolumeMove:
begin
i:=ReadInteger;
b:=APIIsSameVolumeMove(i,ReadWideString);
Seek(0,soBeginning);
WriteInteger(Ord(b));
end;
afNewCopy:
begin
i:=APINewCopy(ReadInteger<>0);
Seek(0,soBeginning);
WriteInteger(i);
end;
afCopyAddBaseList:
begin
i:=ReadInteger;
j:=ReadInteger;
k:=ReadInteger;
APICopyAddBaseList(i,j,TBaseListAddMode(k),ReadWideString);
end;
end;
end;
finally
SetEvent(FAPIEvent);
end;
except // ne pas laisser les exceptions tuer l'API
on E:Exception do dbgln('API Exception: '+E.Message);
end;
end;
dbgln('API thread ending');
end;
procedure TAPI.RemoveHandle(AObject: TObject);
var Idx:Integer;
begin
Idx:=FHandleList.IndexOf(AObject);
if Idx<>-1 then
FHandleList[Idx]:=nil;
end;
end.