FileSystem.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. @: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. return new File(relpath).getAbsolutePath();
  60. }
  61. public static function isDirectory( path : String ) : Bool
  62. {
  63. var f = new File(path);
  64. if (!f.exists())
  65. throw "Path " + path + " doesn't exist";
  66. return f.isDirectory();
  67. }
  68. public static function createDirectory( path : String ) : Void
  69. {
  70. if (!new File(path).mkdirs())
  71. throw "Cannot create dir " + path;
  72. }
  73. public static function deleteFile( path : String ) : Void
  74. {
  75. if (!new File(path).delete())
  76. throw "Cannot delete file " + path;
  77. }
  78. public static function deleteDirectory( path : String ) : Void
  79. {
  80. if (!new File(path).delete())
  81. throw "Cannot delete directory " + path;
  82. }
  83. public static function readDirectory( path : String ) : Array<String>
  84. {
  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. }