FileSystem.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C)2005-2012 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. /**
  26. This class allows you to get informations about the files and directories.
  27. **/
  28. @:coreApi
  29. class FileSystem {
  30. /**
  31. Tells if the given file or directory exists.
  32. **/
  33. public static function exists( path : String ) : Bool
  34. {
  35. return new File(path).exists();
  36. }
  37. /**
  38. Rename the corresponding file or directory, allow to move it accross directories as well.
  39. **/
  40. public static function rename( path : String, newpath : String ) : Void
  41. {
  42. if (!new File(path).renameTo(new File(newpath)))
  43. {
  44. throw "Cannot rename " + path + " to " + newpath;
  45. }
  46. }
  47. /**
  48. Returns informations for the given file/directory.
  49. **/
  50. public static function stat( path : String ) : FileStat
  51. {
  52. var f = new File(path);
  53. if (!f.exists())
  54. throw "Path " + path + " doesn't exist";
  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. Returns the full path for the given path which is relative to the current working directory.
  71. **/
  72. public static function fullPath( relpath : String ) : String
  73. {
  74. return new File(relpath).getAbsolutePath();
  75. }
  76. /**
  77. Tells if the given path is a directory. Throw an exception if it does not exists or is not accesible.
  78. **/
  79. public static function isDirectory( path : String ) : Bool
  80. {
  81. var f = new File(path);
  82. if (!f.exists())
  83. throw "Path " + path + " doesn't exist";
  84. return f.isDirectory();
  85. }
  86. /**
  87. Create the given directory. Not recursive : the parent directory must exists.
  88. **/
  89. public static function createDirectory( path : String ) : Void
  90. {
  91. if (!new File(path).mkdirs())
  92. throw "Cannot create dir " + path;
  93. }
  94. /**
  95. Delete a given file.
  96. **/
  97. public static function deleteFile( path : String ) : Void
  98. {
  99. if (!new File(path).delete())
  100. throw "Cannot delete file " + path;
  101. }
  102. /**
  103. Delete a given directory.
  104. **/
  105. public static function deleteDirectory( path : String ) : Void
  106. {
  107. if (!new File(path).delete())
  108. throw "Cannot delete directory " + path;
  109. }
  110. /**
  111. Read all the files/directories stored into the given directory.
  112. **/
  113. public static function readDirectory( path : String ) : Array<String>
  114. {
  115. var f = new File(path);
  116. if (!f.exists())
  117. throw "Path " + path + " doesn't exist";
  118. return Lib.array( f.list() );
  119. }
  120. }