FileSystem.hx 3.0 KB

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