dir_access.cpp 11 KB

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