FileSystem.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C)2005-2015 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. private enum FileKind {
  24. kdir;
  25. kfile;
  26. kother( k : String );
  27. }
  28. @:coreApi
  29. class FileSystem {
  30. public static inline function exists( path : String ) : Bool {
  31. return untyped __call__("file_exists", path);
  32. }
  33. public static inline function rename( path : String, newPath : String ) : Void {
  34. untyped __call__("rename", path, newPath);
  35. }
  36. public static function stat( path : String ) : FileStat {
  37. untyped __php__("$fp = fopen($path, \"r\"); $fstat = fstat($fp); fclose($fp);");
  38. return untyped {
  39. gid : __php__("$fstat['gid']"),
  40. uid : __php__("$fstat['uid']"),
  41. atime : Date.fromTime(__php__("$fstat['atime']")*1000),
  42. mtime : Date.fromTime(__php__("$fstat['mtime']")*1000),
  43. ctime : Date.fromTime(__php__("$fstat['ctime']")*1000),
  44. dev : __php__("$fstat['dev']"),
  45. ino : __php__("$fstat['ino']"),
  46. nlink : __php__("$fstat['nlink']"),
  47. rdev : __php__("$fstat['rdev']"),
  48. size : __php__("$fstat['size']"),
  49. mode : __php__("$fstat['mode']")
  50. };
  51. }
  52. public static inline function fullPath( relPath : String ) : String {
  53. var p = untyped __call__("realpath", relPath);
  54. if (untyped __physeq__(p, false))
  55. return null;
  56. else
  57. return p;
  58. }
  59. public static function absolutePath ( relPath : String ) : String {
  60. if (haxe.io.Path.isAbsolute(relPath)) return relPath;
  61. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  62. }
  63. static function kind( path : String ) : FileKind {
  64. var k = untyped __call__("filetype", path);
  65. switch(k) {
  66. case "file": return kfile;
  67. case "dir": return kdir;
  68. default: return kother(k);
  69. }
  70. }
  71. public static inline function isDirectory( path : String ) : Bool {
  72. return untyped __call__("is_dir", path);
  73. }
  74. public static inline function createDirectory( path : String ) : Void {
  75. var path = haxe.io.Path.addTrailingSlash(path);
  76. var _p = null;
  77. var parts = [];
  78. while (path != (_p = haxe.io.Path.directory(path))) {
  79. parts.unshift(path);
  80. path = _p;
  81. }
  82. for (part in parts) {
  83. if (part.charCodeAt(part.length - 1) != ":".code && !exists(part))
  84. untyped __call__("@mkdir", part, 493); // php default is 0777, neko is 0755
  85. }
  86. }
  87. public static inline function deleteFile( path : String ) : Void {
  88. untyped __call__("@unlink", path);
  89. }
  90. public static inline function deleteDirectory( path : String ) : Void {
  91. untyped __call__("@rmdir", path);
  92. }
  93. public static function readDirectory( path : String ) : Array<String> {
  94. var l = untyped __call__("array");
  95. untyped __php__("$dh = opendir($path);
  96. while (($file = readdir($dh)) !== false) if(\".\" != $file && \"..\" != $file) $l[] = $file;
  97. closedir($dh);");
  98. return untyped __call__("new _hx_array", l);
  99. }
  100. }