source: trunk/Library/FileUtilsUnit.pas@ 238

Last change on this file since 238 was 238, checked in by RBRi, 18 years ago

improved directory flag handling (see #31)

  • Property svn:eol-style set to native
File size: 33.5 KB
RevLine 
[82]1Unit FileUtilsUnit;
2
3// NewView - a new OS/2 Help Viewer
[219]4// Copyright 2003-2006 Aaron Lawrence
5// Copyright 2006-2007 Ronald Brill (rbri at rbri dot de)
[82]6// This software is released under the GNU Public License - see readme.txt
7
8// Helper functions for file handling
9
10Interface
11
12uses
13 Classes,
14 ACLUtility;
15
16
17const
[187]18 DIRECTORY_SEPARATOR = '\';
[82]19 PATH_SEPARATOR = ';';
[235]20 CURRENT_DIRECTORY = '.';
21 PARENT_DIRECTORY = '..';
22 FILE_EXTENSION_DELIMITER = '.';
[82]23
[187]24 // Drive numbers are one based
25 MinDriveNumber = 1;
26 MaxDriveNumber = 26;
27
28
[82]29 // TODO
30 HelpPathEnvironmentVar = 'HELP';
31 BookshelfEnvironmentVar = 'BOOKSHELF';
32 LanguageEnvironmentVar = 'LANG';
33 DEFAULT_LANGUAGE = 'EN_US';
[235]34 HELP_FILE_EXTENSION = FILE_EXTENSION_DELIMITER + 'hlp';
[82]35
36
[187]37type
38 TDriveType =
39 (
40 dtNone,
41 dtFloppy,
42 dtHard,
43 dtCD,
44 dtNetwork,
45 dtRemovable
46 );
47
48
[82]49 // Adds a slash to the end of dir if not present
50 // if aDir is empty this returns '\'
51 Function AddDirectorySeparator(aDirectory : String) : String;
52
53 // Adds a slash to the end of dir if not present
54 // if aDir is empty this returns ''
55 Function AddDirectorySeparatorIfNotEmpty(aDirectory: String) : String;
56
57 // Removes a directory seperator from the end of aDirectory
58 // (if present)
59 Function RemoveRightDirectorySeparator(aDirectory : String) : String;
60
61 // Expands the path given, relative to aBaseDirectory
62 // Handles leading \ for root dir,
63 // .. for parent, . (ignored),
64 // drive spec at start,
65 // ignores repeated \ e.g. \\
66 Function ExpandPath(aBaseDirectory : String; aPath : String): String;
67
68 Function GetLogFilesDir: String;
69
70 Function SearchPath(const aPathEnvVar: String; const aFilename: String; var aResultFilename: String) : boolean;
71
72 Function SearchHelpPaths(const aFilename: String; var aResultFilename: String; const anIncludeAppDir: boolean) : boolean;
73
74 // Find the help file for the current app based on LANG
75 Function FindDefaultLanguageHelpFile(const anApplicationName: String; const aLanguage : String) : String;
76
77 // Breaks up specified Env var path
[97]78 // this always clears the list at the beginning
[82]79 Procedure GetDirsInPath(const aPathEnvVar: String; var aList: TStrings);
80
[187]81 // Breaks up specified Env var path
82 // then adds all matching files to the list
83 Procedure GetFilesInPath( const aPathEnvVar: String;
84 const aFilter: String;
85 var aList: TStrings );
86
[82]87 // searches for all files in aDirectory matching aFilter and add
88 // them to aList
89 // it is possible to define different filter if you separate them by semicolon
[97]90 Procedure ListFilesInDirectory( const aDirectory : String;
91 const aFilter : String;
92 const aWithDirectoryFlag : boolean;
93 var aList : TStrings);
[82]94
95 // searches for all directories in aDirectory and add them to aList
[238]96 Procedure ListSubDirectories(const aDirectory: String; const anIncludeSystemAndHiddenFlag: boolean; var aList: TStrings);
[82]97
98 Procedure ListFilesInDirectoryRecursiveWithTermination(const aDirectory : String;
99 const aFilter : String;
[97]100 const aWithDirectoryFlag : boolean;
[238]101 const anIncludeSystemAndHiddenFlag: boolean;
[82]102 var aList : TStrings;
103 const aTerminateCheck : TTerminateCheck;
104 const aUseTerminateCheck : boolean);
105
106 Function ParentDir(const aDirectory : String) : String;
107
[187]108 // In the directory startpath, create directory and subdirectories
109 // specified in DirsString
110 // e.g. bob\bill\fred will make bob, then bill in bob, then fred in bob
111 // returns path to lowest dir created
112 Function MakeDirs(const aFullDirectoryPath: String) : String;
113
114
[238]115 // Checks if a directory exists
[82]116 Function DirectoryExists(const aDirectory : String) : boolean;
117
[187]118 Function DriveLetterToDriveNumber(const aDriveLetter : char) : longint;
119 Function DriveNumberToDriveLetter(const aDriveNumber : longint) : char;
120
121 Function GetVolumeLabel(aDrive: char) : String;
122
123 Function GetBootDriveLetter: char;
124
125 // Returns true if file exists and is read only
126 Function FileIsReadOnly(const aFilename : String) : boolean;
127
128
129 // TODO
130 Function IsFloppyDrive( DriveNumber: longint ): Boolean;
131 Function GetLocalDriveType( DriveNumber: longint ): TDriveType;
132 Function GetDriveType( DriveNumber: longint ): TDriveType;
133 Function GetNetworkDriveRemotePath( DriveNumber: longint ): String;
134
135
[82]136Implementation
137
138uses
139 Dos,
140 BseDos,
[187]141 BseErr,
142 BseDev,
[82]143 Os2Def,
144 SysUtils,
[187]145 StringUtilsUnit,
146 CharUtilsUnit;
[82]147
[187]148imports
149 FUNCTION _DosQueryFSAttach( VAR pszDeviceName: CSTRING;
150 ulOrdinal: ULONG;
151 ulFSAInfoLevel:ULONG;
152 pfsqb: PFSQBUFFER2;
153 VAR pcbBuffLength: ULONG ): APIRET; APIENTRY;
154 'DOSCALLS' index 277;
155end;
156
157
158type
159 TWord = Record // Bytes of a Word
160 LoByte, HiByte : Byte;
161 End;
162
163 TBPB = Array[0..30] Of Byte; // Puffer fuer BPB-Struktur
164
165 TDeviceParameters = Record
166 BPB: TBPB;
167 Cylinders: word;
168 DeviceType: Byte;
169 Attributes: Word;
170 End;
171
172
173
[82]174 Function AddDirectorySeparator(aDirectory : String) : String;
175 begin
176 if aDirectory = '' then
177 begin
[187]178 Result:= DIRECTORY_SEPARATOR;
[82]179 exit;
180 end;
181
[187]182 if aDirectory[length(aDirectory)] <> DIRECTORY_SEPARATOR then
[82]183 begin
[187]184 Result := aDirectory + DIRECTORY_SEPARATOR;
[82]185 exit;
186 end;
187
188 Result := aDirectory;
189 end;
190
191
192 Function AddDirectorySeparatorIfNotEmpty(aDirectory: String): String;
193 begin
194 if aDirectory = '' then
195 begin
196 Result := '';
197 exit;
198 end;
199 Result := AddDirectorySeparator(aDirectory);
200 end;
201
202
203 Function RemoveRightDirectorySeparator(aDirectory : String) : String;
204 begin
[187]205 Result := StrTrimRightChars(aDirectory, [DIRECTORY_SEPARATOR]);
[82]206 end;
207
208
209 Function ExpandPath(aBaseDirectory : String; aPath : String): String;
210 var
211 tmpDirectory: String;
212 tmpDirectories : TStringList;
213 i : integer;
214 begin
215 Result:= aBaseDirectory;
216
217 if aPath = '' then
218 begin
[187]219 Result := StrTrimRightChars(Result, [DIRECTORY_SEPARATOR]);
[82]220 exit;
221 end;
222
223 aPath := trim(aPath);
224 if Length(aPath) > 1 then
225 begin
226 // check for drive spec
227 if aPath[2] = ':' then
228 begin
229 Result := AddDirectorySeparator(aPath);
230 if Length(aPath) > 3 then
231 begin
[187]232 Result := StrTrimRightChars(Result, [DIRECTORY_SEPARATOR]);
[82]233 end;
234 exit;
235 end
236 end;
237
238 if Length(aPath) > 0 then
239 begin
240 // check for root dir spec
[187]241 if aPath[1] = DIRECTORY_SEPARATOR then
[82]242 begin
243 // take just the drive from the basedir
244 if aBaseDirectory[2] = ':' then
245 begin
246 Result := StrLeft(aBaseDirectory, 2);
247 end
248 else
249 begin
[187]250 Result := DIRECTORY_SEPARATOR;
[82]251 end;
[187]252 aPath := StrTrimLeftChars(aPath, [DIRECTORY_SEPARATOR]);
[82]253 end;
254 end;
255
256 tmpDirectories := TStringList.Create;
[187]257 StrExtractStringsIgnoreEmpty(tmpDirectories, aPath, [DIRECTORY_SEPARATOR], #0);
[82]258 for i := 0 to tmpDirectories.count-1 do
259 begin
260 tmpDirectory := tmpDirectories[i];
[235]261 if tmpDirectory = PARENT_DIREcTORY then
[82]262 begin
263 if NOT ((Length(Result) = 2) AND (Result[2] = ':')) then
264 begin
265 Result := ParentDir(Result);
266 end;
267 end
[235]268 else if tmpDirectory = CURRENT_DIRECTORY then
[82]269 begin
270 ; // nothing to do
271 end
272 else
273 begin
274 Result := AddDirectorySeparator(Result) + tmpDirectory;
275 end;
276
277 // strip any extra leading slashes
[187]278 aPath := StrTrimLeftChars(aPath, [DIRECTORY_SEPARATOR]);
[82]279 end;
280 tmpDirectories.Destroy;
281
282 if Length(Result) = 2 then
283 begin
284 if Result[2] = ':' then
285 begin
286 // just a drive spec X:, so add a slash
[187]287 Result := Result + DIRECTORY_SEPARATOR;
[82]288 end;
289 end;
290 end;
291
292 Function GetLogFilesDir: String;
293 begin
294 // ecomstation 1.1 compat
295 Result := GetEnv('LOGFILES');
296 if Result <> '' then
297 begin
298 Result := AddDirectorySeparator(Result);
299 exit;
300 end;
301 // TODO
302 Result := AddDirectorySeparator(GetApplicationDir);
303 end;
304
305
306 Function SearchPath(const aPathEnvVar: String;
307 const aFilename: String;
308 var aResultFilename: String) : boolean;
309 var
310 tmpSzEnvVar : CString;
311 tmpSzFilename : CString;
312 tmpSzFilenameFound : CString;
313 tmpRC: APIRET;
314 begin
315 Result := false;
316 aResultFilename := '';
317
318 tmpSzEnvVar := aPathEnvVar;
319 tmpSzFilename := aFilename;
320 tmpRC := DosSearchPath( SEARCH_IGNORENETERRS
321 + SEARCH_ENVIRONMENT
322 + SEARCH_CUR_DIRECTORY,
323 tmpSzEnvVar,
324 tmpSzFilename,
325 tmpSzFilenameFound,
326 sizeof(tmpSzFilenameFound));
327 if tmpRC = 0 then
328 begin
329 Result := true;
330 aResultFilename := tmpSzFilenameFound;
331 end;
332 end;
333
334
335 Function SearchHelpPaths(const aFilename: String;
336 var aResultFilename: String;
337 const anIncludeAppDir: boolean) : boolean;
338 begin
339 Result := SearchPath(HelpPathEnvironmentVar, aFileName, aResultFilename);
340 if not Result then
341 begin
342 Result := SearchPath(BookshelfEnvironmentVar, aFileName, aResultFilename);
343 end;
344
345 if (not Result) and anIncludeAppDir then
346 begin
347 aResultFilename := AddDirectorySeparator(GetApplicationDir) + aFilename;
348 Result := FileExists(aResultFilename);
349 if not Result then
350 begin
351 aResultFilename := '';
352 end;
353 end;
354 end;
355
356
357 Function FindDefaultLanguageHelpFile(const anApplicationName: String; const aLanguage : String) : String;
358 var
359 tmpLanguage : String;
360 tmpLanguageParts : TStringList;
361 tmpMajorLanguage : String;
362 tmpMinorLanguage : String;
363 begin
364 Result := '';
365
366 tmpLanguage := aLanguage;
367 if aLanguage = '' then
368 begin
369 tmpLanguage := DEFAULT_LANGUAGE;
370 end;
371
372 tmpLanguageParts := TStringList.Create;
373 StrExtractStrings(tmpLanguageParts, tmpLanguage, ['_'], #0);
374
375 tmpMajorLanguage := '';
376 if tmpLanguageParts.count > 0 then
377 begin
378 tmpMajorLanguage := tmpLanguageParts[0];
379 end;
380
381 tmpMinorLanguage := '';
382 if tmpLanguageParts.count > 1 then
383 begin
384 tmpMinorLanguage := tmpMinorLanguage[1];
385 end;
386
387 tmpLanguageParts.Destroy;
388
389 // note there might be some other stuff on the end of LANG
390 // such as ES_ES_EURO...
391 if tmpMinorLanguage <> '' then
392 begin
393 if SearchHelpPaths( anApplicationName
394 + '_' + tmpMajorLanguage
395 + '_' + tmpMinorLanguage
396 + HELP_FILE_EXTENSION,
397 Result,
398 true ) then
399 begin
400 // found a specifc language
401 exit;
402 end;
403 end;
404
405 // try generic language?
406 if SearchHelpPaths( anApplicationName
407 + '_' + tmpMajorLanguage
408 + HELP_FILE_EXTENSION,
409 Result,
410 true ) then
411 begin
412 exit;
413 end;
414
415 // nothing specific, search for default
416 SearchHelpPaths(anApplicationName + HELP_FILE_EXTENSION, Result, true);
417 end;
418
419
420 Procedure GetDirsInPath(const aPathEnvVar: String; var aList: TStrings);
421 var
422 tmpRC : APIRET;
423 tmpPszPathEnvVar : PChar;
424 tmpSzEnvVar : CString;
425 begin
426 // do this in any case also if there is an error
427 // to garantie a defined behavior
428 aList.Clear;
429
430 tmpSzEnvVar := aPathEnvVar;
431 tmpRC := DosScanEnv(tmpSzEnvVar, tmpPszPathEnvVar);
432
433 if tmpRC <> 0 then
434 begin
435 exit;
436 end;
437
438 StrExtractStringsIgnoreEmpty(aList, StrPas(tmpPszPathEnvVar), [PATH_SEPARATOR], #0);
439 end;
440
441
[187]442 Procedure GetFilesInPath( const aPathEnvVar: String;
443 const aFilter: String;
444 var aList: TStrings );
445 var
446 tmpDirectories : TStringList;
447 i : integer;
448 begin
449 tmpDirectories := TStringList.Create;
450 GetDirsInPath(aPathEnvVar, tmpDirectories);
451
452 for i:=0 to tmpDirectories.count-1 do
453 begin
454 ListFilesInDirectory(tmpDirectories[i], aFilter, false, aList);
455 end;
456
457 tmpDirectories.Destroy;
458 end;
459
460
[97]461 Procedure ListFilesInDirectory( const aDirectory: String;
462 const aFilter: String;
463 const aWithDirectoryFlag: boolean;
464 var aList: TStrings);
[82]465 var
466 tmpRC : APIRET;
467 tmpSearchResults: TSearchRec;
468 tmpMask: String;
469 tmpFilterParts : TStringList;
[97]470 tmpDirectory : String;
[82]471 i : integer;
472 begin
473 tmpFilterParts := TStringList.Create;
474 StrExtractStrings(tmpFilterParts, aFilter, [PATH_SEPARATOR], #0);
475
476 for i:=0 to tmpFilterParts.count-1 do
477 begin
478 tmpMask := tmpFilterParts[i];
[97]479 tmpDirectory := AddDirectorySeparator(aDirectory);
480 tmpRC := FindFirst(tmpDirectory + tmpMask, faAnyFile, tmpSearchResults);
[82]481
482 while tmpRC = 0 do
483 begin
484 if tmpSearchResults.Attr And faDirectory = 0 then
485 begin
[97]486 if (aWithDirectoryFlag) then
487 begin
488 aList.Add(tmpDirectory + tmpSearchResults.Name);
489 end
490 else
491 begin
492 aList.Add(tmpSearchResults.Name);
493 end;
[82]494 end;
495
496 tmpRC := FindNext(tmpSearchResults);
497 end;
498
499 FindClose(tmpSearchResults);
500 end;
501 tmpFilterParts.Destroy;
502 end;
503
504
[238]505 Procedure ListSubDirectories(const aDirectory: String; const anIncludeSystemAndHiddenFlag: boolean; var aList: TStrings);
[82]506 var
507 tmpRC : APIRET;
508 tmpSearchResults: TSearchRec;
509 tmpName : String;
[238]510 tmpFileAttributes : ULONG;
[82]511 begin
512
[238]513 if anIncludeSystemAndHiddenFlag then
514 begin
515 tmpFileAttributes := faArchive or faReadonly or faHidden or faSysFile or faDirectory or faMustDirectory;
516 end
517 else
518 begin
519 tmpFileAttributes := faArchive or faReadonly or faDirectory or faMustDirectory;
520 end;
521
522 tmpRC := FindFirst(AddDirectorySeparator(aDirectory) + '*', tmpFileAttributes, tmpSearchResults);
[82]523 if (tmpRC <> 0) then
524 begin
525 exit;
526 end;
527
528 while tmpRC = 0 do
529 begin
530 tmpName := tmpSearchResults.Name;
[235]531 if (tmpName <> CURRENT_DIRECTORY) AND (tmpName <> PARENT_DIRECTORY) then
[82]532 begin
533 aList.Add(AddDirectorySeparatorIfNotEmpty(aDirectory) + tmpSearchResults.Name );
534 end;
535 tmpRC := FindNext(tmpSearchResults);
536 end;
537 FindClose(tmpSearchResults);
538 end;
539
540
541 Procedure ListFilesInDirectoryRecursiveWithTermination(const aDirectory : String;
542 const aFilter : String;
[97]543 const aWithDirectoryFlag : boolean;
[238]544 const anIncludeSystemAndHiddenFlag: boolean;
[82]545 var aList : TStrings;
546 const aTerminateCheck : TTerminateCheck;
547 const aUseTerminateCheck : boolean);
548 var
549 i : integer;
550 tmpSubDirectories : TStringList;
551 tmpSubDirectory : String;
552 begin
553 // at first add all files from the directory itself
[97]554 ListFilesInDirectory(aDirectory, aFilter, aWithDirectoryFlag, aList);
[82]555
556 // now determine all subdirectories
557 tmpSubDirectories := TStringList.Create;
[238]558 ListSubDirectories(aDirectory, anIncludeSystemAndHiddenFlag, tmpSubDirectories);
[82]559
560 for i := 0 to tmpSubDirectories.Count - 1 do
561 begin
562 // if Assigned( TerminateCheck ) then - doesn't work in sibyl
563 if aUseTerminateCheck then
564 if aTerminateCheck then
565 break;
566
567 tmpSubDirectory := tmpSubDirectories[i];
568
[238]569 ListFilesInDirectoryRecursiveWithTermination( tmpSubDirectory,
570 aFilter,
571 aWithDirectoryFlag,
572 anIncludeSystemAndHiddenFlag,
573 aList,
574 aTerminateCheck,
575 aUseTerminateCheck);
[82]576 end;
577 tmpSubDirectories.Destroy;
578 end;
579
580
581 Function ParentDir(const aDirectory : String) : String;
582 var
583 tmpPos: integer;
584 begin
585 tmpPos := Length(aDirectory);
586
587 // ends with slash
[187]588 while (aDirectory[tmpPos] = DIRECTORY_SEPARATOR) AND (tmpPos > 0) do
[82]589 begin
590 dec(tmpPos);
591 end;
592
593 // find slash
[187]594 while (aDirectory[tmpPos] <> DIRECTORY_SEPARATOR) AND (tmpPos > 0) do
[82]595 begin
596 dec(tmpPos);
597 end;
598
599 result:= StrLeft(aDirectory, tmpPos-1);
600 end;
601
602
[187]603 Function MakeDirs(const aFullDirectoryPath: String) : String;
604 Var
605 tmpDirectoryParts : TStringList;
606 tmpDirectoryPart : String;
607 tmpCompletePart : String;
608 i : integer;
609 begin
610 tmpDirectoryParts := TStringList.Create;
611 StrExtractStringsIgnoreEmpty(tmpDirectoryParts, aFullDirectoryPath, [DIRECTORY_SEPARATOR], #0);
612
613 tmpCompletePart := '';
614 for i:=0 to tmpDirectoryParts.count-1 do
615 begin
616 tmpDirectoryPart := trim(tmpDirectoryParts[i]);
617
618 if tmpDirectoryPart <> '' then
619 begin
620 tmpCompletePart := AddDirectorySeparatorIfNotEmpty(tmpCompletePart) + tmpDirectoryPart;
621
622 if not DirectoryExists(tmpCompletePart) then
623 begin
624 MkDir(tmpCompletePart);
625 end;
626 end;
627 end;
628
629 Result := tmpCompletePart;
630 end;
631
632
[82]633 Function DirectoryExists(const aDirectory : String) : boolean;
634 Var
635 tmpRC : APIRET;
636 tmpSearchResults : TSearchRec;
637 tmpDriveMap : ULONG;
638 tmpActualDrive : ULONG;
639 tmpDrive : Char;
640 tmpDriveNum : integer;
641 tmpDriveBit : longword;
642 tmpDirectory : String;
643 Begin
644 Result := false;
645 tmpDirectory := RemoveRightDirectorySeparator(aDirectory);
646 if tmpDirectory = '' then
647 begin
648 Result:= true;
649 exit;
650 end;
651
652 if Length(tmpDirectory) = 2 then
653 begin
654 if tmpDirectory[2] = ':' then
655 begin
656 // a drive only has been specified
657 tmpDrive:= UpCase(tmpDirectory[1] );
658 if (tmpDrive < 'A') or (tmpDrive > 'Z') then
659 begin
660 // invalid drive; return false;
661 exit;
662 end;
663
664 DosQueryCurrentDisk(tmpActualDrive, tmpDriveMap);
665 tmpDriveNum := Ord(tmpDrive) - Ord('A') + 1; // A -> 1, B -> 2...
666 tmpDriveBit := 1 shl (tmpDriveNum-1); // 2^DriveNum
667
668 Result := tmpDriveMap and (tmpDriveBit) > 0;
669 exit;
670 end;
671 end;
672
[238]673 tmpRC := FindFirst( tmpDirectory,
674 faArchive or faReadonly or faHidden or faSysFile or faDirectory or faMustDirectory,
675 tmpSearchResults);
[82]676 if tmpRC = 0 then
677 begin
678 Result:= true;
679 FindClose(tmpSearchResults);
680 end;
681 end;
682
683
[187]684 Function DriveLetterToDriveNumber(const aDriveLetter : char) : longint;
685 begin
686 if (aDriveLetter >= 'a')
687 and (aDriveLetter <= 'z') then
688 begin
689 Result := Ord(aDriveLetter) - Ord('a') + 1;
690 exit;
691 end;
692
693 if (aDriveLetter >= 'A')
694 and (aDriveLetter <= 'Z') then
695 begin
696 Result := Ord(aDriveLetter) - Ord('A') + 1;
697 exit;
698 end;
699
700 // not a valid drive letter
701 Result := 0;
702 end;
703
704
705 Function DriveNumberToDriveLetter(const aDriveNumber: longint) : char;
706 begin
707 Result := Chr(aDriveNumber - 1 + Ord('A'));
708 end;
709
710
711 Function GetVolumeLabel(aDrive: char) : String;
712 var
713 tmpRC : APIRET;
714 tmpFileSystemInfo : FSINFO;
715 e : EInOutError;
716 begin
717 DosErrorAPI( FERR_DISABLEHARDERR );
718 Result := '';
719 tmpRC := DosQueryFSInfo( DriveLetterToDriveNumber(aDrive),
720 FSIL_VOLSER,
721 tmpFileSystemInfo,
722 sizeof(tmpFileSystemInfo));
723 if tmpRC = 0 then
724 begin
725 Result := StrPasWithLength(Addr(tmpFileSystemInfo.vol.szVolLabel), tmpFileSystemInfo.vol.cch);
726 Result := LowerCase(Result);
727 end;
728 DosErrorAPI( FERR_ENABLEHARDERR );
729
730 if tmpRC <> 0 then
731 begin
732 e := EInOutError.Create( 'Cannot read drive ' + aDrive + ':');
733 e.ErrorCode := tmpRC;
734 raise e;
735 end;
736 end;
737
738
739 Function GetBootDriveLetter: char;
740 var
741 tmpBuffer: longword;
742 begin
743 DosQuerySysInfo( QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, tmpBuffer, sizeof(tmpBuffer));
744 Result := Chr(ord('A') + tmpBuffer - 1);
745 end;
746
747
748 Function FileIsReadOnly(const aFilename : String ) : boolean;
749 begin
750 Result :=(FileGetAttr(aFilename) AND faReadonly) > 0;
751 end;
752
753
754
755
756 // TODO
757
758
759Function IsFloppyDrive( DriveNumber: longint ): Boolean;
760Var
761 bResult : Byte;
762Begin
763 DosDevConfig( bResult, DEVINFO_FLOPPY );
764 Result := ( Abs( DriveNumber ) <= bResult);
765End;
766
767// -------------------------------------------------------------------------
768// Funktion/Function: QueryCDRoms()
769//
770// Beschreibung:
771// Die Funktion QueryCDRom ermittelt ueber eine nicht dokumentierte
772// Schnittstelle die Anzahl der CDRom-Laufwerke und den ersten, fuer
773// ein CDRom-Laufwerk, vergebenen UnitIdentifier.
774// Der Treiber OS2CDROM.DMD stellt dem System zwei Devices (CD-ROM1$
775// und CD-ROM2$) zur Verfuegung. Die beiden Devices unterscheiden sich
776// durch DeviceAttribute. Beide Devices unterstuetzen (zumindest unter
777// Warp) den undokumentierten Generic IOCtl 0x82/0x60, welcher Infor-
778// mationen ueber die angeschlossenen CDRom-Laufwerke liefert.
779//
780// Description:
781// This Functions finds out how many CD-Rom Drives are present in System
782// and which Drive Letter is the first occupied by a CD-Rom. It uses an
783// undocumented Interface to OS2CDROM.DMD.
784// OS2CDROM.DMD presents two Devices (CD-ROM1$ and CD-ROM2$). These De-
785// vices are distinguished by their Device-Attributes. Both Devices sup-
786// port (under Warp) the undocumented generic IOCtl-Call 0x82/0x60 which
787// deliver some Information about the connected CD-Rom Drives.
788//
789// Parameter:
790// Var ulCDRomCount ULONG Anzahl CD-Rom Laufwerke im System
791// Number of CD-Rom Drives in System
792//
793// Var ulFirstCDRomDiskNo ULONG erste Laufwerksnummer, die an ein
794// CD-Rom vergeben ist
795// first Drive-Letter occupied by a
796// CD-Rom Drive
797//
798// Rueckgabe/Returnvalue: keine/none
799// -------------------------------------------------------------------------
800Procedure QueryCDRoms(Var ulCDRomCount, ulFirstCDRomDiskNo: ULONG);
801
802Const cszDriverName : CSTRING = 'CD-ROM?$';
803
804Var cCurDriver : Char; // Indexvariable fuer aktuell bearbeites Device (1 oder 2)
805 // Index for current Device (1 or 2)
806
807 hDevHandle : HFILE; // Handle fuer Device
808 // Device handle
809
810 ulAction : ULONG; // Aktionscode (DosOpen())
811 // Actioncode (DosOpen())
812
813 ulParams : ULONG; // Anzahl Bytes von IOCtl gelieferter Parameterdaten
814 // Number of Bytes for delivered Parameterdata
815
816 ulData : ULONG; // Anzahl Bytes von IOCtl gelieferter Daten
817 // Number of Bytes delivered by IOCtl
818
819 rCDInfo : Record // Ergebnisstruktur der IOCtl-Funktion (s.o.)
820 // Record for Results of IOCtl-Call (see above)
821 usCDRomCount : USHORT; // Anzahl CD-Roms / Number of CD-Rom Drives
822 usFirstUnitNo: USHORT; // erste vergebene Laufwerksnummer / first Driver Letter
823 End;
824
825Begin (* uQueryCDRom *)
826 /************************************
827 * Vorbelegungen
828 *
829 * initial assignments
830 ************************************/
831 ulCDRomCount := 0;
832 ulFirstCDRomDiskNo := 0;
833
834 ulParams := 0;
835
836 /************************************
837 * die beiden Devices abarbeiten
838 *
839 * accessing both Devices
840 ************************************/
841 For cCurDriver := '1' To '2' Do
842 Begin
843 /************************************
844 * Device oeffnen
845 *
846 * open Device
847 ************************************/
848 cszDriverName[6] := cCurDriver;
849 If (DosOpen(cszDriverName, // Devicename
850 hDevHandle, // Handle
851 ulAction, // Aktionscode
852 0, // DateigrӇe
853 FILE_NORMAL, // Attribute: read/write
854 OPEN_ACTION_OPEN_IF_EXISTS, // OpenFlag: ”ffnen, wenn vorhanden
855 OPEN_FLAGS_FAIL_ON_ERROR Or // Modus: Fehlermeldung per Returncode
856 OPEN_SHARE_DENYNONE Or // keine Einschr„nkungen fr Dritte
857 OPEN_ACCESS_READONLY, // nur lesender Zugriff
858 NIL)=NO_ERROR) Then // keine EA
859 Begin
860 /************************************
861 * IOCtl-Funktion aufrufen
862 *
863 * Call to IOCtl
864 ************************************/
865 If (DosDevIOCtl(hDevHandle, // Handle / Handle
866 $82, // Kategorie / Category
867 $60, // Funktion / Function
868 NIL, // keine Parameterliste / No Parameterlist
869 0, // Laenge Parameterliste / Length of Parameterlist
870 ulParams, // Groesse der gelieferten Parameterdaten
871 // / Number of Bytes for Parameterdata
872 rCDInfo, // Puffer fuer gelieferte Daten
873 // / Buffer for returned Data
874 SizeOf(rCDInfo), // Groesse des Datenpuffers
875 // / Size of Databuffer
876 ulData)=NO_ERROR) Then // Groesse der gelieferten Daten
877 // / Number of Bytes for returned Data
878 Begin
879 ulCDRomCount := rCDInfo.usCDRomCount;
880 ulFirstCDRomDiskNo := Succ(rCDInfo.usFirstUnitNo);
881 End;
882
883 DosClose(hDevHandle);
884 End;
885
886 End; (* For *)
887
888End; (* uQueryCDRom *)
889
890
891Function GetLocalDriveType( DriveNumber: longint ): TDriveType;
892var
893 IOCtlParameters: Word;
894 rc: APIRET;
895 ParameterLength: longWord;
896 DataLength: longword;
897 DeviceData: TDeviceParameters;
898 Fixed: boolean;
899 FirstCDDrive: ULONG;
900 NumCDDrives: ULONG;
901begin
902
903 TWord( IOCtlParameters ).LoByte := 0; // BPB of physical Device
904 TWord( IOCtlParameters ).HiByte := DriveNumber - 1; // drive number, zero base
905
906 ParameterLength := SizeOf( IOCtlParameters ); // input length of parameters
907 DataLength := 0; // input length of data (none)
908
909 rc := DosDevIOCTL( HFILE(-1), // Open Device (not a file)
910 IOCTL_DISK, // Category
911 DSK_GETDEVICEPARAMS, // Function
912 IOCtlParameters, // Parameters
913 SizeOf( IOCtlParameters ), // (max) size of parameters
914 ParameterLength, // parameters length
915 DeviceData, // results
916 SizeOf( DeviceData ), // (max) size of data block
917 DataLength ); // data block length
918
919 Fixed := ( DeviceData.Attributes and 1 ) > 0; // bit 0 indicates fixed (1) or removable (0)
920 if not Fixed then
921 begin
922 result := dtRemovable;
923
924 QueryCDRoms( FirstCDDrive,
925 NumCDDrives );
926
927 if ( DriveNumber >= FirstCDDrive )
928 and ( DriveNumber < FirstCDDrive + NumCDDrives ) then
929 result := dtCD;
930
931 exit;
932 end;
933
934 result := dtHard;
935end;
936
937// Takes a one-based drive number
938Function GetDriveType( DriveNumber: longint ): TDriveType;
939var
940 szDrive: CString;
941
942 FSData: array[ 0..sizeof( FSQBuffer) + 3*_MAX_PATH ] of char;
943 pBuffer: PFSQBUFFER2;
944 FSDataLength: ULONG;
945
946 rc: APIRET;
947begin
948 assert( DriveNumber >= 1 );
949 assert( DriveNumber <= 26 );
950
951 if ( DriveNumber >=1 ) and ( DriveNumber <= 2 ) then
952 begin
953 if IsFloppyDrive( DriveNumber ) then
954 begin
955 result := dtFloppy;
956 exit;
957 end;
958
959 result := dtNone; // don't let OS/2 try a fake B: drive
960 exit;
961 end;
962
963 DosErrorAPI( FERR_DISABLEHARDERR );
964
965 szDrive := DriveNumberToDriveLetter( DriveNumber ) + ':';
966 FSDataLength := sizeof( FSData );
967 pBuffer := Addr( FSData );
968 rc := _DosQueryFSAttach( szDrive,
969 0, // ignored
970 FSAIL_QUERYNAME,
971 pBuffer,
972 FSDataLength );
973
974 if rc = 0 then
975 begin
976 case pBuffer^.iType of
977 FSAT_REMOTEDRV:
978 result := dtNetwork;
979
980 FSAT_LOCALDRV:
981 // Figure out what kind of local drive it is...
982 result := GetLocalDriveType( DriveNumber );
983
984 else
985 begin
986 // should never happen
987 result := dtNone;
988 exit;
989 end;
990 end;
991 end
992 else if rc = ERROR_NOT_READY then
993 begin
994 // No media?
995 // Have a look for a local disk anyway.
996 result := GetLocalDriveType( DriveNumber );
997 end
998 else
999 begin
1000 result := dtNone;
1001 end;
1002
1003 DosErrorAPI( FERR_ENABLEHARDERR );
1004end;
1005
1006const
1007 DEVLEN = 8;
1008 CNLEN = 15; // Computer name length
1009 UNCLEN = (CNLEN+2); // UNC computer name length
1010 NNLEN = 12; // 8.3 Net name length (share name length)
1011 RMLEN = (UNCLEN+1+NNLEN); // Maximum remote name length
1012
1013type
1014 use_info_0 = record
1015 ui0_local: cstring[ DEVLEN ]; // note this is of size DEVLEN + 1
1016 ui0_pad_1: char;
1017 ui0_remote: pchar;
1018 space: array[ 0..RMLEN ] of char; // remote path is written to somewhere in here
1019 end;
1020
1021 use_info_1 = record
1022 ui0_local: cstring[ DEVLEN ];
1023 ui0_pad_1: char;
1024 ui0_remote: pchar; // address of a buffer to hold remote path
1025 ui1_password: pchar; //
1026 ui1_status: USHORT;
1027 ui1_asg_type: SHORT;
1028 ui1_refcount: USHORT;
1029 ui1_usecount: USHORT;
1030 space: array[ 0..RMLEN ] of char; // remote path is written to somewhere in here
1031 end;
1032
1033 TNet32UseGetInfo = Function( pszServer: pchar;
1034 pszUseName: pchar; // e.g. drive x:
1035 ulLevel: ULONG;
1036 pbBuffer: pointer; // pointer to output buffer
1037 ulBuffer: ULONG; // size of output in buffer
1038 Var pulTotalAvail: ULONG )
1039 : word; CDecl;
1040
1041Var
1042 Net32UseGetInfo: TNet32UseGetInfo;
1043 hNetAPI32DLL: HMODULE;
1044 TriedLoading: boolean;
1045
1046// 129 Net32UseGetInfo
1047Function GetNetworkDriveRemotePath( DriveNumber: longint ): string;
1048var
1049 ErrorName: array[ 0..255 ] of char;
1050 dummy: cstring;
1051 rc: word;
1052 UseName: array[ 0..255 ] of char;
1053 UseInfo: use_info_0;
1054 pUseInfo: pointer;
1055 TotalBytesNeeded: ULONG;
1056 RemotePath: array[ 0..255 ] of char;
1057 Dummy2: array[ 0..4096 ] of char; // try to fix stack probs
1058begin
1059 Result := '';
1060
1061 if not TriedLoading then
1062 begin
1063 TriedLoading := true;
1064 rc := DosLoadModule( ErrorName,
1065 sizeof( ErrorName ),
1066 'NETAPI32',
1067 hNetAPI32DLL );
1068 if rc = NO_ERROR then
1069 begin
1070 // NetAPI32.DLL loaded OK
1071 rc := DosQueryProcAddr( hNetAPI32DLL,
1072 129,
1073 dummy,
1074 pointer( Net32UseGetInfo ) );
1075 if rc <> 0 then
1076 Net32UseGetInfo := nil;
1077 end;
1078 end;
1079
1080 if Assigned( Net32UseGetInfo ) then
1081 begin
1082 UseName[ 0 ] := DriveNumberToDriveLetter( DriveNumber );
1083 UseName[ 1 ] := ':';
1084 UseName[ 2 ] := #0;
1085
1086 RemotePath[ 0 ] := #0;
1087// UseInfo.ui0_remote := Addr( RemotePath );
1088
1089 pUseInfo := Addr( UseInfo );
1090 rc := Net32UseGetInfo( nil, // server - always nil
1091 Addr( UseName ),
1092 0, // info level 0
1093 pUseInfo,
1094 sizeof( UseInfo ),
1095 TotalBytesNeeded );
1096
1097 if rc = 0 then
1098 Result := StrPas( UseInfo.ui0_remote );
1099
1100 end;
1101end;
1102
1103
[82]1104Initialization
1105End.
Note: See TracBrowser for help on using the repository browser.