FileSystem.hx 3.6 KB

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