dir_access.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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-2014 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. static Error _erase_recursive(DirAccess *da) {
  51. List<String> dirs;
  52. List<String> files;
  53. da->list_dir_begin();
  54. String n = da->get_next();
  55. while(n!=String()) {
  56. if (n!="." && n!="..") {
  57. if (da->current_is_dir())
  58. dirs.push_back(n);
  59. else
  60. files.push_back(n);
  61. }
  62. n=da->get_next();
  63. }
  64. da->list_dir_end();
  65. for(List<String>::Element *E=dirs.front();E;E=E->next()) {
  66. Error err = da->change_dir(E->get());
  67. if (err==OK) {
  68. err = _erase_recursive(da);
  69. if (err) {
  70. print_line("err recurso "+E->get());
  71. return err;
  72. }
  73. err = da->change_dir("..");
  74. if (err) {
  75. print_line("no go back "+E->get());
  76. return err;
  77. }
  78. err = da->remove(da->get_current_dir().plus_file(E->get()));
  79. if (err) {
  80. print_line("no remove dir"+E->get());
  81. return err;
  82. }
  83. } else {
  84. print_line("no change to "+E->get());
  85. return err;
  86. }
  87. }
  88. for(List<String>::Element *E=files.front();E;E=E->next()) {
  89. Error err = da->remove(da->get_current_dir().plus_file(E->get()));
  90. if (err) {
  91. print_line("no remove file"+E->get());
  92. return err;
  93. }
  94. }
  95. return OK;
  96. }
  97. Error DirAccess::erase_contents_recursive() {
  98. return _erase_recursive(this);
  99. }
  100. Error DirAccess::make_dir_recursive(String p_dir) {
  101. if (p_dir.length() < 1) {
  102. return OK;
  103. };
  104. String full_dir;
  105. Globals* g = Globals::get_singleton();
  106. if (!p_dir.is_abs_path()) {
  107. //append current
  108. String cur = normalize_path(g->globalize_path(get_current_dir()));
  109. if (cur[cur.length()-1] != '/') {
  110. cur = cur + "/";
  111. };
  112. full_dir=(cur+"/"+p_dir).simplify_path();
  113. } else {
  114. //validate and use given
  115. String dir = normalize_path(g->globalize_path(p_dir));
  116. if (dir.length() < 1) {
  117. return OK;
  118. };
  119. if (dir[dir.length()-1] != '/') {
  120. dir = dir + "/";
  121. };
  122. full_dir=dir;
  123. }
  124. //int slices = full_dir.get_slice_count("/");
  125. int pos = 0;
  126. while (pos < full_dir.length()) {
  127. int n = full_dir.find("/", pos);
  128. if (n < 0) {
  129. n = full_dir.length();
  130. };
  131. pos = n + 1;
  132. if (pos > 1) {
  133. String to_create = full_dir.substr(0, pos -1);
  134. //print_line("MKDIR: "+to_create);
  135. Error err = make_dir(to_create);
  136. if (err != OK && err != ERR_ALREADY_EXISTS) {
  137. ERR_FAIL_V(err);
  138. };
  139. };
  140. };
  141. return OK;
  142. };
  143. String DirAccess::normalize_path(const String &p_path) {
  144. static const int max_depth = 64;
  145. int pos_stack[max_depth];
  146. int curr = 0;
  147. int pos = 0;
  148. String cur_dir;
  149. do {
  150. if (curr >= max_depth) {
  151. ERR_PRINT("Directory depth too deep.");
  152. return "";
  153. };
  154. int start = pos;
  155. int next = p_path.find("/", pos);
  156. if (next < 0) {
  157. next = p_path.length() - 1;
  158. };
  159. pos = next + 1;
  160. cur_dir = p_path.substr(start, next - start);
  161. if (cur_dir == "" || cur_dir == ".") {
  162. continue;
  163. };
  164. if (cur_dir == "..") {
  165. if (curr > 0) { // pop a dir
  166. curr -= 2;
  167. };
  168. continue;
  169. };
  170. pos_stack[curr++] = start;
  171. pos_stack[curr++] = next;
  172. } while (pos < p_path.length());
  173. String path;
  174. if (p_path[0] == '/') {
  175. path = "/";
  176. };
  177. int i=0;
  178. while (i < curr) {
  179. int start = pos_stack[i++];
  180. while ( ((i+1)<curr) && (pos_stack[i] == pos_stack[i+1]) ) {
  181. ++i;
  182. };
  183. path = path + p_path.substr(start, (pos_stack[i++] - start) + 1);
  184. };
  185. return path;
  186. };
  187. String DirAccess::get_next(bool* p_is_dir) {
  188. String next=get_next();
  189. if (p_is_dir)
  190. *p_is_dir=current_is_dir();
  191. return next;
  192. }
  193. String DirAccess::fix_path(String p_path) const {
  194. switch(_access_type) {
  195. case ACCESS_RESOURCES: {
  196. if (Globals::get_singleton()) {
  197. if (p_path.begins_with("res://")) {
  198. String resource_path = Globals::get_singleton()->get_resource_path();
  199. if (resource_path != "") {
  200. return p_path.replace("res:/",resource_path);
  201. };
  202. return p_path.replace("res://", "");
  203. }
  204. }
  205. } break;
  206. case ACCESS_USERDATA: {
  207. if (p_path.begins_with("user://")) {
  208. String data_dir=OS::get_singleton()->get_data_dir();
  209. if (data_dir != "") {
  210. return p_path.replace("user:/",data_dir);
  211. };
  212. return p_path.replace("user://", "");
  213. }
  214. } break;
  215. case ACCESS_FILESYSTEM: {
  216. return p_path;
  217. } break;
  218. }
  219. return p_path;
  220. }
  221. DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX]={0,0,0};
  222. DirAccess *DirAccess::create_for_path(const String& p_path) {
  223. DirAccess *da=NULL;
  224. if (p_path.begins_with("res://")) {
  225. da=create(ACCESS_RESOURCES);
  226. } else if (p_path.begins_with("user://")) {
  227. da=create(ACCESS_USERDATA);
  228. } else {
  229. da=create(ACCESS_FILESYSTEM);
  230. }
  231. return da;
  232. }
  233. DirAccess *DirAccess::open(const String& p_path,Error *r_error) {
  234. DirAccess *da=create_for_path(p_path);
  235. ERR_FAIL_COND_V(!da,NULL);
  236. Error err = da->change_dir(p_path);
  237. if (r_error)
  238. *r_error=err;
  239. if (err!=OK) {
  240. memdelete(da);
  241. return NULL;
  242. }
  243. return da;
  244. }
  245. DirAccess *DirAccess::create(AccessType p_access) {
  246. DirAccess * da = create_func[p_access]?create_func[p_access]():NULL;
  247. if (da) {
  248. da->_access_type=p_access;
  249. }
  250. return da;
  251. };
  252. String DirAccess::get_full_path(const String& p_path,AccessType p_access) {
  253. DirAccess *d=DirAccess::create(p_access);
  254. if (!d)
  255. return p_path;
  256. d->change_dir(p_path);
  257. String full=d->get_current_dir();
  258. memdelete(d);
  259. return full;
  260. }
  261. Error DirAccess::copy(String p_from,String p_to) {
  262. //printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data());
  263. Error err;
  264. FileAccess *fsrc = FileAccess::open(p_from, FileAccess::READ,&err);
  265. if (err) {
  266. ERR_FAIL_COND_V( err, err );
  267. }
  268. FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE,&err );
  269. if (err) {
  270. fsrc->close();
  271. memdelete( fsrc );
  272. ERR_FAIL_COND_V( err, err );
  273. }
  274. fsrc->seek_end(0);
  275. int size = fsrc->get_pos();
  276. fsrc->seek(0);
  277. err = OK;
  278. while(size--) {
  279. if (fsrc->get_error()!=OK) {
  280. err= fsrc->get_error();
  281. break;
  282. }
  283. if (fdst->get_error()!=OK) {
  284. err= fdst->get_error();
  285. break;
  286. }
  287. fdst->store_8( fsrc->get_8() );
  288. }
  289. memdelete(fsrc);
  290. memdelete(fdst);
  291. return err;
  292. }
  293. bool DirAccess::exists(String p_dir) {
  294. DirAccess* da = DirAccess::create_for_path(p_dir);
  295. bool valid = da->change_dir(p_dir)==OK;
  296. memdelete(da);
  297. return valid;
  298. }
  299. DirAccess::DirAccess(){
  300. _access_type=ACCESS_FILESYSTEM;
  301. }
  302. DirAccess::~DirAccess() {
  303. }