Reworked change_dir to support symlinks
This commit is contained in:
parent
a0b0dff6fd
commit
d3b7d42784
@ -222,36 +222,37 @@ Error DirAccessUnix::make_dir(String p_dir) {
|
|||||||
Error DirAccessUnix::change_dir(String p_dir) {
|
Error DirAccessUnix::change_dir(String p_dir) {
|
||||||
|
|
||||||
GLOBAL_LOCK_FUNCTION
|
GLOBAL_LOCK_FUNCTION
|
||||||
|
|
||||||
|
// make sure current_dir is valid absolute path
|
||||||
|
if (current_dir == "." || current_dir == "") {
|
||||||
|
char real_current_dir_name[2048];
|
||||||
|
getcwd(real_current_dir_name, 2048);
|
||||||
|
current_dir.parse_utf8(real_current_dir_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p_dir == ".") {
|
||||||
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
p_dir = fix_path(p_dir);
|
p_dir = fix_path(p_dir);
|
||||||
|
|
||||||
char real_current_dir_name[2048];
|
String prev_dir = current_dir;
|
||||||
getcwd(real_current_dir_name, 2048);
|
|
||||||
String prev_dir;
|
|
||||||
if (prev_dir.parse_utf8(real_current_dir_name))
|
|
||||||
prev_dir = real_current_dir_name; //no utf8, maybe latin?
|
|
||||||
|
|
||||||
chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants
|
if (p_dir.is_rel_path()) {
|
||||||
bool worked = (chdir(p_dir.utf8().get_data()) == 0); // we can only give this utf8
|
String next_dir = current_dir + "/" + p_dir;
|
||||||
|
next_dir = next_dir.simplify_path();
|
||||||
String base = _get_root_path();
|
current_dir = next_dir;
|
||||||
if (base != "") {
|
} else {
|
||||||
|
current_dir = p_dir;
|
||||||
getcwd(real_current_dir_name, 2048);
|
|
||||||
String new_dir;
|
|
||||||
new_dir.parse_utf8(real_current_dir_name);
|
|
||||||
if (!new_dir.begins_with(base))
|
|
||||||
worked = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (worked) {
|
bool worked = (chdir(current_dir.utf8().get_data()) == 0); // we can only give this utf8
|
||||||
|
if (!worked) {
|
||||||
getcwd(real_current_dir_name, 2048);
|
current_dir = prev_dir;
|
||||||
if (current_dir.parse_utf8(real_current_dir_name))
|
return ERR_INVALID_PARAMETER;
|
||||||
current_dir = real_current_dir_name; //no utf8, maybe latin?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
chdir(prev_dir.utf8().get_data());
|
return OK;
|
||||||
return worked ? OK : ERR_INVALID_PARAMETER;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String DirAccessUnix::get_current_dir() {
|
String DirAccessUnix::get_current_dir() {
|
||||||
|
Loading…
Reference in New Issue
Block a user