translates windows drive letters to canonical paths

This commit is contained in:
Nicholas O'Connor 2019-05-15 18:03:15 -07:00
parent 2e1217adf3
commit 66c8175423
3 changed files with 53 additions and 1 deletions

View File

@ -46,7 +46,7 @@ include_directories("${CMAKE_CURRENT_BINARY_DIR}/include")
set(src "${CMAKE_SOURCE_DIR}/src")
lava_create_gutlib(
SUBDIRS bottles config launch
SUBDIRS bottles config launch paths
DEPENDS cog)
lava_create_executable(TARGET cellar

View File

@ -5,5 +5,6 @@
namespace cellar {
namespace paths {
extern std::string translate(std::string in_path, bool lazy = false);
extern std::string resolve_drive_letter(std::string in_path);
}
}

View File

@ -0,0 +1,51 @@
#include <exception>
#include <regex>
#include <string>
#include <unistd.h>
#include <boost/algorithm/string.hpp>
#include "bottles.hpp"
#include "output.hpp"
#include "paths.hpp"
using namespace std;
string cellar::paths::resolve_drive_letter(string in_path) {
bool windows_input;
static regex drive_letter_rgx(R"([a-zA-Z]:\\)");
windows_input = regex_match(in_path.substr(0, 3), drive_letter_rgx);
string out_path = "";
if (windows_input) {
string drive_letter = boost::algorithm::to_lower_copy(in_path.substr(0, 2));
string link_path = "";
link_path.append(bottles::active_bottle.canonical_path);
link_path.append("/dosdevices/");
link_path.append(drive_letter);
char stringbuffer[512];
ssize_t bufflen = readlink(link_path.c_str(), stringbuffer, sizeof(stringbuffer) - 1);
if (bufflen != -1) {
stringbuffer[bufflen] = '\0';
out_path.append(stringbuffer);
} else {
throw runtime_error("readlink isn't having it");
}
string rest_of_path = in_path.substr(3, in_path.size() - 2);
size_t slashpos = rest_of_path.find("\\");
while (slashpos != std::string::npos) {
rest_of_path.replace(0, 1, "/");
slashpos = rest_of_path.find("\\");
}
out_path.append(rest_of_path);
}
return out_path;
}