FileSystem.hx 3.6 KB

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