dir_access.cpp 11 KB

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