FileSystem.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. return {
  40. gid: 0, // java doesn't let you get this info
  41. uid: 0, // same
  42. atime: Date.now(), // same
  43. mtime: Date.fromTime(cast(f.lastModified(), Float)),
  44. ctime: Date.fromTime(cast(f.lastModified(), Float)), // same
  45. size: cast(f.length(), Int), // TODO: maybe change to Int64 for Haxe 3?
  46. dev: 0, // FIXME: not sure what that is
  47. ino: 0, // FIXME: not sure what that is
  48. nlink: 0, // FIXME: not sure what that is
  49. rdev: 0, // FIXME: not sure what that is
  50. mode: 0 // FIXME: not sure what that is
  51. };
  52. }
  53. public static function fullPath(relPath:String):String {
  54. try {
  55. return new File(relPath).getCanonicalPath();
  56. } catch (e:java.io.IOException) {
  57. throw new java.lang.RuntimeException(e);
  58. }
  59. }
  60. public static function absolutePath(relPath:String):String {
  61. if (haxe.io.Path.isAbsolute(relPath))
  62. return relPath;
  63. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  64. }
  65. public static function isDirectory(path:String):Bool {
  66. var f = new File(path);
  67. if (!f.exists())
  68. throw "Path " + path + " doesn't exist";
  69. return f.isDirectory();
  70. }
  71. public static function createDirectory(path:String):Void {
  72. var f = new File(path);
  73. if (!f.isDirectory() && !f.mkdirs())
  74. throw "Cannot create dir " + path;
  75. }
  76. public static function deleteFile(path:String):Void {
  77. if (!new File(path).delete())
  78. throw "Cannot delete file " + path;
  79. }
  80. public static function deleteDirectory(path:String):Void {
  81. if (!new File(path).delete())
  82. throw "Cannot delete directory " + path;
  83. }
  84. public static function readDirectory(path:String):Array<String> {
  85. var f = new File(path);
  86. if (!f.exists())
  87. throw "Path " + path + " doesn't exist";
  88. return Lib.array(f.list());
  89. }
  90. }