FileSystem.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (C)2005-2012 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. static function kind( path : String ) : FileKind {
  60. var k = untyped __call__("filetype", path);
  61. switch(k) {
  62. case "file": return kfile;
  63. case "dir": return kdir;
  64. default: return kother(k);
  65. }
  66. }
  67. public static inline function isDirectory( path : String ) : Bool {
  68. return untyped __call__("is_dir", path);
  69. }
  70. public static inline function createDirectory( path : String ) : Void {
  71. var parts = [while ((path = haxe.io.Path.directory(path)) != "") path];
  72. parts.reverse();
  73. for (part in parts) {
  74. if (!exists(part))
  75. untyped __call__("@mkdir", part, 493); // php default is 0777, neko is 0755
  76. }
  77. }
  78. public static inline function deleteFile( path : String ) : Void {
  79. untyped __call__("@unlink", path);
  80. }
  81. public static inline function deleteDirectory( path : String ) : Void {
  82. untyped __call__("@rmdir", path);
  83. }
  84. public static function readDirectory( path : String ) : Array<String> {
  85. var l = untyped __call__("array");
  86. untyped __php__("$dh = opendir($path);
  87. while (($file = readdir($dh)) !== false) if(\".\" != $file && \"..\" != $file) $l[] = $file;
  88. closedir($dh);");
  89. return untyped __call__("new _hx_array", l);
  90. }
  91. }