dir_access.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*************************************************************************/
  2. /* dir_access.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "dir_access.h"
  30. #include "os/file_access.h"
  31. #include "os/memory.h"
  32. #include "os/os.h"
  33. #include "globals.h"
  34. String DirAccess::_get_root_path() const {
  35. switch(_access_type) {
  36. case ACCESS_RESOURCES: return Globals::get_singleton()->get_resource_path();
  37. case ACCESS_USERDATA: return OS::get_singleton()->get_data_dir();
  38. default: return "";
  39. }
  40. return "";
  41. }
  42. String DirAccess::_get_root_string() const {
  43. switch(_access_type) {
  44. case ACCESS_RESOURCES: return "res://";
  45. case ACCESS_USERDATA: return "user://";
  46. default: return "";
  47. }
  48. return "";
  49. }
  50. int DirAccess::get_current_drive() {
  51. String path = get_current_dir().to_lower();
  52. for(int i=0;i<get_drive_count();i++) {
  53. String d = get_drive(i).to_lower();
  54. if (path.begins_with(d))
  55. return i;
  56. }
  57. return 0;
  58. }
  59. static Error _erase_recursive(DirAccess *da) {
  60. List<String> dirs;
  61. List<String> files;
  62. da->list_dir_begin();
  63. String n = da->get_next();
  64. while(n!=String()) {
  65. if (n!="." && n!="..") {
  66. if (da->current_is_dir())
  67. dirs.push_back(n);
  68. else
  69. files.push_back(n);
  70. }
  71. n=da->get_next();
  72. }
  73. da->list_dir_end();
  74. for(List<String>::Element *E=dirs.front();E;E=E->next()) {
  75. Error err = da->change_dir(E->get());
  76. if (err==OK) {
  77. err = _erase_recursive(da);
  78. if (err) {
  79. print_line("err recurso "+E->get());
  80. return err;
  81. }
  82. err = da->change_dir("..");
  83. if (err) {
  84. print_line("no go back "+E->get());
  85. return err;
  86. }
  87. err = da->remove(da->get_current_dir().plus_file(E->get()));
  88. if (err) {
  89. print_line("no remove dir"+E->get());
  90. return err;
  91. }
  92. } else {
  93. print_line("no change to "+E->get());
  94. return err;
  95. }
  96. }
  97. for(List<String>::Element *E=files.front();E;E=E->next()) {
  98. Error err = da->remove(da->get_current_dir().plus_file(E->get()));
  99. if (err) {
  100. print_line("no remove file"+E->get());
  101. return err;
  102. }
  103. }
  104. return OK;
  105. }
  106. Error DirAccess::erase_contents_recursive() {
  107. return _erase_recursive(this);
  108. }
  109. Error DirAccess::make_dir_recursive(String p_dir) {
  110. if (p_dir.length() < 1) {
  111. return OK;
  112. };
  113. String full_dir;
  114. Globals* g = Globals::get_singleton();
  115. if (!p_dir.is_abs_path()) {
  116. //append current
  117. String cur = normalize_path(g->globalize_path(get_current_dir()));
  118. if (cur[cur.length()-1] != '/') {
  119. cur = cur + "/";
  120. };
  121. full_dir=(cur+"/"+p_dir).simplify_path();
  122. } else {
  123. //validate and use given
  124. String dir = normalize_path(g->globalize_path(p_dir));
  125. if (dir.length() < 1) {
  126. return OK;
  127. };
  128. if (dir[dir.length()-1] != '/') {
  129. dir = dir + "/";
  130. };
  131. full_dir=dir;
  132. }
  133. //int slices = full_dir.get_slice_count("/");
  134. int pos = 0;
  135. while (pos < full_dir.length()) {
  136. int n = full_dir.find("/", pos);
  137. if (n < 0) {
  138. n = full_dir.length();
  139. };
  140. pos = n + 1;
  141. if (pos > 1) {
  142. String to_create = full_dir.substr(0, pos -1);
  143. //print_line("MKDIR: "+to_create);
  144. Error err = make_dir(to_create);
  145. if (err != OK && err != ERR_ALREADY_EXISTS) {
  146. ERR_FAIL_V(err);
  147. };
  148. };
  149. };
  150. return OK;
  151. };
  152. String DirAccess::normalize_path(const String &p_path) {
  153. static const int max_depth = 64;
  154. int pos_stack[max_depth];
  155. int curr = 0;
  156. int pos = 0;
  157. String cur_dir;
  158. do {
  159. if (curr >= max_depth) {
  160. ERR_PRINT("Directory depth too deep.");
  161. return "";
  162. };
  163. int start = pos;
  164. int next = p_path.find("/", pos);
  165. if (next < 0) {
  166. next = p_path.length() - 1;
  167. };
  168. pos = next + 1;
  169. cur_dir = p_path.substr(start, next - start);
  170. if (cur_dir == "" || cur_dir == ".") {
  171. continue;
  172. };
  173. if (cur_dir == "..") {
  174. if (curr > 0) { // pop a dir
  175. curr -= 2;
  176. };
  177. continue;
  178. };
  179. pos_stack[curr++] = start;
  180. pos_stack[curr++] = next;
  181. } while (pos < p_path.length());
  182. String path;
  183. if (p_path[0] == '/') {
  184. path = "/";
  185. };
  186. int i=0;
  187. while (i < curr) {
  188. int start = pos_stack[i++];
  189. while ( ((i+1)<curr) && (pos_stack[i] == pos_stack[i+1]) ) {
  190. ++i;
  191. };
  192. path = path + p_path.substr(start, (pos_stack[i++] - start) + 1);
  193. };
  194. return path;
  195. };
  196. String DirAccess::get_next(bool* p_is_dir) {
  197. String next=get_next();
  198. if (p_is_dir)
  199. *p_is_dir=current_is_dir();
  200. return next;
  201. }
  202. String DirAccess::fix_path(String p_path) const {
  203. switch(_access_type) {
  204. case ACCESS_RESOURCES: {
  205. if (Globals::get_singleton()) {
  206. if (p_path.begins_with("res://")) {
  207. String resource_path = Globals::get_singleton()->get_resource_path();
  208. if (resource_path != "") {
  209. return p_path.replace("res:/",resource_path);
  210. };
  211. return p_path.replace("res://", "");
  212. }
  213. }
  214. } break;
  215. case ACCESS_USERDATA: {
  216. if (p_path.begins_with("user://")) {
  217. String data_dir=OS::get_singleton()->get_data_dir();
  218. if (data_dir != "") {
  219. return p_path.replace("user:/",data_dir);
  220. };
  221. return p_path.replace("user://", "");
  222. }
  223. } break;
  224. case ACCESS_FILESYSTEM: {
  225. return p_path;
  226. } break;
  227. }
  228. return p_path;
  229. }
  230. DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX]={0,0,0};
  231. DirAccess *DirAccess::create_for_path(const String& p_path) {
  232. DirAccess *da=NULL;
  233. if (p_path.begins_with("res://")) {
  234. da=create(ACCESS_RESOURCES);
  235. } else if (p_path.begins_with("user://")) {
  236. da=create(ACCESS_USERDATA);
  237. } else {
  238. da=create(ACCESS_FILESYSTEM);
  239. }
  240. return da;
  241. }
  242. DirAccess *DirAccess::open(const String& p_path,Error *r_error) {
  243. DirAccess *da=create_for_path(p_path);
  244. ERR_FAIL_COND_V(!da,NULL);
  245. Error err = da->change_dir(p_path);
  246. if (r_error)
  247. *r_error=err;
  248. if (err!=OK) {
  249. memdelete(da);
  250. return NULL;
  251. }
  252. return da;
  253. }
  254. DirAccess *DirAccess::create(AccessType p_access) {
  255. DirAccess * da = create_func[p_access]?create_func[p_access]():NULL;
  256. if (da) {
  257. da->_access_type=p_access;
  258. }
  259. return da;
  260. };
  261. String DirAccess::get_full_path(const String& p_path,AccessType p_access) {
  262. DirAccess *d=DirAccess::create(p_access);
  263. if (!d)
  264. return p_path;
  265. d->change_dir(p_path);
  266. String full=d->get_current_dir();
  267. memdelete(d);
  268. return full;
  269. }
  270. Error DirAccess::copy(String p_from,String p_to) {
  271. //printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data());
  272. Error err;
  273. FileAccess *fsrc = FileAccess::open(p_from, FileAccess::READ,&err);
  274. if (err) {
  275. ERR_FAIL_COND_V( err, err );
  276. }
  277. FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE,&err );
  278. if (err) {
  279. fsrc->close();
  280. memdelete( fsrc );
  281. ERR_FAIL_COND_V( err, err );
  282. }
  283. fsrc->seek_end(0);
  284. int size = fsrc->get_pos();
  285. fsrc->seek(0);
  286. err = OK;
  287. while(size--) {
  288. if (fsrc->get_error()!=OK) {
  289. err= fsrc->get_error();
  290. break;
  291. }
  292. if (fdst->get_error()!=OK) {
  293. err= fdst->get_error();
  294. break;
  295. }
  296. fdst->store_8( fsrc->get_8() );
  297. }
  298. memdelete(fsrc);
  299. memdelete(fdst);
  300. return err;
  301. }
  302. bool DirAccess::exists(String p_dir) {
  303. DirAccess* da = DirAccess::create_for_path(p_dir);
  304. bool valid = da->change_dir(p_dir)==OK;
  305. memdelete(da);
  306. return valid;
  307. }
  308. DirAccess::DirAccess(){
  309. _access_type=ACCESS_FILESYSTEM;
  310. }
  311. DirAccess::~DirAccess() {
  312. }