FileSystem.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 java.io.File;
  24. @:coreApi
  25. class FileSystem {
  26. public static function exists(path:String):Bool {
  27. return new File(path).exists();
  28. }
  29. public static function rename(path:String, newPath:String):Void {
  30. if (!new File(path).renameTo(new File(newPath))) {
  31. throw "Cannot rename " + path + " to " + newPath;
  32. }
  33. }
  34. public static function stat(path:String):FileStat {
  35. var f = new File(path);
  36. if (!f.exists())
  37. throw "Path " + path + " doesn't exist";
  38. try {
  39. final pathObject = java.nio.file.Paths.get(path);
  40. final attributes = java.nio.file.Files.readAttributes(pathObject, "unix:*");
  41. return {
  42. atime: Date.fromTime(cast(attributes.get("lastAccessTime").toMillis(), Float)),
  43. ctime: Date.fromTime(cast(attributes.get("creationTime").toMillis(), Float)),
  44. dev: cast(attributes.get("dev"), Int),
  45. gid: attributes.get("gid"),
  46. ino: cast(attributes.get("ino"), Int),
  47. mode: attributes.get("mode"),
  48. mtime: Date.fromTime(cast(attributes.get("lastModifiedTime").toMillis(), Float)),
  49. nlink: attributes.get("nlink"),
  50. rdev: cast(attributes.get("rdev"), Int),
  51. size: cast(attributes.get("size"), Int),
  52. uid: attributes.get("uid"),
  53. };
  54. } catch (e) {
  55. return {
  56. gid: 0, // java doesn't let you get this info
  57. uid: 0, // same
  58. atime: Date.now(), // same
  59. mtime: Date.fromTime(cast(f.lastModified(), Float)),
  60. ctime: Date.fromTime(cast(f.lastModified(), Float)), // same
  61. size: cast(f.length(), Int), // TODO: maybe change to Int64 for Haxe 3?
  62. dev: 0, // FIXME: not sure what that is
  63. ino: 0, // FIXME: not sure what that is
  64. nlink: 0, // FIXME: not sure what that is
  65. rdev: 0, // FIXME: not sure what that is
  66. mode: 0 // FIXME: not sure what that is
  67. };
  68. }
  69. }
  70. public static function fullPath(relPath:String):String {
  71. try {
  72. return new File(relPath).getCanonicalPath();
  73. } catch (e:java.io.IOException) {
  74. throw new java.lang.RuntimeException(e);
  75. }
  76. }
  77. public static function absolutePath(relPath:String):String {
  78. if (haxe.io.Path.isAbsolute(relPath))
  79. return relPath;
  80. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  81. }
  82. public static function isDirectory(path:String):Bool {
  83. var f = new File(path);
  84. if (!f.exists())
  85. throw "Path " + path + " doesn't exist";
  86. return f.isDirectory();
  87. }
  88. public static function createDirectory(path:String):Void {
  89. var f = new File(path);
  90. if (!f.isDirectory() && !f.mkdirs())
  91. throw "Cannot create dir " + path;
  92. }
  93. public static function deleteFile(path:String):Void {
  94. if (!new File(path).delete())
  95. throw "Cannot delete file " + path;
  96. }
  97. public static function deleteDirectory(path:String):Void {
  98. if (!new File(path).delete())
  99. throw "Cannot delete directory " + path;
  100. }
  101. public static function readDirectory(path:String):Array<String> {
  102. var f = new File(path);
  103. if (!f.exists())
  104. throw "Path " + path + " doesn't exist";
  105. return @:privateAccess Array.ofNative(f.list());
  106. }
  107. }