FileSystem.hx 4.0 KB

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