FileSystem.hx 4.1 KB

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