forked from fmd-project-team/FMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.lpr
119 lines (111 loc) · 2.68 KB
/
updater.lpr
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
program updater;
uses
{$ifdef windows}
windows,
{$else}
fileutil,
{$endif}
sysutils, process;
const
RetryCount = 30;
var
exefile, zipexefile, updatepackagefile, maindir, ozipexefile, oexefile: String;
counter: Integer;
{$R *.res}
procedure cleanupsuccess;
begin
DeleteFile(oexefile);
DeleteFile(ozipexefile);
DeleteFile(updatepackagefile);
end;
procedure cleanupfailed;
begin
if FileExists(oexefile) then
begin
if FileExists(exefile) then
DeleteFile(oexefile)
else
RenameFile(oexefile,exefile);
end;
if FileExists(ozipexefile) then
begin
if FileExists(zipexefile) then
DeleteFile(ozipexefile)
else
RenameFile(ozipexefile,zipexefile);
end;
end;
begin
if ParamCount<4 then
begin
writeln('Parameters insuficient, ',ParamCount,' of 4');
readln;
exit;
end;
exefile:=ParamStr(1);
zipexefile:=ParamStr(2);
updatepackagefile:=ParamStr(3);
maindir:=ParamStr(4);
if not (FileExists(zipexefile) or FileExists(updatepackagefile)) then Exit;
oexefile:=ExtractFilePath(exefile)+'old_'+ExtractFileName(exefile);
if FileExists(oexefile) then DeleteFile(oexefile);
while true do
begin
counter:=0;
{$ifdef windows}
windows.CopyFile(pchar(exefile),pchar(oexefile),false);
{$else}
CopyFile(exefile,oexefile);
{$endif}
while FileExists(exefile) do begin
if DeleteFile(exefile) then break;
Inc(counter);
writeln('Waiting to close ',counter,'/',RetryCount,' ',exefile);
Sleep(1000);
if counter=RetryCount then break;
end;
if not FileExists(exefile) then
begin
with TProcess.Create(nil) do try
ozipexefile:=ExtractFilePath(zipexefile)+'old_'+ExtractFileName(zipexefile);
if FileExists(ozipexefile) then
DeleteFile(ozipexefile);
RenameFile(zipexefile,ozipexefile);
Executable:=ozipexefile;
CurrentDirectory:=maindir;
Parameters.Add('x');
Parameters.Add(updatepackagefile);
Parameters.Add('-o'+AnsiQuotedStr(maindir,'"'));
Parameters.Add('-aoa');
Options:=Options+[poWaitOnExit];
Execute;
if ExitStatus=0 then
begin
cleanupsuccess;
Executable:=exefile;
Parameters.Clear;
Options:=Options-[poWaitOnExit];
Execute;
Break;
end
else
begin
cleanupfailed;
writeln;
writeln('Press enter to retry.');
readln;
end;
finally
Free;
end;
end
else
begin
cleanupfailed;
writeln;
writeln('Can''t delete file ',exefile);
writeln('Press enter to retry.');
readln;
end;
end;
end.