2
0

FileSystem.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C)2005-2019 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 cs.system.io.DirectoryInfo;
  24. import cs.system.io.File;
  25. import cs.system.io.Directory;
  26. import cs.system.io.FileInfo;
  27. @:coreApi
  28. class FileSystem {
  29. public static function exists(path:String):Bool {
  30. return (File.Exists(path) || Directory.Exists(path));
  31. }
  32. public static function rename(path:String, newPath:String):Void {
  33. Directory.Move(path, newPath);
  34. }
  35. @:access(Date.fromNative)
  36. public static function stat(path:String):FileStat {
  37. if (File.Exists(path)) {
  38. var fi = new FileInfo(path);
  39. return {
  40. gid: 0, // C# doesn't let you get this info
  41. uid: 0, // same
  42. atime: Date.fromNative(fi.LastAccessTime),
  43. mtime: Date.fromNative(fi.LastWriteTime),
  44. ctime: Date.fromNative(fi.CreationTime),
  45. size: cast(fi.Length, Int), // TODO: maybe change to Int64 for Haxe 3?
  46. dev: 0, // FIXME: not sure what that is
  47. ino: 0, // FIXME: not sure what that is
  48. nlink: 0, // FIXME: not sure what that is
  49. rdev: 0, // FIXME: not sure what that is
  50. mode: 0 // FIXME: not sure what that is
  51. };
  52. } else if (Directory.Exists(path)) {
  53. var fi = new DirectoryInfo(path);
  54. return {
  55. gid: 0, // C# doesn't let you get this info
  56. uid: 0, // same
  57. atime: Date.fromNative(fi.LastAccessTime),
  58. mtime: Date.fromNative(fi.LastWriteTime),
  59. ctime: Date.fromNative(fi.CreationTime),
  60. size: 0, // TODO: maybe change to Int64 for Haxe 3?
  61. dev: 0, // FIXME: not sure what that is
  62. ino: 0, // FIXME: not sure what that is
  63. nlink: 0, // FIXME: not sure what that is
  64. rdev: 0, // FIXME: not sure what that is
  65. mode: 0 // FIXME: not sure what that is
  66. };
  67. } else {
  68. throw "Path '" + path + "' doesn't exist";
  69. }
  70. }
  71. public static function fullPath(relPath:String):String {
  72. return new FileInfo(relPath).FullName;
  73. }
  74. public static function absolutePath(relPath:String):String {
  75. if (haxe.io.Path.isAbsolute(relPath))
  76. return relPath;
  77. return haxe.io.Path.join([Sys.getCwd(), relPath]);
  78. }
  79. public static function isDirectory(path:String):Bool {
  80. var isdir = Directory.Exists(path);
  81. if (isdir != File.Exists(path))
  82. return isdir;
  83. throw "Path '" + path + "' doesn't exist";
  84. }
  85. public static function createDirectory(path:String):Void {
  86. Directory.CreateDirectory(path);
  87. }
  88. public static function deleteFile(path:String):Void {
  89. if (!File.Exists(path))
  90. throw "Path '" + path + "' doesn't exist";
  91. File.Delete(path);
  92. }
  93. public static function deleteDirectory(path:String):Void {
  94. if (!Directory.Exists(path))
  95. throw "Path '" + path + "' doesn't exist";
  96. Directory.Delete(path);
  97. }
  98. public static function readDirectory(path:String):Array<String> {
  99. var ret = Directory.GetFileSystemEntries(path);
  100. if (ret.Length > 0) {
  101. var fst = ret[0];
  102. var sep = "/";
  103. if (fst.lastIndexOf(sep) < fst.lastIndexOf("\\"))
  104. sep = "\\";
  105. for (i in 0...ret.Length) {
  106. var path = ret[i];
  107. ret[i] = path.substr(path.lastIndexOf(sep) + 1);
  108. }
  109. }
  110. return cs.Lib.array(ret);
  111. }
  112. }