2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* dir_access.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "dir_access.h"
|
|
|
|
#include "os/file_access.h"
|
|
|
|
#include "os/memory.h"
|
|
|
|
#include "os/os.h"
|
|
|
|
#include "globals.h"
|
|
|
|
|
|
|
|
|
|
|
|
String DirAccess::_get_root_path() const {
|
|
|
|
|
|
|
|
switch(_access_type) {
|
|
|
|
|
|
|
|
case ACCESS_RESOURCES: return Globals::get_singleton()->get_resource_path();
|
|
|
|
case ACCESS_USERDATA: return OS::get_singleton()->get_data_dir();
|
|
|
|
default: return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
String DirAccess::_get_root_string() const {
|
|
|
|
|
|
|
|
switch(_access_type) {
|
|
|
|
|
|
|
|
case ACCESS_RESOURCES: return "res://";
|
|
|
|
case ACCESS_USERDATA: return "user://";
|
|
|
|
default: return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-05-20 02:36:35 +02:00
|
|
|
int DirAccess::get_current_drive() {
|
|
|
|
|
|
|
|
String path = get_current_dir().to_lower();
|
|
|
|
for(int i=0;i<get_drive_count();i++) {
|
|
|
|
String d = get_drive(i).to_lower();
|
|
|
|
if (path.begins_with(d))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
static Error _erase_recursive(DirAccess *da) {
|
|
|
|
|
|
|
|
List<String> dirs;
|
|
|
|
List<String> files;
|
|
|
|
|
|
|
|
da->list_dir_begin();
|
|
|
|
String n = da->get_next();
|
|
|
|
while(n!=String()) {
|
|
|
|
|
|
|
|
if (n!="." && n!="..") {
|
|
|
|
|
|
|
|
if (da->current_is_dir())
|
|
|
|
dirs.push_back(n);
|
|
|
|
else
|
|
|
|
files.push_back(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
n=da->get_next();
|
|
|
|
}
|
|
|
|
|
|
|
|
da->list_dir_end();
|
|
|
|
|
|
|
|
for(List<String>::Element *E=dirs.front();E;E=E->next()) {
|
|
|
|
|
|
|
|
Error err = da->change_dir(E->get());
|
|
|
|
if (err==OK) {
|
|
|
|
|
|
|
|
err = _erase_recursive(da);
|
|
|
|
if (err) {
|
|
|
|
print_line("err recurso "+E->get());
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
err = da->change_dir("..");
|
|
|
|
if (err) {
|
|
|
|
print_line("no go back "+E->get());
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
err = da->remove(da->get_current_dir().plus_file(E->get()));
|
|
|
|
if (err) {
|
|
|
|
print_line("no remove dir"+E->get());
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print_line("no change to "+E->get());
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(List<String>::Element *E=files.front();E;E=E->next()) {
|
|
|
|
|
|
|
|
Error err = da->remove(da->get_current_dir().plus_file(E->get()));
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
print_line("no remove file"+E->get());
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Error DirAccess::erase_contents_recursive() {
|
|
|
|
|
|
|
|
return _erase_recursive(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Error DirAccess::make_dir_recursive(String p_dir) {
|
|
|
|
|
|
|
|
if (p_dir.length() < 1) {
|
|
|
|
return OK;
|
|
|
|
};
|
2014-09-19 23:39:50 +02:00
|
|
|
|
|
|
|
String full_dir;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
if (p_dir.is_rel_path()) {
|
|
|
|
//append current
|
|
|
|
full_dir=get_current_dir().plus_file(p_dir);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2014-09-19 23:39:50 +02:00
|
|
|
} else {
|
2016-06-18 16:12:08 +02:00
|
|
|
full_dir=p_dir;
|
2014-09-19 23:39:50 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
full_dir=full_dir.replace("\\","/");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
//int slices = full_dir.get_slice_count("/");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
String base;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
if (full_dir.begins_with("res://"))
|
|
|
|
base="res://";
|
|
|
|
else if (full_dir.begins_with("user://"))
|
|
|
|
base="user://";
|
|
|
|
else if (full_dir.begins_with("/"))
|
|
|
|
base="/";
|
|
|
|
else if (full_dir.find(":/")!=-1) {
|
|
|
|
base=full_dir.substr(0,full_dir.find(":/")+2);
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_V(ERR_INVALID_PARAMETER);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
full_dir=full_dir.replace_first(base,"").simplify_path();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
Vector<String> subdirs=full_dir.split("/");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
String curpath=base;
|
|
|
|
for(int i=0;i<subdirs.size();i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
curpath=curpath.plus_file(subdirs[i]);
|
2017-02-13 02:52:02 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
Error err = make_dir(curpath);
|
|
|
|
if (err != OK && err != ERR_ALREADY_EXISTS) {
|
|
|
|
ERR_FAIL_V(err);
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String DirAccess::get_next(bool* p_is_dir) {
|
|
|
|
|
|
|
|
String next=get_next();
|
|
|
|
if (p_is_dir)
|
|
|
|
*p_is_dir=current_is_dir();
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
String DirAccess::fix_path(String p_path) const {
|
|
|
|
|
|
|
|
switch(_access_type) {
|
|
|
|
|
|
|
|
case ACCESS_RESOURCES: {
|
|
|
|
|
|
|
|
if (Globals::get_singleton()) {
|
|
|
|
if (p_path.begins_with("res://")) {
|
|
|
|
|
|
|
|
String resource_path = Globals::get_singleton()->get_resource_path();
|
|
|
|
if (resource_path != "") {
|
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
return p_path.replace_first("res:/",resource_path);
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
2016-06-18 16:12:08 +02:00
|
|
|
return p_path.replace_first("res://", "");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ACCESS_USERDATA: {
|
|
|
|
|
|
|
|
|
|
|
|
if (p_path.begins_with("user://")) {
|
|
|
|
|
|
|
|
String data_dir=OS::get_singleton()->get_data_dir();
|
|
|
|
if (data_dir != "") {
|
|
|
|
|
2016-06-18 16:12:08 +02:00
|
|
|
return p_path.replace_first("user:/",data_dir);
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
2016-06-18 16:12:08 +02:00
|
|
|
return p_path.replace_first("user://", "");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case ACCESS_FILESYSTEM: {
|
|
|
|
|
|
|
|
return p_path;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return p_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DirAccess::CreateFunc DirAccess::create_func[ACCESS_MAX]={0,0,0};
|
|
|
|
|
|
|
|
DirAccess *DirAccess::create_for_path(const String& p_path) {
|
|
|
|
|
|
|
|
DirAccess *da=NULL;
|
|
|
|
if (p_path.begins_with("res://")) {
|
|
|
|
|
|
|
|
da=create(ACCESS_RESOURCES);
|
|
|
|
} else if (p_path.begins_with("user://")) {
|
|
|
|
|
|
|
|
da=create(ACCESS_USERDATA);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
da=create(ACCESS_FILESYSTEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
return da;
|
|
|
|
}
|
|
|
|
|
|
|
|
DirAccess *DirAccess::open(const String& p_path,Error *r_error) {
|
|
|
|
|
|
|
|
DirAccess *da=create_for_path(p_path);
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!da,NULL);
|
|
|
|
Error err = da->change_dir(p_path);
|
|
|
|
if (r_error)
|
|
|
|
*r_error=err;
|
|
|
|
if (err!=OK) {
|
|
|
|
memdelete(da);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return da;
|
|
|
|
}
|
|
|
|
|
|
|
|
DirAccess *DirAccess::create(AccessType p_access) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
DirAccess * da = create_func[p_access]?create_func[p_access]():NULL;
|
|
|
|
if (da) {
|
|
|
|
da->_access_type=p_access;
|
|
|
|
}
|
|
|
|
|
|
|
|
return da;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
String DirAccess::get_full_path(const String& p_path,AccessType p_access) {
|
|
|
|
|
|
|
|
DirAccess *d=DirAccess::create(p_access);
|
|
|
|
if (!d)
|
|
|
|
return p_path;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
d->change_dir(p_path);
|
|
|
|
String full=d->get_current_dir();
|
|
|
|
memdelete(d);
|
|
|
|
return full;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error DirAccess::copy(String p_from,String p_to) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data());
|
|
|
|
Error err;
|
|
|
|
FileAccess *fsrc = FileAccess::open(p_from, FileAccess::READ,&err);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (err) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V( err, err );
|
|
|
|
}
|
|
|
|
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE,&err );
|
|
|
|
if (err) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
fsrc->close();
|
|
|
|
memdelete( fsrc );
|
|
|
|
ERR_FAIL_COND_V( err, err );
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
fsrc->seek_end(0);
|
|
|
|
int size = fsrc->get_pos();
|
|
|
|
fsrc->seek(0);
|
|
|
|
err = OK;
|
|
|
|
while(size--) {
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (fsrc->get_error()!=OK) {
|
|
|
|
err= fsrc->get_error();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (fdst->get_error()!=OK) {
|
|
|
|
err= fdst->get_error();
|
|
|
|
break;
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
fdst->store_8( fsrc->get_8() );
|
|
|
|
}
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
memdelete(fsrc);
|
|
|
|
memdelete(fdst);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-04-12 21:45:59 +02:00
|
|
|
bool DirAccess::exists(String p_dir) {
|
|
|
|
|
|
|
|
DirAccess* da = DirAccess::create_for_path(p_dir);
|
|
|
|
bool valid = da->change_dir(p_dir)==OK;
|
|
|
|
memdelete(da);
|
|
|
|
return valid;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
DirAccess::DirAccess(){
|
|
|
|
|
|
|
|
_access_type=ACCESS_FILESYSTEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DirAccess::~DirAccess() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|