FileSystem.hx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C)2005-2019 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(1000 * s.st_atime),
  36. mtime: Date.fromTime(1000 * s.st_mtime),
  37. ctime: Date.fromTime(1000 * 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))
  54. return relPath;
  55. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  56. }
  57. public static function isDirectory(path:String):Bool {
  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. }