From cc26cb33156f53cb66b1f70a9f2430f7a8ceae57 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 23 Feb 2020 20:45:07 +0100 Subject: [PATCH] Improve basedir calculation on Windows Now basedir with backslashes or forward slashes should work. --- src/Config.cpp | 1 + src/Util.cpp | 11 ++++++++++- unittest/test_argument_processing.cpp | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Config.cpp b/src/Config.cpp index 831dd9b1d..0dfb2d071 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -683,6 +683,7 @@ Config::set_item(const std::string& key, m_base_dir = parse_env_string(value); if (!m_base_dir.empty()) { // The empty string means "disable" verify_absolute_path(m_base_dir); + m_base_dir = Util::normalize_absolute_path(m_base_dir); } break; diff --git a/src/Util.cpp b/src/Util.cpp index e59d5a759..abca1d34f 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -119,6 +119,9 @@ common_dir_prefix_length(string_view dir, string_view path) return 0; } + assert(dir[0] == '/'); + assert(path[0] == '/'); + const size_t limit = std::min(dir.length(), path.length()); size_t i = 0; @@ -134,7 +137,7 @@ common_dir_prefix_length(string_view dir, string_view path) do { --i; - } while (i > 0 && i != string_view::npos && dir[i] != '/' && path[i] != '/'); + } while (i > 0 && dir[i] != '/' && path[i] != '/'); return i; } @@ -220,7 +223,13 @@ get_actual_cwd() { char buffer[PATH_MAX]; if (getcwd(buffer, sizeof(buffer))) { +#ifndef _WIN32 return buffer; +#else + std::string cwd = buffer; + std::replace(cwd.begin(), cwd.end(), '\\', '/'); + return cwd; +#endif } else { return {}; } diff --git a/unittest/test_argument_processing.cpp b/unittest/test_argument_processing.cpp index bf0708999..31e90022e 100644 --- a/unittest/test_argument_processing.cpp +++ b/unittest/test_argument_processing.cpp @@ -35,6 +35,7 @@ get_root() #else char volume[4]; // "C:\" GetVolumePathName(Util::get_actual_cwd().c_str(), volume, sizeof(volume)); + volume[2] = '/'; // Since base directory is normalized to forward slashes return volume; #endif } -- 2.47.2