dir_access.cpp 11 KB

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