dir_access.cpp 11 KB

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