FileSystem.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 sys;
  23. import lua.Io;
  24. import haxe.io.Path;
  25. import lua.lib.luv.fs.FileSystem as LFileSystem;
  26. class FileSystem {
  27. public static function exists(path:String):Bool {
  28. if (path == null)
  29. return false;
  30. else {
  31. var res = LFileSystem.stat(path);
  32. return res.result != null;
  33. }
  34. }
  35. public inline static function rename(path:String, newPath:String):Void {
  36. var ret = lua.Os.rename(path, newPath);
  37. if (!ret.success) {
  38. throw ret.message;
  39. }
  40. }
  41. public inline static function stat(path:String):FileStat {
  42. var ls = LFileSystem.stat(path);
  43. if (ls.result == null)
  44. throw ls.message;
  45. var l = ls.result;
  46. return {
  47. gid: l.gid,
  48. uid: l.uid,
  49. rdev: l.rdev,
  50. size: l.size,
  51. nlink: l.nlink,
  52. mtime: Date.fromTime(l.mtime.sec + l.mtime.nsec / 1000000),
  53. mode: l.mode,
  54. ino: l.ino,
  55. dev: l.dev,
  56. ctime: Date.fromTime(l.ctime.sec + l.ctime.nsec / 1000000),
  57. atime: Date.fromTime(l.atime.sec + l.atime.nsec / 1000000)
  58. };
  59. }
  60. public inline static function fullPath(relPath:String):String {
  61. return LFileSystem.realpath(Path.normalize(absolutePath(relPath)));
  62. }
  63. public inline static function absolutePath(relPath:String):String {
  64. if (haxe.io.Path.isAbsolute(relPath)) {
  65. return relPath;
  66. }
  67. var pwd = lua.lib.luv.Misc.cwd();
  68. if (pwd == null)
  69. return relPath;
  70. return Path.join([pwd, relPath]);
  71. }
  72. public inline static function deleteFile(path:String):Void {
  73. var ret = lua.Os.remove(path);
  74. if (!ret.success) {
  75. throw ret.message;
  76. }
  77. }
  78. public inline static function readDirectory(path:String):Array<String> {
  79. var scandir = LFileSystem.scandir(path);
  80. var itr = function() {
  81. var next = LFileSystem.scandir_next(scandir).name;
  82. return next;
  83. }
  84. return lua.Lib.fillArray(itr);
  85. }
  86. public inline static function isDirectory(path:String):Bool {
  87. var result = LFileSystem.stat(path).result;
  88. if (result == null)
  89. return false;
  90. else
  91. return result.type == "directory";
  92. }
  93. public inline static function deleteDirectory(path:String):Void {
  94. var ret = LFileSystem.rmdir(path);
  95. if (ret.result == null) {
  96. throw ret.message;
  97. }
  98. }
  99. public static function createDirectory(path:String):Void {
  100. var path = haxe.io.Path.addTrailingSlash(path);
  101. var _p = null;
  102. var parts = [];
  103. while (path != (_p = haxe.io.Path.directory(path))) {
  104. parts.unshift(path);
  105. path = _p;
  106. }
  107. for (part in parts) {
  108. if (part.charCodeAt(part.length - 1) != ":".code && !exists(part) && !LFileSystem.mkdir(part, 511).result)
  109. throw "Could not create directory:" + part;
  110. }
  111. }
  112. }