FileSystem.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 php.*;
  24. import haxe.io.Path;
  25. private enum FileKind {
  26. kdir;
  27. kfile;
  28. kother(k:String);
  29. }
  30. @:coreApi
  31. class FileSystem {
  32. public static inline function exists(path:String):Bool {
  33. Global.clearstatcache(true, path);
  34. return Global.file_exists(path);
  35. }
  36. public static inline function rename(path:String, newPath:String):Void {
  37. Global.rename(path, newPath);
  38. }
  39. public static function stat(path:String):FileStat {
  40. Global.clearstatcache(true, path);
  41. var info = Global.stat(path);
  42. if (info == false)
  43. throw 'Unable to stat $path';
  44. var info:NativeArray = info;
  45. return {
  46. gid: info['gid'],
  47. uid: info['uid'],
  48. atime: Date.fromTime(info['atime'] * 1000),
  49. mtime: Date.fromTime(info['mtime'] * 1000),
  50. ctime: Date.fromTime(info['ctime'] * 1000),
  51. dev: info['dev'],
  52. ino: info['ino'],
  53. nlink: info['nlink'],
  54. rdev: info['rdev'],
  55. size: info['size'],
  56. mode: info['mode']
  57. };
  58. }
  59. public static inline function fullPath(relPath:String):String {
  60. return Syntax.shortTernary(Global.realpath(relPath), null);
  61. }
  62. public static function absolutePath(relPath:String):String {
  63. if (Path.isAbsolute(relPath))
  64. return relPath;
  65. return Path.join([Sys.getCwd(), relPath]);
  66. }
  67. static function kind(path:String):FileKind {
  68. Global.clearstatcache(true, path);
  69. var kind = Global.filetype(path);
  70. if (kind == false)
  71. throw 'Failed to check file type $path';
  72. switch (kind) {
  73. case "file":
  74. return kfile;
  75. case "dir":
  76. return kdir;
  77. default:
  78. return kother(kind);
  79. }
  80. }
  81. public static function isDirectory(path:String):Bool {
  82. Global.clearstatcache(true, path);
  83. return Global.is_dir(path);
  84. }
  85. public static function createDirectory(path:String):Void {
  86. Global.clearstatcache(true, path);
  87. if (!Global.is_dir(path))
  88. Global.mkdir(path, 493, true);
  89. }
  90. public static inline function deleteFile(path:String):Void {
  91. Global.unlink(path);
  92. }
  93. public static inline function deleteDirectory(path:String):Void {
  94. Global.rmdir(path);
  95. }
  96. public static function readDirectory(path:String):Array<String> {
  97. var list = [];
  98. var dir = Global.opendir(path);
  99. var file;
  100. while ((file = Global.readdir(dir)) != false) {
  101. if (file != '.' && file != '..') {
  102. list.push(file);
  103. }
  104. }
  105. Global.closedir(dir);
  106. return list;
  107. }
  108. }