dir_access.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*************************************************************************/
  2. /* dir_access.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "dir_access.h"
  31. #include "globals.h"
  32. #include "os/file_access.h"
  33. #include "os/memory.h"
  34. #include "os/os.h"
  35. String DirAccess::_get_root_path() const {
  36. switch (_access_type) {
  37. case ACCESS_RESOURCES: return Globals::get_singleton()->get_resource_path();
  38. case ACCESS_USERDATA: return OS::get_singleton()->get_data_dir();
  39. default: return "";
  40. }
  41. return "";
  42. }
  43. String DirAccess::_get_root_string() const {
  44. switch (_access_type) {
  45. case ACCESS_RESOURCES: return "res://";
  46. case ACCESS_USERDATA: return "user://";
  47. default: return "";
  48. }
  49. return "";
  50. }
  51. int DirAccess::get_current_drive() {
  52. String path = get_current_dir().to_lower();
  53. for (int i = 0; i < get_drive_count(); i++) {
  54. String d = get_drive(i).to_lower();
  55. if (path.begins_with(d))
  56. return i;
  57. }
  58. return 0;
  59. }
  60. static Error _erase_recursive(DirAccess *da) {
  61. List<String> dirs;
  62. List<String> files;
  63. da->list_dir_begin();
  64. String n = da->get_next();
  65. while (n != String()) {
  66. if (n != "." && n != "..") {
  67. if (da->current_is_dir())
  68. dirs.push_back(n);
  69. else
  70. files.push_back(n);
  71. }
  72. n = da->get_next();
  73. }
  74. da->list_dir_end();
  75. for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
  76. Error err = da->change_dir(E->get());
  77. if (err == OK) {
  78. err = _erase_recursive(da);
  79. if (err) {
  80. print_line("err recurso " + E->get());
  81. return err;
  82. }
  83. err = da->change_dir("..");
  84. if (err) {
  85. print_line("no go back " + E->get());
  86. return err;
  87. }
  88. err = da->remove(da->get_current_dir().plus_file(E->get()));
  89. if (err) {
  90. print_line("no remove dir" + E->get());
  91. return err;
  92. }
  93. } else {
  94. print_line("no change to " + E->get());
  95. return err;
  96. }
  97. }
  98. for (List<String>::Element *E = files.front(); E; E = E->next()) {
  99. Error err = da->remove(da->get_current_dir().plus_file(E->get()));
  100. if (err) {
  101. print_line("no remove file" + E->get());
  102. return err;
  103. }
  104. }
  105. return OK;
  106. }
  107. Error DirAccess::erase_contents_recursive() {
  108. return _erase_recursive(this);
  109. }
  110. Error DirAccess::make_dir_recursive(String p_dir) {
  111. if (p_dir.length() < 1) {
  112. return OK;
  113. };
  114. String full_dir;
  115. if (p_dir.is_rel_path()) {
  116. //append current
  117. full_dir = get_current_dir().plus_file(p_dir);
  118. } else {
  119. full_dir = p_dir;
  120. }
  121. full_dir = full_dir.replace("\\", "/");
  122. //int slices = full_dir.get_slice_count("/");
  123. String base;
  124. if (full_dir.begins_with("res://"))
  125. base = "res://";
  126. else if (full_dir.begins_with("user://"))
  127. base = "user://";
  128. else if (full_dir.begins_with("/"))
  129. base = "/";
  130. else if (full_dir.find(":/") != -1) {
  131. base = full_dir.substr(0, full_dir.find(":/") + 2);
  132. } else {
  133. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  134. }
  135. full_dir = full_dir.replace_first(base, "").simplify_path();
  136. Vector<String> subdirs = full_dir.split("/");
  137. String curpath = base;
  138. for (int i = 0; i < subdirs.size(); i++) {
  139. curpath = curpath.plus_file(subdirs[i]);
  140. Error err = make_dir(curpath);
  141. if (err != OK && err != ERR_ALREADY_EXISTS) {
  142. ERR_FAIL_V(err);
  143. }
  144. }
  145. return OK;
  146. }
  147. String DirAccess::get_next(bool *p_is_dir) {
  148. String next = get_next();
  149. if (p_is_dir)
  150. *p_is_dir = current_is_dir();
  151. return next;
  152. }
  153. String DirAccess::fix_path(String p_path) const {
  154. switch (_access_type) {
  155. case ACCESS_RESOURCES: {
  156. if (Globals::get_singleton()) {
  157. if (p_path.begins_with("res://")) {
  158. String resource_path = Globals::get_singleton()->get_resource_path();
  159. if (resource_path != "") {
  160. return p_path.replace_first("res:/", resource_path);
  161. };
  162. return p_path.replace_first("res://", "");
  163. }
  164. }
  165. } break;
  166. case ACCESS_USERDATA: {
  167. if (p_path.begins_with("user://")) {
  168. String data_dir = OS::get_singleton()->get_data_dir();
  169. if (data_dir != "") {
  170. return p_path.replace_first("user:/", data_dir);
  171. };
  172. return p_path.replace_first("user://", "");
  173. }
  174. } break;
  175. case ACCESS_FILESYSTEM: {
  176. return p_path;
  177. } break;
  178. }
  179. return p_path;
  180. }
  181. DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX] = { 0, 0, 0 };
  182. DirAccess *DirAccess::create_for_path(const String &p_path) {
  183. DirAccess *da = NULL;
  184. if (p_path.begins_with("res://")) {
  185. da = create(ACCESS_RESOURCES);
  186. } else if (p_path.begins_with("user://")) {
  187. da = create(ACCESS_USERDATA);
  188. } else {
  189. da = create(ACCESS_FILESYSTEM);
  190. }
  191. return da;
  192. }
  193. DirAccess *DirAccess::open(const String &p_path, Error *r_error) {
  194. DirAccess *da = create_for_path(p_path);
  195. ERR_FAIL_COND_V(!da, NULL);
  196. Error err = da->change_dir(p_path);
  197. if (r_error)
  198. *r_error = err;
  199. if (err != OK) {
  200. memdelete(da);
  201. return NULL;
  202. }
  203. return da;
  204. }
  205. DirAccess *DirAccess::create(AccessType p_access) {
  206. DirAccess *da = create_func[p_access] ? create_func[p_access]() : NULL;
  207. if (da) {
  208. da->_access_type = p_access;
  209. }
  210. return da;
  211. };
  212. String DirAccess::get_full_path(const String &p_path, AccessType p_access) {
  213. DirAccess *d = DirAccess::create(p_access);
  214. if (!d)
  215. return p_path;
  216. d->change_dir(p_path);
  217. String full = d->get_current_dir();
  218. memdelete(d);
  219. return full;
  220. }
  221. Error DirAccess::copy(String p_from, String p_to) {
  222. //printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data());
  223. Error err;
  224. FileAccess *fsrc = FileAccess::open(p_from, FileAccess::READ, &err);
  225. if (err) {
  226. ERR_FAIL_COND_V(err, err);
  227. }
  228. FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE, &err);
  229. if (err) {
  230. fsrc->close();
  231. memdelete(fsrc);
  232. ERR_FAIL_COND_V(err, err);
  233. }
  234. fsrc->seek_end(0);
  235. int size = fsrc->get_pos();
  236. fsrc->seek(0);
  237. err = OK;
  238. while (size--) {
  239. if (fsrc->get_error() != OK) {
  240. err = fsrc->get_error();
  241. break;
  242. }
  243. if (fdst->get_error() != OK) {
  244. err = fdst->get_error();
  245. break;
  246. }
  247. fdst->store_8(fsrc->get_8());
  248. }
  249. memdelete(fsrc);
  250. memdelete(fdst);
  251. return err;
  252. }
  253. bool DirAccess::exists(String p_dir) {
  254. DirAccess *da = DirAccess::create_for_path(p_dir);
  255. bool valid = da->change_dir(p_dir) == OK;
  256. memdelete(da);
  257. return valid;
  258. }
  259. DirAccess::DirAccess() {
  260. _access_type = ACCESS_FILESYSTEM;
  261. }
  262. DirAccess::~DirAccess() {
  263. }