disk.inc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. {$push}
  12. {$i-}
  13. Function SetCurrentDir(Const NewDir: PathStr): Boolean;
  14. var
  15. PInOutRes: ^Word;
  16. OrigInOutRes: Word;
  17. begin
  18. { inoutres is a threadvar -> cache address }
  19. PInOutRes:=@InOutRes;
  20. OrigInOutRes:=PInOutRes^;
  21. PInOutRes^:=0;
  22. ChDir(NewDir);
  23. Result:=PInOutRes^=0;
  24. InOutRes:=OrigInOutRes;
  25. end;
  26. Function CreateDir (Const NewDir: PathStr): Boolean;
  27. var
  28. PInOutRes: ^Word;
  29. OrigInOutRes: Word;
  30. begin
  31. { inoutres is a threadvar -> cache address }
  32. PInOutRes:=@InOutRes;
  33. OrigInOutRes:=PInOutRes^;
  34. PInOutRes^:=0;
  35. MkDir(NewDir);
  36. Result:=PInOutRes^=0;
  37. InOutRes:=OrigInOutRes;
  38. end;
  39. Function RemoveDir (Const Dir: PathStr): Boolean;
  40. var
  41. PInOutRes: ^Word;
  42. OrigInOutRes: Word;
  43. begin
  44. { inoutres is a threadvar -> cache address }
  45. PInOutRes:=@InOutRes;
  46. OrigInOutRes:=PInOutRes^;
  47. PInOutRes^:=0;
  48. RmDir(Dir);
  49. Result:=PInOutRes^=0;
  50. InOutRes:=OrigInOutRes;
  51. end;
  52. {$pop}
  53. function ForceDirectories(Const Dir: PathStr): Boolean;
  54. var
  55. E: EInOutError;
  56. ADrv: PathStr;
  57. function DoForceDirectories(Const Dir: PathStr): Boolean;
  58. var
  59. ADir: PathStr;
  60. APath: PathStr;
  61. begin
  62. Result:=True;
  63. ADir:=ExcludeTrailingPathDelimiter(Dir);
  64. if (ADir='') then Exit;
  65. if Not DirectoryExists(ADir) then
  66. begin
  67. APath:=ExtractFilePath(ADir);
  68. //this can happen on Windows if user specifies Dir like \user\name/test/
  69. //and would, if not checked for, cause an infinite recusrsion and a stack overflow
  70. if (APath=ADir) then
  71. Result:=False
  72. else
  73. Result:=DoForceDirectories(APath);
  74. if Result then
  75. Result:=CreateDir(ADir);
  76. end;
  77. end;
  78. function IsUncDrive(const Drv: PathStr): Boolean;
  79. begin
  80. Result:=
  81. (Length(Drv)>2) and
  82. (Drv[1]=PathDelim) and
  83. (Drv[2]=PathDelim);
  84. end;
  85. begin
  86. Result:=False;
  87. ADrv:=ExtractFileDrive(Dir);
  88. if (ADrv<>'') and
  89. (not DirectoryExists(ADrv))
  90. {$IFNDEF FORCEDIR_NO_UNC_SUPPORT} and (not IsUncDrive(ADrv)){$ENDIF} then
  91. Exit;
  92. if Dir='' then
  93. begin
  94. E:=EInOutError.Create(SCannotCreateEmptyDir);
  95. E.ErrorCode:=3;
  96. Raise E;
  97. end;
  98. Result:=DoForceDirectories(SetDirSeparators(Dir));
  99. end;