2
0

disk.inc 2.8 KB

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