FileSystem.hx 4.7 KB

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