sysdir.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2010 by Sven Barth
  4. FPC Pascal system unit for the Native NT API.
  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. {*****************************************************************************
  12. Directory Handling
  13. *****************************************************************************}
  14. procedure MkDir(s: pchar; len: sizeuint); [IOCheck, public, alias : 'FPC_SYS_MKDIR'];
  15. var
  16. objattr: TObjectAttributes;
  17. name: TNtUnicodeString;
  18. res: LongInt;
  19. iostatus: TIOStatusBlock;
  20. h: THandle;
  21. begin
  22. if not Assigned(s) or (len <= 1) or (InOutRes <> 0) then
  23. Exit;
  24. SysPCharToNtStr(name, s, len);
  25. { first we try to create a directory object }
  26. SysInitializeObjectAttributes(objattr, @name, OBJ_PERMANENT, 0, Nil);
  27. res := NtCreateDirectoryObject(@h, 0, @objattr);
  28. if res <> STATUS_OBJECT_TYPE_MISMATCH then begin
  29. if res = STATUS_SUCCESS then
  30. NtClose(h);
  31. errno := res;
  32. Errno2InoutRes;
  33. SysFreeNtStr(name);
  34. Exit;
  35. end;
  36. { so the parent directory isn't a directory object... retry as normal file
  37. object }
  38. objattr.Attributes := 0; // OBJ_PERMANENT is not valid for file objects
  39. { the flags are based on ReactOS' CreateDirectoryW except the missing LIST
  40. access }
  41. res := NtCreateFile(@h, NT_SYNCHRONIZE, @objattr, @iostatus, Nil,
  42. FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ or FILE_SHARE_WRITE,
  43. FILE_CREATE, FILE_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT,
  44. Nil, 0);
  45. if res = STATUS_SUCCESS then
  46. NtClose(h);
  47. errno := res;
  48. Errno2InOutRes;
  49. SysFreeNtStr(name);
  50. end;
  51. procedure RmDir(s: pchar;len:sizeuint);[IOCheck, public, alias : 'FPC_SYS_RMDIR'];
  52. var
  53. ntstr: TNtUnicodeString;
  54. objattr: TObjectAttributes;
  55. iostatus: TIOStatusBlock;
  56. h: THandle;
  57. disp: TFileDispositionInformation;
  58. res: LongInt;
  59. begin
  60. if (len = 1) and (s^ = '.') then
  61. InOutRes := 16;
  62. if not assigned(s) or (len = 0) or (InOutRes <> 0) then
  63. Exit;
  64. if (len = 2) and (s[0] = '.') and (s[1] = '.') then
  65. InOutRes := 5;
  66. SysPCharToNtStr(ntstr, s, len);
  67. SysInitializeObjectAttributes(objattr, @ntstr, 0, 0, Nil);
  68. res := NtOpenDirectoryObject(@h, STANDARD_RIGHTS_REQUIRED, @objattr);
  69. if res >= 0 then begin
  70. { this is a directory object, so just make it temporary }
  71. {$message warning 'Add check for subdirectories'}
  72. res := NtMakeTemporaryObject(h);
  73. NtClose(h);
  74. errno := res;
  75. Errno2InoutRes;
  76. SysFreeNtStr(ntstr);
  77. end else
  78. if res = STATUS_OBJECT_TYPE_MISMATCH then begin
  79. { this is a file directory or file, so do it like RemoveDirectoryW }
  80. res := NtCreateFile(@h, NT_DELETE or NT_SYNCHRONIZE, @objattr, @iostatus, Nil,
  81. 0, FILE_SHARE_READ or FILE_SHARE_WRITE or FILE_SHARE_DELETE,
  82. FILE_OPEN, FILE_DIRECTORY_FILE or FILE_SYNCHRONOUS_IO_NONALERT,
  83. Nil, 0);
  84. if res >= 0 then begin
  85. disp.DeleteFile := True;
  86. { NtDeleteFile does not work here... }
  87. res := NtSetInformationFile(h, @iostatus, @disp,
  88. SizeOf(TFileDispositionInformation), FileDispositionInformation);
  89. NtClose(h);
  90. end;
  91. end;
  92. SysFreeNtStr(ntstr);
  93. errno := res;
  94. Errno2InoutRes;
  95. end;
  96. procedure ChDir(s: pchar; len: sizeuint);[IOCheck, public, alias : 'FPC_SYS_CHDIR'];
  97. begin
  98. { for now this is not supported }
  99. InOutRes := 3;
  100. end;
  101. procedure GetDir(DriveNr: byte; var Dir: ShortString);
  102. begin
  103. { for now we return simply the root directory }
  104. Dir := DirectorySeparator;
  105. end;