dir_access.cpp 10 KB

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