| 1 |
/* |
| 2 |
* Copyright (c) 2025 The Khronos Group Inc. |
| 3 |
* Copyright (c) 2025 Valve Corporation |
| 4 |
* Copyright (c) 2025 LunarG, Inc. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and/or associated documentation files (the "Materials"), to |
| 8 |
* deal in the Materials without restriction, including without limitation the |
| 9 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 |
* sell copies of the Materials, and to permit persons to whom the Materials are |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice(s) and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Materials. |
| 15 |
* |
| 16 |
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 |
* |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 23 |
* USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "folder_manager.h" |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <fstream> |
| 31 |
|
| 32 |
#include "gtest/gtest.h" |
| 33 |
|
| 34 |
#include "util/json_writer.h" |
| 35 |
#include "util/env_var_wrapper.h" |
| 36 |
|
| 37 |
namespace fs { |
| 38 |
Folder::Folder(std::filesystem::path root_path, std::string name) noexcept : folder((root_path / name).lexically_normal()) { |
| 39 |
clear(); |
| 40 |
// Don't actually create the folder yet, as we will do it on demand |
| 41 |
} |
| 42 |
Folder::~Folder() noexcept { clear(); } |
| 43 |
Folder::Folder(Folder&& other) noexcept : actually_created(other.actually_created), folder(other.folder) { other.folder.clear(); } |
| 44 |
Folder& Folder::operator=(Folder&& other) noexcept { |
| 45 |
folder = other.folder; |
| 46 |
actually_created = other.actually_created; |
| 47 |
other.folder.clear(); |
| 48 |
return *this; |
| 49 |
} |
| 50 |
|
| 51 |
void Folder::check_if_first_use() { |
| 52 |
if (!actually_created) { |
| 53 |
if (!::testing::internal::InDeathTestChild()) { |
| 54 |
std::error_code err; |
| 55 |
if (!std::filesystem::create_directories(folder, err)) { |
| 56 |
std::cerr << "Failed to create folder " << folder << " because " << err.message() << "\n"; |
| 57 |
} |
| 58 |
} |
| 59 |
actually_created = true; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
std::filesystem::path Folder::write_manifest(std::filesystem::path const& name, std::string const& contents) { |
| 64 |
check_if_first_use(); |
| 65 |
std::filesystem::path out_path = (folder / name).lexically_normal(); |
| 66 |
if (!::testing::internal::InDeathTestChild()) { |
| 67 |
auto file = std::ofstream(out_path, std::ios_base::trunc | std::ios_base::out); |
| 68 |
if (!file) { |
| 69 |
std::cerr << "Failed to create manifest " << name << " at " << out_path << "\n"; |
| 70 |
return out_path; |
| 71 |
} |
| 72 |
file << contents << std::endl; |
| 73 |
} |
| 74 |
insert_file_to_tracking(name); |
| 75 |
return out_path; |
| 76 |
} |
| 77 |
|
| 78 |
// close file handle, delete file, remove `name` from managed file list. |
| 79 |
void Folder::remove(std::filesystem::path const& name) { |
| 80 |
check_if_first_use(); |
| 81 |
std::filesystem::path out_path = folder / name; |
| 82 |
if (!::testing::internal::InDeathTestChild()) { |
| 83 |
std::error_code err; |
| 84 |
if (!std::filesystem::remove(out_path, err)) { |
| 85 |
std::cerr << "Failed to remove file " << name << " at " << out_path << " because " << err.message() << "\n"; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
auto found = std::find(added_files.begin(), added_files.end(), name); |
| 90 |
if (found != added_files.end()) { |
| 91 |
added_files.erase(found); |
| 92 |
} else { |
| 93 |
std::cout << "File " << name << " not in tracked files of folder " << folder << ".\n"; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// copy file into this folder |
| 98 |
std::filesystem::path Folder::copy_file(std::filesystem::path const& file, std::filesystem::path const& new_name) { |
| 99 |
check_if_first_use(); |
| 100 |
insert_file_to_tracking(new_name); |
| 101 |
|
| 102 |
auto new_filepath = folder / new_name; |
| 103 |
if (!::testing::internal::InDeathTestChild()) { |
| 104 |
std::error_code err; |
| 105 |
if (!std::filesystem::copy_file(file, new_filepath, err)) { |
| 106 |
std::cerr << "Failed to copy file " << file << " to " << new_filepath << " because " << err.message() << "\n"; |
| 107 |
} |
| 108 |
} |
| 109 |
return new_filepath; |
| 110 |
} |
| 111 |
|
| 112 |
std::vector<std::filesystem::path> Folder::get_files() const { return added_files; } |
| 113 |
|
| 114 |
std::filesystem::path Folder::add_symlink(std::filesystem::path const& target, std::filesystem::path const& link_name) { |
| 115 |
check_if_first_use(); |
| 116 |
|
| 117 |
if (!::testing::internal::InDeathTestChild()) { |
| 118 |
std::error_code err; |
| 119 |
std::filesystem::create_symlink(target, folder / link_name, err); |
| 120 |
if (err.value() != 0) { |
| 121 |
std::cerr << "Failed to create symlink with target" << target << " with name " << folder / link_name << " because " |
| 122 |
<< err.message() << "\n"; |
| 123 |
} |
| 124 |
} |
| 125 |
insert_file_to_tracking(link_name); |
| 126 |
return folder / link_name; |
| 127 |
} |
| 128 |
void Folder::insert_file_to_tracking(std::filesystem::path const& name) { |
| 129 |
auto found = std::find(added_files.begin(), added_files.end(), name); |
| 130 |
if (found != added_files.end()) { |
| 131 |
std::cout << "Overwriting manifest " << name << ". Was this intended?\n"; |
| 132 |
} else { |
| 133 |
added_files.emplace_back(name); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void Folder::clear() const noexcept { |
| 138 |
if (!::testing::internal::InDeathTestChild()) { |
| 139 |
std::error_code err; |
| 140 |
std::filesystem::remove_all(folder, err); |
| 141 |
if (err.value() != 0) { |
| 142 |
std::cerr << "Failed to remove folder " << folder << " because " << err.message() << "\n"; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
std::filesystem::path category_path_name(ManifestCategory category) { |
| 148 |
if (category == ManifestCategory::settings) return "settings.d"; |
| 149 |
if (category == ManifestCategory::implicit_layer) return "implicit_layer.d"; |
| 150 |
if (category == ManifestCategory::explicit_layer) |
| 151 |
return "explicit_layer.d"; |
| 152 |
else |
| 153 |
return "icd.d"; |
| 154 |
} |
| 155 |
|
| 156 |
FileSystemManager::FileSystemManager() |
| 157 |
: root_folder(std::filesystem::path(TEST_EXECUTION_DIRECTORY), |
| 158 |
std::string(::testing::UnitTest::GetInstance()->GetInstance()->current_test_suite()->name()) + "_" + |
| 159 |
::testing::UnitTest::GetInstance()->current_test_info()->name()) { |
| 160 |
// Clean out test folder in case a previous run's files are still around |
| 161 |
root_folder.clear(); |
| 162 |
|
| 163 |
auto const& root = root_folder.location(); |
| 164 |
folders.try_emplace(ManifestLocation::null, root, std::string("null_dir")); |
| 165 |
folders.try_emplace(ManifestLocation::driver, root, std::string("icd_manifests")); |
| 166 |
folders.try_emplace(ManifestLocation::driver_env_var, root, std::string("icd_env_vars_manifests")); |
| 167 |
folders.try_emplace(ManifestLocation::explicit_layer, root, std::string("explicit_layer_manifests")); |
| 168 |
folders.try_emplace(ManifestLocation::explicit_layer_env_var, root, std::string("explicit_env_var_layer_folder")); |
| 169 |
folders.try_emplace(ManifestLocation::explicit_layer_add_env_var, root, std::string("explicit_add_env_var_layer_folder")); |
| 170 |
folders.try_emplace(ManifestLocation::implicit_layer, root, std::string("implicit_layer_manifests")); |
| 171 |
folders.try_emplace(ManifestLocation::implicit_layer_env_var, root, std::string("implicit_env_var_layer_manifests")); |
| 172 |
folders.try_emplace(ManifestLocation::implicit_layer_add_env_var, root, std::string("implicit_add_env_var_layer_manifests")); |
| 173 |
folders.try_emplace(ManifestLocation::override_layer, root, std::string("override_layer_manifests")); |
| 174 |
#if defined(_WIN32) |
| 175 |
folders.try_emplace(ManifestLocation::windows_app_package, root, std::string("app_package_manifests")); |
| 176 |
#endif |
| 177 |
#if defined(__APPLE__) |
| 178 |
folders.try_emplace(ManifestLocation::macos_bundle, root, std::string("macos_bundle")); |
| 179 |
#endif |
| 180 |
folders.try_emplace(ManifestLocation::settings_location, root, std::string("settings_location")); |
| 181 |
folders.try_emplace(ManifestLocation::unsecured_driver, root, std::string("unsecured_driver")); |
| 182 |
folders.try_emplace(ManifestLocation::unsecured_explicit_layer, root, std::string("unsecured_explicit_layer")); |
| 183 |
folders.try_emplace(ManifestLocation::unsecured_implicit_layer, root, std::string("unsecured_implicit_layer")); |
| 184 |
folders.try_emplace(ManifestLocation::unsecured_settings, root, std::string("unsecured_settings")); |
| 185 |
} |
| 186 |
|
| 187 |
fs::Folder& FileSystemManager::get_folder(ManifestLocation location) noexcept { return folders.at(location); } |
| 188 |
fs::Folder const& FileSystemManager::get_folder(ManifestLocation location) const noexcept { return folders.at(location); } |
| 189 |
|
| 190 |
Folder* FileSystemManager::get_folder_for_given_path(std::string_view given_path) noexcept { |
| 191 |
for (auto const& [manifest_location, folder] : folders) { |
| 192 |
if (folder.location() == given_path) { |
| 193 |
return &folders.at(manifest_location); |
| 194 |
} |
| 195 |
} |
| 196 |
for (auto const& [redirected_path, redirected_location] : redirected_paths) { |
| 197 |
if (redirected_path == given_path) { |
| 198 |
return &folders.at(redirected_location); |
| 199 |
} |
| 200 |
const auto mismatch_pair = |
| 201 |
std::mismatch(given_path.begin(), given_path.end(), redirected_path.begin(), redirected_path.end()); |
| 202 |
if (mismatch_pair.second == redirected_path.end()) return &folders.at(redirected_location); |
| 203 |
} |
| 204 |
return nullptr; |
| 205 |
} |
| 206 |
|
| 207 |
bool FileSystemManager::is_folder_path(std::filesystem::path const& path) const noexcept { |
| 208 |
for (auto const& [location, folder] : folders) { |
| 209 |
if (path == folder.location()) { |
| 210 |
return true; |
| 211 |
} |
| 212 |
} |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
void FileSystemManager::add_path_redirect(std::filesystem::path const& redirected_path, ManifestLocation location) { |
| 217 |
redirected_paths[redirected_path.string()] = location; |
| 218 |
} |
| 219 |
|
| 220 |
std::filesystem::path FileSystemManager::get_real_path_of_redirected_path(std::filesystem::path const& path) const { |
| 221 |
if (redirected_paths.count(path.string()) > 0) { |
| 222 |
return folders.at(redirected_paths.at(path.lexically_normal().string())).location(); |
| 223 |
} |
| 224 |
for (auto const& [redirected_path_str, redirected_location] : redirected_paths) { |
| 225 |
std::filesystem::path redirected_path = redirected_path_str; |
| 226 |
const auto mismatch_pair = std::mismatch(path.begin(), path.end(), redirected_path.begin(), redirected_path.end()); |
| 227 |
if (mismatch_pair.second == redirected_path.end()) return folders.at(redirected_location).location(); |
| 228 |
} |
| 229 |
return std::filesystem::path{}; |
| 230 |
} |
| 231 |
|
| 232 |
std::filesystem::path FileSystemManager::get_path_redirect_by_manifest_location(ManifestLocation location) const { |
| 233 |
for (auto const& [path, redirected_location] : redirected_paths) { |
| 234 |
if (redirected_location == location) { |
| 235 |
return path; |
| 236 |
} |
| 237 |
} |
| 238 |
return folders.at(ManifestLocation::null).location(); |
| 239 |
} |
| 240 |
|
| 241 |
} // namespace fs |