FileSystem.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 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) throw 'Unable to stat $path';
  43. var info:NativeArray = info;
  44. return {
  45. gid : info['gid'],
  46. uid : info['uid'],
  47. atime : Date.fromTime(info['atime'] * 1000),
  48. mtime : Date.fromTime(info['mtime'] * 1000),
  49. ctime : Date.fromTime(info['ctime'] * 1000),
  50. dev : info['dev'],
  51. ino : info['ino'],
  52. nlink : info['nlink'],
  53. rdev : info['rdev'],
  54. size : info['size'],
  55. mode : info['mode']
  56. };
  57. }
  58. public static inline function fullPath( relPath : String ) : String {
  59. return Syntax.shortTernary(Global.realpath(relPath), null);
  60. }
  61. public static function absolutePath ( relPath : String ) : String {
  62. if (Path.isAbsolute(relPath)) return relPath;
  63. return Path.join([Sys.getCwd(), relPath]);
  64. }
  65. static function kind( path : String ) : FileKind {
  66. Global.clearstatcache(true, path);
  67. var kind = Global.filetype(path);
  68. if (kind == false) throw 'Failed to check file type $path';
  69. switch(kind) {
  70. case "file": return kfile;
  71. case "dir": return kdir;
  72. default: return kother(kind);
  73. }
  74. }
  75. public static function isDirectory( path : String ) : Bool {
  76. Global.clearstatcache(true, path);
  77. return Global.is_dir(path);
  78. }
  79. public static function createDirectory( path : String ) : Void {
  80. Global.clearstatcache(true, path);
  81. if (!Global.is_dir(path))
  82. Global.mkdir(path, 493, true);
  83. }
  84. public static inline function deleteFile( path : String ) : Void {
  85. Global.unlink(path);
  86. }
  87. public static inline function deleteDirectory( path : String ) : Void {
  88. Global.rmdir(path);
  89. }
  90. public static function readDirectory( path : String ) : Array<String> {
  91. var list = [];
  92. var dir = Global.opendir(path);
  93. var file;
  94. while ((file = Global.readdir(dir)) != false) {
  95. if (file != '.' && file != '..') {
  96. list.push(file);
  97. }
  98. }
  99. Global.closedir(dir);
  100. return list;
  101. }
  102. }