refactor drive letter

This commit is contained in:
2025-09-27 22:04:25 -05:00
parent 3997dbff79
commit 4f24a1bc1d
4 changed files with 60 additions and 20 deletions

View File

@@ -431,25 +431,8 @@ void ui_server::handle_put_mount_location(const httplib::Request &req,
void ui_server::handle_get_available_locations(httplib::Response &res) {
#if defined(_WIN32)
constexpr std::array<std::string_view, 26U> letters{
"a:", "b:", "c:", "d:", "e:", "f:", "g:", "h:", "i:",
"j:", "k:", "l:", "m:", "n:", "o:", "p:", "q:", "r:",
"s:", "t:", "u:", "v:", "w:", "x:", "y:", "z:",
};
auto available = std::accumulate(
letters.begin(), letters.end(), std::vector<std::string_view>(),
[](auto &&vec, auto &&letter) -> std::vector<std::string_view> {
if (utils::file::directory{utils::path::combine(letter, {"\\"})}
.exists()) {
return vec;
}
vec.emplace_back(letter);
return vec;
});
res.set_content(nlohmann::json(available).dump(), "application/json");
res.set_content(nlohmann::json(utils::get_available_drive_letters()).dump(),
"application/json");
#else // !defined(_WIN32)
res.set_content(nlohmann::json(std::vector<std::string_view>()).dump(),
"application/json");

View File

@@ -332,7 +332,9 @@ protected:
mount_location2 = mount_location;
#if defined(_WIN32)
mount_location = utils::string::to_lower(std::string{"V:"});
auto letter = utils::get_available_drive_letter('d');
ASSERT_TRUE(letter.has_value());
mount_location = utils::string::to_lower(std::string{letter.value()});
#else // !defined(_WIN32)
mount_location = utils::path::combine(test_dir, {"mount"});
ASSERT_TRUE(utils::file::directory(mount_location).create_directory());

View File

@@ -30,6 +30,12 @@ void create_console();
void free_console();
[[nodiscard]] auto get_available_drive_letter(char first = 'a')
-> std::optional<std::string_view>;
[[nodiscard]] auto get_available_drive_letters(char first = 'a')
-> std::vector<std::string_view>;
[[nodiscard]] auto get_local_app_data_directory() -> const std::string &;
[[nodiscard]] auto get_last_error_code() -> DWORD;

View File

@@ -29,6 +29,14 @@
#include "utils/path.hpp"
#include "utils/string.hpp"
namespace {
constexpr std::array<std::string_view, 26U> drive_letters{
"a:", "b:", "c:", "d:", "e:", "f:", "g:", "h:", "i:",
"j:", "k:", "l:", "m:", "n:", "o:", "p:", "q:", "r:",
"s:", "t:", "u:", "v:", "w:", "x:", "y:", "z:",
};
}
namespace repertory::utils {
void create_console() {
if (::AllocConsole() == 0) {
@@ -61,6 +69,47 @@ void create_console() {
void free_console() { ::FreeConsole(); }
auto get_available_drive_letter(char first) -> std::optional<std::string_view> {
const auto *begin = std::ranges::find_if(
drive_letters, [first](auto &&val) { return val.at(0U) == first; });
if (begin == drive_letters.end()) {
begin = drive_letters.begin();
}
auto available =
std::ranges::find_if(begin, drive_letters.end(), [](auto &&val) -> bool {
return not utils::file::directory{utils::path::combine(val, {"\\"})}
.exists();
});
if (available == drive_letters.end()) {
return std::nullopt;
}
return *available;
}
auto get_available_drive_letters(char first) -> std::vector<std::string_view> {
const auto *begin =
std::ranges::find_if(drive_letters, [first](auto &&val) -> bool {
return val.at(0U) == first;
});
if (begin == drive_letters.end()) {
begin = drive_letters.begin();
}
return std::accumulate(
begin, drive_letters.end(), std::vector<std::string_view>(),
[](auto &&vec, auto &&letter) -> auto {
if (utils::file::directory{utils::path::combine(letter, {"\\"})}
.exists()) {
return vec;
}
vec.emplace_back(letter);
return vec;
});
}
auto get_last_error_code() -> DWORD { return ::GetLastError(); }
auto get_local_app_data_directory() -> const std::string & {