FileSystem.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package sys;
  26. import java.io.File;
  27. import java.Lib;
  28. /**
  29. This class allows you to get informations about the files and directories.
  30. **/
  31. @:coreApi
  32. class FileSystem {
  33. /**
  34. Tells if the given file or directory exists.
  35. **/
  36. public static function exists( path : String ) : Bool
  37. {
  38. return new File(path).exists();
  39. }
  40. /**
  41. Rename the corresponding file or directory, allow to move it accross directories as well.
  42. **/
  43. public static function rename( path : String, newpath : String ) : Void
  44. {
  45. if (!new File(path).renameTo(new File(newpath)))
  46. {
  47. throw "Cannot rename " + path + " to " + newpath;
  48. }
  49. }
  50. /**
  51. Returns informations for the given file/directory.
  52. **/
  53. public static function stat( path : String ) : FileStat
  54. {
  55. var f = new File(path);
  56. if (!f.exists())
  57. throw "Path " + path + " doesn't exist";
  58. return {
  59. gid: 0, //java doesn't let you get this info
  60. uid: 0, //same
  61. atime: Date.now(), //same
  62. mtime: Date.fromTime(cast(f.lastModified(), Float)),
  63. ctime: Date.fromTime(cast(f.lastModified(), Float)), //same
  64. size: cast(f.length(), Int), //TODO: maybe change to Int64 for Haxe 3?
  65. dev: 0, //FIXME: not sure what that is
  66. ino: 0, //FIXME: not sure what that is
  67. nlink: 0, //FIXME: not sure what that is
  68. rdev: 0, //FIXME: not sure what that is
  69. mode: 0 //FIXME: not sure what that is
  70. };
  71. }
  72. /**
  73. Returns the full path for the given path which is relative to the current working directory.
  74. **/
  75. public static function fullPath( relpath : String ) : String
  76. {
  77. return new File(relpath).getAbsolutePath();
  78. }
  79. /**
  80. Tells if the given path is a directory. Throw an exception if it does not exists or is not accesible.
  81. **/
  82. public static function isDirectory( path : String ) : Bool
  83. {
  84. var f = new File(path);
  85. if (!f.exists())
  86. throw "Path " + path + " doesn't exist";
  87. return f.isDirectory();
  88. }
  89. /**
  90. Create the given directory. Not recursive : the parent directory must exists.
  91. **/
  92. public static function createDirectory( path : String ) : Void
  93. {
  94. if (!new File(path).mkdir())
  95. throw "Cannot create dir " + path;
  96. }
  97. /**
  98. Delete a given file.
  99. **/
  100. public static function deleteFile( path : String ) : Void
  101. {
  102. if (!new File(path).delete())
  103. throw "Cannot delete file " + path;
  104. }
  105. /**
  106. Delete a given directory.
  107. **/
  108. public static function deleteDirectory( path : String ) : Void
  109. {
  110. if (!new File(path).delete())
  111. throw "Cannot delete directory " + path;
  112. }
  113. /**
  114. Read all the files/directories stored into the given directory.
  115. **/
  116. public static function readDirectory( path : String ) : Array<String>
  117. {
  118. var f = new File(path);
  119. if (!f.exists())
  120. throw "Path " + path + " doesn't exist";
  121. return Lib.array( f.list() );
  122. }
  123. }