dir_access.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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-2017 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 GlobalConfig::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. if (p_dir.is_rel_path()) {
  115. //append current
  116. full_dir=get_current_dir().plus_file(p_dir);
  117. } else {
  118. full_dir=p_dir;
  119. }
  120. full_dir=full_dir.replace("\\","/");
  121. //int slices = full_dir.get_slice_count("/");
  122. String base;
  123. if (full_dir.begins_with("res://"))
  124. base="res://";
  125. else if (full_dir.begins_with("user://"))
  126. base="user://";
  127. else if (full_dir.begins_with("/"))
  128. base="/";
  129. else if (full_dir.find(":/")!=-1) {
  130. base=full_dir.substr(0,full_dir.find(":/")+2);
  131. } else {
  132. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  133. }
  134. full_dir=full_dir.replace_first(base,"").simplify_path();
  135. Vector<String> subdirs=full_dir.split("/");
  136. String curpath=base;
  137. for(int i=0;i<subdirs.size();i++) {
  138. curpath=curpath.plus_file(subdirs[i]);
  139. Error err = make_dir(curpath);
  140. if (err != OK && err != ERR_ALREADY_EXISTS) {
  141. ERR_FAIL_V(err);
  142. }
  143. }
  144. return OK;
  145. }
  146. String DirAccess::get_next(bool* p_is_dir) {
  147. String next=get_next();
  148. if (p_is_dir)
  149. *p_is_dir=current_is_dir();
  150. return next;
  151. }
  152. String DirAccess::fix_path(String p_path) const {
  153. switch(_access_type) {
  154. case ACCESS_RESOURCES: {
  155. if (GlobalConfig::get_singleton()) {
  156. if (p_path.begins_with("res://")) {
  157. String resource_path = GlobalConfig::get_singleton()->get_resource_path();
  158. if (resource_path != "") {
  159. return p_path.replace_first("res:/",resource_path);
  160. };
  161. return p_path.replace_first("res://", "");
  162. }
  163. }
  164. } break;
  165. case ACCESS_USERDATA: {
  166. if (p_path.begins_with("user://")) {
  167. String data_dir=OS::get_singleton()->get_data_dir();
  168. if (data_dir != "") {
  169. return p_path.replace_first("user:/",data_dir);
  170. };
  171. return p_path.replace_first("user://", "");
  172. }
  173. } break;
  174. case ACCESS_FILESYSTEM: {
  175. return p_path;
  176. } break;
  177. }
  178. return p_path;
  179. }
  180. DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX]={0,0,0};
  181. DirAccess *DirAccess::create_for_path(const String& p_path) {
  182. DirAccess *da=NULL;
  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(!da,NULL);
  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 NULL;
  201. }
  202. return da;
  203. }
  204. DirAccess *DirAccess::create(AccessType p_access) {
  205. DirAccess * da = create_func[p_access]?create_func[p_access]():NULL;
  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) {
  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_FAIL_COND_V( err, err );
  226. }
  227. FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE,&err );
  228. if (err) {
  229. fsrc->close();
  230. memdelete( fsrc );
  231. ERR_FAIL_COND_V( err, err );
  232. }
  233. fsrc->seek_end(0);
  234. int size = fsrc->get_pos();
  235. fsrc->seek(0);
  236. err = OK;
  237. while(size--) {
  238. if (fsrc->get_error()!=OK) {
  239. err= fsrc->get_error();
  240. break;
  241. }
  242. if (fdst->get_error()!=OK) {
  243. err= fdst->get_error();
  244. break;
  245. }
  246. fdst->store_8( fsrc->get_8() );
  247. }
  248. memdelete(fsrc);
  249. memdelete(fdst);
  250. return err;
  251. }
  252. bool DirAccess::exists(String p_dir) {
  253. DirAccess* da = DirAccess::create_for_path(p_dir);
  254. bool valid = da->change_dir(p_dir)==OK;
  255. memdelete(da);
  256. return valid;
  257. }
  258. DirAccess::DirAccess(){
  259. _access_type=ACCESS_FILESYSTEM;
  260. }
  261. DirAccess::~DirAccess() {
  262. }