FileSystem.hx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package asys;
  2. import haxe.Error;
  3. import haxe.io.Bytes;
  4. import haxe.io.FilePath;
  5. import asys.io.*;
  6. typedef FileReadStreamCreationOptions = {
  7. ?flags:FileOpenFlags,
  8. ?mode:FilePermissions
  9. } &
  10. asys.io.FileReadStream.FileReadStreamOptions;
  11. /**
  12. This class provides methods for synchronous operations on files and
  13. directories. For asynchronous operations, see `asys.async.FileSystem`.
  14. Passing `null` as a path to any of the functions in this class will result
  15. in unspecified behaviour.
  16. **/
  17. extern class FileSystem {
  18. public static inline final async = asys.AsyncFileSystem;
  19. /**
  20. Tests specific user permissions for the file specified by `path`. If the
  21. check fails, throws an exception. `mode` is one or more `FileAccessMode`
  22. values:
  23. - `FileAccessMode.Ok` - file is visible to the calling process (it exists)
  24. - `FileAccessMode.Execute` - file can be executed by the calling proces
  25. - `FileAccessMode.Write` - file can be written to by the calling proces
  26. - `FileAccessMode.Read` - file can be read from by the calling proces
  27. Mode values can be combined with the bitwise or operator, e.g. calling
  28. `access` with the `mode`:
  29. ```haxe
  30. FileAccessMode.Execute | FileAccessMode.Read
  31. ```
  32. will check that the file is both readable and executable.
  33. The result of this call should not be used in a condition before a call to
  34. e.g. `open`, because this would introduce a race condition (the file could
  35. be deleted after the `access` call, but before the `open` call). Instead,
  36. the latter function should be called immediately and errors should be
  37. handled with a `try ... catch` block.
  38. **/
  39. static function access(path:FilePath, ?mode:FileAccessMode = FileAccessMode.Ok):Void;
  40. /**
  41. Appends `data` at the end of the file located at `path`.
  42. **/
  43. static function appendFile(path:FilePath, data:Bytes, ?flags:FileOpenFlags /* a */, ?mode:FilePermissions /* 0666 */):Void;
  44. /**
  45. Changes the permissions of the file specific by `path` to `mode`.
  46. If `path` points to a symbolic link, this function will change the
  47. permissions of the target file, not the symbolic link itself, unless
  48. `followSymLinks` is set to `false`.
  49. TODO: `followSymLinks == false` is not implemented and will throw.
  50. **/
  51. static function chmod(path:FilePath, mode:FilePermissions, ?followSymLinks:Bool = true):Void;
  52. /**
  53. Changes the owner and group of the file specific by `path` to `uid` and
  54. `gid`, respectively.
  55. If `path` points to a symbolic link, this function will change the
  56. permissions of the target file, not the symbolic link itself, unless
  57. `followSymLinks` is set to `false`.
  58. TODO: `followSymLinks == false` is not implemented and will throw.
  59. **/
  60. static function chown(path:FilePath, uid:Int, gid:Int, ?followSymLinks:Bool = true):Void;
  61. /**
  62. Copies the file at `src` to `dest`. If `dest` exists, it is overwritten.
  63. **/
  64. static function copyFile(src:FilePath, dest:FilePath /* , ?flags:FileCopyFlags */):Void;
  65. /**
  66. Creates a read stream (an instance of `IReadable`) for the given path.
  67. `options` can be used to specify how the file is opened, as well as which
  68. part of the file will be read by the stream.
  69. - `options.flags` - see `open`.
  70. - `options.mode` - see `open`.
  71. - `options.autoClose` - whether the file should be closed automatically
  72. once the stream is fully consumed.
  73. - `options.start` - starting position in bytes (inclusive).
  74. - `options.end` - end position in bytes (non-inclusive).
  75. **/
  76. static function createReadStream(path:FilePath, ?options:FileReadStreamCreationOptions):FileReadStream;
  77. // static function createWriteStream(path:FilePath, ?options:{?flags:FileOpenFlags, ?mode:FilePermissions, ?autoClose:Bool, ?start:Int}):FileWriteStream;
  78. /**
  79. Returns `true` if the file or directory specified by `path` exists.
  80. The result of this call should not be used in a condition before a call to
  81. e.g. `open`, because this would introduce a race condition (the file could
  82. be deleted after the `exists` call, but before the `open` call). Instead,
  83. the latter function should be called immediately and errors should be
  84. handled with a `try ... catch` block.
  85. **/
  86. static function exists(path:FilePath):Bool;
  87. static function link(existingPath:FilePath, newPath:FilePath):Void;
  88. /**
  89. Creates a directory at the path `path`, with file mode `mode`.
  90. If `recursive` is `false` (default), this function can only create one
  91. directory at a time, the last component of `path`. If `recursive` is `true`,
  92. intermediate directories will be created as needed.
  93. **/
  94. static function mkdir(path:FilePath, ?recursive:Bool = false, ?mode:FilePermissions /* 0777 */):Void;
  95. /**
  96. Creates a unique temporary directory. `prefix` should be a path template
  97. ending in six `X` characters, which will be replaced with random characters.
  98. Returns the path to the created directory.
  99. The generated directory needs to be manually deleted by the process.
  100. **/
  101. static function mkdtemp(prefix:FilePath):FilePath;
  102. /**
  103. Opens the file located at `path`.
  104. **/
  105. static function open(path:FilePath, ?flags:FileOpenFlags /* a */, ?mode:FilePermissions /* 0666 */, ?binary:Bool = true):File;
  106. /**
  107. Reads the contents of a directory specified by `path`. Returns an array of
  108. `FilePath`s relative to the specified directory (i.e. the paths are not
  109. absolute). The array will not include `.` or `..`.
  110. **/
  111. static function readdir(path:FilePath):Array<FilePath>;
  112. /**
  113. Same as `readdir`, but returns an array of `DirectoryEntry` values instead.
  114. **/
  115. static function readdirTypes(path:FilePath):Array<DirectoryEntry>;
  116. /**
  117. Reads all the bytes of the file located at `path`.
  118. **/
  119. static function readFile(path:FilePath, ?flags:FileOpenFlags /* r */):Bytes;
  120. /**
  121. Returns the contents (target path) of the symbolic link located at `path`.
  122. **/
  123. static function readlink(path:FilePath):FilePath;
  124. /**
  125. Returns the canonical path name of `path` (which may be a relative path)
  126. by resolving `.`, `..`, and symbolic links.
  127. **/
  128. static function realpath(path:FilePath):FilePath;
  129. /**
  130. Renames the file or directory located at `oldPath` to `newPath`. If a file
  131. already exists at `newPath`, it is overwritten. If a directory already
  132. exists at `newPath`, an exception is thrown.
  133. **/
  134. static function rename(oldPath:FilePath, newPath:FilePath):Void;
  135. /**
  136. Deletes the directory located at `path`. If the directory is not empty or
  137. cannot be deleted, an error is thrown.
  138. **/
  139. static function rmdir(path:FilePath):Void;
  140. /**
  141. Returns information about the file located at `path`.
  142. If `path` points to a symbolic link, this function will return information
  143. about the target file, not the symbolic link itself, unless `followSymLinks`
  144. is set to `false`.
  145. **/
  146. static function stat(path:FilePath, ?followSymLinks:Bool = true):asys.FileStat;
  147. /**
  148. Creates a symbolic link at `path`, pointing to `target`.
  149. The `type` argument is ignored on all platforms except `Windows`.
  150. **/
  151. static function symlink(target:FilePath, path:FilePath, ?type:SymlinkType = SymlinkType.SymlinkDir):Void;
  152. /**
  153. Truncates the file located at `path` to exactly `len` bytes. If the file was
  154. larger than `len` bytes, the extra data is lost. If the file was smaller
  155. than `len` bytes, the file is extended with null bytes.
  156. **/
  157. static function truncate(path:FilePath, ?len:Int = 0):Void;
  158. /**
  159. Deletes the file located at `path`.
  160. **/
  161. static function unlink(path:FilePath):Void;
  162. /**
  163. Modifies the system timestamps of the file located at `path`.
  164. **/
  165. static function utimes(path:FilePath, atime:Date, mtime:Date):Void;
  166. /**
  167. Creates a file watcher for `path`.
  168. @param recursive If `true`, the file watcher will signal for changes in
  169. sub-directories of `path` as well.
  170. **/
  171. static function watch(path:FilePath, ?recursive:Bool = false):FileWatcher;
  172. /**
  173. Writes `data` to the file located at `path`.
  174. **/
  175. static function writeFile(path:FilePath, data:Bytes, ?flags:FileOpenFlags /* w */, ?mode:FilePermissions /* 0666 */):Void;
  176. }