Factor out directory finding from prefix detection

This will also be used to locate the `yabridge.tmol` configuration file.
This commit is contained in:
Robbert van der Helm
2020-05-14 19:11:17 +02:00
parent e728dbe5a2
commit 9a82e82c87
3 changed files with 63 additions and 11 deletions
+52
View File
@@ -0,0 +1,52 @@
// yabridge: a Wine VST bridge
// Copyright (C) 2020 Robbert van der Helm
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include <filesystem>
#include <optional>
/**
* Starting from the starting file or directory, go up in the directory
* hierarchy until we find a file named `filename`.
*
* @param filename The name of the file we're looking for. This can also be a
* directory name since directories are also files.
* @param starting_from The directory to start searching in. If this is a file,
* then start searching in the directory the file is located in.
* @param predicate The predicate to use to check if the path matches a file.
* Needed as an easy way to limit the search to directories only since C++17
* does not have any built in coroutines or generators.
*
* @return The path to the *file* found, or `std::nullopt` if the file could not
* be found.
*/
template <typename F = bool(const std::filesystem::path&)>
std::optional<std::filesystem::path> find_dominating_file(
const std::string& filename,
std::filesystem::path starting_dir,
F predicate = std::filesystem::exists) {
while (starting_dir != "") {
const std::filesystem::path candidate = starting_dir / filename;
if (predicate(candidate)) {
return candidate;
}
starting_dir = starting_dir.parent_path();
}
return std::nullopt;
}
+9 -10
View File
@@ -28,6 +28,8 @@
// Generated inside of build directory
#include <src/common/config/config.h>
#include <../common/configuration.h>
namespace bp = boost::process;
namespace fs = boost::filesystem;
@@ -53,16 +55,13 @@ std::string create_logger_prefix(const fs::path& socket_path) {
}
std::optional<fs::path> find_wineprefix() {
// Try to locate the Wine prefix the plugin's .dll file is located in by
// finding the first parent directory that contains a directory named
// `dosdevices`
fs::path wineprefix_path = find_vst_plugin();
while (wineprefix_path != "") {
if (fs::is_directory(wineprefix_path / "dosdevices")) {
return wineprefix_path;
}
wineprefix_path = wineprefix_path.parent_path();
// We need these string conversions because Boost still doesn't use
// std::filesystem paths
std::optional<std::filesystem::path> dosdevices_dir =
find_dominating_file("dosdevices", find_vst_plugin().string(),
std::filesystem::is_directory);
if (dosdevices_dir.has_value()) {
return dosdevices_dir->parent_path().string();
}
return std::nullopt;
+2 -1
View File
@@ -101,7 +101,8 @@ boost::filesystem::path find_vst_plugin();
/**
* Locate the Wine prefix this file is located in, if it is inside of a wine
* prefix.
* prefix. This is done by locating the first parent directory that contains a
* directory named `dosdevices`.
*
* @return Either the path to the Wine prefix (containing the `drive_c?`
* directory), or `std::nullopt` if it is not inside of a wine prefix.