-
Notifications
You must be signed in to change notification settings - Fork 4
/
fpcuputil.pas
executable file
·974 lines (913 loc) · 33.5 KB
/
fpcuputil.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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
{ Utility unit for FPCUp
Copyright (C) 2012-2014 Reinier Olislagers, Ludo Brands
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program 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 Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit fpcuputil;
{ Utility functions that might be needed by fpcup core and plugin units }
{$mode objfpc}{$H+}
{Define NOCONSOLE e.g. if using Windows GUI {$APPTYPE GUI} or -WG
this will disable writeln calls
}
{not $DEFINE NOCONSOLE}
interface
uses
Classes, SysUtils, eventlog;
type
{ TLogger }
TLogger = class(TObject)
private
FLog: TEventLog; //Logging/debug output to file
function GetLogFile: string;
procedure SetLogFile(AValue: string);
public
// Write to log and optionally console with seriousness etInfo
procedure WriteLog(Message: string; ToConsole: Boolean=true);
// Write to log and optionally console with specified seriousness
procedure WriteLog(EventType: TEventType;Message: string; ToConsole: Boolean);
property LogFile: string read GetLogFile write SetLogFile ;
constructor Create;
destructor Destroy; override;
end;
// Create shortcut on desktop to Target file
procedure CreateDesktopShortCut(Target, TargetArguments, ShortcutName: string) ;
// Create shell script in ~ directory that links to Target
procedure CreateHomeStartLink(Target, TargetArguments, ShortcutName: string);
{$IFDEF MSWINDOWS}
// Delete shortcut on desktop
procedure DeleteDesktopShortcut(ShortcutName: string);
{$ENDIF MSWINDOWS}
// Delete directory and children, even read-only. Equivalent to rm -rf <directory>:
function DeleteDirectoryEx(DirectoryName: string): boolean;
// Recursively delete files with specified name(s), only if path contains specfied directory name somewhere (or no directory name specified):
function DeleteFilesSubDirs(const DirectoryName: string; const Names:TStringList; const OnlyIfPathHas: string): boolean;
// Recursively delete files with specified extension(s),
// only if path contains specfied directory name somewhere (or no directory name specified):
function DeleteFilesExtensionsSubdirs(const DirectoryName: string; const Extensions:TstringList; const OnlyIfPathHas: string): boolean;
// Download from HTTP (includes Sourceforge redirection support) or FTP
// HTTP download can work with http proxy
function Download(URL, TargetFile: string; HTTPProxyHost: string=''; HTTPProxyPort: string=''; HTTPProxyUser: string=''; HTTPProxyPassword: string=''): boolean;
// File size; returns 0 if empty, non-existent or error.
function FileSizeUTF8(FileName: string): int64;
{$IFDEF MSWINDOWS}
// Get Windows major and minor version number (e.g. 5.0=Windows 2000)
function GetWin32Version(var Major,Minor,Build : Integer): Boolean;
{$ENDIF}
//check if there is at least one directory between Dir and root
function ParentDirectoryIsNotRoot(Dir:string):boolean;
{$IFDEF MSWINDOWS}
// Get path for Windows per user storage of application data. Useful for storing settings
function GetLocalAppDataPath: string;
{$ENDIF MSWINDOWS}
// Shows non-debug messages on screen (no logging); also shows debug messages if DEBUG defined
procedure infoln(Message: string; Level: TEventType);
// Moves file if it exists, overwriting destination file
function MoveFile(const SrcFilename, DestFilename: string): boolean;
// Like ExpandFilename but does not expand an empty string to current directory
function SafeExpandFileName (Const FileName : String): String;
// Like ExpandFilenameUTF8 but does not expand an empty string to current directory
function SafeExpandFileNameUTF8 (Const FileName : String): String;
// Copies specified resource (e.g. fpcup.ini, settings.ini)
// to application directory
procedure SaveInisFromResource(filename,resourcename:string);
// Searches for SearchFor in the stringlist and returns the index if found; -1 if not
// Search optionally starts from position SearchFor
function StringListStartsWith(SearchIn: TStringList; SearchFor: string; StartIndex: integer=0): integer;
{$IFDEF UNIX}
function XdgConfigHome: String;
{$ENDIF UNIX}
// Emulates/runs which to find executable in path. If not found, returns empty string
function Which(Executable: string): string;
implementation
uses
httpsend {for downloading from http},
ftpsend {for downloading from ftp},
FileUtil {lazutils, for utf8 functions?!?},
strutils
{$IFDEF MSWINDOWS}
//Mostly for shortcut code
,windows, shlobj {for special folders}, ActiveX, ComObj
{$ENDIF MSWINDOWS}
{$IFDEF UNIX}
,baseunix,processutils
{$ENDIF UNIX}
;
{$ifdef mswindows}
function GetWin32Version(var Major,Minor,Build : Integer): Boolean;
var
Info: TOSVersionInfo;
begin
Info.dwOSVersionInfoSize := SizeOf(Info);
if GetVersionEx(Info) then
begin
with Info do
begin
Win32Platform:=dwPlatformId;
Major:=dwMajorVersion;
Minor:=dwMinorVersion;
Build:=dwBuildNumber;
result:=true
end;
end
else
result:=false;
end;
{$endif}
function SafeExpandFileName (Const FileName : String): String;
begin
if FileName='' then
result:=''
else
result:=ExpandFileName(FileName);
end;
function SafeExpandFileNameUTF8 (Const FileName : String): String;
begin
if FileName='' then
result:=''
else
result:=ExpandFileNameUTF8(FileName);
end;
procedure SaveInisFromResource(filename,resourcename:string);
var fs:Tfilestream;
begin
with TResourceStream.Create(hInstance, resourcename, 'file') do
try
try
fs:=Tfilestream.Create(Filename,fmCreate);
Savetostream(fs);
finally
fs.Free;
end;
finally
Free;
end;
end;
{$IFDEF MSWINDOWS}
procedure CreateDesktopShortCut(Target, TargetArguments, ShortcutName: string);
var
IObject: IUnknown;
ISLink: IShellLink;
IPFile: IPersistFile;
PIDL: PItemIDList;
InFolder: array[0..MAX_PATH] of Char;
LinkName: WideString;
begin
{ Creates an instance of IShellLink }
IObject := CreateComObject(CLSID_ShellLink);
ISLink := IObject as IShellLink;
IPFile := IObject as IPersistFile;
ISLink.SetPath(pChar(Target));
ISLink.SetArguments(pChar(TargetArguments));
ISLink.SetWorkingDirectory(pChar(ExtractFilePath(Target)));
{ Get the desktop location }
SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, PIDL);
SHGetPathFromIDList(PIDL, InFolder);
LinkName := InFolder + PathDelim + ShortcutName+'.lnk';
{ Get rid of any existing shortcut first }
SysUtils.DeleteFile(LinkName);
{ Create the link }
IPFile.Save(PWChar(LinkName), false);
end;
{$ENDIF MSWINDOWS}
{$IFDEF UNIX}
procedure CreateDesktopShortCut(Target, TargetArguments, ShortcutName: string);
var
OperationSucceeded: boolean;
ResultCode: boolean;
XdgDesktopContent: TStringList;
XdgDesktopFile: string;
begin
// Fail by default:
OperationSucceeded:=false;
XdgDesktopFile:=IncludeTrailingPathDelimiter(GetTempDir(false))+'fpcup-'+shortcutname+'.desktop';
XdgDesktopContent:=TStringList.Create;
try
XdgDesktopContent.Add('[Desktop Entry]');
XdgDesktopContent.Add('Encoding=UTF-8');
XdgDesktopContent.Add('Type=Application');
XdgDesktopContent.Add('Icon='+ExtractFilePath(Target)+'images/icons/lazarus.ico');
XdgDesktopContent.Add('Exec='+Target+' '+TargetArguments);
XdgDesktopContent.Add('Name='+ShortcutName);
XdgDesktopContent.Add('Category=Application;IDE;Development;GUIDesigner;');
XdgDesktopContent.Add('Keywords=editor;Pascal;IDE;FreePascal;fpc;Design;Designer;');
// We're going to try and call xdg-desktop-icon
// this may fail if shortcut exists already
try
XdgDesktopContent.SaveToFile(XdgDesktopFile);
OperationSucceeded:=(ExecuteCommand('xdg-desktop-icon install ' + XdgDesktopFile,false)=0);
except
OperationSucceeded:=false;
end;
if OperationSucceeded=false then
infoln('CreateDesktopShortcut: failed to create shortcut to '+Target,etWarning);
// Temp file is no longer needed....
try
DeleteFile(XdgDesktopFile);
finally
// Swallow, let filesystem maintenance clear it up
end;
finally
XdgDesktopContent.Free;
end;
end;
{$ENDIF UNIX}
procedure CreateHomeStartLink(Target, TargetArguments,
ShortcutName: string);
var
ScriptText: TStringList;
ScriptFile: string;
begin
{$IFDEF MSWINDOWS}
infoln('todo: write me (CreateHomeStartLink)!', eterror);
{$ENDIF MSWINDOWS}
{$IFDEF UNIX}
//create dir if it doesn't exist
ForceDirectoriesUTF8(ExtractFilePath(IncludeTrailingPathDelimiter(SafeExpandFileNameUTF8('~'))+ShortcutName));
ScriptText:=TStringList.Create;
try
// No quotes here, either, we're not in a shell, apparently...
ScriptFile:=IncludeTrailingPathDelimiter(SafeExpandFileNameUTF8('~'))+ShortcutName;
SysUtils.DeleteFile(ScriptFile); //Get rid of any existing remnants
ScriptText.Add('#!/bin/sh');
ScriptText.Add('# shortcut generated by fcpup');
ScriptText.Add(Target+' '+TargetArguments);
try
ScriptText.SaveToFile(ScriptFile);
FPChmod(ScriptFile, &700); //rwx------
except
on E: Exception do
infoln('CreateHomeStartLink: could not create link: '+E.Message,etWarning);
end;
finally
ScriptText.Free;
end;
{$ENDIF UNIX}
end;
function DownloadFTP(URL, TargetFile: string): boolean;
const
FTPPort=21; //default ftp server port
FTPScheme='ftp://'; //URI scheme name for FTP URLs
var
Host: string;
Port: integer;
Source: string;
FoundPos: integer;
begin
// Strip out scheme info:
if LeftStr(URL, length(FTPScheme))=FTPScheme then
URL:=Copy(URL, length(FTPScheme)+1, length(URL));
// Crude parsing; could have used URI parsing code in FPC packages...
FoundPos:=pos('/', URL);
Host:=LeftStr(URL, FoundPos-1);
Source:=Copy(URL, FoundPos+1, Length(URL));
//Check for port numbers:
FoundPos:=pos(':', Host);
Port:=FTPPort;
if FoundPos>0 then
begin
Host:=LeftStr(Host, FoundPos-1);
Port:=StrToIntDef(Copy(Host, FoundPos+1, Length(Host)),21);
end;
Result:=FtpGetFile(Host, IntToStr(Port), Source, TargetFile, 'anonymous', 'fpc@example.com');
if result=false then infoln('DownloadFTP: error downloading '+URL+'. Details: host: '+Host+'; port: '+Inttostr(Port)+'; remote path: '+Source+' to '+TargetFile, eterror);
end;
function SFDirectLinkURL(URL: string; Document: TMemoryStream): string;
{
Transform this part of the body:
<noscript>
<meta http-equiv="refresh" content="5; url=http://downloads.sourceforge.net/project/base64decoder/base64decoder/version%202.0/b64util.zip?r=&ts=1329648745&use_mirror=kent">
</noscript>
into a valid URL:
http://downloads.sourceforge.net/project/base64decoder/base64decoder/version%202.0/b64util.zip?r=&ts=1329648745&use_mirror=kent
}
const
Refresh='<meta http-equiv="refresh"';
URLMarker='url=';
var
Counter: integer;
HTMLBody: TStringList;
RefreshStart: integer;
URLStart: integer;
begin
HTMLBody:=TStringList.Create;
try
HTMLBody.LoadFromStream(Document);
for Counter:=0 to HTMLBody.Count-1 do
begin
// This line should be between noscript tags and give the direct download locations:
RefreshStart:=Ansipos(Refresh, HTMLBody[Counter]);
if RefreshStart>0 then
begin
URLStart:=AnsiPos(URLMarker, HTMLBody[Counter])+Length(URLMarker);
if URLStart>RefreshStart then
begin
// Look for closing "
URL:=Copy(HTMLBody[Counter],
URLStart,
PosEx('"',HTMLBody[Counter],URLStart+1)-URLStart);
break;
end;
end;
end;
finally
HTMLBody.Free;
end;
result:=URL;
end;
function SourceForgeURL(URL: string; HTTPSender: THTTPSend): string;
// Detects sourceforge download and tries to deal with
// redirection, and extracting direct download link.
// Thanks to
// Ocye: http://lazarus.freepascal.org/index.php/topic,13425.msg70575.html#msg70575
const
SFProjectPart = '//sourceforge.net/projects/';
SFFilesPart = '/files/';
SFDownloadPart ='/download';
var
ExistingUserAgent: string;
i, j: integer;
FoundCorrectURL: boolean;
SFDirectory: string; //Sourceforge directory
SFDirectoryBegin: integer;
SFFileBegin: integer;
SFFilename: string; //Sourceforge name of file
SFProject: string;
SFProjectBegin: integer;
begin
// Detect SourceForge download; e.g. from URL
// 1 2 3 4 5 6 7 8 9
// 1234557890123456789012345578901234567890123455789012345678901234557890123456789012345578901234567890
// http://sourceforge.net/projects/base64decoder/files/base64decoder/version%202.0/b64util.zip/download
// ^^^project^^^ ^^^directory............^^^ ^^^file^^^
FoundCorrectURL:=true; //Assume not a SF download
i:=Pos(SFProjectPart, URL);
if i>0 then
begin
// Possibly found project; now extract project, directory and filename parts.
SFProjectBegin:=i+Length(SFProjectPart);
j := PosEx(SFFilesPart, URL, SFProjectBegin);
if (j>0) then
begin
SFProject:=Copy(URL, SFProjectBegin, j-SFProjectBegin);
SFDirectoryBegin:=PosEx(SFFilesPart, URL, SFProjectBegin)+Length(SFFilesPart);
if SFDirectoryBegin>0 then
begin
// Find file
// URL might have trailing arguments... so: search for first
// /download coming up from the right, but it should be after
// /files/
i:=RPos(SFDownloadPart, URL);
// Now look for previous / so we can make out the file
// This might perhaps be the trailing / in /files/
SFFileBegin:=RPosEx('/',URL,i-1)+1;
if SFFileBegin>0 then
begin
SFFilename:=Copy(URL,SFFileBegin, i-SFFileBegin);
//Include trailing /
SFDirectory:=Copy(URL, SFDirectoryBegin, SFFileBegin-SFDirectoryBegin);
FoundCorrectURL:=false;
end;
end;
end;
end;
if not FoundCorrectURL then
begin
// Rewrite URL if needed for Sourceforge download redirection
// Detect direct link in HTML body and get URL from that
//Who knows, this might help:
ExistingUserAgent:=HTTPSender.UserAgent;
HTTPSender.UserAgent:='curl/7.21.0 (i686-pc-linux-gnu) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.18';
while not FoundCorrectURL do
begin
HTTPSender.HTTPMethod('GET', URL);
case HTTPSender.Resultcode of
301, 302, 307:
begin
for i := 0 to HTTPSender.Headers.Count - 1 do
if (Pos('Location: ', HTTPSender.Headers.Strings[i]) > 0) or
(Pos('location: ', HTTPSender.Headers.Strings[i]) > 0) then
begin
j := Pos('use_mirror=', HTTPSender.Headers.Strings[i]);
if j > 0 then
URL :=
'http://' + RightStr(HTTPSender.Headers.Strings[i],
length(HTTPSender.Headers.Strings[i]) - j - 10) +
'.dl.sourceforge.net/project/' +
SFProject + '/' + SFDirectory + SFFilename
else
URL:=StringReplace(
HTTPSender.Headers.Strings[i], 'Location: ', '', []);
HTTPSender.Clear;//httpsend
FoundCorrectURL:=true;
break; //out of rewriting loop
end;
end;
100..200:
begin
//Could be a sourceforge timer/direct link page, but...
if AnsiPos('Content-Type: text/html', HTTPSender.Headers.Text)>0 then
begin
// find out... it's at least not a binary
URL:=SFDirectLinkURL(URL, HTTPSender.Document);
end;
FoundCorrectURL:=true; //We're done by now
end;
500: raise Exception.Create('Internal server error 500; perhaps no internet connection available');
//Internal Server Error ('+aURL+')');
else
raise Exception.Create('Download failed with error code ' +
IntToStr(HTTPSender.ResultCode) + ' (' + HTTPSender.ResultString + ')');
end;//case
end;//while
HTTPSender.UserAgent:=ExistingUserAgent;
end;
result:=URL;
end;
function DownloadHTTP(URL, TargetFile: string; HTTPProxyHost: string=''; HTTPProxyPort: string=''; HTTPProxyUser: string=''; HTTPProxyPassword: string=''): boolean;
// Download file; retry if necessary.
// Deals with SourceForge download links
// Could use Synapse HttpGetBinary, but that doesn't deal
// with result codes (i.e. it happily downloads a 404 error document)
const
MaxRetries=3;
var
HTTPGetResult: boolean;
HTTPSender: THTTPSend;
RetryAttempt: integer;
begin
result:=false;
RetryAttempt:=1;
HTTPSender:=THTTPSend.Create;
try
try
if HTTPProxyHost<>'' then
begin
HTTPSender.ProxyHost:=HTTPProxyHost;
if HTTPProxyPort='' then
HTTPSender.ProxyPort:='8080'
else
HTTPSender.ProxyPort:=HTTPProxyPort;
HTTPSender.ProxyUser:=HTTPProxyUser;
HTTPSender.ProxyPass:=HTTPProxyPassword;
end;
// Follow URL if necessary
URL:=SourceForgeURL(URL,HTTPSender); //Deal with sourceforge URLs
// Try to get the file
HTTPGetResult:=HTTPSender.HTTPMethod('GET', URL);
while (HTTPGetResult=false) and (RetryAttempt<MaxRetries) do
begin
sleep(500*RetryAttempt);
HTTPGetResult:=HTTPSender.HTTPMethod('GET', URL);
RetryAttempt:=RetryAttempt+1;
end;
// If we have an answer from the server, check if the file
// was sent to us.
case HTTPSender.Resultcode of
100..299:
begin
with TFileStream.Create(TargetFile,fmCreate or fmOpenWrite) do
try
Seek(0, soFromBeginning);
CopyFrom(HTTPSender.Document, 0);
finally
Free;
end;
result:=true;
end; //informational, success
300..399: result:=false; //redirection. Not implemented, but could be.
400..499: result:=false; //client error; 404 not found etc
500..599: result:=false; //internal server error
else result:=false; //unknown code
end;
except
// We don't care for the reason for this error; the download failed.
result:=false;
end;
finally
HTTPSender.Free;
end;
end;
{$IFDEF MSWINDOWS}
procedure DeleteDesktopShortcut(ShortcutName: string);
var
PIDL: PItemIDList;
InFolder: array[0..MAX_PATH] of Char;
LinkName: WideString;
begin
{ Get the desktop location }
SHGetSpecialFolderLocation(0, CSIDL_DESKTOPDIRECTORY, PIDL);
SHGetPathFromIDList(PIDL, InFolder);
LinkName := InFolder + PathDelim + ShortcutName+'.lnk';
SysUtils.DeleteFile(LinkName);
end;
{$ENDIF MSWINDOWS}
function DeleteDirectoryEx(DirectoryName: string): boolean;
// Lazarus fileutil.DeleteDirectory on steroids, works like
// deltree <directory>, rmdir /s /q <directory> or rm -rf <directory>
// - removes read-only files/directories (DeleteDirectory doesn't)
// - removes directory itself
// Adapted from fileutil.DeleteDirectory, thanks to Paweł Dmitruk
var
FileInfo: TSearchRec;
CurSrcDir: String;
CurFilename: String;
begin
Result:=false;
CurSrcDir:=CleanAndExpandDirectory(DirectoryName);
if FindFirstUTF8(CurSrcDir+GetAllFilesMask,faAnyFile{$ifdef unix} or faSymLink {$endif unix},FileInfo)=0 then
begin
repeat
// Ignore directories and files without name:
if (FileInfo.Name<>'.') and (FileInfo.Name<>'..') and (FileInfo.Name<>'') then
begin
// Look at all files and directories in this directory:
CurFilename:=CurSrcDir+FileInfo.Name;
// Remove read-only file attribute so we can delete it:
if (FileInfo.Attr and faReadOnly)>0 then
FileSetAttrUTF8(CurFilename, FileInfo.Attr-faReadOnly);
if ((FileInfo.Attr and faDirectory)>0) {$ifdef unix} and ((FileInfo.Attr and faSymLink)=0) {$endif unix} then
begin
// Directory; exit with failure on error
if not DeleteDirectoryEx(CurFilename) then
begin
FindCloseUTF8(FileInfo);
exit;
end;
end
else
begin
// File; exit with failure on error
if not DeleteFileUTF8(CurFilename) then
begin
FindCloseUTF8(FileInfo);
exit;
end;
end;
end;
until FindNextUTF8(FileInfo)<>0;
end;
FindCloseUTF8(FileInfo);
// Remove root directory; exit with failure on error:
if (not RemoveDirUTF8(DirectoryName)) then exit;
Result:=true;
end;
function DeleteFilesSubDirs(const DirectoryName: string;
const Names: TStringList; const OnlyIfPathHas: string): boolean;
// Deletes all named files starting from DirectoryName and recursing down.
// If the Names are empty, all files will be deleted
// It only deletes files if any directory of the path contains OnlyIfPathHas,
// unless that is empty
// Will try to remove read-only files.
//todo: check how this works with case insensitive file system like Windows
var
AllFiles: boolean;
CurSrcDir: String;
CurFilename: String;
FileInfo: TSearchRec;
begin
Result:=false;
AllFiles:=(Names.Count=0);
CurSrcDir:=CleanAndExpandDirectory(DirectoryName);
if FindFirstUTF8(CurSrcDir+GetAllFilesMask,faAnyFile{$ifdef unix} or faSymLink {$endif unix},FileInfo)=0 then
begin
repeat
// Ignore directories and files without name:
if (FileInfo.Name<>'.') and (FileInfo.Name<>'..') and (FileInfo.Name<>'') then
begin
// Look at all files and directories in this directory:
CurFilename:=CurSrcDir+FileInfo.Name;
if ((FileInfo.Attr and faDirectory)>0) {$ifdef unix} and ((FileInfo.Attr and faSymLink)=0) {$endif unix} then
begin
// Directory; call recursively exit with failure on error
if not DeleteFilesSubDirs(CurFilename,Names,OnlyIfPathHas) then
begin
FindCloseUTF8(FileInfo);
exit;
end;
end
else
begin
// If we are in the right path:
//todo: get utf8 replacement for ExtractFilePath
if (OnlyIfPathHas='') or
(pos(DirectorySeparator+OnlyIfPathHas+DirectorySeparator,ExtractFilePath(CurFileName))>0) then
begin
// Only delete if file name is right
//todo: get utf8 extractfilename
if AllFiles or (Names.IndexOf(ExtractFileName(FileInfo.Name))>=0) then
begin
// Remove read-only file attribute so we can delete it:
if (FileInfo.Attr and faReadOnly)>0 then
FileSetAttrUTF8(CurFilename, FileInfo.Attr-faReadOnly);
if not DeleteFileUTF8(CurFilename) then
begin
FindCloseUTF8(FileInfo);
exit;
end;
end;
end;
end;
end;
until FindNextUTF8(FileInfo)<>0;
end;
FindCloseUTF8(FileInfo);
Result:=true;
end;
function DeleteFilesExtensionsSubdirs(const DirectoryName: string; const Extensions:TstringList; const OnlyIfPathHas: string): boolean;
// Deletes all files ending in one of the extensions, starting from
// DirectoryName and recursing down.
// It only deletes files if any directory of the path contains OnlyIfPathHas,
// unless that is empty
// Extensions can contain * to cover everything (other extensions will then be
// ignored), making it delete all files, but leaving the directories.
// Will try to remove read-only files.
//todo: check how this works with case insensitive file system like Windows
var
AllFiles: boolean;
CurSrcDir: String;
CurFilename: String;
FileInfo: TSearchRec;
i: integer;
begin
Result:=false;
// Make sure we can compare extensions using ExtractFileExt
for i:=0 to Extensions.Count-1 do
begin
if copy(Extensions[i],1,1)<>'.' then Extensions[i]:='.'+Extensions[i];
end;
AllFiles:=(Extensions.Count=0) or (Extensions.IndexOf('.*')>=0);
CurSrcDir:=CleanAndExpandDirectory(DirectoryName);
if FindFirstUTF8(CurSrcDir+GetAllFilesMask,faAnyFile{$ifdef unix} or faSymLink {$endif unix},FileInfo)=0 then
begin
repeat
// Ignore directories and files without name:
if (FileInfo.Name<>'.') and (FileInfo.Name<>'..') and (FileInfo.Name<>'') then
begin
// Look at all files and directories in this directory:
CurFilename:=CurSrcDir+FileInfo.Name;
if ((FileInfo.Attr and faDirectory)>0) {$ifdef unix} and ((FileInfo.Attr and faSymLink)=0) {$endif unix} then
begin
// Directory; call recursively exit with failure on error
if not DeleteFilesExtensionsSubdirs(CurFilename, Extensions,OnlyIfPathHas) then
begin
FindCloseUTF8(FileInfo);
exit;
end;
end
else
begin
// If we are in the right path:
//todo: get utf8 replacement for ExtractFilePath
if (OnlyIfPathHas='') or
(pos(DirectorySeparator+OnlyIfPathHas+DirectorySeparator,ExtractFilePath(CurFileName))>0) then
begin
// Only delete if extension is right
if AllFiles or (Extensions.IndexOf(ExtractFileExt(FileInfo.Name))>=0) then
begin
// Remove read-only file attribute so we can delete it:
if (FileInfo.Attr and faReadOnly)>0 then
FileSetAttrUTF8(CurFilename, FileInfo.Attr-faReadOnly);
if not DeleteFileUTF8(CurFilename) then
begin
FindCloseUTF8(FileInfo);
exit;
end;
end;
end;
end;
end;
until FindNextUTF8(FileInfo)<>0;
end;
FindCloseUTF8(FileInfo);
Result:=true;
end;
function Download(URL, TargetFile: string; HTTPProxyHost: string=''; HTTPProxyPort: string=''; HTTPProxyUser: string=''; HTTPProxyPassword: string=''): boolean;
begin
result:=false;
// Assume http if no ftp detected
try
infoln('Going to download '+TargetFile+' from URL: ' + URL, etinfo);
if (Copy(URL, 1, Length('ftp://'))='ftp://') or
(Copy(URL,1,Length('ftp.'))='ftp.') then
begin
result:=DownloadFTP(URL, TargetFile);
end
else
begin
result:=DownloadHTTP(URL, TargetFile, HTTPProxyHost, HTTPProxyPort, HTTPProxyUser, HTTPProxyPassword);
end;
except
on E: Exception do
begin
infoln('Download: error occurred downloading file '+TargetFile+' from URL: '+URL+
'. Exception occurred: '+E.ClassName+'/'+E.Message+')', eterror);
end;
//Not writing message here; to be handled by calling code with more context.
//if result:=false then infoln('Download: download of '+TargetFile+' from URL: '+URL+' failed.');
end;
end;
// returns file size in bytes or 0 if not found.
function FileSizeUTF8(FileName: string) : Int64;
var
sr : TSearchRec;
begin
{$ifdef unix}
result:=filesize(FileName);
{$else}
if FindFirstUTF8(FileName, faAnyFile, sr ) = 0 then
result := Int64(sr.FindData.nFileSizeHigh) shl Int64(32) + Int64(sr.FindData.nFileSizeLow)
else
result := 0;
FindCloseUTF8(sr);
{$endif}
end;
function ParentDirectoryIsNotRoot(Dir: string): boolean;
var s:string;
begin
result:=false;
Dir:=ExcludeTrailingBackslash(Dir);
s:=ExtractFileDir(Dir);
if s<>Dir then //to avoid fe. c:\\\
begin // this is one level up
Dir:=ExcludeTrailingBackslash(s);
s:=ExtractFileDir(Dir);
result:=s<>Dir; //to avoid fe. c:\\\
end;
end;
{$IFDEF MSWINDOWS}
function GetLocalAppDataPath: string;
var
AppDataPath: array[0..MaxPathLen] of char; //Allocate memory
begin
AppDataPath := '';
SHGetSpecialFolderPath(0, AppDataPath, CSIDL_LOCAL_APPDATA, False);
result:=AppDataPath;
end;
{$ENDIF MSWINDOWS}
procedure infoln(Message: string; Level: TEventType);
const
BeginSnippet='fpcup: '; //helps identify messages as comfing from fpcup instead of make etc
var
Seriousness: string;
begin
{$IFNDEF NOCONSOLE}
// Note: these strings should remain as is so any fpcupgui highlighter can pick it up
case Level of
etCustom: Seriousness:='custom:';
etDebug: Seriousness:='debug:';
etInfo: Seriousness:='info:';
etWarning: Seriousness:='WARNING:';
etError: Seriousness:='ERROR:';
else Seriousness:='UNKNOWN CATEGORY!!:'
end;
if (Level<>etDebug) then
begin
if AnsiPos(LineEnding, Message)>0 then writeln(''); //Write an empty line before multiline messagse
writeln(BeginSnippet+Seriousness+' '+ Message); //we misuse this for info output
sleep(200); //hopefully allow output to be written without interfering with other output
end
else
begin
{$IFDEF DEBUG}
{DEBUG conditional symbol is defined using
Project Options/Other/Custom Options using -dDEBUG}
if AnsiPos(LineEnding, Message)>0 then writeln(''); //Write an empty line before multiline messagse
writeln(BeginSnippet+Seriousness+' '+ Message); //we misuse this for info output
sleep(200); //hopefully allow output to be written without interfering with other output
{$ENDIF}
end;
{$ENDIF NOCONSOLE}
end;
function MoveFile(const SrcFilename, DestFilename: string): boolean;
// We might (in theory) be moving files across partitions so we cannot use renamefile
begin
try
if FileExistsUTF8(SrcFileName) then
begin
if FileUtil.CopyFile(SrcFilename, DestFileName) then Sysutils.DeleteFile(SrcFileName);
result:=true;
end
else
begin
//Source file does not exist, so cannot move
result:=false;
end;
except
result:=false;
end;
end;
function StringListStartsWith(SearchIn: TStringList; SearchFor: string; StartIndex: integer): integer;
var
Found:boolean=false;
i:integer;
begin
for i:=StartIndex to SearchIn.Count-1 do
begin
if copy(SearchIn[i],1,length(SearchFor))=SearchFor then
begin
Found:=true;
break;
end;
end;
if Found then
result:=i
else
result:=-1;
end;
function Which(Executable: string): string;
var
Output: string;
begin
{$IFDEF UNIX}
// Note: we're using external which because
// FindDefaultExecutablePath
// doesn't check if the user has execute permission
// on the found file.
ExecuteCommand('which '+Executable,Output,false);
// Remove trailing LF(s) and other control codes:
while (length(output)>0) and (ord(output[length(output)])<$20) do
delete(output,length(output),1);
{$ELSE}
Output:=FindDefaultExecutablePath(Executable);
{$ENDIF UNIX}
// We could have checked for ExecuteCommandHidden exitcode, but why not
// do file existence check instead:
if (Output<>'') and fileexists(Output) then
begin
result:=Output;
end
else
begin
result:=''; //command failed
end;
end;
{$IFDEF UNIX}
//Adapted from sysutils; Unix/Linux only
Function XdgConfigHome: String;
{ Follows base-dir spec,
see [http://freedesktop.org/Standards/basedir-spec].
Always ends with PathDelim. }
begin
Result:=GetEnvironmentVariable('XDG_CONFIG_HOME');
if (Result='') then
Result:=IncludeTrailingPathDelimiter(SafeExpandFileNameUTF8('~'))+'.config'+DirectorySeparator
else
Result:=IncludeTrailingPathDelimiter(Result);
end;
{$ENDIF UNIX}
{ TLogger }
function TLogger.GetLogFile: string;
begin
result:=FLog.FileName;
end;
procedure TLogger.SetLogFile(AValue: string);
begin
if AValue<>FLog.FileName then
begin
FLog.Active:=false;//save WriteLog output
FLog.FileName:=AValue;
end;
end;
procedure TLogger.WriteLog(Message: string; ToConsole: Boolean);
begin
FLog.Log(etInfo, Message);
if ToConsole then infoln(Message,etInfo);
end;
procedure TLogger.WriteLog(EventType: TEventType;Message: string; ToConsole: Boolean);
begin
FLog.Log(EventType, Message);
if ToConsole then infoln(Message,etInfo);
end;
constructor TLogger.Create;
begin
FLog:=TEventLog.Create(nil);
FLog.LogType:=ltFile;
FLog.AppendContent:=true;
FLog.RaiseExceptionOnError:=false; //Don't throw exceptions on log errors.
end;
destructor TLogger.Destroy;
begin
FLog.Active:=false;//save WriteLog text
FLog.Free;
inherited Destroy;
end;
end.