FileSystem.hx 3.2 KB

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