dir_access.cpp 11 KB

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