2
0

disk.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2012 by the Free Pascal development team
  4. Disk calls
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. function GetDriveIDFromLetter(Const ADrive : PathStr) : Byte;
  12. begin
  13. if length(ADrive)=0 then
  14. Result:=0
  15. else
  16. Result := (Ord(ADrive[1]))-64;
  17. end;
  18. {$push}
  19. {$i-}
  20. Function SetCurrentDir(Const NewDir: PathStr): Boolean;
  21. var
  22. PInOutRes: ^Word;
  23. OrigInOutRes: Word;
  24. begin
  25. { inoutres is a threadvar -> cache address }
  26. PInOutRes:=@InOutRes;
  27. OrigInOutRes:=PInOutRes^;
  28. PInOutRes^:=0;
  29. ChDir(NewDir);
  30. Result:=PInOutRes^=0;
  31. InOutRes:=OrigInOutRes;
  32. end;
  33. Function CreateDir (Const NewDir: PathStr): Boolean;
  34. var
  35. PInOutRes: ^Word;
  36. OrigInOutRes: Word;
  37. begin
  38. { inoutres is a threadvar -> cache address }
  39. PInOutRes:=@InOutRes;
  40. OrigInOutRes:=PInOutRes^;
  41. PInOutRes^:=0;
  42. MkDir(NewDir);
  43. Result:=PInOutRes^=0;
  44. InOutRes:=OrigInOutRes;
  45. end;
  46. Function RemoveDir (Const Dir: PathStr): Boolean;
  47. var
  48. PInOutRes: ^Word;
  49. OrigInOutRes: Word;
  50. begin
  51. { inoutres is a threadvar -> cache address }
  52. PInOutRes:=@InOutRes;
  53. OrigInOutRes:=PInOutRes^;
  54. PInOutRes^:=0;
  55. RmDir(Dir);
  56. Result:=PInOutRes^=0;
  57. InOutRes:=OrigInOutRes;
  58. end;
  59. {$pop}
  60. function ForceDirectories(Const Dir: PathStr): Boolean;
  61. var
  62. E: EInOutError;
  63. ADrv: PathStr;
  64. function DoForceDirectories(Const Dir: PathStr): Boolean;
  65. var
  66. ADir: PathStr;
  67. APath: PathStr;
  68. begin
  69. Result:=True;
  70. ADir:=ExcludeTrailingPathDelimiter(Dir);
  71. if (ADir='') then Exit;
  72. if Not DirectoryExists(ADir) then
  73. begin
  74. APath:=ExtractFilePath(ADir);
  75. //this can happen on Windows if user specifies Dir like \user\name/test/
  76. //and would, if not checked for, cause an infinite recusrsion and a stack overflow
  77. if (APath=ADir) then
  78. Result:=False
  79. else
  80. Result:=DoForceDirectories(APath);
  81. if Result then
  82. Result:=CreateDir(ADir);
  83. end;
  84. end;
  85. function IsUncDrive(const Drv: PathStr): Boolean;
  86. begin
  87. Result:=
  88. (Length(Drv)>2) and
  89. (Drv[1]=PathDelim) and
  90. (Drv[2]=PathDelim);
  91. end;
  92. begin
  93. Result:=False;
  94. ADrv:=ExtractFileDrive(Dir);
  95. if (ADrv<>'') and
  96. (not DirectoryExists(ADrv))
  97. {$IFNDEF FORCEDIR_NO_UNC_SUPPORT} and (not IsUncDrive(ADrv)){$ENDIF} then
  98. Exit;
  99. if Dir='' then
  100. begin
  101. E:=EInOutError.Create(SCannotCreateEmptyDir);
  102. E.ErrorCode:=3;
  103. Raise E;
  104. end;
  105. Result:=DoForceDirectories(SetDirSeparators(Dir));
  106. end;