-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUpdateThread.pas
616 lines (514 loc) · 16 KB
/
UpdateThread.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
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
{**********************************************************************}
{ }
{ UpdateThread }
{ for ARSSE }
{ }
{ Copyright (c) 2008-2009 Harsányi László (a.k.a. KeFear }
{ Copyright (c) 2008-2011 Gregor A. Cieslak (a.k.a. Shoozza) }
{ All rights reserved }
{ }
{ NOT free to distribute or modify }
{ }
{**********************************************************************}
unit UpdateThread;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ComCtrls, Menus, ExtCtrls, IdHTTP, ShellApi,
VersionInfo;
const
// update files
CHANGESFILE = 'changes' + CHANGESPERFIX + VERSIONSTATUS + '.txt';
UPDATEFILE = 'update' + CHANGESPERFIX + VERSIONSTATUS + '.dat';
UPDATEWEBFILE = 'update.dat.php?v=' + VERSIONSTATUS;
type
TUpdateThread = class(TThread)
private
{ Private declarations }
protected
procedure DoUpdate;
procedure ShowRestartQuestion;
procedure ShowUptoDate;
procedure Execute; override;
public
UpdateTime: TDateTime;
IsAuto: boolean;
end;
function VersionCheck(CurrentVersion: string; UpdateVersion: string): Boolean;
function IsFileInUse(const fName: TFileName): Boolean;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure UpdateThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ UpdateThread }
uses Unit1, StrUtils;
var
IdHttp1: TIdHTTP;
FileS: TFileStream;
F: TextFile;
FARSSE: file;
text, filename: string;
temppath: array[0..MAX_PATH] of char;
needrestart: boolean;
procedure TUpdateThread.ShowUptoDate;
begin
ShowMessage('Your version is up-to-date.')
end;
function IsFileInUse(const fName: TFileName): Boolean;
var
HFileRes: HFILE;
begin
HFileRes := CreateFile(PChar(fName),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;
procedure TUpdateThread.ShowRestartQuestion;
var
delay : byte;
begin
if needrestart and (IDYes = MessageBoxA(Application.Handle,
'Update finished, and ARSSE needs a restart. Do you wish to restart ARSSE now?',
'Restart', MB_YesNo + MB_IconQuestion + MB_DefButton2)) then
begin
Form1.SaveConfig(ExtractFilePath(Application.ExeName) + 'arsse.ini');
ShellExecute(0, nil, PChar(Application.ExeName), Pchar(''), Pchar(''),
SW_NORMAL);
delay:=0;
while not IsFileInUse(Application.ExeName) and (delay<37) do
begin
sleep(100);
delay := delay+1;
end;
if delay >= 37 then
begin
ShowMessage('Restarting failed. Closing in 2 seconds');
Sleep(2000);
end;
Application.Terminate;
end;
end;
function HTTPFileExists(aURL: string): Boolean;
begin
with TIdHTTP.Create(nil) do
try
try
Head(aURL);
Result := ResponseCode = 200;
except
Result := False;
end;
finally
Free;
end;
end;
function VersionCheck(CurrentVersion: string; UpdateVersion: string): Boolean;
var
DotPos: array[1..3] of integer;
VersionNum, UpdateNum: array[1..4] of integer;
begin
// example: 'v22.33.44.111'
DotPos[1] := PosEx('.', CurrentVersion);
DotPos[2] := PosEx('.', CurrentVersion, DotPos[1] + 1);
DotPos[3] := PosEx('.', CurrentVersion, DotPos[2] + 1);
// check if version number is correnct
if (CurrentVersion[1] <> 'v') or (DotPos[1] = 0) or (DotPos[2] = 0) or
(DotPos[3] = 0) then
begin
Result := False;
exit;
end;
VersionNum[1] := StrToIntDef(Copy(CurrentVersion, 2, DotPos[1] - 2), -1);
VersionNum[2] := StrToIntDef(Copy(CurrentVersion, DotPos[1] + 1, DotPos[2] -
DotPos[1] - 1), -1);
VersionNum[3] := StrToIntDef(Copy(CurrentVersion, DotPos[2] + 1, DotPos[3] -
DotPos[2] - 1), -1);
VersionNum[4] := StrToIntDef(Copy(CurrentVersion, DotPos[3] + 1,
Length(CurrentVersion) - DotPos[3]), -1);
// check if version number was really a number
if (VersionNum[1] = -1) or (VersionNum[2] = -1) or (VersionNum[3] = -1) or
(VersionNum[4] = -1) then
begin
Result := False;
exit;
end;
// same for UpdateVersion
DotPos[1] := PosEx('.', UpdateVersion);
DotPos[2] := PosEx('.', UpdateVersion, DotPos[1] + 1);
DotPos[3] := PosEx('.', UpdateVersion, DotPos[2] + 1);
// check if version number is correnct
if (UpdateVersion[1] <> 'v') or (DotPos[1] = 0) or (DotPos[2] = 0) or
(DotPos[3] = 0) then
begin
Result := False;
exit;
end;
UpdateNum[1] := StrToIntDef(Copy(UpdateVersion, 2, DotPos[1] - 2), -1);
UpdateNum[2] := StrToIntDef(Copy(UpdateVersion, DotPos[1] + 1, DotPos[2] -
DotPos[1] - 1), -1);
UpdateNum[3] := StrToIntDef(Copy(UpdateVersion, DotPos[2] + 1, DotPos[3] -
DotPos[2] - 1), -1);
UpdateNum[4] := StrToIntDef(Copy(UpdateVersion, DotPos[3] + 1,
Length(UpdateVersion) - DotPos[3]), -1);
// check if version number was really a number
if (UpdateNum[1] = -1) or (UpdateNum[2] = -1) or (UpdateNum[3] = -1) or
(UpdateNum[4] = -1) then
begin
Result := False;
exit;
end;
// check if UpdateVersion is newer then CurrentVersion
if (UpdateNum[1] > VersionNum[1]) or
(UpdateNum[1] = VersionNum[1]) and (UpdateNum[2] > VersionNum[2]) or
(UpdateNum[1] = VersionNum[1]) and (UpdateNum[2] = VersionNum[2]) and
(UpdateNum[3] > VersionNum[3]) or
(UpdateNum[1] = VersionNum[1]) and (UpdateNum[2] = VersionNum[2]) and
(UpdateNum[3] = VersionNum[3]) and (UpdateNum[4] > VersionNum[4]) then
begin
Result := True;
end
else
Result := False;
end;
procedure TUpdateThread.DoUpdate;
const
HOST = 'http://arsse.u13.net/';
var
filelist, filenamelist: TStrings;
i: integer;
var
extension: string;
begin
// TODO:
// Make sure we can write to the ARSSE folder (request admin rights)
// Rename the files we are coping to
// Delete renamed files after successful copying
// Rename files back when copying fails
// Check downloaded files with CRC or MD5
GetTempPath(SizeOf(temppath) - 1, temppath);
needrestart := false;
// delete temporary files from last update
if (fileexists(StrPas(temppath) + UPDATEFILE)) then
begin
if not DeleteFile(StrPas(temppath) + UPDATEFILE) then
begin
if not IsAuto then
ShowMessage('Cannot delete temporary file.' + #13#10 +
'Update Canceled.');
exit;
end;
end;
IdHttp1 := TIdHttp.Create(nil); // does this need special checking?
// check if serverfile is reachable
if (not HTTPFileExists(HOST + UPDATEWEBFILE)) then
begin
if not IsAuto then
ShowMessage('Cannot find file on the server.' + #13#10 +
'Are you connected to the internet?');
// clean up
IdHttp1.Free;
exit;
end;
// download update file
try
FileS := TFileStream.Create(StrPas(temppath) + UPDATEFILE, fmcreate);
IdHttp1.Get(HOST + UPDATEWEBFILE, FileS);
except
if not IsAuto then
ShowMessage('Cannot download update information file.' + #13#10 +
'Are you connected to the internet?');
// clean up
IdHttp1.Free;
FileS.Free;
exit;
end;
// clean up
IdHttp1.Free;
FileS.Free;
// check if information file was downloaded
if not fileexists(StrPas(temppath) + UPDATEFILE) then
begin
if not IsAuto then
ShowMessage('Cannot find downloaded update information file.' + #13#10 +
'Update canceled.');
exit;
end;
// check if update file can be opened
try
AssignFile(F, StrPas(temppath) + UPDATEFILE);
FileMode := fmOpenRead;
Reset(F);
except
if not IsAuto then
ShowMessage('Cannot open update information file.' + #13#10 +
'Update canceled.');
// just to be sure...
try
CloseFile(F);
finally;
end;
exit;
end;
try
filelist := TStringlist.Create();
except
if not IsAuto then
ShowMessage('Cannot allocate list of files.' + #13#10 +
'Update canceled.');
CloseFile(F);
exit;
end;
// loop through the update information file and download updates
while (not Eof(F)) and (not terminated) do
begin
ReadLn(F, text);
// only handle correct lines
if not Form1.Matches('v*.*.*.*|http://*/*.*^', text) then
begin
continue;
end;
// skip old version files
if (not VersionCheck('v' + VERSION + '.' + VERSIONBUILD, Copy(text, 1,
Pos('|', text) - 1))) then
begin
continue;
end;
// cut out the http://... link
filename := Copy(text, Pos('|', text) + 1, Pos('^', text) -
Pos('|', text) - 1);
// add to our download list
filelist.Add(filename);
end;
CloseFile(F);
// check if there are any updates available
if filelist.Count=0 then
begin
filelist.Free;
if not IsAuto then
ShowMessage('Your version is up-to-date.');
exit;
end;
// ask if we want to update
if IDno = MessageBoxA(Application.Handle,
'New Version of ARSSE available! Do you wish to download updates?',
'New version of ARSSE', MB_YesNo + MB_IconQuestion + MB_DefButton2)
then
begin
filelist.Free;
exit;
end;
i := 0;
// check if server has the file we want to download
try
while i<filelist.Count do
begin
if not HTTPFileExists(filelist[i]) then
begin
showMessage('File not found on the server:' + #13#10 +
filelist[i]+ #13#10 + 'Update Canceled.');
filelist.Free;
exit;
end;
i := i + 1;
end;
except
try
showMessage('Error while checking for webserver files:' +
#13#10 + 'Update Canceled.');
filelist.Free;
finally;
end;
exit;
end;
i := 0;
filenamelist := nil;
// download all files into the temp folder
try
filenamelist := TStringlist.Create();
while i<filelist.Count do
begin
filename := filelist[i];
// extract filename
while Pos('/', filename) > 0 do
begin
filename := Copy(filename, Pos('/', filename) + 1, Length(filename));
end;
// remove additional html link stuff
if Pos('?', filename)>0 then
begin
filename:=Copy(filename,1,Pos('?', filename) - 1);
end;
extension := filename;
// get real extension
while Pos('.', extension) > 0 do
begin
extension := Copy(extension, Pos('.', extension) + 1,
Length(extension));
end;
// assume that if the extension is exe it must be the application exe
if(extension = 'exe') then
begin
filename := ExtractFileName(Application.ExeName);
end
else if(extension = 'php') then // remove .php
begin
filename := Copy(filename, 1, Pos('.php', filename) - 1);
end;
// cleanup tempfile
if fileexists(StrPas(temppath) + filename) then
begin
if not DeleteFile(StrPas(temppath) + filename) then
begin
ShowMessage('Cannot prepare temporary files.' + #13#10 +
'Update Canceled.');
filelist.Free;
filenamelist.Free;
exit;
end;
end;
// creating tempfile for downloading
try
FileS := TFileStream.Create(StrPas(temppath) + filename, fmcreate)
except
ShowMessage('Could not create tempfile.' + #13#10 + filename +
'Update Canceled.');
filelist.Free;
filenamelist.Free;
exit;
end;
filenamelist.Add(filename); // we need to move them to arsse folder later
// Downloading updates
try
IdHttp1 := TIdHttp.Create(nil);
IdHttp1.Get(filelist[i], FileS);
except
ShowMessage('File download error:' + #13#10 + filelist[i] + #13#10 +
'Update Canceled.');
try
filelist.Free;
filenamelist.Free;
FileS.Free;
IdHttp1.Free;
finally;
end;
exit;
end;
//clean up
IdHttp1.Free;
FileS.Free;
i := i + 1;
end;
except
ShowMessage('Error while downloading files:' + #13#10 +
'Update Canceled.');
// clean up
try
filelist.Free;
filenamelist.Free;
FileS.Free;
IdHttp1.Free;
finally
end;
exit;
end;
// clean up
filelist.Free;
i := 0;
// copy temporary files into arsse folder
while i < filenamelist.Count do
begin
extension := filenamelist[i];
// get extension
while Pos('.', extension) > 0 do
begin
extension := Copy(extension, Pos('.', extension) + 1, Length(extension));
end;
try
AssignFile(FARSSE, ExtractFilePath(Application.ExeName) + filenamelist[i]);
if extension = 'exe' then
begin
Rename(FARSSE, ExtractFilePath(Application.ExeName) + filenamelist[i] +
'_old');
CloseFile(FARSSE);
end
else if not (extension = 'adb') then
begin
if (FileExists(ExtractFilePath(Application.ExeName) + filenamelist[i]))
and (not DeleteFile(ExtractFilePath(Application.ExeName) +
filenamelist[i])) then
begin
ShowMessage('Could not delete file:' + #13#10 +
ExtractFilePath(Application.ExeName) + filenamelist[i] + #13#10 +
'Update Canceled.');
try
filenamelist.Free;
CloseFile(FARSSE);
finally
end;
exit;
end;
CloseFile(FARSSE);
end;
except
ShowMessage('Copying temporary files into arsse folder failed.' + #13#10 +
'Update Canceled.');
try
filenamelist.Free;
CloseFile(FARSSE);
finally
end;
exit;
end;
if extension = 'adb' then
begin
if not movefile(PAnsiChar(StrPas(temppath) + filenamelist[i]),
PAnsiChar(ExtractFilePath(Application.ExeName)+ '\\data\\' +
filenamelist[i] + '_new')) then
begin
ShowMessage('Error copying temporary file into arsse folder.' + #13#10 +
'Update Canceled.');
filenamelist.Free;
exit;
end;
end
else
begin
if not movefile(PAnsiChar(StrPas(temppath) + filenamelist[i]),
PAnsiChar(ExtractFilePath(Application.ExeName) + filenamelist[i]))
then
begin
ShowMessage('Error copying temporary file into arsse folder.' + #13#10 +
'Update Canceled.');
filenamelist.Free;
exit;
end;
end;
Inc(i);
end;
filenamelist.Free;
needrestart := true;
UpdateTime := Date;
Synchronize(ShowRestartQuestion);
end;
procedure TUpdateThread.Execute;
begin
// Synchronize(DoUpdate);
if not Terminated then
begin
Synchronize(DoUpdate);
// we want to use visual compoments thats why Synchronize
PostMessage(Form1.Handle, WM_UPDATE_COMPLETE, 0, 0);
end;
end;
end.