FileSystem.hx 3.3 KB

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