FileSystem.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. @:coreApi
  24. @:access(Sys)
  25. class FileSystem {
  26. public static function exists(path:String):Bool {
  27. return sys_exists(Sys.getPath(makeCompatiblePath(path)));
  28. }
  29. public static function rename(path:String, newPath:String):Void {
  30. if (!sys_rename(Sys.getPath(path), Sys.getPath(newPath)))
  31. throw new Sys.SysError("Failed to rename " + path + " to " + newPath);
  32. }
  33. public static function stat(path:String):FileStat {
  34. var values = sys_stat(Sys.getPath(makeCompatiblePath(path)));
  35. if (values == null)
  36. throw new Sys.SysError("Failed to stat " + path);
  37. return {
  38. gid: values[0],
  39. uid: values[1],
  40. atime: @:privateAccess Date.fromInt(values[2]),
  41. mtime: @:privateAccess Date.fromInt(values[3]),
  42. ctime: @:privateAccess Date.fromInt(values[4]),
  43. size: values[5],
  44. dev: values[6],
  45. ino: values[7],
  46. nlink: values[8],
  47. rdev: values[9],
  48. mode: values[10],
  49. };
  50. }
  51. public static function fullPath(relPath:String):String {
  52. return Sys.makePath(sys_full_path(Sys.getPath(relPath)));
  53. }
  54. public static function absolutePath(relPath:String):String {
  55. if (haxe.io.Path.isAbsolute(relPath))
  56. return relPath;
  57. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  58. }
  59. public static function isDirectory(path:String):Bool {
  60. return sys_is_dir(Sys.getPath(makeCompatiblePath(path)));
  61. }
  62. public static function createDirectory(path:String):Void {
  63. var path = haxe.io.Path.addTrailingSlash(path);
  64. var _p = null;
  65. var parts = [];
  66. while (path != (_p = haxe.io.Path.directory(path))) {
  67. parts.unshift(path);
  68. path = _p;
  69. }
  70. for (part in parts) {
  71. if (part.charCodeAt(part.length - 1) != ":".code && !exists(part))
  72. if (!sys_create_dir(Sys.getPath(part), 493))
  73. throw new Sys.SysError("Failed to create directory " + part);
  74. }
  75. }
  76. public static function deleteFile(path:String):Void {
  77. if (!sys_delete(Sys.getPath(path)))
  78. throw new Sys.SysError("Can't delete file " + path);
  79. }
  80. public static function deleteDirectory(path:String):Void {
  81. if (!sys_remove_dir(Sys.getPath(path)))
  82. throw new Sys.SysError("Can't delete directory " + path);
  83. }
  84. public static function readDirectory(path:String):Array<String> {
  85. var content = sys_read_dir(Sys.getPath(path));
  86. if (content == null)
  87. throw new Sys.SysError("Failed to read directory " + path);
  88. return [for (c in content) Sys.makePath(c)];
  89. }
  90. private static inline function makeCompatiblePath(path:String):String {
  91. return if (path.charCodeAt(1) == ":".code && path.length <= 3) {
  92. haxe.io.Path.addTrailingSlash(path);
  93. } else if (path == "/") {
  94. "/";
  95. } else {
  96. haxe.io.Path.removeTrailingSlashes(path);
  97. }
  98. }
  99. @:hlNative("std", "sys_read_dir") static function sys_read_dir(path:hl.Bytes):hl.NativeArray<hl.Bytes> {
  100. return null;
  101. }
  102. @:hlNative("std", "sys_create_dir") static function sys_create_dir(path:hl.Bytes, rights:Int):Bool {
  103. return false;
  104. }
  105. @:hlNative("std", "sys_is_dir") static function sys_is_dir(path:hl.Bytes):Bool {
  106. return false;
  107. }
  108. @:hlNative("std", "sys_stat") static function sys_stat(path:hl.Bytes):hl.NativeArray<Int> {
  109. return null;
  110. }
  111. @:hlNative("std", "sys_rename") static function sys_rename(path:hl.Bytes, to:hl.Bytes):Bool {
  112. return true;
  113. }
  114. @:hlNative("std", "sys_delete") static function sys_delete(path:hl.Bytes):Bool {
  115. return true;
  116. };
  117. @:hlNative("std", "sys_full_path") static function sys_full_path(path:hl.Bytes):hl.Bytes {
  118. return null;
  119. }
  120. @:hlNative("std", "sys_remove_dir") static function sys_remove_dir(path:hl.Bytes):Bool {
  121. return true;
  122. }
  123. @:hlNative("std", "sys_exists") static function sys_exists(path:hl.Bytes):Bool {
  124. return true;
  125. }
  126. }