1 /*************************************************************************/
2 /*  dir_access.cpp                                                       */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 "globals.h"
32 #include "os/file_access.h"
33 #include "os/memory.h"
34 #include "os/os.h"
35 
_get_root_path() const36 String DirAccess::_get_root_path() const {
37 
38 	switch (_access_type) {
39 
40 		case ACCESS_RESOURCES: return Globals::get_singleton()->get_resource_path();
41 		case ACCESS_USERDATA: return OS::get_singleton()->get_data_dir();
42 		default: return "";
43 	}
44 
45 	return "";
46 }
_get_root_string() const47 String DirAccess::_get_root_string() const {
48 
49 	switch (_access_type) {
50 
51 		case ACCESS_RESOURCES: return "res://";
52 		case ACCESS_USERDATA: return "user://";
53 		default: return "";
54 	}
55 
56 	return "";
57 }
58 
get_current_drive()59 int DirAccess::get_current_drive() {
60 
61 	String path = get_current_dir().to_lower();
62 	for (int i = 0; i < get_drive_count(); i++) {
63 		String d = get_drive(i).to_lower();
64 		if (path.begins_with(d))
65 			return i;
66 	}
67 
68 	return 0;
69 }
70 
_erase_recursive(DirAccess * da)71 static Error _erase_recursive(DirAccess *da) {
72 
73 	List<String> dirs;
74 	List<String> files;
75 
76 	da->list_dir_begin();
77 	String n = da->get_next();
78 	while (n != String()) {
79 
80 		if (n != "." && n != "..") {
81 
82 			if (da->current_is_dir())
83 				dirs.push_back(n);
84 			else
85 				files.push_back(n);
86 		}
87 
88 		n = da->get_next();
89 	}
90 
91 	da->list_dir_end();
92 
93 	for (List<String>::Element *E = dirs.front(); E; E = E->next()) {
94 
95 		Error err = da->change_dir(E->get());
96 		if (err == OK) {
97 
98 			err = _erase_recursive(da);
99 			if (err) {
100 				print_line("err recurso " + E->get());
101 				return err;
102 			}
103 			err = da->change_dir("..");
104 			if (err) {
105 				print_line("no go back " + E->get());
106 				return err;
107 			}
108 			err = da->remove(da->get_current_dir().plus_file(E->get()));
109 			if (err) {
110 				print_line("no remove dir" + E->get());
111 				return err;
112 			}
113 		} else {
114 			print_line("no change to " + E->get());
115 			return err;
116 		}
117 	}
118 
119 	for (List<String>::Element *E = files.front(); E; E = E->next()) {
120 
121 		Error err = da->remove(da->get_current_dir().plus_file(E->get()));
122 		if (err) {
123 
124 			print_line("no remove file" + E->get());
125 			return err;
126 		}
127 	}
128 
129 	return OK;
130 }
131 
erase_contents_recursive()132 Error DirAccess::erase_contents_recursive() {
133 
134 	return _erase_recursive(this);
135 }
136 
make_dir_recursive(String p_dir)137 Error DirAccess::make_dir_recursive(String p_dir) {
138 
139 	if (p_dir.length() < 1) {
140 		return OK;
141 	};
142 
143 	String full_dir;
144 
145 	if (p_dir.is_rel_path()) {
146 		//append current
147 		full_dir = get_current_dir().plus_file(p_dir);
148 
149 	} else {
150 		full_dir = p_dir;
151 	}
152 
153 	full_dir = full_dir.replace("\\", "/");
154 
155 	//int slices = full_dir.get_slice_count("/");
156 
157 	String base;
158 
159 	if (full_dir.begins_with("res://"))
160 		base = "res://";
161 	else if (full_dir.begins_with("user://"))
162 		base = "user://";
163 	else if (full_dir.begins_with("/"))
164 		base = "/";
165 	else if (full_dir.find(":/") != -1) {
166 		base = full_dir.substr(0, full_dir.find(":/") + 2);
167 	} else {
168 		ERR_FAIL_V(ERR_INVALID_PARAMETER);
169 	}
170 
171 	full_dir = full_dir.replace_first(base, "").simplify_path();
172 
173 	Vector<String> subdirs = full_dir.split("/");
174 
175 	String curpath = base;
176 	for (int i = 0; i < subdirs.size(); i++) {
177 
178 		curpath = curpath.plus_file(subdirs[i]);
179 
180 		Error err = make_dir(curpath);
181 		if (err != OK && err != ERR_ALREADY_EXISTS) {
182 			ERR_FAIL_V(err);
183 		}
184 	}
185 
186 	return OK;
187 }
188 
get_next(bool * p_is_dir)189 String DirAccess::get_next(bool *p_is_dir) {
190 
191 	String next = get_next();
192 	if (p_is_dir)
193 		*p_is_dir = current_is_dir();
194 	return next;
195 }
196 
fix_path(String p_path) const197 String DirAccess::fix_path(String p_path) const {
198 
199 	switch (_access_type) {
200 
201 		case ACCESS_RESOURCES: {
202 
203 			if (Globals::get_singleton()) {
204 				if (p_path.begins_with("res://")) {
205 
206 					String resource_path = Globals::get_singleton()->get_resource_path();
207 					if (resource_path != "") {
208 
209 						return p_path.replace_first("res:/", resource_path);
210 					};
211 					return p_path.replace_first("res://", "");
212 				}
213 			}
214 
215 		} break;
216 		case ACCESS_USERDATA: {
217 
218 			if (p_path.begins_with("user://")) {
219 
220 				String data_dir = OS::get_singleton()->get_data_dir();
221 				if (data_dir != "") {
222 
223 					return p_path.replace_first("user:/", data_dir);
224 				};
225 				return p_path.replace_first("user://", "");
226 			}
227 
228 		} break;
229 		case ACCESS_FILESYSTEM: {
230 
231 			return p_path;
232 		} break;
233 	}
234 
235 	return p_path;
236 }
237 
238 DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX] = { 0, 0, 0 };
239 
create_for_path(const String & p_path)240 DirAccess *DirAccess::create_for_path(const String &p_path) {
241 
242 	DirAccess *da = NULL;
243 	if (p_path.begins_with("res://")) {
244 
245 		da = create(ACCESS_RESOURCES);
246 	} else if (p_path.begins_with("user://")) {
247 
248 		da = create(ACCESS_USERDATA);
249 	} else {
250 
251 		da = create(ACCESS_FILESYSTEM);
252 	}
253 
254 	return da;
255 }
256 
open(const String & p_path,Error * r_error)257 DirAccess *DirAccess::open(const String &p_path, Error *r_error) {
258 
259 	DirAccess *da = create_for_path(p_path);
260 
261 	ERR_FAIL_COND_V(!da, NULL);
262 	Error err = da->change_dir(p_path);
263 	if (r_error)
264 		*r_error = err;
265 	if (err != OK) {
266 		memdelete(da);
267 		return NULL;
268 	}
269 
270 	return da;
271 }
272 
create(AccessType p_access)273 DirAccess *DirAccess::create(AccessType p_access) {
274 
275 	DirAccess *da = create_func[p_access] ? create_func[p_access]() : NULL;
276 	if (da) {
277 		da->_access_type = p_access;
278 	}
279 
280 	return da;
281 };
282 
get_full_path(const String & p_path,AccessType p_access)283 String DirAccess::get_full_path(const String &p_path, AccessType p_access) {
284 
285 	DirAccess *d = DirAccess::create(p_access);
286 	if (!d)
287 		return p_path;
288 
289 	d->change_dir(p_path);
290 	String full = d->get_current_dir();
291 	memdelete(d);
292 	return full;
293 }
294 
copy(String p_from,String p_to)295 Error DirAccess::copy(String p_from, String p_to) {
296 
297 	//printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data());
298 	Error err;
299 	FileAccess *fsrc = FileAccess::open(p_from, FileAccess::READ, &err);
300 
301 	if (err) {
302 
303 		ERR_FAIL_COND_V(err, err);
304 	}
305 
306 	FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE, &err);
307 	if (err) {
308 
309 		fsrc->close();
310 		memdelete(fsrc);
311 		ERR_FAIL_COND_V(err, err);
312 	}
313 
314 	fsrc->seek_end(0);
315 	int size = fsrc->get_pos();
316 	fsrc->seek(0);
317 	err = OK;
318 	while (size--) {
319 
320 		if (fsrc->get_error() != OK) {
321 			err = fsrc->get_error();
322 			break;
323 		}
324 		if (fdst->get_error() != OK) {
325 			err = fdst->get_error();
326 			break;
327 		}
328 
329 		fdst->store_8(fsrc->get_8());
330 	}
331 
332 	memdelete(fsrc);
333 	memdelete(fdst);
334 
335 	return err;
336 }
337 
exists(String p_dir)338 bool DirAccess::exists(String p_dir) {
339 
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 }
345 
DirAccess()346 DirAccess::DirAccess() {
347 
348 	_access_type = ACCESS_FILESYSTEM;
349 }
350 
~DirAccess()351 DirAccess::~DirAccess() {
352 }
353