FileSystem.hx 3.6 KB

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