dir_access.cpp 11 KB

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