FileSystem.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C)2005-2016 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 lua.lib.lfs.Lfs;
  24. import lua.Io;
  25. import lua.Os;
  26. import lua.Lib;
  27. import lua.Table;
  28. import haxe.io.Path;
  29. class FileSystem {
  30. public static function exists( path : String ) : Bool {
  31. if (path == null) return false;
  32. else{
  33. var f = Io.open(path);
  34. if (f == null) return false;
  35. else {
  36. f.close();
  37. return true;
  38. }
  39. }
  40. }
  41. public inline static function rename( path : String, newPath : String ) : Void {
  42. return Os.rename(path, newPath);
  43. }
  44. public inline static function stat( path : String ) : FileStat {
  45. // the lua lfs attributes command uses a string for "mode".
  46. // we just need to patch it.
  47. var attr : sys.FileStat = cast Lfs.attributes(path);
  48. var lfs_mode : String = cast(attr.mode, String);
  49. var mode = switch(lfs_mode){
  50. case "file" : 0x0100000;
  51. case "directory" : 0x0040000;
  52. case "link" : 0x0120000;
  53. case "socket" : 0x0140000;
  54. case "named pipe" : 0x0010000;
  55. case "char device" : 0x0020000;
  56. case "block device" : 0x0060000;
  57. default : 0x0000000;
  58. }
  59. attr.mode = mode;
  60. return attr;
  61. }
  62. public inline static function fullPath( relPath : String ) : String {
  63. return Path.normalize(absolutePath(relPath));
  64. }
  65. public inline static function absolutePath( relPath : String ) : String {
  66. if (relPath == null) return null;
  67. var pwd = Lfs.currentdir() ;
  68. if (pwd == null) return relPath;
  69. return Path.join([pwd, relPath]);
  70. }
  71. public inline static function deleteFile( path : String ) : Void {
  72. lua.Os.remove(path);
  73. }
  74. public inline static function readDirectory( path : String ) : Array<String> {
  75. var parts : Table<Dynamic, Dynamic> = lua.TableTools.pack(Lfs.dir(path));
  76. var itr = function(){
  77. var res = parts[1](parts[2]);
  78. while(res == "." || res == ".."){
  79. res = parts[1](parts[2]);
  80. }
  81. return res;
  82. }
  83. return lua.Lib.fillArray(itr);
  84. }
  85. public inline static function isDirectory( path : String ) : Bool {
  86. return Lfs.attributes(path, "mode") == "directory";
  87. }
  88. public inline static function deleteDirectory( path : String ) : Void {
  89. Lfs.rmdir(path);
  90. }
  91. public inline static function createDirectory( path : String ) : Void {
  92. Lfs.mkdir(path);
  93. }
  94. }