From dccdef1327ed3e6854ba2c9db35ff5669eee831c Mon Sep 17 00:00:00 2001 From: Fabian Mathews Date: Sun, 3 Sep 2017 13:46:05 +0930 Subject: Another take at fixing symlinks --- drivers/unix/dir_access_unix.cpp | 44 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'drivers/unix/dir_access_unix.cpp') diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp index ddc3b2ed33..a4e4c79dbf 100644 --- a/drivers/unix/dir_access_unix.cpp +++ b/drivers/unix/dir_access_unix.cpp @@ -211,36 +211,38 @@ Error DirAccessUnix::make_dir(String p_dir) { Error DirAccessUnix::change_dir(String p_dir) { GLOBAL_LOCK_FUNCTION - p_dir = fix_path(p_dir); - char real_current_dir_name[2048]; - 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? + // 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); + } - chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants - bool worked = (chdir(p_dir.utf8().get_data()) == 0); // we can only give this utf8 + if (p_dir == ".") { + return OK; + } - String base = _get_root_path(); - if (base != "") { + p_dir = fix_path(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; - } + String prev_dir = current_dir; - if (worked) { + if (p_dir.is_rel_path()) { + String next_dir = current_dir + "/" + p_dir; + next_dir = next_dir.simplify_path(); + current_dir = next_dir; + } else { + current_dir = p_dir; + } - getcwd(real_current_dir_name, 2048); - if (current_dir.parse_utf8(real_current_dir_name)) - current_dir = real_current_dir_name; //no utf8, maybe latin? + bool worked = (chdir(current_dir.utf8().get_data()) == 0); // we can only give this utf8 + if (!worked) { + current_dir = prev_dir; + return ERR_INVALID_PARAMETER; } chdir(prev_dir.utf8().get_data()); - return worked ? OK : ERR_INVALID_PARAMETER; + return OK; } String DirAccessUnix::get_current_dir() { -- cgit v1.2.3 From f4994e750df6c8c19035249f7fdf89c02a109bf4 Mon Sep 17 00:00:00 2001 From: supagu Date: Wed, 6 Sep 2017 18:09:27 +0930 Subject: Further symlink fixes --- drivers/unix/dir_access_unix.cpp | 42 +++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 16 deletions(-) (limited to 'drivers/unix/dir_access_unix.cpp') diff --git a/drivers/unix/dir_access_unix.cpp b/drivers/unix/dir_access_unix.cpp index a4e4c79dbf..b85a63a44a 100644 --- a/drivers/unix/dir_access_unix.cpp +++ b/drivers/unix/dir_access_unix.cpp @@ -212,36 +212,46 @@ Error DirAccessUnix::change_dir(String p_dir) { 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); - String prev_dir = current_dir; + // prev_dir is the directory we are changing out of + String prev_dir; + char real_current_dir_name[2048]; + getcwd(real_current_dir_name, 2048); + if (prev_dir.parse_utf8(real_current_dir_name)) + prev_dir = real_current_dir_name; //no utf8, maybe latin? + //print_line("directory we are changing out of (prev_dir): " + prev_dir); + + // try_dir is the directory we are trying to change into + String try_dir = ""; if (p_dir.is_rel_path()) { String next_dir = current_dir + "/" + p_dir; + //print_line("p_dir is relative: " + p_dir + " about to simplfy: " + next_dir); next_dir = next_dir.simplify_path(); - current_dir = next_dir; + try_dir = next_dir; } else { - current_dir = p_dir; + try_dir = p_dir; + //print_line("p_dir is absolute: " + p_dir); + } + + // if try_dir is nothing, it is not changing directory so change it to a "." otherwise chdir will fail + if (try_dir == "") { + try_dir = "."; } - bool worked = (chdir(current_dir.utf8().get_data()) == 0); // we can only give this utf8 + //print_line("directory we are changing in to (try_dir): " + try_dir); + + bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8 if (!worked) { - current_dir = prev_dir; + //print_line("directory does not exist"); return ERR_INVALID_PARAMETER; } + // the directory exists, so set current_dir to try_dir + current_dir = try_dir; chdir(prev_dir.utf8().get_data()); + //print_line("directory exists, setting current_dir to: " + current_dir); return OK; } -- cgit v1.2.3