FileSystem.hx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. var ret = Os.rename(path, newPath);
  43. if (!ret.success){
  44. throw ret.message;
  45. }
  46. }
  47. public inline static function stat( path : String ) : FileStat {
  48. // the lua lfs attributes command uses a string for "mode".
  49. // we just need to patch it.
  50. var attr : sys.FileStat = cast Lfs.attributes(path);
  51. var lfs_mode : String = cast(attr.mode, String);
  52. var mode = switch(lfs_mode){
  53. case "file" : 0x0100000;
  54. case "directory" : 0x0040000;
  55. case "link" : 0x0120000;
  56. case "socket" : 0x0140000;
  57. case "named pipe" : 0x0010000;
  58. case "char device" : 0x0020000;
  59. case "block device" : 0x0060000;
  60. default : 0x0000000;
  61. }
  62. attr.mode = mode;
  63. return attr;
  64. }
  65. public inline static function fullPath( relPath : String ) : String {
  66. return Path.normalize(absolutePath(relPath));
  67. }
  68. public inline static function absolutePath( relPath : String ) : String {
  69. if (relPath == null) return null;
  70. var pwd = Lfs.currentdir().status;
  71. if (pwd == null) return relPath;
  72. return Path.join([pwd, relPath]);
  73. }
  74. public inline static function deleteFile( path : String ) : Void {
  75. var ret = lua.Os.remove(path);
  76. if (!ret.success){
  77. throw ret.message;
  78. }
  79. }
  80. public inline static function readDirectory( path : String ) : Array<String> {
  81. var parts : Table<Dynamic, Dynamic> = lua.TableTools.pack(Lfs.dir(path));
  82. var itr = function(){
  83. var res = parts[1](parts[2]);
  84. while(res == "." || res == ".."){
  85. res = parts[1](parts[2]);
  86. }
  87. return res;
  88. }
  89. return lua.Lib.fillArray(itr);
  90. }
  91. public inline static function isDirectory( path : String ) : Bool {
  92. return Lfs.attributes(path, "mode") == "directory";
  93. }
  94. public inline static function deleteDirectory( path : String ) : Void {
  95. var ret = Lfs.rmdir(path);
  96. if (ret.status == null){
  97. throw ret.message;
  98. }
  99. }
  100. public inline static function createDirectory( path : String ) : Void {
  101. Lfs.mkdir(path);
  102. }
  103. }