2
0

FileSystem.hx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package lua.lib.luv.fs;
  23. import lua.lib.luv.fs.Open;
  24. import lua.Result;
  25. @:luaRequire("luv")
  26. extern class FileSystem {
  27. static final constants:Table<String,Int>;
  28. @:native("fs_close")
  29. @:overload(function(file:FileDescriptor, cb:String->Bool->Void):Request {})
  30. static function close(file:FileDescriptor):Result<Bool>;
  31. @:native("fs_open")
  32. @:overload(function(path:String, flags:Int, mode:Int):Result<FileDescriptor> {})
  33. @:overload(function(path:String, flags:Int, mode:Int, ?cb:String->FileDescriptor->Void):Request {})
  34. @:overload(function(path:String, flags:Open, mode:Int, ?cb:String->FileDescriptor->Void):Request {})
  35. static function open(path:String, flags:Open, mode:Int):Result<FileDescriptor>;
  36. @:native("fs_read")
  37. @:overload(function(file:FileDescriptor, len:Int, offset:Int, ?cb:String->String->Void):Request {})
  38. static function read(file:FileDescriptor, len:Int, offset:Int):Result<String>;
  39. @:native("fs_unlink")
  40. @:overload(function(file:String, ?cb:String->Bool->Void):Request {})
  41. static function unlink(file:String, content:String):Result<Bool>;
  42. @:native("fs_write")
  43. @:overload(function(file:FileDescriptor, content:String, offset:Int, ?cb:String->Int->Void):Int {})
  44. static function write(file:FileDescriptor, content:String, offset:Int):Result<Int>;
  45. @:native("fs_mkdir")
  46. @:overload(function(path:String, mode:Int, cb:String->Bool->Void):Request {})
  47. static function mkdir(path:String, mode:Int):Result<Bool>;
  48. @:native("fs_mkdtemp")
  49. @:overload(function(template:String, cb:String->String->Void):Request {})
  50. static function mkdtemp(template:String):Result<String>;
  51. @:native("fs_mkstemp")
  52. @:overload(function(template:String, cb:String->FileDescriptor->String->Void):Request {})
  53. static function mkstemp(template:String):Result<FileDescriptor>;
  54. @:native("fs_rmdir")
  55. @:overload(function(path:String, cb:String->Bool->Void):Request {})
  56. static function rmdir(path:String):Result<Int>;
  57. @:native("fs_scandir")
  58. @:overload(function(path:String, cb:String->Bool->Void):Request {})
  59. static function scandir(path:String):ScanDirMarker;
  60. @:native("fs_scandir_next")
  61. static function scandir_next(scandir:ScanDirMarker):ScandirNext;
  62. @:native("fs_stat")
  63. @:overload(function(path:String, cb:String->Stat->Void):Request {})
  64. static function stat(path:String):Result<Stat>;
  65. @:native("fs_fstat")
  66. @:overload(function(descriptor:FileDescriptor, cb:String->Stat->Void):Request {})
  67. static function fstat(descriptor:FileDescriptor):Result<Stat>;
  68. @:native("fs_lstat")
  69. @:overload(function(path:String, cb:String->Stat->Void):Request {})
  70. static function lstat(path:String):Result<Stat>;
  71. @:native("fs_rename")
  72. @:overload(function(path:String, newpath:String, cb:String->Bool->Void):Request {})
  73. static function rename(path:String, newpath:String):Result<Bool>;
  74. @:native("fs_fsync")
  75. @:overload(function(descriptor:FileDescriptor, cb:String->Bool->Void):Request {})
  76. static function fsync(descriptor:FileDescriptor):Result<Bool>;
  77. @:native("fs_fdatasync")
  78. @:overload(function(descriptor:FileDescriptor, cb:String->Bool->Void):Request {})
  79. static function fdatasync(descriptor:FileDescriptor):Result<Bool>;
  80. @:native("fs_ftruncate")
  81. @:overload(function(descriptor:FileDescriptor, offset:Int, cb:String->Bool->Void):Request {})
  82. static function ftruncate(descriptor:FileDescriptor, offset:Int):Result<Bool>;
  83. @:native("fs_sendfile")
  84. @:overload(function(fin:FileDescriptor, fout:FileDescriptor, cb:String->Int->Void):Request {})
  85. static function sendfile(fin:FileDescriptor, fout:FileDescriptor):Result<Int>;
  86. @:native("fs_access")
  87. @:overload(function(path:String, mode:Int, cb:String->Bool->Void):Request {})
  88. static function access(path:String, mode:Int):Result<Bool>;
  89. @:native("fs_chmod")
  90. @:overload(function(path:String, mode:Int, cb:String->Bool->Void):Request {})
  91. static function chmod(path:String, mode:Int):Result<Bool>;
  92. @:native("fs_fchmod")
  93. @:overload(function(descriptor:FileDescriptor, mode:Int, cb:String->Bool->Void):Request {})
  94. static function fchmod(descriptor:FileDescriptor, mode:Int):Result<Bool>;
  95. @:native("fs_futime")
  96. @:overload(function(descriptor:FileDescriptor, actime:Int, modtime:Int, cb:String->Bool->Void):Request {})
  97. static function futime(descriptor:FileDescriptor, actime:Int, modtime:Int):Result<Bool>;
  98. @:native("fs_utime")
  99. @:overload(function(path:String, actime:Int, modtime:Int, cb:String->Bool->Void):Request {})
  100. static function utime(path:String, actime:Int, modtime:Int):Result<Bool>;
  101. @:native("fs_link")
  102. @:overload(function(oldpath:String, newpath:String, cb:String->Bool->Void):Request {})
  103. static function link(oldpath:String, newpath:String):Result<Bool>;
  104. @:native("fs_symlink")
  105. @:overload(function(oldpath:String, newpath:String, flags:Int, cb:String->Bool->Void):Request {})
  106. static function symlink(oldpath:String, newpath:String, flags:Int):Bool;
  107. @:native("fs_readlink")
  108. @:overload(function(path:String, cb:String->String->Void):Request {})
  109. static function readlink(path:String):String;
  110. @:native("fs_realpath")
  111. @:overload(function(path:String, cb:String->String->Void):Request {})
  112. static function realpath(path:String):String;
  113. @:native("fs_chown")
  114. @:overload(function(path:String, uid:Int, gid:Int, cb:String->Bool->Void):Request {})
  115. static function chown(path:String, uid:Int, gid:Int):Bool;
  116. @:native("fs_fchown")
  117. @:overload(function(descriptor:FileDescriptor, uid:Int, gid:Int, cb:String->Bool->Void):Request {})
  118. static function fchown(descriptor:FileDescriptor, uid:Int, gid:Int):Bool;
  119. /**
  120. Not available on windows
  121. **/
  122. @:native("fs_lchown")
  123. @:overload(function(descriptor:String, uid:Int, gid:Int, cb:String->Bool->Void):Request {})
  124. static function lchown(descriptor:String, uid:Int, gid:Int):Bool;
  125. @:native("fs_copyfile")
  126. @:overload(function(path:String, newPath:String, flags:Null<CopyFlags>, cb:String->Bool->Void):Request {})
  127. static function copyfile(path:String, newPath:String, ?flags:CopyFlags):Bool;
  128. @:native("fs_statfs")
  129. @:overload(function(path:String, cb:StatFs->Bool->Void):Request {})
  130. static function statfs(path:String):StatFs;
  131. @:native("fs_opendir")
  132. @:overload(function(path:String, cb:String->Handle->Void, ?entries:Int):Request {})
  133. static function opendir(path:String):Handle;
  134. @:native("fs_readdir")
  135. @:overload(function(dir:Handle, cb:String->Table<Int, NameType>->Void):Request {})
  136. static function readdir(path:Handle):Table<Int, NameType>;
  137. @:native("fs_closedir")
  138. @:overload(function(dir:Handle, cb:String->Bool->Void):Request {})
  139. static function closedir(dir:Handle):Bool;
  140. }
  141. extern class ScanDirMarker {}
  142. @:multiReturn
  143. extern class ScandirNext {
  144. var name:String;
  145. var type:String;
  146. }
  147. typedef NameType = {
  148. name:String,
  149. type:String
  150. }
  151. typedef Stat = {
  152. ino:Int,
  153. ctime:TimeStamp,
  154. uid:Int,
  155. dev:Int,
  156. nlink:Int,
  157. mode:Int,
  158. size:Int,
  159. birthtime:TimeStamp,
  160. gid:Int,
  161. type:String,
  162. rdev:Int,
  163. gen:Int,
  164. blocks:Int,
  165. mtime:TimeStamp,
  166. atime:TimeStamp,
  167. blksize:Int,
  168. flags:Int
  169. }
  170. typedef TimeStamp = {
  171. sec:Int,
  172. nsec:Int
  173. }
  174. typedef StatFs = {
  175. type:Int,
  176. bsize:Int,
  177. blocks:Int,
  178. bfree:Int,
  179. bavail:Int,
  180. files:Int,
  181. ffree:Int
  182. }
  183. typedef CopyFlags = {
  184. ?excl:Bool,
  185. ?ficlone:Bool,
  186. ?ficlone_force:Bool
  187. }
  188. enum abstract AccessMode(Int) to Int {
  189. // from libuv sources
  190. /* Test for read permission. */
  191. var R_OK = 4;
  192. /* Test for write permission. */
  193. var W_OK = 2;
  194. /* Test for execute permission. */
  195. var X_OK = 1;
  196. /* Test for existence. */
  197. var F_OK = 0;
  198. @:op(A | B) function or(b:AccessMode):Int;
  199. }