-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
WinFileInfo.pas
2150 lines (1939 loc) · 85.2 KB
/
WinFileInfo.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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
WinFileInfo
Main aim of this library is to provide a simple way of obtaining file
information such as size, attributes, time of creation and, in case of
binaries on Windows OS, a version information.
A complete parsing of raw version information data is implemented, so
it is possible to obtain information even from badly constructed version
info resource.
Although the library was intended only for Windows OS, it can now be
compiled for other systems too. But in such case, it provides only a
limited file information set.
Version 1.1.3 (2024-04-28)
Last change 2024-10-04
©2015-2024 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.WinFileInfo
Dependencies:
* AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
* StrRect - github.com/TheLazyTomcat/Lib.StrRect
Library AuxExceptions is required only when rebasing local exception classes
(see symbol WinFileInfo_UseAuxExceptions for details).
StrRect is required only when compiling for Windows OS.
Library StrRect might also be required as an indirect dependency.
Indirect dependencies:
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
===============================================================================}
unit WinFileInfo;
{
WinFileInfo_UseAuxExceptions
If you want library-specific exceptions to be based on more advanced classes
provided by AuxExceptions library instead of basic Exception class, and don't
want to or cannot change code in this unit, you can define global symbol
WinFileInfo_UseAuxExceptions to achieve this.
}
{$IF Defined(WinFileInfo_UseAuxExceptions)}
{$DEFINE UseAuxExceptions}
{$IFEND}
//------------------------------------------------------------------------------
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$ELSEIF Defined(LINUX) and Defined(FPC)}
{
There is FPC-specific code used in non-Windows systems, therefore compilation
for these systems has to be allowed only on FPC.
}
{$DEFINE Linux}
{$ELSE}
{$MESSAGE FATAL 'Unsupported OS-compiler combination.'}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ENDIF}
{$H+}
interface
uses
SysUtils, Classes, {$IFDEF Windows}Windows,{$ELSE}UnixType,{$ENDIF}
AuxTypes{$IFDEF UseAuxExceptions}, AuxExceptions{$ENDIF};
{===============================================================================
Library-specific exceptions
===============================================================================}
type
EWFIException = class({$IFDEF UseAuxExceptions}EAEGeneralException{$ELSE}Exception{$ENDIF});
EWFIFileError = class(EWFIException);
EWFIProcessingError = class(EWFIException);
EWFISystemError = class(EWFIException);
EWFIIndexOutOfBounds = class(EWFIException); // used only in windows
{===============================================================================
--------------------------------------------------------------------------------
Utility functions
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Utility functions - declaration
===============================================================================}
{
Returns size of the given file in bytes.
If the file does not exist, it will raise an EWFIFileError exception.
}
Function GetFileSize(const FileName: String): UInt64;
//------------------------------------------------------------------------------
{
FileSizeToStr expects passed number to be a file size and converts it to its
string representation (as a decimal number if needed), including proper unit
(KiB, MiB, ...).
NOTE - version without FormatSettings parameter is not thread safe, use
FileSizeToStrThr in non-main thread(s) if you cannot provide filled
format settings (it uses default settings provided by OS or RLT).
}
Function FileSizeToStr(FileSize: UInt64; FormatSettings: TFormatSettings; SpaceUnit: Boolean = True): String; overload;
Function FileSizeToStr(FileSize: UInt64; SpaceUnit: Boolean = True): String; overload;
Function FileSizeToStrThr(FileSize: UInt64; SpaceUnit: Boolean = True): String;
//------------------------------------------------------------------------------
{
Returns true when both paths (A and B) points to the same file, false
otherwise.
If either of the two paths points to a file that does not exist, the function
will raise an EWFIFileError exception.
}
Function SameFile(const A,B: String): Boolean;
{===============================================================================
--------------------------------------------------------------------------------
TWinFileInfo
--------------------------------------------------------------------------------
===============================================================================}
type
TWFIFileHandle = {$IFDEF Windows}THandle{$ELSE}cint{$ENDIF};
{===============================================================================
TWinFileInfo - constants
===============================================================================}
{$IFDEF Windows}
const
// File attributes flags
INVALID_FILE_ATTRIBUTES = DWORD(-1);
FILE_ATTRIBUTE_ARCHIVE = $20;
FILE_ATTRIBUTE_COMPRESSED = $800;
FILE_ATTRIBUTE_DEVICE = $40;
FILE_ATTRIBUTE_DIRECTORY = $10;
FILE_ATTRIBUTE_ENCRYPTED = $4000;
FILE_ATTRIBUTE_HIDDEN = $2;
FILE_ATTRIBUTE_INTEGRITY_STREAM = $8000;
FILE_ATTRIBUTE_NORMAL = $80;
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = $2000;
FILE_ATTRIBUTE_NO_SCRUB_DATA = $20000;
FILE_ATTRIBUTE_OFFLINE = $1000;
FILE_ATTRIBUTE_READONLY = $1;
FILE_ATTRIBUTE_REPARSE_POINT = $400;
FILE_ATTRIBUTE_SPARSE_FILE = $200;
FILE_ATTRIBUTE_SYSTEM = $4;
FILE_ATTRIBUTE_TEMPORARY = $100;
FILE_ATTRIBUTE_VIRTUAL = $10000;
// Flags for field TVSFixedFileInfo.dwFileFlags
VS_FF_DEBUG = $00000001;
VS_FF_INFOINFERRED = $00000010;
VS_FF_PATCHED = $00000004;
VS_FF_PRERELEASE = $00000002;
VS_FF_PRIVATEBUILD = $00000008;
VS_FF_SPECIALBUILD = $00000020;
// Flags for field TVSFixedFileInfo.dwFileOS
VOS_DOS = $00010000;
VOS_NT = $00040000;
VOS__WINDOWS16 = $00000001;
VOS__WINDOWS32 = $00000004;
VOS_OS216 = $00020000;
VOS_OS232 = $00030000;
VOS__PM16 = $00000002;
VOS__PM32 = $00000003;
VOS_UNKNOWN = $00000000;
VOS_DOS_WINDOWS16 = $00010001;
VOS_DOS_WINDOWS32 = $00010004;
VOS_NT_WINDOWS32 = $00040004;
VOS_OS216_PM16 = $00020002;
VOS_OS232_PM32 = $00030003;
// Flags for field TVSFixedFileInfo.dwFileType
VFT_APP = $00000001;
VFT_DLL = $00000002;
VFT_DRV = $00000003;
VFT_FONT = $00000004;
VFT_STATIC_LIB = $00000007;
VFT_UNKNOWN = $00000000;
VFT_VXD = $00000005;
// Flags for field TVSFixedFileInfo.dwFileSubtype when
// TVSFixedFileInfo.dwFileType is set to VFT_DRV
VFT2_DRV_COMM = $0000000A;
VFT2_DRV_DISPLAY = $00000004;
VFT2_DRV_INSTALLABLE = $00000008;
VFT2_DRV_KEYBOARD = $00000002;
VFT2_DRV_LANGUAGE = $00000003;
VFT2_DRV_MOUSE = $00000005;
VFT2_DRV_NETWORK = $00000006;
VFT2_DRV_PRINTER = $00000001;
VFT2_DRV_SOUND = $00000009;
VFT2_DRV_SYSTEM = $00000007;
VFT2_DRV_VERSIONED_PRINTER = $0000000C;
VFT2_UNKNOWN = $00000000;
// Flags for field TVSFixedFileInfo.dwFileSubtype when
// TVSFixedFileInfo.dwFileType is set to VFT_FONT
VFT2_FONT_RASTER = $00000001;
VFT2_FONT_TRUETYPE = $00000003;
VFT2_FONT_VECTOR = $00000002;
{$ENDIF}
{===============================================================================
TWinFileInfo - types
===============================================================================}
{$IFDEF Windows}
{
Following structures are used to store information about requested file in
a more user-friendly and better accessible way.
}
type
TWFIFileAttributesDecoded = record
Archive: Boolean;
Compressed: Boolean;
Device: Boolean;
Directory: Boolean;
Encrypted: Boolean;
Hidden: Boolean;
IntegrityStream: Boolean;
Normal: Boolean;
NotContentIndexed: Boolean;
NoScrubData: Boolean;
Offline: Boolean;
ReadOnly: Boolean;
ReparsePoint: Boolean;
SparseFile: Boolean;
System: Boolean;
Temporary: Boolean;
Virtual: Boolean;
end;
//------------------------------------------------------------------------------
{
Group of structures used to store decoded information from fixed file info
part of version information resource.
}
type
TWFIFixedFileInfo_VersionMembers = record
Major: UInt16;
Minor: UInt16;
Release: UInt16;
Build: UInt16;
end;
TWFIFixedFileInfo_FileFlags = record
Debug: Boolean;
InfoInferred: Boolean;
Patched: Boolean;
Prerelease: Boolean;
PrivateBuild: Boolean;
SpecialBuild: Boolean;
end;
TWFIFixedFileInfoDecoded = record
FileVersionFull: UInt64;
FileVersionMembers: TWFIFixedFileInfo_VersionMembers;
FileVersionStr: String;
ProductVersionFull: UInt64;
ProductVersionMembers: TWFIFixedFileInfo_VersionMembers;
ProductVersionStr: String;
FileFlags: TWFIFixedFileInfo_FileFlags;
FileOSStr: String;
FileTypeStr: String;
FileSubTypeStr: String;
FileDateFull: UInt64;
end;
//------------------------------------------------------------------------------
{
Following structures are used to hold partially parsed information from
version information structure.
}
type
TWFIVersionInfoStruct_String = record
Address: Pointer;
Size: TMemSize;
Key: WideString;
ValueType: Integer;
ValueSize: TMemSize;
Value: Pointer;
end;
TWFIVersionInfoStruct_StringTable = record
Address: Pointer;
Size: TMemSize;
Key: WideString;
ValueType: Integer;
ValueSize: TMemSize;
Strings: array of TWFIVersionInfoStruct_String;
end;
TWFIVersionInfoStruct_StringFileInfo = record
Address: Pointer;
Size: TMemSize;
Key: WideString;
ValueType: Integer;
ValueSize: TMemSize;
StringTables: array of TWFIVersionInfoStruct_StringTable;
end;
TWFIVersionInfoStruct_Var = record
Address: Pointer;
Size: TMemSize;
Key: WideString;
ValueType: Integer;
ValueSize: TMemSize;
Value: Pointer;
end;
TWFIVersionInfoStruct_VarFileInfo = record
Address: Pointer;
Size: TMemSize;
Key: WideString;
ValueType: Integer;
ValueSize: TMemSize;
Vars: array of TWFIVersionInfoStruct_Var;
end;
TWFIVersionInfoStruct = record
Address: Pointer;
Size: TMemSize;
Key: WideString;
ValueType: Integer;
ValueSize: TMemSize;
FixedFileInfo: Pointer;
FixedFileInfoSize: TMemSize;
StringFileInfos: array of TWFIVersionInfoStruct_StringFileInfo;
VarFileInfos: array of TWFIVersionInfoStruct_VarFileInfo;
end;
//------------------------------------------------------------------------------
{
Following structures are used to store fully parsed information from version
information structure.
}
type
TWFITranslationItem = record
LanguageName: String;
LanguageStr: String;
case Integer of
0: (Language: UInt16;
CodePage: UInt16);
1: (Translation: UInt32);
end;
TWFIStringTableItem = record
Key: String;
Value: String;
end;
TWFIStringTable = record
Translation: TWFITranslationItem;
Strings: array of TWFIStringTableItem;
end;
{$ELSE}//-----------------------------------------------------------------------
{
Types used for decoded file mode - stores information about file type and
permissions.
}
type
TWFIFileType = (ftUnknown,ftFIFO,ftCharacterDevice,ftDirectory,ftBlockDevice,
ftRegularFile,ftSymbolicLink,ftSocket);
TWFIFilePermission = (fpUserRead,fpUserWrite,fpUserExecute,
fpGroupRead,fpGroupWrite,fpGroupExecute,
fpOthersRead,fpOthersWrite,fpOthersExecute,
fpSetUserID,fpSetGroupID,fpSticky);
TWFIFilePermissions = set of TWFIFilePermission;
{$ENDIF}
{===============================================================================
TWinFileInfo - loading strategy
===============================================================================}
{
Loading strategy determines what file information will be loaded and decoded
or parsed and how.
Only one operation cannot be affected by loading strategy and is always
performed even when loading strategy indicates no operation - a check whether
the file actually exists.
If one strategy requires some other strategy to be active, it means this
strategy will not produce any result if the required one is not active, it
does NOT mean an error will occur.
lsaKeepOpen
the file is kept open until the TWinFileInfo object is destroyed, when not
present the file is closed as soon as possible
lsaLoadBasicInfo
load size, times, attributes and other basic info
lsaDecodeBasicInfo
decode attributes and fills size string, requires lsaLoadBasicInfo
lsaLoadVersionInfo (windows only)
load version info, also loads translations and strings
lsaParseVersionInfo (windows only)
do low-level parsing of version info data and enumerates keys, requires
lsaLoadVersionInfo
lsaLoadFixedFileInfo (windows only)
load fixed file info, requires lsaLoadVersionInfo
lsaDecodeFixedFileInfo (windows only)
decode fixed file info if present, has effect only if FFI is present
(indicated by (f)VersionInfoFixedFileInfoPresent), requires
lsaLoadFixedFileInfo
lsaVerInfoPredefinedKeys (windows only)
when no key is successfully enumerated (see lsaParseVersionInfo),
a predefined set of keys is used, requires lsaParseVersionInfo
lsaVerInfoExtractTranslations (windows only)
extract translations from parsed version info - might get some translation
that normal translation loading (see lsaLoadVersionInfo) missed, requires
lsaParseVersionInfo
}
type
TWFILoadingStrategyAction = (lsaKeepOpen,lsaLoadBasicInfo,lsaDecodeBasicInfo
{$IFDEF Windows},lsaLoadVersionInfo,lsaParseVersionInfo,lsaLoadFixedFileInfo,
lsaDecodeFixedFileInfo,lsaVerInfoPredefinedKeys,lsaVerInfoExtractTranslations{$ENDIF});
TWFILoadingStrategy = set of TWFILoadingStrategyAction;
// some predefined loading strategies (no need to define type of the set)
const
WFI_LS_LoadNone = [];
WFI_LS_BasicInfo = [lsaLoadBasicInfo,lsaDecodeBasicInfo];
{$IFDEF Windows}
WFI_LS_FullInfo = WFI_LS_BasicInfo + [lsaLoadVersionInfo,lsaParseVersionInfo,
lsaLoadFixedFileInfo,lsaDecodeFixedFileInfo];
WFI_LS_VersionInfo = [lsaLoadVersionInfo,lsaParseVersionInfo,lsaVerInfoExtractTranslations];
WFI_LS_VersionInfoAndFFI = WFI_LS_VersionInfo + [lsaLoadFixedFileInfo,lsaDecodeFixedFileInfo];
WFI_LS_All = WFI_LS_FullInfo + [lsaVerInfoPredefinedKeys,lsaVerInfoExtractTranslations];
{$ELSE}
WFI_LS_All = WFI_LS_BasicInfo;
{$ENDIF}
{===============================================================================
TWinFileInfo - class declaration
===============================================================================}
type
TWinFileInfo = class(TObject)
protected
// internals
fLoadingStrategy: TWFILoadingStrategy;
fFormatSettings: TFormatSettings;
// basic initial file info
fName: String;
fLongName: String;
{$IFDEF Windows}
fShortName: String;
{$ENDIF}
fExists: Boolean;
fFileHandle: TWFIFileHandle;
// basic loaded file info
fSize: UInt64;
fSizeStr: String;
{$IFDEF Windows}
fCreationTimeRaw: TDateTime; // time how it is actually stored for the file
fLastAccessTimeRaw: TDateTime;
fLastWriteTimeRaw: TDateTime;
fCreationTime: TDateTime; // stored time converted to local time
fLastAccessTime: TDateTime;
fLastWriteTime: TDateTime;
fNumberOfLinks: UInt32;
{
combination of volume serial and file id can be used to determine file
path equality (also for directories, but WFI does not support dirs)
}
fVolumeSerialNumber: UInt32;
fFileID: UInt64;
// file attributes (part of basic info)
fAttributesFlags: DWORD;
fAttributesStr: String;
fAttributesText: String;
fAttributesDecoded: TWFIFileAttributesDecoded;
{$ELSE}
fLastAccessTimeRaw: TDateTime;
fLastModificationTimeRaw: TDateTime;
fLastStatusChangeTimeRaw: TDateTime;
fLastAccessTime: TDateTime;
fLastModificationTime: TDateTime;
fLastStatusChangeTime: TDateTime;
fNumberOfHardLinks: PtrUInt;
// device ID and inode is used to determine file path equality
fDeviceID: UInt64;
fiNodeNumber: UInt64;
fBlockSize: PtrUInt;
fBlocks: UInt64;
fOwnerUserID: UInt32;
fOwnerGroupID: UInt32;
fMode: UInt32;
// decoded mode
fFileType: TWFIFileType;
fFileTypeStr: String;
fPermissions: TWFIFilePermissions;
fPermissionsStr: String;
{$ENDIF}
{$IFDEF Windows}
// version info unparsed data
fVerInfoSize: TMemSize;
fVerInfoData: Pointer;
// version info data
fVersionInfoPresent: Boolean;
// version info - fixed file info
fVersionInfoFFIPresent: Boolean;
fVersionInfoFFI: TVSFixedFileInfo;
fVersionInfoFFIDecoded: TWFIFixedFileInfoDecoded;
// version info partially parsed data
fVersionInfoStruct: TWFIVersionInfoStruct;
// version info fully parsed data
fVersionInfoParsed: Boolean;
fVersionInfoStringTables: array of TWFIStringTable;
// getters for fVersionInfoStruct fields
Function GetVersionInfoStringTableCount: Integer; virtual;
Function GetVersionInfoStringTable(Index: Integer): TWFIStringTable; virtual;
Function GetVersionInfoTranslationCount: Integer; virtual;
Function GetVersionInfoTranslation(Index: Integer): TWFITranslationItem; virtual;
Function GetVersionInfoStringCount(Table: Integer): Integer; virtual;
Function GetVersionInfoString(Table,Index: Integer): TWFIStringTableItem; virtual;
Function GetVersionInfoValue(const Language,Key: String): String; virtual;
// version info loading methods
procedure VersionInfo_LoadTranslations; virtual;
procedure VersionInfo_LoadStrings; virtual;
// version info parsing methods
procedure VersionInfo_Parse; virtual;
procedure VersionInfo_ExtractTranslations; virtual;
procedure VersionInfo_EnumerateKeys; virtual;
{$ENDIF}
// loading and decoding methods
procedure LoadBasicInfo; virtual;
procedure DecodeBasicInfo; virtual;
{$IFDEF Windows}
procedure LoadVersionInfo; virtual;
procedure LoadFixedFileInfo; virtual;
procedure DecodeFixedFileInfo; virtual;
{$ENDIF}
// other protected methods
procedure Clear; virtual;
procedure Initialize(const FileName: String); virtual;
procedure Finalize; virtual;
public
constructor Create(LoadingStrategy: TWFILoadingStrategy = WFI_LS_All); overload;
constructor Create(const FileName: String; LoadingStrategy: TWFILoadingStrategy = WFI_LS_All); overload;
destructor Destroy; override;
procedure Refresh; virtual;
procedure CreateReport(Strings: TStrings); overload; virtual;
Function CreateReport: String; overload; virtual;
{$IFDEF Windows}
Function IndexOfVersionInfoStringTable(Translation: DWORD): Integer; virtual;
Function IndexOfVersionInfoString(Table: Integer; const Key: String): Integer; virtual;
Function FindVersionInfoStringTable(Translation: DWORD; out Index: Integer): Boolean; virtual;
Function FindVersionInfoString(Table: Integer; const Key: String; out Index: Integer): Boolean; virtual;
{$ENDIF}
// internals
property LoadingStrategy: TWFILoadingStrategy read fLoadingStrategy write fLoadingStrategy;
property FormatSettings: TFormatSettings read fFormatSettings write fFormatSettings;
// basic initial file info
property Name: String read fName;
property LongName: String read fLongName;
{$IFDEF Windows}
property ShortName: String read fShortName;
{$ENDIF}
property Exists: Boolean read fExists;
property FileHandle: TWFIFileHandle read fFileHandle;
// basic loaded file info
property Size: UInt64 read fSize;
property SizeStr: String read fSizeStr;
{$IFDEF Windows}
property CreationTimeRaw: TDateTime read fCreationTime;
property LastAccessTimeRaw: TDateTime read fLastAccessTime;
property LastWriteTimeRaw: TDateTime read fLastWriteTime;
property CreationTime: TDateTime read fCreationTime;
property LastAccessTime: TDateTime read fLastAccessTime;
property LastWriteTime: TDateTime read fLastWriteTime;
property NumberOfLinks: UInt32 read fNumberOfLinks;
property VolumeSerialNumber: UInt32 read fVolumeSerialNumber;
property FileID: UInt64 read fFileID;
// file attributes (part of basic info)
property AttributesFlags: DWORD read fAttributesFlags;
property AttributesStr: String read fAttributesStr;
property AttributesText: String read fAttributesText;
property AttributesDecoded: TWFIFileAttributesDecoded read fAttributesDecoded;
{$ELSE}
property LastAccessTime: TDateTime read fLastAccessTime;
property LastModificationTime: TDateTime read fLastModificationTime;
property LastStatusChangeTime: TDateTime read fLastStatusChangeTime;
property NumberOfHardLinks: PtrUInt read fNumberOfHardLinks;
property DeviceID: UInt64 read fDeviceID;
property iNodeNumber: UInt64 read fiNodeNumber;
property BlockSize: PtrUInt read fBlockSize;
property Blocks: UInt64 read fBlocks;
property OwnerUserID: UInt32 read fOwnerUserID;
property OwnerGroupID: UInt32 read fOwnerGroupID;
property Mode: UInt32 read fMode;
// decoded mode
property FileType: TWFIFileType read fFileType;
property FileTypeStr: String read fFileTypeStr;
property Permissions: TWFIFilePermissions read fPermissions;
property PermissionsStr: String read fPermissionsStr;
{$ENDIF}
{$IFDEF Windows}
// version info unparsed data
property VerInfoSize: PtrUInt read fVerInfoSize;
property VerInfoData: Pointer read fVerInfoData;
// version info data
property VersionInfoPresent: Boolean read fVersionInfoPresent;
// version info - fixed file info
property VersionInfoFixedFileInfoPresent: Boolean read fVersionInfoFFIPresent;
property VersionInfoFixedFileInfo: TVSFixedFileInfo read fVersionInfoFFI;
property VersionInfoFixedFileInfoDecoded: TWFIFixedFileInfoDecoded read fVersionInfoFFIDecoded;
// version info partially parsed data
property VersionInfoStruct: TWFIVersionInfoStruct read fVersionInfoStruct;
// version info fully parsed data
property VersionInfoParsed: Boolean read fVersionInfoParsed;
property VersionInfoStringTableCount: Integer read GetVersionInfoStringTableCount;
property VersionInfoStringTables[Index: Integer]: TWFIStringTable read GetVersionInfoStringTable;
property VersionInfoTranslationCount: Integer read GetVersionInfoTranslationCount;
property VersionInfoTranslations[Index: Integer]: TWFITranslationItem read GetVersionInfoTranslation;
property VersionInfoStringCount[Table: Integer]: Integer read GetVersionInfoStringCount;
property VersionInfoStrings[Table,Index: Integer]: TWFIStringTableItem read GetVersionInfoString;
property VersionInfoValues[const Language,Key: String]: String read GetVersionInfoValue; default;
{$ENDIF}
end;
implementation
uses
{$IFDEF Windows}
{$IFDEF FPC} jwaPSApi{$ELSE} PSApi{$ENDIF}, StrRect
{$ELSE}
DateUtils, BaseUnix, DL
{$ENDIF};
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4055:={$WARN 4055 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W5057:={$WARN 5057 OFF}} // Local variable "$1" does not seem to be initialized
{$PUSH}{$WARN 2005 OFF} // Comment level $1 found
{$IF Defined(FPC) and (FPC_FULLVERSION >= 30000)}
{$DEFINE W5058:=}
{$DEFINE W5092:={$WARN 5092 OFF}} // Variable "$1" of a managed type does not seem to be initialized
{$ELSE}
{$DEFINE W5058:={$WARN 5058 OFF}} // Variable "$1" does not seem to be initialized
{$DEFINE W5092:=}
{$IFEND}
{$IF Defined(FPC) and (FPC_FULLVERSION >= 30200)}
{$DEFINE W6058:={$WARN 6058 OFF}} // Call to subroutine "$1" marked as inline is not inlined
{$ELSE}
{$DEFINE W6058:=}
{$IFEND}
{$POP}
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
Utility functions
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Utility functions - implementation
===============================================================================}
{-------------------------------------------------------------------------------
Utility functions - internal functions
-------------------------------------------------------------------------------}
procedure ProcessFileSize(FileSize: UInt64; out Number: Double; out Decimals: Integer; out UnitPrefix: String);
{
This routine takes size of a file in bytes and produces floating-point number
that can be used in textual representation of this size. Depending on a
"magnitude" of the size, a unit prefix is selected and number of decimal
digits to show is calculated. Also, the number is properly divided for the
selected unit prefix.
}
const
BinaryPrefix: array[0..8] of String = ('','Ki','Mi','Gi','Ti','Pi','Ei','Zi','Yi');
PrefixShift = 10;
var
Magnitude: Integer;
begin
Magnitude := -1;
repeat
Inc(Magnitude);
until ((FileSize shr (PrefixShift * Succ(Magnitude))) = 0) or (Magnitude >= 8);
case FileSize shr (PrefixShift * Magnitude) of
1..9: Decimals := 2;
10..99: Decimals := 1;
else
Decimals := 0;
end;
Number := (FileSize shr (PrefixShift * Magnitude));
If Magnitude > 0 then
Number := Number + (((FileSize shr (PrefixShift * Pred(Magnitude))) and 1023) / 1024)
else
Decimals := 0;
UnitPrefix := BinaryPrefix[Magnitude];
end;
//------------------------------------------------------------------------------
{$IFDEF Windows}{$IFDEF FPCDWM}{$PUSH}W5058 W5092{$ENDIF}{$ENDIF}
procedure InitFormatSettings(out FormatSettings: TFormatSettings);
begin
{$WARN SYMBOL_PLATFORM OFF}
{$IF not Defined(FPC) and (CompilerVersion >= 18)} // Delphi 2006+
FormatSettings := TFormatSettings.Create(LOCALE_USER_DEFAULT);
{$ELSE}
{$IFDEF Windows}
GetLocaleFormatSettings(LOCALE_USER_DEFAULT,FormatSettings);
{$ELSE}
// non-windows
FormatSettings := DefaultFormatSettings;
{$ENDIF}
{$IFEND}
{$WARN SYMBOL_PLATFORM ON}
end;
{$IFDEF Windows}{$IFDEF FPCDWM}{$POP}{$ENDIF}{$ENDIF}
{$IFDEF Windows}
//------------------------------------------------------------------------------
{$IF not Declared(CP_THREAD_ACP)}
const
CP_THREAD_ACP = 3;
{$IFEND}
Function WideToString(const WStr: WideString; AnsiCodePage: UINT = CP_THREAD_ACP): String;
begin
{$IFDEF Unicode}
// unicode Delphi or FPC (String = UnicodeString)
Result := WStr;
{$ELSE}
// non-unicode...
If not UTF8AnsiDefaultStrings then
begin
// CP ansi strings - bare FPC or Delphi
Result := '';
SetLength(Result,WideCharToMultiByte(AnsiCodePage,0,PWideChar(WStr),Length(WStr),nil,0,nil,nil));
WideCharToMultiByte(AnsiCodePage,0,PWideChar(WStr),Length(WStr),PAnsiChar(Result),Length(Result) * SizeOf(AnsiChar),nil,nil);
// a wrong codepage might be stored, try translation with default cp
If (AnsiCodePage <> CP_THREAD_ACP) and (Length(Result) <= 0) and (Length(WStr) > 0) then
Result := WideToString(WStr);
end
// UTF8 ansi strings
else Result := StringToUTF8(WStr);
{$ENDIF}
end;
{$ENDIF}
{-------------------------------------------------------------------------------
Utility functions - public functions
-------------------------------------------------------------------------------}
Function GetFileSize(const FileName: String): UInt64;
begin
Result := 0;
with TWinFileInfo.Create(FileName,[lsaLoadBasicInfo]) do
try
If Exists then
Result := Size
else
raise EWFIFileError.CreateFmt('GetFileSize: File "%s" does not exist.',[FileName]);
finally
Free;
end;
end;
//------------------------------------------------------------------------------
Function FileSizeToStr(FileSize: UInt64; FormatSettings: TFormatSettings; SpaceUnit: Boolean = True): String;
var
Number: Double;
Decimals: Integer;
UnitPrefix: String;
begin
ProcessFileSize(FileSize,Number,Decimals,UnitPrefix);
If SpaceUnit then
Result := Format('%.*f %sB',[Decimals,Number,UnitPrefix],FormatSettings)
else
Result := Format('%.*f%sB',[Decimals,Number,UnitPrefix],FormatSettings);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function FileSizeToStr(FileSize: UInt64; SpaceUnit: Boolean = True): String;
var
Number: Double;
Decimals: Integer;
UnitPrefix: String;
begin
ProcessFileSize(FileSize,Number,Decimals,UnitPrefix);
If SpaceUnit then
Result := Format('%.*f %sB',[Decimals,Number,UnitPrefix])
else
Result := Format('%.*f%sB',[Decimals,Number,UnitPrefix]);
end;
//------------------------------------------------------------------------------
Function FileSizeToStrThr(FileSize: UInt64; SpaceUnit: Boolean = True): String;
var
FormatSettings: TFormatSettings;
begin
InitFormatSettings(FormatSettings);
Result := FileSizeToStr(FileSize,FormatSettings,SpaceUnit);
end;
//------------------------------------------------------------------------------
Function SameFile(const A,B: String): Boolean;
var
AInfo,BInfo: TWinFileInfo;
begin
Result := False;
AInfo := TWinFileInfo.Create(A,[lsaLoadBasicInfo,lsaKeepOpen]);
try
If AInfo.Exists then
begin
BInfo := TWinFileInfo.Create(B,[lsaLoadBasicInfo,lsaKeepOpen]);
try
If BInfo.Exists then
{$IFDEF Windows}
Result := (AInfo.VolumeSerialNumber = BInfo.VolumeSerialNumber) and (AInfo.FileID = BInfo.FileID)
{$ELSE}
Result := (AInfo.DeviceID = BInfo.DeviceID) and (AInfo.iNodeNumber = BInfo.iNodeNumber)
{$ENDIF}
else
raise EWFIFileError.CreateFmt('SameFile: File "%s" does not exist.',[B]);
finally
BInfo.Free;
end;
end
else raise EWFIFileError.CreateFmt('SameFile: File "%s" does not exist.',[A]);
finally
AInfo.Free;
end;
end;
{===============================================================================
--------------------------------------------------------------------------------
TWinFileInfo
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TWinFileInfo - system constants
===============================================================================}
{$IFNDEF Windows}
const
O_NOATIME = $40000; // do not set atime
O_PATH = $200000; // resolve pathname but do not open file
S_ISUID = $800; // set-user-ID bit
S_ISGID = $400; // set-group-ID bit
S_ISVTX = $200; // sticky bit
{$ENDIF}
{===============================================================================
TWinFileInfo - conversion tables
===============================================================================}
{$IFDEF Windows}
// structures used in conversion tables as items
type
TWFIAttributeString = record
Flag: DWORD;
Text: String;
Str: String;
end;
TWFIFlagText = record
Flag: DWORD;
Text: String;
end;
//------------------------------------------------------------------------------
{
tables used to convert some binary information (mainly flags) to a textual
representation
}
const
WFI_FILE_ATTR_STRS: array[0..16] of TWFIAttributeString = (
(Flag: FILE_ATTRIBUTE_ARCHIVE; Text: 'Archive'; Str: 'A'),
(Flag: FILE_ATTRIBUTE_COMPRESSED; Text: 'Compressed'; Str: 'C'),
(Flag: FILE_ATTRIBUTE_DEVICE; Text: 'Device'; Str: ''),
(Flag: FILE_ATTRIBUTE_DIRECTORY; Text: 'Directory'; Str: 'D'),
(Flag: FILE_ATTRIBUTE_ENCRYPTED; Text: 'Encrypted'; Str: 'E'),
(Flag: FILE_ATTRIBUTE_HIDDEN; Text: 'Hidden'; Str: 'H'),
(Flag: FILE_ATTRIBUTE_INTEGRITY_STREAM; Text: 'Integrity stream'; Str: ''),
(Flag: FILE_ATTRIBUTE_NORMAL; Text: 'Normal'; Str: 'N'),
(Flag: FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; Text: 'Not content indexed'; Str: 'I'),
(Flag: FILE_ATTRIBUTE_NO_SCRUB_DATA; Text: 'No scrub data'; Str: ''),
(Flag: FILE_ATTRIBUTE_OFFLINE; Text: 'Offline'; Str: 'O'),
(Flag: FILE_ATTRIBUTE_READONLY; Text: 'Read only'; Str: 'R'),
(Flag: FILE_ATTRIBUTE_REPARSE_POINT; Text: 'Reparse point'; Str: 'L'),
(Flag: FILE_ATTRIBUTE_SPARSE_FILE; Text: 'Sparse file'; Str: 'P'),
(Flag: FILE_ATTRIBUTE_SYSTEM; Text: 'System'; Str: 'S'),
(Flag: FILE_ATTRIBUTE_TEMPORARY; Text: 'Temporary'; Str: 'T'),
(Flag: FILE_ATTRIBUTE_VIRTUAL; Text: 'Virtual'; Str: ''));
//------------------------------------------------------------------------------
WFI_FFI_FILE_OS_STRS: array[0..13] of TWFIFlagText = (
(Flag: VOS_DOS; Text: 'MS-DOS'),
(Flag: VOS_NT; Text: 'Windows NT'),
(Flag: VOS__WINDOWS16; Text: '16-bit Windows'),
(Flag: VOS__WINDOWS32; Text: '32-bit Windows'),
(Flag: VOS_OS216; Text: '16-bit OS/2'),
(Flag: VOS_OS232; Text: '32-bit OS/2'),
(Flag: VOS__PM16; Text: '16-bit Presentation Manager'),
(Flag: VOS__PM32; Text: '32-bit Presentation Manager'),
(Flag: VOS_UNKNOWN; Text: 'Unknown'),
(Flag: VOS_DOS_WINDOWS16; Text: '16-bit Windows running on MS-DOS'),
(Flag: VOS_DOS_WINDOWS32; Text: '32-bit Windows running on MS-DOS'),
(Flag: VOS_NT_WINDOWS32; Text: 'Windows NT'),
(Flag: VOS_OS216_PM16; Text: '16-bit Presentation Manager running on 16-bit OS/2'),
(Flag: VOS_OS232_PM32; Text: '32-bit Presentation Manager running on 32-bit OS/2'));
//------------------------------------------------------------------------------
WFI_FFI_FILE_TYPE_STRS: array[0..6] of TWFIFlagText = (
(Flag: VFT_APP; Text: 'Application'),
(Flag: VFT_DLL; Text: 'DLL'),
(Flag: VFT_DRV; Text: 'Device driver'),
(Flag: VFT_FONT; Text: 'Font'),
(Flag: VFT_STATIC_LIB; Text: 'Static-link library'),
(Flag: VFT_UNKNOWN; Text: 'Unknown'),
(Flag: VFT_VXD; Text: 'Virtual device'));
//------------------------------------------------------------------------------
WFI_FFI_FILE_SUBTYPE_DRV_STRS: array[0..11] of TWFIFlagText = (
(Flag: VFT2_DRV_COMM; Text: 'Communications driver'),
(Flag: VFT2_DRV_DISPLAY; Text: 'Display driver'),
(Flag: VFT2_DRV_INSTALLABLE; Text: 'Installable driver'),
(Flag: VFT2_DRV_KEYBOARD; Text: 'Keyboard driver'),
(Flag: VFT2_DRV_LANGUAGE; Text: 'Language driver'),
(Flag: VFT2_DRV_MOUSE; Text: 'Mouse driver'),
(Flag: VFT2_DRV_NETWORK; Text: 'Network driver'),
(Flag: VFT2_DRV_PRINTER; Text: 'Printer driver'),
(Flag: VFT2_DRV_SOUND; Text: 'Sound driver'),
(Flag: VFT2_DRV_SYSTEM; Text: 'System driver'),
(Flag: VFT2_DRV_VERSIONED_PRINTER; Text: 'Versioned printer driver'),
(Flag: VFT2_UNKNOWN; Text: 'Unknown'));
//------------------------------------------------------------------------------
WFI_FFI_FILE_SUBTYPE_FONT_STRS: array[0..3] of TWFIFlagText = (
(Flag: VFT2_FONT_RASTER; Text: 'Raster font'),
(Flag: VFT2_FONT_TRUETYPE; Text: 'TrueType font'),