FileSystem.hx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (C)2005-2018 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 python.lib.Os;
  24. import python.lib.os.Path;
  25. @:coreApi
  26. class FileSystem {
  27. public static function exists( path : String ) : Bool {
  28. return Path.exists(path);
  29. }
  30. public static function stat( path : String ) : sys.FileStat {
  31. var s = Os.stat(path);
  32. return {
  33. gid : s.st_gid,
  34. uid : s.st_uid,
  35. atime : Date.fromTime(s.st_atime),
  36. mtime : Date.fromTime(s.st_mtime),
  37. ctime : Date.fromTime(s.st_ctime),
  38. size : s.st_size,
  39. dev : s.st_dev,
  40. ino : s.st_ino,
  41. nlink : s.st_nlink,
  42. rdev : python.internal.UBuiltins.getattr(s, "st_rdev", 0), // st_rdev is not available on Windows
  43. mode : s.st_mode
  44. }
  45. }
  46. public static function rename( path : String, newPath : String ) : Void {
  47. Os.rename(path, newPath);
  48. }
  49. public static function fullPath( relPath : String ) : String {
  50. return Path.realpath(relPath);
  51. }
  52. public static function absolutePath ( relPath : String ) : String {
  53. if (haxe.io.Path.isAbsolute(relPath)) return relPath;
  54. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  55. }
  56. public static function isDirectory( path : String ) : Bool
  57. {
  58. return Path.isdir(path);
  59. }
  60. public static function createDirectory( path : String ) : Void {
  61. Os.makedirs(path, 511, true);
  62. }
  63. public static function deleteFile( path : String ) : Void {
  64. Os.remove(path);
  65. }
  66. public static function deleteDirectory( path : String ) : Void {
  67. Os.rmdir(path);
  68. }
  69. public static function readDirectory( path : String ) : Array<String> {
  70. return Os.listdir(path);
  71. }
  72. }