dir_access.cpp 12 KB

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