Debian Codegraph @

plugins/wcx/zip/src/fparchive/abutils.pas

raw
1 (* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is TurboPower Abbrevia
15 *
16 * The Initial Developer of the Original Code is
17 * TurboPower Software
18 *
19 * Portions created by the Initial Developer are Copyright (C) 1997-2002
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * ***** END LICENSE BLOCK ***** *)
25
26 {*********************************************************}
27 {* ABBREVIA: AbUtils.pas *}
28 {*********************************************************}
29 {* ABBREVIA: Utility classes and routines *}
30 {*********************************************************}
31
32 unit AbUtils;
33
34 {$I AbDefine.inc}
35
36 interface
37
38 uses
39 {$IFDEF MSWINDOWS}
40 Windows,
41 DCWindows,
42 DCConvertEncoding,
43 {$ENDIF}
44 {$IFDEF LibcAPI}
45 Libc,
46 {$ENDIF}
47 {$IFDEF FPCUnixAPI}
48 baseunix,
49 {$IFDEF Linux}
50 initc,
51 {$ENDIF}
52 unix,
53 {$ENDIF}
54 {$IFDEF PosixAPI}
55 Posix.SysStatvfs,
56 Posix.SysStat,
57 Posix.Utime,
58 Posix.Base,
59 Posix.Unistd,
60 Posix.Fcntl,
61 Posix.SysTypes,
62 {$ENDIF}
63 {$IFDEF UNIX}
64 DCClassesUtf8,
65 {$ENDIF}
66 DateUtils,
67 SysUtils,
68 Classes;
69
70
71 type
72 {describe the pending action for an archive item}
73 TAbArchiveAction =
74 (aaFailed, aaNone, aaAdd, aaDelete, aaFreshen, aaMove, aaReplace,
75 aaStreamAdd);
76 TAbProcessType =
77 (ptAdd, ptDelete, ptExtract, ptFreshen, ptMove, ptReplace, ptFoundUnhandled);
78 TAbLogType =
79 (ltAdd, ltDelete, ltExtract, ltFreshen, ltMove, ltReplace, ltStart, ltFoundUnhandled);
80 TAbErrorClass =
81 (ecAbbrevia, ecInOutError, ecFilerError, ecFileCreateError,
82 ecFileOpenError, ecCabError, ecOther);
83
84 const
85 AbPathDelim = PathDelim; { Delphi/Linux constant }
86 AbPathSep = PathSep; { Delphi/Linux constant }
87 AbDosPathDelim = '\';
88 AbUnixPathDelim = '/';
89 AbDosPathSep = ';';
90 AbUnixPathSep = ':';
91 AbDosAnyFile = '*.*';
92 AbUnixAnyFile = '*';
93 AbAnyFile = {$IFDEF UNIX} AbUnixAnyFile; {$ELSE} AbDosAnyFile; {$ENDIF}
94 AbThisDir = '.';
95 AbParentDir = '..';
96
97 type
98 TAbArchiveType = (atUnknown, atZip, atSpannedZip, atSelfExtZip,
99 atTar, atGzip, atGzippedTar, atCab, atBzip2, atBzippedTar,
100 atXz, atXzippedTar, atLzma, atLzmaTar, atZstd, atZstdTar);
101
102
103 {$IF NOT DECLARED(DWORD)}
104 type
105 DWORD = LongWord;
106 {$IFEND}
107
108 {$IF NOT DECLARED(PtrInt)}
109 type
110 // Delphi 7-2007 declared NativeInt incorrectly
111 {$IFDEF CPU386}
112 PtrInt = LongInt;
113 PtrUInt = LongWord;
114 {$ELSE}
115 PtrInt = NativeInt;
116 PtrUInt = NativeUInt;
117 {$ENDIF}
118 {$IFEND}
119
120 { Unicode backwards compatibility types }
121 {$IF NOT DECLARED(RawByteString)}
122 type
123 RawByteString = AnsiString;
124 {$IFEND}
125
126 { System-encoded SBCS string (formerly AnsiString) }
127 type
128 AbSysString = {$IFDEF Posix}UTF8String{$ELSE}AnsiString{$ENDIF};
129
130 const
131 AbCrc32Table : array[0..255] of DWord = (
132 $00000000, $77073096, $ee0e612c, $990951ba,
133 $076dc419, $706af48f, $e963a535, $9e6495a3,
134 $0edb8832, $79dcb8a4, $e0d5e91e, $97d2d988,
135 $09b64c2b, $7eb17cbd, $e7b82d07, $90bf1d91,
136 $1db71064, $6ab020f2, $f3b97148, $84be41de,
137 $1adad47d, $6ddde4eb, $f4d4b551, $83d385c7,
138 $136c9856, $646ba8c0, $fd62f97a, $8a65c9ec,
139 $14015c4f, $63066cd9, $fa0f3d63, $8d080df5,
140 $3b6e20c8, $4c69105e, $d56041e4, $a2677172,
141 $3c03e4d1, $4b04d447, $d20d85fd, $a50ab56b,
142 $35b5a8fa, $42b2986c, $dbbbc9d6, $acbcf940,
143 $32d86ce3, $45df5c75, $dcd60dcf, $abd13d59,
144 $26d930ac, $51de003a, $c8d75180, $bfd06116,
145 $21b4f4b5, $56b3c423, $cfba9599, $b8bda50f,
146 $2802b89e, $5f058808, $c60cd9b2, $b10be924,
147 $2f6f7c87, $58684c11, $c1611dab, $b6662d3d,
148 $76dc4190, $01db7106, $98d220bc, $efd5102a,
149 $71b18589, $06b6b51f, $9fbfe4a5, $e8b8d433,
150 $7807c9a2, $0f00f934, $9609a88e, $e10e9818,
151 $7f6a0dbb, $086d3d2d, $91646c97, $e6635c01,
152 $6b6b51f4, $1c6c6162, $856530d8, $f262004e,
153 $6c0695ed, $1b01a57b, $8208f4c1, $f50fc457,
154 $65b0d9c6, $12b7e950, $8bbeb8ea, $fcb9887c,
155 $62dd1ddf, $15da2d49, $8cd37cf3, $fbd44c65,
156 $4db26158, $3ab551ce, $a3bc0074, $d4bb30e2,
157 $4adfa541, $3dd895d7, $a4d1c46d, $d3d6f4fb,
158 $4369e96a, $346ed9fc, $ad678846, $da60b8d0,
159 $44042d73, $33031de5, $aa0a4c5f, $dd0d7cc9,
160 $5005713c, $270241aa, $be0b1010, $c90c2086,
161 $5768b525, $206f85b3, $b966d409, $ce61e49f,
162 $5edef90e, $29d9c998, $b0d09822, $c7d7a8b4,
163 $59b33d17, $2eb40d81, $b7bd5c3b, $c0ba6cad,
164 $edb88320, $9abfb3b6, $03b6e20c, $74b1d29a,
165 $ead54739, $9dd277af, $04db2615, $73dc1683,
166 $e3630b12, $94643b84, $0d6d6a3e, $7a6a5aa8,
167 $e40ecf0b, $9309ff9d, $0a00ae27, $7d079eb1,
168 $f00f9344, $8708a3d2, $1e01f268, $6906c2fe,
169 $f762575d, $806567cb, $196c3671, $6e6b06e7,
170 $fed41b76, $89d32be0, $10da7a5a, $67dd4acc,
171 $f9b9df6f, $8ebeeff9, $17b7be43, $60b08ed5,
172 $d6d6a3e8, $a1d1937e, $38d8c2c4, $4fdff252,
173 $d1bb67f1, $a6bc5767, $3fb506dd, $48b2364b,
174 $d80d2bda, $af0a1b4c, $36034af6, $41047a60,
175 $df60efc3, $a867df55, $316e8eef, $4669be79,
176 $cb61b38c, $bc66831a, $256fd2a0, $5268e236,
177 $cc0c7795, $bb0b4703, $220216b9, $5505262f,
178 $c5ba3bbe, $b2bd0b28, $2bb45a92, $5cb36a04,
179 $c2d7ffa7, $b5d0cf31, $2cd99e8b, $5bdeae1d,
180 $9b64c2b0, $ec63f226, $756aa39c, $026d930a,
181 $9c0906a9, $eb0e363f, $72076785, $05005713,
182 $95bf4a82, $e2b87a14, $7bb12bae, $0cb61b38,
183 $92d28e9b, $e5d5be0d, $7cdcefb7, $0bdbdf21,
184 $86d3d2d4, $f1d4e242, $68ddb3f8, $1fda836e,
185 $81be16cd, $f6b9265b, $6fb077e1, $18b74777,
186 $88085ae6, $ff0f6a70, $66063bca, $11010b5c,
187 $8f659eff, $f862ae69, $616bffd3, $166ccf45,
188 $a00ae278, $d70dd2ee, $4e048354, $3903b3c2,
189 $a7672661, $d06016f7, $4969474d, $3e6e77db,
190 $aed16a4a, $d9d65adc, $40df0b66, $37d83bf0,
191 $a9bcae53, $debb9ec5, $47b2cf7f, $30b5ffe9,
192 $bdbdf21c, $cabac28a, $53b39330, $24b4a3a6,
193 $bad03605, $cdd70693, $54de5729, $23d967bf,
194 $b3667a2e, $c4614ab8, $5d681b02, $2a6f2b94,
195 $b40bbe37, $c30c8ea1, $5a05df1b, $2d02ef8d
196 );
197
198
199 type
200 TAbPathType = ( ptNone, ptRelative, ptAbsolute );
201
202 {===Helper functions===}
203 function AbCopyFile(const Source, Destination: string; FailIfExists: Boolean): Boolean;
204
205 procedure AbCreateDirectory( const Path : string );
206 {creates the requested directory tree. CreateDir is insufficient,
207 because if you have a path x:\dir, and request x:\dir\sub1\sub2,
208 (/dir and /dir/sub1/sub2 on Unix) it fails.}
209
210 function AbCreateTempFile(const Dir : string) : string;
211
212 function AbGetTempDirectory : string;
213 {-Return the system temp directory}
214
215 function AbGetTempFile(const Dir : string; CreateIt : Boolean) : string;
216
217 function AbDrive(const ArchiveName : string) : Char;
218
219 function AbDriveIsRemovable(const ArchiveName : string) : Boolean;
220
221 function AbFileMatch(FileName : string; FileMask : string ) : Boolean;
222 {see if FileName matches FileMask}
223
224 procedure AbFindFiles(const FileMask : string; SearchAttr : Integer;
225 FileList : TStrings; Recurse : Boolean );
226
227 procedure AbFindFilesEx( const FileMask : string; SearchAttr : Integer;
228 FileList : TStrings; Recurse : Boolean );
229
230 function AbAddBackSlash(const DirName : string) : string;
231
232 function AbFindNthSlash( const Path : string; n : Integer ) : Integer;
233 {return the position of the character just before the nth backslash}
234
235 function AbGetDriveFreeSpace(const ArchiveName : string) : Int64;
236 {return the available space on the specified drive }
237
238 function AbGetPathType( const Value : string ) : TAbPathType;
239 {returns path type - none, relative or absolute}
240
241 {$IFDEF MSWINDOWS}
242 function AbGetShortFileSpec(const LongFileSpec : string ) : string;
243 {$ENDIF}
244
245 procedure AbIncFilename( var Filename : string; Value : Word );
246
247 procedure AbParseFileName( FileSpec : string;
248 out Drive : string;
249 out Path : string;
250 out FileName : string );
251
252 procedure AbParsePath( Path : string; SubPaths : TStrings );
253 {- break abart path into subpaths --- Path : abbrevia/examples ->
254 SubPaths[0] = abbrevia
255 SubPaths[1] = examples}
256
257 function AbPatternMatch(const Source : string; iSrc : Integer;
258 const Pattern : string; iPat : Integer ) : Boolean;
259 { recursive routine to see if the source string matches
260 the pattern. Both ? and * wildcard characters are allowed.}
261
262 function AbPercentage(V1, V2 : Int64) : Byte;
263 {-Returns the ratio of V1 to V2 * 100}
264
265 procedure AbStripDots( var FName : string );
266 {-strips relative path information}
267
268 procedure AbStripDrive( var FName : string );
269 {-strips the drive off a filename}
270
271 procedure AbFixName( var FName : string );
272 {-changes backslashes to forward slashes}
273
274 procedure AbUnfixName( var FName : string );
275 {-changes forward slashes to backslashes}
276
277 procedure AbUpdateCRC( var CRC : LongInt; const Buffer; Len : Integer );
278
279 function AbUpdateCRC32(CurByte : Byte; CurCrc : LongInt) : LongInt;
280 {-Returns an updated crc32}
281
282 function AbCRC32Of( const aValue : RawByteString ) : LongInt;
283
284
285 function AbWriteVolumeLabel(const VolName : string;
286 Drive : Char) : Cardinal;
287 const
288 AB_SPAN_VOL_LABEL = 'PKBACK# %3.3d';
289
290 function AbGetVolumeLabel(Drive : Char) : string;
291 procedure AbSetSpanVolumeLabel(Drive: Char; VolNo : Integer);
292 function AbTestSpanVolumeLabel(Drive: Char; VolNo : Integer): Boolean;
293
294 procedure AbSetFileAttr(const aFileName : string; aAttr: Integer);
295 {-Sets platform-native file attributes (DOS attr or Unix mode)}
296 function AbFileGetSize(const aFileName : string) : Int64;
297
298 type
299 TAbAttrExRec = record
300 Time: TDateTime;
301 Size: Int64;
302 Attr: Integer;
303 Mode: {$IFDEF UNIX}mode_t{$ELSE}Cardinal{$ENDIF};
304 end;
305
306 function AbFileGetAttrEx(const aFileName: string; out aAttr: TAbAttrExRec; FollowLinks: Boolean = True) : Boolean;
307
308 function AbSwapLongEndianness(Value : LongInt): LongInt;
309
310
311 { date and time stuff }
312 const
313 Date1900 {: LongInt} = $0001AC05; {Julian day count for 01/01/1900 -- TDateTime Start Date}
314 Date1970 {: LongInt} = $00020FE4; {Julian day count for 01/01/1970 -- Unix Start Date}
315 Unix0Date: TDateTime = 25569; {Date1970 - Date1900}
316
317 SecondsInDay = 86400; {Number of seconds in a day}
318 SecondsInHour = 3600; {Number of seconds in an hour}
319 SecondsInMinute = 60; {Number of seconds in a minute}
320 HoursInDay = 24; {Number of hours in a day}
321 MinutesInHour = 60; {Number of minutes in an hour}
322 MinutesInDay = 1440; {Number of minutes in a day}
323
324
325 function AbUnixTimeToLocalDateTime(UnixTime : Int64) : TDateTime;
326 function AbLocalDateTimeToUnixTime(DateTime : TDateTime) : Int64;
327
328 function AbDosFileDateToDateTime(FileDate, FileTime : Word) : TDateTime;
329 function AbDateTimeToDosFileDate(Value : TDateTime) : LongInt;
330
331 function AbGetFileTime(const aFileName: string): TDateTime;
332 function AbSetFileTime(const aFileName: string; aValue: TDateTime): Boolean;
333
334 { file attributes }
335 function AbDOS2UnixFileAttributes(Attr: LongInt): LongInt;
336 function AbUnix2DosFileAttributes(Attr: LongInt): LongInt;
337
338 { UNIX File Types and Permissions }
339 const
340 AB_FMODE_FILE = $0000;
341 AB_FMODE_FIFO = $1000;
342 AB_FMODE_CHARSPECFILE = $2000;
343 AB_FMODE_DIR = $4000;
344 AB_FMODE_BLOCKSPECFILE = $6000;
345 AB_FMODE_FILE2 = $8000;
346 AB_FMODE_FILELINK = $A000;
347 AB_FMODE_SOCKET = $C000;
348
349
350 AB_FPERMISSION_OWNERREAD = $0100; { read by owner }
351 AB_FPERMISSION_OWNERWRITE = $0080; { write by owner }
352 AB_FPERMISSION_OWNEREXECUTE = $0040; { execute/search by owner }
353 AB_FPERMISSION_GROUPREAD = $0020; { read by group }
354 AB_FPERMISSION_GROUPWRITE = $0010; { write by group }
355 AB_FPERMISSION_GROUPEXECUTE = $0008; { execute/search by group }
356 AB_FPERMISSION_OTHERREAD = $0004; { read by other }
357 AB_FPERMISSION_OTHERWRITE = $0002; { write by other }
358 AB_FPERMISSION_OTHEREXECUTE = $0001; { execute/search by other }
359
360 AB_FPERMISSION_GENERIC =
361 AB_FPERMISSION_OWNERREAD or
362 AB_FPERMISSION_OWNERWRITE or
363 AB_FPERMISSION_GROUPREAD or
364 AB_FPERMISSION_OTHERREAD;
365
366 { Unicode backwards compatibility functions }
367 {$IFNDEF UNICODE}
368 function CharInSet(C: AnsiChar; CharSet: TSysCharSet): Boolean;
369 {$ENDIF}
370
371 implementation
372
373 uses
374 StrUtils,
375 LazUTF8,
376 AbConst,
377 AbExcept,
378 DCOSUtils,
379 DCStrUtils,
380 DCBasicTypes,
381 DCDateTimeUtils;
382
383 (*
384 {$IF DEFINED(FPCUnixAPI)}
385 function mktemp(template: PAnsiChar): PAnsiChar; cdecl;
386 external clib name 'mktemp';
387 {$ELSEIF DEFINED(PosixAPI)}
388 function mktemp(template: PAnsiChar): PAnsiChar; cdecl;
389 external libc name _PU + 'mktemp';
390 {$IFEND}
391
392 {$IF DEFINED(FPCUnixAPI) AND DEFINED(Linux)}
393 // FreePascal libc definitions
394 type
395 nl_item = cint;
396
397 const
398 __LC_CTYPE = 0;
399 _NL_CTYPE_CLASS = (__LC_CTYPE shl 16);
400 _NL_CTYPE_CODESET_NAME = (_NL_CTYPE_CLASS)+14;
401
402 function nl_langinfo(__item: nl_item): PAnsiChar; cdecl;
403 external clib name 'nl_langinfo';
404 {$IFEND}
405 *)
406
407 {===platform independent routines for platform dependent stuff=======}
408 function ExtractShortName(const SR : TSearchRec) : string;
409 begin
410 {$IFDEF MSWINDOWS}
411 {$WARN SYMBOL_PLATFORM OFF}
412 if SR.FindData.cAlternateFileName[0] <> #0 then
413 Result := SR.FindData.cAlternateFileName
414 else
415 Result := SR.FindData.cFileName;
416 {$WARN SYMBOL_PLATFORM ON}
417 {$ENDIF}
418 {$IFDEF UNIX}
419 Result := SR.Name;
420 {$ENDIF}
421 end;
422 {====================================================================}
423
424
425 { ========================================================================== }
426 function AbCopyFile(const Source, Destination: string; FailIfExists: Boolean): Boolean;
427 {$IFDEF UNIX}
428 var
429 DesStream, SrcStream: TFileStreamEx;
430 {$ENDIF}
431 begin
432 {$IFDEF UNIX}
433 Result := False;
434 if not FailIfExists or not mbFileExists(Destination) then
435 try
436 SrcStream := TFileStreamEx.Create(Source, fmOpenRead or fmShareDenyWrite);
437 try
438 DesStream := TFileStreamEx.Create(Destination, fmCreate);
439 try
440 DesStream.CopyFrom(SrcStream, 0);
441 Result := True;
442 finally
443 DesStream.Free;
444 end;
445 finally
446 SrcStream.Free;
447 end;
448 except
449 // Ignore errors and just return false
450 end;
451 {$ENDIF UNIX}
452 {$IFDEF MSWINDOWS}
453 Result := CopyFileW(PWideChar(CeUtf8ToUtf16(Source)), PWideChar(CeUtf8ToUtf16(Destination)), FailIfExists);
454 {$ENDIF MSWINDOWS}
455 end;
456 { -------------------------------------------------------------------------- }
457 procedure AbCreateDirectory( const Path : string );
458 {creates the requested directory tree. CreateDir is insufficient,
459 because if you have a path x:\dir, and request x:\dir\sub1\sub2,
460 (/dir and /dir/sub1/sub2 on Unix) it fails.}
461 var
462 iStartSlash : Integer;
463 i : Integer;
464 TempPath : string;
465 begin
466 if mbDirectoryExists( Path ) then
467 Exit;
468 {see how much of the path currently exists}
469 if Pos( '\\', Path ) > 0 then
470 {UNC Path \\computername\sharename\path1..\pathn}
471 iStartSlash := 5
472 else
473 {standard Path drive:\path1..\pathn}
474 iStartSlash := 2;
475
476 repeat
477 {find the Slash at iStartSlash}
478 i := AbFindNthSlash( Path, iStartSlash );
479 {get a temp path to try: drive:\path1}
480 TempPath := Copy( Path, 1, i );
481 {if it doesn't exist, create it}
482 if not mbDirectoryExists( TempPath ) then
483 if mbCreateDir( TempPath ) = False then
484 Exit;
485 inc( iStartSlash );
486 until ( Length( TempPath ) = Length( Path ) );
487 end;
488 { -------------------------------------------------------------------------- }
489 function AbCreateTempFile(const Dir : string) : string;
490 begin
491 Result := AbGetTempFile(Dir, True);
492 end;
493 { -------------------------------------------------------------------------- }
494 function AbGetTempDirectory : string;
495 begin
496 Result:= SysToUTF8(GetTempDir);
497 end;
498 { -------------------------------------------------------------------------- }
499 function AbGetTempFile(const Dir : string; CreateIt : Boolean) : string;
500 var
501 hFile: System.THandle;
502 TempPath : String;
503 begin
504 if mbDirectoryExists(Dir) then
505 TempPath := IncludeTrailingPathDelimiter(Dir)
506 else
507 TempPath := AbGetTempDirectory;
508
509 Result := GetTempName(TempPath + 'VMS');
510
511 if CreateIt then begin
512 hFile := mbFileCreate(Result);
513 if hFile <> feInvalidHandle then
514 FileClose(hFile);
515 end;
516 end;
517 { -------------------------------------------------------------------------- }
518 function AbDrive(const ArchiveName : string) : Char;
519 var
520 iPos: Integer;
521 Path : string;
522 begin
523 Path := ExpandFileName(ArchiveName);
524 iPos := Pos(':', Path);
525 if (iPos <= 0) then
526 Result := 'A'
527 else
528 Result := Path[1];
529 end;
530 { -------------------------------------------------------------------------- }
531 function AbDriveIsRemovable(const ArchiveName : string) : Boolean;
532 {$IFDEF MSWINDOWS}
533 var
534 Path: string;
535 {$ENDIF}
536 begin
537 {$IFDEF MSWINDOWS}
538 Path := ExpandFileName(ArchiveName);
539 if AnsiStartsText('\\?\UNC\', Path) then
540 Delete(Path, 1, 8)
541 else if AnsiStartsText('\\?\', Path) then
542 Delete(Path, 1, 4);
543 Path := IncludeTrailingPathDelimiter(ExtractFileDrive(Path));
544 Result := GetDriveType(PChar(Path)) = DRIVE_REMOVABLE;
545 {$ENDIF}
546 {$IFDEF LINUX}
547 {LINUX -- Following may not cover all the bases}
548 Result := AnsiStartsText('/mnt/floppy', ExtractFilePath(ExpandFileName(ArchiveName)));
549 {$ENDIF}
550 {$IFDEF DARWIN}
551 Result := False;
552 {$ENDIF}
553 end;
554 { -------------------------------------------------------------------------- }
555 function AbGetDriveFreeSpace(const ArchiveName : string) : Int64;
556 { attempt to find free space (in bytes) on drive/volume,
557 returns -1 if fails for some reason }
558 {$IFDEF MSWINDOWS}
559 var
560 FreeAvailable, TotalSpace: Int64;
561 begin
562 if GetDiskFreeSpaceExW(PWideChar(CeUtf8ToUtf16(ExtractFilePath(ExpandFileName(ArchiveName)))),
563 FreeAvailable, TotalSpace, nil) then
564 Result := FreeAvailable
565 else
566 Result := -1;
567 {$ENDIF}
568 {$IFDEF UNIX}
569 var
570 FStats : {$IFDEF PosixAPI}_statvfs{$ELSE}TStatFs{$ENDIF};
571 begin
572 {$IF DEFINED(LibcAPI)}
573 if statfs(PAnsiChar(ExtractFilePath(ArchiveName)), FStats) = 0 then
574 Result := Int64(FStats.f_bAvail) * Int64(FStats.f_bsize)
575 {$ELSEIF DEFINED(FPCUnixAPI)}
576 if fpStatFS(PAnsiChar(UTF8ToSys(ExtractFilePath(ArchiveName))), @FStats) = 0 then
577 Result := Int64(FStats.bAvail) * Int64(FStats.bsize)
578 {$ELSEIF DEFINED(PosixAPI)}
579 if statvfs(PAnsiChar(AbSysString(ExtractFilePath(ArchiveName))), FStats) = 0 then
580 Result := Int64(FStats.f_bavail) * Int64(FStats.f_bsize)
581 {$IFEND}
582 else
583 Result := -1;
584 {$ENDIF}
585 end;
586 { -------------------------------------------------------------------------- }
587 function AbFileMatch(FileName: string; FileMask: string ): Boolean;
588 {see if FileName matches FileMask}
589 var
590 DirMatch : Boolean;
591 MaskDir : string;
592 begin
593 //FileName := UpperCase( FileName );
594 //FileMask := UpperCase( FileMask );
595 MaskDir := ExtractFilePath( FileMask );
596 if MaskDir = '' then
597 DirMatch := True
598 else
599 DirMatch := AbPatternMatch( ExtractFilePath( FileName ), 1, MaskDir, 1 );
600
601 Result := DirMatch and AbPatternMatch( ExtractFileName( FileName ), 1,
602 ExtractFileName( FileMask ), 1 );
603 end;
604 { -------------------------------------------------------------------------- }
605 procedure AbFindFiles( const FileMask : string; SearchAttr : Integer;
606 FileList : TStrings; Recurse : Boolean );
607 var
608 NewFile : string;
609 SR : TSearchRec;
610 Found : Integer;
611 NameMask: string;
612 begin
613 Found := FindFirst( FileMask, SearchAttr, SR );
614 if Found = 0 then begin
615 try
616 NameMask := ExtractFileName(FileMask);
617 while Found = 0 do begin
618 NewFile := ExtractFilePath( FileMask ) + SR.Name;
619 if (SR.Name <> AbThisDir) and
620 (SR.Name <> AbParentDir) and
621 AbPatternMatch(SR.Name, 1, NameMask, 1) then
622 if (SR.Attr and faDirectory) <> 0 then
623 FileList.Add( NewFile + PathDelim )
624 else
625 FileList.Add( NewFile );
626 Found := FindNext( SR );
627 end;
628 finally
629 FindClose( SR );
630 end;
631 end;
632 if not Recurse then
633 Exit;
634 NewFile := ExtractFilePath( FileMask );
635 if ( NewFile <> '' ) and ( NewFile[Length(NewFile)] <> AbPathDelim) then
636 NewFile := NewFile + AbPathDelim;
637 NewFile := NewFile + AbAnyFile;
638
639 Found := FindFirst( NewFile, faDirectory or SearchAttr, SR );
640 if Found = 0 then begin
641 try
642 while ( Found = 0 ) do begin
643 if ( SR.Name <> AbThisDir ) and
644 ( SR.Name <> AbParentDir ) and
645 ((SR.Attr and faDirectory) > 0 ) then
646 AbFindFiles( ExtractFilePath( NewFile ) + SR.Name + AbPathDelim +
647 ExtractFileName( FileMask ), SearchAttr,
648 FileList, True );
649 Found := FindNext( SR );
650 end;
651 finally
652 FindClose( SR );
653 end;
654 end;
655 end;
656 { -------------------------------------------------------------------------- }
657 procedure AbFindFilesEx( const FileMask : string; SearchAttr : Integer;
658 FileList : TStrings; Recurse : Boolean );
659 var
660 I, J: Integer;
661 MaskPart: string;
662 begin
663 I := 1;
664 while I <= Length(FileMask) do begin
665 J := I;
666 while (I <= Length(FileMask)) and (FileMask[I] <> AbPathSep) do Inc(I);
667 MaskPart := Trim(Copy(FileMask, J, I - J));
668 if (I <= Length(FileMask)) and (FileMask[I] = AbPathSep) then Inc(I);
669
670 AbFindFiles(MaskPart, SearchAttr, FileList, Recurse);
671 end;
672 end;
673 { -------------------------------------------------------------------------- }
674 function AbAddBackSlash(const DirName : string) : string;
675 { Add a default slash to a directory name }
676 const
677 AbDelimSet : set of AnsiChar = [AbPathDelim, ':', #0];
678 begin
679 Result := DirName;
680 if Length(DirName) = 0 then
681 Exit;
682 if not CharInSet(DirName[Length(DirName)], AbDelimSet) then
683 Result := DirName + AbPathDelim;
684 end;
685 { -------------------------------------------------------------------------- }
686 function AbFindNthSlash( const Path : string; n : Integer ) : Integer;
687 { return the position of the character just before the nth slash }
688 var
689 i : Integer;
690 Len : Integer;
691 iSlash : Integer;
692 begin
693 Len := Length( Path );
694 Result := Len;
695 iSlash := 0;
696 i := 0;
697 while i <= Len do begin
698 if Path[i] = AbPathDelim then begin
699 inc( iSlash );
700 if iSlash = n then begin
701 Result := pred( i );
702 break;
703 end;
704 end;
705 inc( i );
706 end;
707 end;
708 { -------------------------------------------------------------------------- }
709 function AbGetPathType( const Value : string ) : TAbPathType;
710 { returns path type - none, relative or absolute }
711 begin
712 Result := ptNone;
713 {$IFDEF MSWINDOWS}
714 {check for drive/unc info}
715 if ( Pos( '\\', Value ) > 0 ) or ( Pos( ':', Value ) > 0 ) then
716 {$ENDIF MSWINDOWS}
717 {$IFDEF UNIX}
718 { UNIX absolute paths start with a slash }
719 if (Length(Value) > 0) and (Value[1] = AbPathDelim) then
720 {$ENDIF UNIX}
721 Result := ptAbsolute
722 else if ( Pos( AbPathDelim, Value ) > 0 ) or ( Pos( AB_ZIPPATHDELIM, Value ) > 0 ) then
723 Result := ptRelative;
724 end;
725 { -------------------------------------------------------------------------- }
726 {$IFDEF MSWINDOWS}
727 {$WARN SYMBOL_PLATFORM OFF}
728 function AbGetShortFileSpec(const LongFileSpec : string ) : string;
729 var
730 SR : TSearchRec;
731 Search : string;
732 Drive : string;
733 Path : string;
734 FileName : string;
735 Found : Integer;
736 SubPaths : TStrings;
737 i : Integer;
738 begin
739 AbParseFileName( LongFileSpec, Drive, Path, FileName );
740 SubPaths := TStringList.Create;
741 try
742 AbParsePath( Path, SubPaths );
743 Search := Drive;
744 Result := Search + AbPathDelim;
745 if SubPaths.Count > 0 then
746 for i := 0 to pred( SubPaths.Count ) do begin
747 Search := Search + AbPathDelim + SubPaths[i];
748 Found := FindFirst( Search, faHidden + faSysFile + faDirectory, SR );
749 if Found <> 0 then
750 raise EAbException.Create( 'Path not found' );
751 try
752 Result := Result + ExtractShortName(SR) + AbPathDelim;
753 finally
754 FindClose( SR );
755 end;
756 end;
757 Search := Search + AbPathDelim + FileName;
758 Found := FindFirst( Search,
759 faReadOnly + faHidden + faSysFile + faArchive, SR );
760 if Found <> 0 then
761 raise EAbFileNotFound.Create;
762 try
763 Result := Result + ExtractShortName(SR);
764 finally
765 FindClose( SR );
766 end;
767 finally
768 SubPaths.Free;
769 end;
770 end;
771 {$WARN SYMBOL_PLATFORM ON}
772 {$ENDIF}
773 { -------------------------------------------------------------------------- }
774 procedure AbIncFilename( var Filename : string; Value : Word );
775 { place value at the end of filename, e.g. Files.C04 }
776 var
777 Ext : string;
778 I : Word;
779 begin
780 I := (Value + 1) mod 100;
781 Ext := ExtractFileExt(Filename);
782 if (Length(Ext) < 2) then
783 Ext := '.' + Format('%.2d', [I])
784 else
785 Ext := Ext[1] + Ext[2] + Format('%.2d', [I]);
786 Filename := ChangeFileExt(Filename, Ext);
787 end;
788 { -------------------------------------------------------------------------- }
789 procedure AbParseFileName( FileSpec : string;
790 out Drive : string;
791 out Path : string;
792 out FileName : string );
793 var
794 i : Integer;
795 iColon : Integer;
796 iStartSlash : Integer;
797 begin
798 if Pos( AB_ZIPPATHDELIM, FileSpec ) > 0 then
799 AbUnfixName( FileSpec );
800 FileName := ExtractFileName( FileSpec );
801 Path := ExtractFilePath( FileSpec );
802 {see how much of the path currently exists}
803 iColon := Pos( ':', Path );
804 if Pos( '\\', Path ) > 0 then begin
805 {UNC Path \\computername\sharename\path1..\pathn}
806 {everything up to the 4th slash is the drive}
807 iStartSlash := 4;
808 i := AbFindNthSlash( Path, iStartSlash );
809 Drive := Copy( Path, 1, i );
810 Delete( Path, 1, i + 1 );
811 end
812 else if iColon > 0 then begin
813 Drive := Copy( Path, 1, iColon );
814 Delete( Path, 1, iColon );
815 if Path[1] = AbPathDelim then
816 Delete( Path, 1, 1 );
817 end;
818 end;
819 { -------------------------------------------------------------------------- }
820 procedure AbParsePath( Path : string; SubPaths : TStrings );
821 { break abart path into subpaths --- Path : abbrevia/examples >
822 SubPaths[0] = abbrevia
823 SubPaths[1] = examples}
824 var
825 i : Integer;
826 iStart : Integer;
827 iStartSlash : Integer;
828 SubPath : string;
829 begin
830 if Path = '' then Exit;
831 if Path[ Length( Path ) ] = AbPathDelim then
832 Delete( Path, Length( Path ), 1 );
833 iStart := 1;
834 iStartSlash := 1;
835 repeat
836 {find the Slash at iStartSlash}
837 i := AbFindNthSlash( Path, iStartSlash );
838 {get the subpath}
839 SubPath := Copy( Path, iStart, i - iStart + 1 );
840 iStart := i + 2;
841 inc( iStartSlash );
842 SubPaths.Add( SubPath );
843 until ( i = Length( Path ) );
844 end;
845 { -------------------------------------------------------------------------- }
846 function AbPatternMatch(const Source : string; iSrc : Integer;
847 const Pattern : string; iPat : Integer ) : Boolean;
848 { recursive routine to see if the source string matches
849 the pattern. Both ? and * wildcard characters are allowed.
850 Compares Source from iSrc to Length(Source) to
851 Pattern from iPat to Length(Pattern)}
852 var
853 Matched : Boolean;
854 k : Integer;
855 begin
856 if Length( Source ) = 0 then begin
857 Result := Length( Pattern ) = 0;
858 Exit;
859 end;
860
861 if iPat = 1 then begin
862 if ( CompareStr( Pattern, AbDosAnyFile) = 0 ) or
863 ( CompareStr( Pattern, AbUnixAnyFile ) = 0 ) then begin
864 Result := True;
865 Exit;
866 end;
867 end;
868
869 if Length( Pattern ) = 0 then begin
870 Result := (Length( Source ) - iSrc + 1 = 0);
871 Exit;
872 end;
873
874 while True do begin
875 if ( Length( Source ) < iSrc ) and
876 ( Length( Pattern ) < iPat ) then begin
877 Result := True;
878 Exit;
879 end;
880
881 if Length( Pattern ) < iPat then begin
882 Result := False;
883 Exit;
884 end;
885
886 if Pattern[iPat] = '*' then begin
887 k := iPat;
888 if ( Length( Pattern ) < iPat + 1 ) then begin
889 Result := True;
890 Exit;
891 end;
892
893 while True do begin
894 Matched := AbPatternMatch( Source, k, Pattern, iPat + 1 );
895 if Matched or ( Length( Source ) < k ) then begin
896 Result := Matched;
897 Exit;
898 end;
899 inc( k );
900 end;
901 end
902 else begin
903 if ( (Pattern[iPat] = '?') and
904 ( Length( Source ) <> iSrc - 1 ) ) or
905 ( Pattern[iPat] = Source[iSrc] ) then begin
906 inc( iPat );
907 inc( iSrc );
908 end
909 else begin
910 Result := False;
911 Exit;
912 end;
913 end;
914 end;
915 end;
916 { -------------------------------------------------------------------------- }
917 function AbPercentage(V1, V2 : Int64): Byte;
918 { Returns the ratio of V1 to V2 * 100 }
919 begin
920 if V2 <= 0 then
921 Result := 0
922 else if V1 >= V2 then
923 Result := 100
924 else
925 Result := (V1 * 100) div V2;
926 end;
927 { -------------------------------------------------------------------------- }
928 procedure AbStripDots( var FName : string );
929 { strips relative path information, e.g. ".."}
930 begin
931 while Pos( AbParentDir + AbPathDelim, FName ) = 1 do
932 System.Delete( FName, 1, 3 );
933 end;
934 { -------------------------------------------------------------------------- }
935 procedure AbStripDrive( var FName : string );
936 { strips the drive off a filename }
937 var
938 Drive, Path, Name : string;
939 begin
940 AbParseFileName( FName, Drive, Path, Name );
941 FName := Path + Name;
942 end;
943 { -------------------------------------------------------------------------- }
944 procedure AbFixName( var FName : string );
945 { changes backslashes to forward slashes }
946 var
947 i : Integer;
948 begin
949 for i := 1 to Length( FName ) do
950 if FName[i] = AbPathDelim then
951 FName[i] := AB_ZIPPATHDELIM;
952 end;
953 { -------------------------------------------------------------------------- }
954 procedure AbUnfixName( var FName : string );
955 { changes forward slashes to backslashes }
956 var
957 i : Integer;
958 begin
959 for i := 1 to Length( FName ) do
960 if FName[i] = AB_ZIPPATHDELIM then
961 FName[i] := AbPathDelim;
962 end;
963 { -------------------------------------------------------------------------- }
964 procedure AbUpdateCRC( var CRC : LongInt; const Buffer; Len : Integer );
965 var
966 BufPtr : PByte;
967 i : Integer;
968 CRCTemp : DWORD;
969 begin
970 BufPtr := @Buffer;
971 CRCTemp := CRC;
972 for i := 0 to pred( Len ) do
973 begin
974 CRCTemp := AbCrc32Table[ Byte(CrcTemp) xor (BufPtr^) ] xor
975 ((CrcTemp shr 8) and $00FFFFFF);
976 Inc(BufPtr);
977 end;
978 CRC := CRCTemp;
979 end;
980 { -------------------------------------------------------------------------- }
981 function AbUpdateCRC32(CurByte : Byte; CurCrc : LongInt) : LongInt;
982 { Return the updated 32bit CRC }
983 { Normally a good candidate for basm, but Delphi32's code
984 generation couldn't be beat on this one!}
985 begin
986 Result := DWORD(AbCrc32Table[ Byte(CurCrc xor LongInt( CurByte ) ) ] xor
987 ((CurCrc shr 8) and DWORD($00FFFFFF)));
988 end;
989 { -------------------------------------------------------------------------- }
990 function AbCRC32Of( const aValue : RawByteString ) : LongInt;
991 begin
992 Result := -1;
993 AbUpdateCRC(Result, aValue[1], Length(aValue));
994 Result := not Result;
995 end;
996 { -------------------------------------------------------------------------- }
997 function AbWriteVolumeLabel(const VolName : string;
998 Drive : Char) : Cardinal;
999 {$IFDEF MSWINDOWS}
1000 var
1001 Temp : UnicodeString;
1002 Vol : array[0..11] of WideChar;
1003 Root : array[0..3] of WideChar;
1004 {$ENDIF}
1005 begin
1006 {$IFDEF MSWINDOWS}
1007 Temp := CeUtf8ToUtf16(VolName);
1008 StrPCopyW(Root, '%:' + AbPathDelim);
1009 Root[0] := Drive;
1010 if Length(Temp) > 11 then
1011 SetLength(Temp, 11);
1012 StrPCopyW(Vol, Temp);
1013 if Windows.SetVolumeLabelW(Root, Vol) then
1014 Result := 0
1015 else Result := GetLastError;
1016 {$ENDIF MSWINDOWS}
1017 {$IFDEF UNIX}
1018 { Volume labels not supported on Unix }
1019 Result := 0;
1020 {$ENDIF UNIX}
1021 end;
1022 { -------------------------------------------------------------------------- }
1023 {$IFDEF MSWINDOWS}
1024 function AbOffsetFromUTC: LongInt;
1025 { local timezone's offset from UTC in seconds (UTC = local + bias) }
1026 var
1027 TZI: TTimeZoneInformation;
1028 begin
1029 case GetTimeZoneInformation(TZI) of
1030 TIME_ZONE_ID_UNKNOWN:
1031 Result := TZI.Bias;
1032 TIME_ZONE_ID_DAYLIGHT:
1033 Result := TZI.Bias + TZI.DaylightBias;
1034 TIME_ZONE_ID_STANDARD:
1035 Result := TZI.Bias + TZI.StandardBias
1036 else
1037 Result := 0
1038 end;
1039 Result := Result * SecondsInMinute;
1040 end;
1041 {$ENDIF}
1042 { -------------------------------------------------------------------------- }
1043 function AbUnixTimeToLocalDateTime(UnixTime : Int64) : TDateTime;
1044 { convert UTC unix date to Delphi TDateTime in local timezone }
1045 {$IFDEF MSWINDOWS}
1046 var
1047 Hrs, Mins, Secs : Word;
1048 TodaysSecs : LongInt;
1049 Time: TDateTime;
1050 begin
1051 UnixTime := UnixTime - AbOffsetFromUTC;
1052 TodaysSecs := UnixTime mod SecondsInDay;
1053 Hrs := TodaysSecs div SecondsInHour;
1054 TodaysSecs := TodaysSecs - (Hrs * SecondsInHour);
1055 Mins := TodaysSecs div SecondsInMinute;
1056 Secs := TodaysSecs - (Mins * SecondsInMinute);
1057
1058 if TryEncodeTime(Hrs, Mins, Secs, 0, Time) then
1059 Result := Unix0Date + (UnixTime div SecondsInDay) + Time
1060 else
1061 Result := 0;
1062 {$ENDIF}
1063 {$IFDEF UNIX}
1064 begin
1065 Result := UnixFileTimeToDateTime(TUnixFileTime(UnixTime));
1066 {$ENDIF}
1067 end;
1068
1069 { -------------------------------------------------------------------------- }
1070 function AbLocalDateTimeToUnixTime(DateTime : TDateTime) : Int64;
1071 { convert local Delphi TDateTime to UTC unix date }
1072 {$IFDEF MSWINDOWS}
1073 var
1074 Hrs, Mins, Secs, MSecs : Word;
1075 Dt, Tm : TDateTime;
1076 begin
1077 Dt := Trunc(DateTime);
1078 Tm := DateTime - Dt;
1079 if Dt < Unix0Date then
1080 Result := 0
1081 else
1082 Result := Trunc(Dt - Unix0Date) * SecondsInDay;
1083
1084 DecodeTime(Tm, Hrs, Mins, Secs, MSecs);
1085 Result := Result + (Hrs * SecondsInHour) + (Mins * SecondsInMinute) + Secs;
1086 Result := Result + AbOffsetFromUTC;
1087 {$ENDIF}
1088 {$IFDEF UNIX}
1089 begin
1090 Result := Int64(DateTimeToUnixFileTime(DateTime));
1091 {$ENDIF}
1092 end;
1093 { -------------------------------------------------------------------------- }
1094 function AbDosFileDateToDateTime(FileDate, FileTime : Word) : TDateTime;
1095 var
1096 Yr, Mo, Dy : Word;
1097 Hr, Mn, S : Word;
1098 begin
1099 Yr := FileDate shr 9 + 1980;
1100
1101 Mo := FileDate shr 5 and 15;
1102 if Mo < 1 then Mo := 1;
1103 if Mo > 12 then Mo := 12;
1104
1105 Dy := FileDate and 31;
1106 if Dy < 1 then Dy := 1;
1107 if Dy > DaysInAMonth(Yr, Mo) then
1108 Dy := DaysInAMonth(Yr, Mo);
1109
1110 Hr := FileTime shr 11;
1111 if Hr > 23 then Hr := 23;
1112
1113 Mn := FileTime shr 5 and 63;
1114 if Mn > 59 then Mn := 59;
1115
1116 S := FileTime and 31 shl 1;
1117 if S > 59 then S := 59;
1118
1119 Result :=
1120 EncodeDate(Yr, Mo, Dy) +
1121 EncodeTime(Hr, Mn, S, 0);
1122 end;
1123
1124 function AbDateTimeToDosFileDate(Value : TDateTime) : LongInt;
1125 {$IFDEF MSWINDOWS}
1126 begin
1127 Result := DateTimeToFileDate(Value);
1128 {$ENDIF MSWINDOWS}
1129 {$IFDEF UNIX}
1130 var
1131 Yr, Mo, Dy : Word;
1132 Hr, Mn, S, MS: Word;
1133 begin
1134 DecodeDate(Value, Yr, Mo, Dy);
1135 if (Yr < 1980) or (Yr > 2107) then { outside DOS file date year range }
1136 Yr := 1980;
1137 DecodeTime(Value, Hr, Mn, S, MS);
1138
1139 LongRec(Result).Lo := (S shr 1) or (Mn shl 5) or (Hr shl 11);
1140 LongRec(Result).Hi := Dy or (Mo shl 5) or (Word(Yr - 1980) shl 9);
1141 {$ENDIF UNIX}
1142 end;
1143
1144 { -------------------------------------------------------------------------- }
1145
1146 function AbGetFileTime(const aFileName: string): TDateTime;
1147 var
1148 Attr: TAbAttrExRec;
1149 begin
1150 AbFileGetAttrEx(aFileName, Attr);
1151 Result := Attr.Time;
1152 end;
1153
1154 function AbSetFileTime(const aFileName: string; aValue: TDateTime): Boolean;
1155 begin
1156 Result:= mbFileSetTime(aFileName, DateTimeToFileTime(aValue));
1157 end;
1158
1159 { -------------------------------------------------------------------------- }
1160 function AbSwapLongEndianness(Value : LongInt): LongInt;
1161 { convert BigEndian <-> LittleEndian 32-bit value }
1162 type
1163 TCastArray = array [0..3] of Byte;
1164 var
1165 i : Integer;
1166 begin
1167 for i := 3 downto 0 do
1168 TCastArray(Result)[3-i] := TCastArray(Value)[i];
1169 end;
1170 { -------------------------------------------------------------------------- }
1171 function AbDOS2UnixFileAttributes(Attr: LongInt): LongInt;
1172 begin
1173 {$IFDEF LINUX} {$IF NOT ((FPC_VERSION = 2) and (FPC_RELEASE = 6) and (FPC_PATCH = 0))} {$WARN SYMBOL_PLATFORM OFF} {$ENDIF}
1174 {$ELSE} {$WARN SYMBOL_PLATFORM OFF} {$ENDIF}
1175 Result := { default permissions }
1176 AB_FPERMISSION_OWNERREAD or
1177 AB_FPERMISSION_GROUPREAD or
1178 AB_FPERMISSION_OTHERREAD;
1179
1180 if (Attr and faReadOnly) = 0 then
1181 Result := Result or AB_FPERMISSION_OWNERWRITE;
1182
1183 if (Attr and faSymLink) <> 0 then
1184 Result := Result or AB_FMODE_FILELINK or AB_FPERMISSION_OWNEREXECUTE
1185 else if (Attr and faDirectory) <> 0 then
1186 Result := Result or AB_FMODE_DIR or AB_FPERMISSION_OWNEREXECUTE
1187 else
1188 Result := Result or AB_FMODE_FILE;
1189 {$IFDEF LINUX} {$IF NOT ((FPC_VERSION = 2) and (FPC_RELEASE = 6) and (FPC_PATCH = 0))} {$WARN SYMBOL_PLATFORM ON} {$ENDIF}
1190 {$ELSE} {$WARN SYMBOL_PLATFORM ON} {$ENDIF}
1191 end;
1192 { -------------------------------------------------------------------------- }
1193 function AbUnix2DosFileAttributes(Attr: LongInt): LongInt;
1194 begin
1195 {$IFDEF LINUX} {$IF NOT ((FPC_VERSION = 2) and (FPC_RELEASE = 6) and (FPC_PATCH = 0))} {$WARN SYMBOL_PLATFORM OFF} {$ENDIF}
1196 {$ELSE} {$WARN SYMBOL_PLATFORM OFF} {$ENDIF}
1197 Result := 0;
1198 case (Attr and $F000) of
1199 AB_FMODE_FILE, AB_FMODE_FILE2: { standard file }
1200 Result := 0;
1201
1202 AB_FMODE_DIR: { directory }
1203 Result := Result or faDirectory;
1204
1205 AB_FMODE_FILELINK: { symlink}
1206 Result := Result or faSymLink;
1207
1208 AB_FMODE_FIFO,
1209 AB_FMODE_CHARSPECFILE,
1210 AB_FMODE_BLOCKSPECFILE,
1211 AB_FMODE_SOCKET:
1212 Result := Result or faSysFile;
1213 end;
1214
1215 if (Attr and AB_FPERMISSION_OWNERWRITE) <> AB_FPERMISSION_OWNERWRITE then
1216 Result := Result or faReadOnly;
1217 {$IFDEF LINUX} {$IF NOT ((FPC_VERSION = 2) and (FPC_RELEASE = 6) and (FPC_PATCH = 0))} {$WARN SYMBOL_PLATFORM ON} {$ENDIF}
1218 {$ELSE} {$WARN SYMBOL_PLATFORM ON} {$ENDIF}
1219 end;
1220 { -------------------------------------------------------------------------- }
1221 procedure AbSetFileAttr(const aFileName : string; aAttr: Integer);
1222 begin
1223 {$IFDEF MSWINDOWS}
1224 mbFileSetAttr(aFileName, aAttr);
1225 {$ENDIF}
1226 {$IF DEFINED(LibcAPI) OR DEFINED(PosixAPI)}
1227 chmod(PAnsiChar(AbSysString(aFileName)), aAttr);
1228 {$ELSEIF DEFINED(FPCUnixAPI)}
1229 mbFileSetAttr(aFileName, aAttr);
1230 {$IFEND}
1231 end;
1232 { -------------------------------------------------------------------------- }
1233 function AbFileGetSize(const aFileName : string) : Int64;
1234 var
1235 SR: TAbAttrExRec;
1236 begin
1237 if AbFileGetAttrEx(aFileName, SR) then
1238 Result := SR.Size
1239 else
1240 Result := -1;
1241 end;
1242 { -------------------------------------------------------------------------- }
1243 function AbFileGetAttrEx(const aFileName: string; out aAttr: TAbAttrExRec; FollowLinks: Boolean = True) : Boolean;
1244 var
1245 {$IFDEF MSWINDOWS}
1246 FileDate: LongRec;
1247 FindData: TWin32FindDataW;
1248 LocalFileTime: Windows.TFileTime;
1249 {$ENDIF}
1250 {$IFDEF FPCUnixAPI}
1251 StatBuf: stat;
1252 {$ENDIF}
1253 {$IFDEF LibcAPI}
1254 StatBuf: TStatBuf64;
1255 {$ENDIF}
1256 {$IFDEF PosixAPI}
1257 StatBuf: _stat;
1258 {$ENDIF}
1259 begin
1260 aAttr.Time := 0;
1261 aAttr.Size := -1;
1262 aAttr.Attr := -1;
1263 aAttr.Mode := 0;
1264 {$IFDEF MSWINDOWS}
1265 Result := GetFileAttributesExW(PWideChar(UTF16LongName(aFileName)), GetFileExInfoStandard, @FindData);
1266 if Result then begin
1267 aAttr.Time := WinFileTimeToDateTime(FindData.ftLastWriteTime);
1268 LARGE_INTEGER(aAttr.Size).LowPart := FindData.nFileSizeLow;
1269 LARGE_INTEGER(aAttr.Size).HighPart := FindData.nFileSizeHigh;
1270 aAttr.Attr := FindData.dwFileAttributes;
1271 aAttr.Mode := AbDOS2UnixFileAttributes(FindData.dwFileAttributes);
1272 end;
1273 {$ENDIF}
1274 {$IFDEF UNIX}
1275 {$IFDEF FPCUnixAPI}
1276 if FollowLinks then
1277 Result := (FpStat(UTF8ToSys(aFileName), StatBuf) = 0)
1278 else
1279 Result := (FpLStat(UTF8ToSys(aFileName), StatBuf) = 0);
1280 {$ENDIF}
1281 {$IFDEF LibcAPI}
1282 // Work around Kylix QC#2761: Stat64, et al., are defined incorrectly
1283 Result := (__lxstat64(_STAT_VER, PAnsiChar(aFileName), StatBuf) = 0);
1284 {$ENDIF}
1285 {$IFDEF PosixAPI}
1286 Result := (stat(PAnsiChar(AbSysString(aFileName)), StatBuf) = 0);
1287 {$ENDIF}
1288 if Result then begin
1289 aAttr.Time := FileDateToDateTime(StatBuf.st_mtime);
1290 aAttr.Size := StatBuf.st_size;
1291 aAttr.Attr := AbUnix2DosFileAttributes(StatBuf.st_mode);
1292 aAttr.Mode := StatBuf.st_mode;
1293 end;
1294 {$ENDIF UNIX}
1295 end;
1296
1297
1298 const
1299 MAX_VOL_LABEL = 16;
1300
1301 function AbGetVolumeLabel(Drive : Char) : string;
1302 {-Get the volume label for the specified drive.}
1303 {$IFDEF MSWINDOWS}
1304 var
1305 Root : WideString;
1306 Flags, MaxLength : DWORD;
1307 NameSize : Integer;
1308 VolName : WideString;
1309 {$ENDIF}
1310 begin
1311 {$IFDEF MSWINDOWS}
1312 NameSize := 0;
1313 Root := Drive + ':\';
1314 SetLength(VolName, MAX_VOL_LABEL);
1315
1316 Result := '';
1317
1318 if GetVolumeInformationW(PWideChar(Root), PWideChar(VolName), Length(VolName),
1319 nil, MaxLength, Flags, nil, NameSize)
1320 then
1321 Result := Utf16ToUtf8(VolName);
1322 {$ELSE}
1323 Result := ''; //Stop Gap, spanning support needs to be rethought for Unix
1324 {$ENDIF}
1325 end;
1326
1327 procedure AbSetSpanVolumeLabel(Drive: Char; VolNo : Integer);
1328 begin
1329 AbWriteVolumeLabel(Format(AB_SPAN_VOL_LABEL,
1330 [VolNo]), Drive);
1331 end;
1332
1333 function AbTestSpanVolumeLabel(Drive: Char; VolNo : Integer): Boolean;
1334 var
1335 VolLabel, TestLabel : string;
1336 begin
1337 TestLabel := Format(AB_SPAN_VOL_LABEL, [VolNo]);
1338 VolLabel := UpperCase(AbGetVolumeLabel(Drive));
1339 Result := VolLabel = TestLabel;
1340 end;
1341
1342 { Unicode backwards compatibility functions }
1343 {$IFNDEF UNICODE}
1344 function CharInSet(C: AnsiChar; CharSet: TSysCharSet): Boolean;
1345 begin
1346 Result := C in CharSet;
1347 end;
1348 {$ENDIF}
1349
1350 end.