dir_access.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*************************************************************************/
  2. /* dir_access.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "os/file_access.h"
  32. #include "os/memory.h"
  33. #include "os/os.h"
  34. #include "project_settings.h"
  35. String DirAccess::_get_root_path() const {
  36. switch (_access_type) {
  37. case ACCESS_RESOURCES: return ProjectSettings::get_singleton()->get_resource_path();
  38. case ACCESS_USERDATA: return OS::get_singleton()->get_user_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. da->change_dir("..");
  82. return err;
  83. }
  84. err = da->change_dir("..");
  85. if (err) {
  86. print_line("no go back " + E->get());
  87. return err;
  88. }
  89. err = da->remove(da->get_current_dir().plus_file(E->get()));
  90. if (err) {
  91. print_line("no remove dir" + E->get());
  92. return err;
  93. }
  94. } else {
  95. print_line("no change to " + E->get());
  96. return err;
  97. }
  98. }
  99. for (List<String>::Element *E = files.front(); E; E = E->next()) {
  100. Error err = da->remove(da->get_current_dir().plus_file(E->get()));
  101. if (err) {
  102. print_line("no remove file" + E->get());
  103. return err;
  104. }
  105. }
  106. return OK;
  107. }
  108. Error DirAccess::erase_contents_recursive() {
  109. return _erase_recursive(this);
  110. }
  111. Error DirAccess::make_dir_recursive(String p_dir) {
  112. if (p_dir.length() < 1) {
  113. return OK;
  114. };
  115. String full_dir;
  116. if (p_dir.is_rel_path()) {
  117. //append current
  118. full_dir = get_current_dir().plus_file(p_dir);
  119. } else {
  120. full_dir = p_dir;
  121. }
  122. full_dir = full_dir.replace("\\", "/");
  123. //int slices = full_dir.get_slice_count("/");
  124. String base;
  125. if (full_dir.begins_with("res://"))
  126. base = "res://";
  127. else if (full_dir.begins_with("user://"))
  128. base = "user://";
  129. else if (full_dir.begins_with("/"))
  130. base = "/";
  131. else if (full_dir.find(":/") != -1) {
  132. base = full_dir.substr(0, full_dir.find(":/") + 2);
  133. } else {
  134. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  135. }
  136. full_dir = full_dir.replace_first(base, "").simplify_path();
  137. Vector<String> subdirs = full_dir.split("/");
  138. String curpath = base;
  139. for (int i = 0; i < subdirs.size(); i++) {
  140. curpath = curpath.plus_file(subdirs[i]);
  141. Error err = make_dir(curpath);
  142. if (err != OK && err != ERR_ALREADY_EXISTS) {
  143. ERR_FAIL_V(err);
  144. }
  145. }
  146. return OK;
  147. }
  148. String DirAccess::get_next(bool *p_is_dir) {
  149. String next = get_next();
  150. if (p_is_dir)
  151. *p_is_dir = current_is_dir();
  152. return next;
  153. }
  154. String DirAccess::fix_path(String p_path) const {
  155. switch (_access_type) {
  156. case ACCESS_RESOURCES: {
  157. if (ProjectSettings::get_singleton()) {
  158. if (p_path.begins_with("res://")) {
  159. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  160. if (resource_path != "") {
  161. return p_path.replace_first("res:/", resource_path);
  162. };
  163. return p_path.replace_first("res://", "");
  164. }
  165. }
  166. } break;
  167. case ACCESS_USERDATA: {
  168. if (p_path.begins_with("user://")) {
  169. String data_dir = OS::get_singleton()->get_user_data_dir();
  170. if (data_dir != "") {
  171. return p_path.replace_first("user:/", data_dir);
  172. };
  173. return p_path.replace_first("user://", "");
  174. }
  175. } break;
  176. case ACCESS_FILESYSTEM: {
  177. return p_path;
  178. } break;
  179. }
  180. return p_path;
  181. }
  182. DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX] = { 0, 0, 0 };
  183. DirAccess *DirAccess::create_for_path(const String &p_path) {
  184. DirAccess *da = NULL;
  185. if (p_path.begins_with("res://")) {
  186. da = create(ACCESS_RESOURCES);
  187. } else if (p_path.begins_with("user://")) {
  188. da = create(ACCESS_USERDATA);
  189. } else {
  190. da = create(ACCESS_FILESYSTEM);
  191. }
  192. return da;
  193. }
  194. DirAccess *DirAccess::open(const String &p_path, Error *r_error) {
  195. DirAccess *da = create_for_path(p_path);
  196. ERR_FAIL_COND_V(!da, NULL);
  197. Error err = da->change_dir(p_path);
  198. if (r_error)
  199. *r_error = err;
  200. if (err != OK) {
  201. memdelete(da);
  202. return NULL;
  203. }
  204. return da;
  205. }
  206. DirAccess *DirAccess::create(AccessType p_access) {
  207. DirAccess *da = create_func[p_access] ? create_func[p_access]() : NULL;
  208. if (da) {
  209. da->_access_type = p_access;
  210. }
  211. return da;
  212. };
  213. String DirAccess::get_full_path(const String &p_path, AccessType p_access) {
  214. DirAccess *d = DirAccess::create(p_access);
  215. if (!d)
  216. return p_path;
  217. d->change_dir(p_path);
  218. String full = d->get_current_dir();
  219. memdelete(d);
  220. return full;
  221. }
  222. Error DirAccess::copy(String p_from, String p_to, int p_chmod_flags) {
  223. //printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data());
  224. Error err;
  225. FileAccess *fsrc = FileAccess::open(p_from, FileAccess::READ, &err);
  226. if (err) {
  227. ERR_FAIL_COND_V(err, err);
  228. }
  229. FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE, &err);
  230. if (err) {
  231. fsrc->close();
  232. memdelete(fsrc);
  233. ERR_FAIL_COND_V(err, err);
  234. }
  235. fsrc->seek_end(0);
  236. int size = fsrc->get_position();
  237. fsrc->seek(0);
  238. err = OK;
  239. while (size--) {
  240. if (fsrc->get_error() != OK) {
  241. err = fsrc->get_error();
  242. break;
  243. }
  244. if (fdst->get_error() != OK) {
  245. err = fdst->get_error();
  246. break;
  247. }
  248. fdst->store_8(fsrc->get_8());
  249. }
  250. if (err == OK && p_chmod_flags != -1) {
  251. fdst->close();
  252. err = fdst->_chmod(p_to, p_chmod_flags);
  253. // If running on a platform with no chmod support (i.e., Windows), don't fail
  254. if (err == ERR_UNAVAILABLE)
  255. err = OK;
  256. }
  257. memdelete(fsrc);
  258. memdelete(fdst);
  259. return err;
  260. }
  261. // Changes dir for the current scope, returning back to the original dir
  262. // when scope exits
  263. class DirChanger {
  264. DirAccess *da;
  265. String original_dir;
  266. public:
  267. DirChanger(DirAccess *p_da, String p_dir) {
  268. da = p_da;
  269. original_dir = p_da->get_current_dir();
  270. p_da->change_dir(p_dir);
  271. }
  272. ~DirChanger() {
  273. da->change_dir(original_dir);
  274. }
  275. };
  276. Error DirAccess::_copy_dir(DirAccess *p_target_da, String p_to, int p_chmod_flags) {
  277. List<String> dirs;
  278. String curdir = get_current_dir();
  279. list_dir_begin();
  280. String n = get_next();
  281. while (n != String()) {
  282. if (n != "." && n != "..") {
  283. if (current_is_dir())
  284. dirs.push_back(n);
  285. else {
  286. String rel_path = n;
  287. if (!n.is_rel_path()) {
  288. list_dir_end();
  289. return ERR_BUG;
  290. }
  291. Error err = copy(get_current_dir() + "/" + n, p_to + rel_path, p_chmod_flags);
  292. if (err) {
  293. list_dir_end();
  294. return err;
  295. }
  296. }
  297. }
  298. n = get_next();
  299. }
  300. list_dir_end();
  301. for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
  302. String rel_path = E->get();
  303. String target_dir = p_to + rel_path;
  304. if (!p_target_da->dir_exists(target_dir)) {
  305. Error err = p_target_da->make_dir(target_dir);
  306. ERR_FAIL_COND_V(err, err);
  307. }
  308. Error err = change_dir(E->get());
  309. ERR_FAIL_COND_V(err, err);
  310. err = _copy_dir(p_target_da, p_to + rel_path + "/", p_chmod_flags);
  311. if (err) {
  312. change_dir("..");
  313. ERR_PRINT("Failed to copy recursively");
  314. return err;
  315. }
  316. err = change_dir("..");
  317. if (err) {
  318. ERR_PRINT("Failed to go back");
  319. return err;
  320. }
  321. }
  322. return OK;
  323. }
  324. Error DirAccess::copy_dir(String p_from, String p_to, int p_chmod_flags) {
  325. ERR_FAIL_COND_V(!dir_exists(p_from), ERR_FILE_NOT_FOUND);
  326. DirAccess *target_da = DirAccess::create_for_path(p_to);
  327. ERR_FAIL_COND_V(!target_da, ERR_CANT_CREATE);
  328. if (!target_da->dir_exists(p_to)) {
  329. Error err = target_da->make_dir_recursive(p_to);
  330. if (err) {
  331. memdelete(target_da);
  332. }
  333. ERR_FAIL_COND_V(err, err);
  334. }
  335. DirChanger dir_changer(this, p_from);
  336. Error err = _copy_dir(target_da, p_to + "/", p_chmod_flags);
  337. memdelete(target_da);
  338. return err;
  339. }
  340. bool DirAccess::exists(String p_dir) {
  341. DirAccess *da = DirAccess::create_for_path(p_dir);
  342. bool valid = da->change_dir(p_dir) == OK;
  343. memdelete(da);
  344. return valid;
  345. }
  346. DirAccess::DirAccess() {
  347. _access_type = ACCESS_FILESYSTEM;
  348. }
  349. DirAccess::~DirAccess() {
  350. }