| 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 |
#pragma once |
| 28 |
|
| 29 |
#include <filesystem> |
| 30 |
#include <string> |
| 31 |
#include <string_view> |
| 32 |
#include <unordered_map> |
| 33 |
#include <vector> |
| 34 |
|
| 35 |
#include "test_defines.h" |
| 36 |
|
| 37 |
namespace fs { |
| 38 |
|
| 39 |
class Folder { |
| 40 |
public: |
| 41 |
explicit Folder(std::filesystem::path root_path, std::string name) noexcept; |
| 42 |
~Folder() noexcept; |
| 43 |
Folder(Folder const&) = delete; |
| 44 |
Folder& operator=(Folder const&) = delete; |
| 45 |
Folder(Folder&& other) noexcept; |
| 46 |
Folder& operator=(Folder&& other) noexcept; |
| 47 |
|
| 48 |
// Add a manifest to the folder |
| 49 |
std::filesystem::path write_manifest(std::filesystem::path const& name, std::string const& contents); |
| 50 |
|
| 51 |
// close file handle, delete file, remove `name` from managed file list. |
| 52 |
void remove(std::filesystem::path const& name); |
| 53 |
|
| 54 |
// Remove all contents in the path |
| 55 |
void clear() const noexcept; |
| 56 |
|
| 57 |
// copy file into this folder with name `new_name`. Returns the full path of the file that was copied |
| 58 |
std::filesystem::path copy_file(std::filesystem::path const& file, std::filesystem::path const& new_name); |
| 59 |
|
| 60 |
// location of the managed folder |
| 61 |
std::filesystem::path const& location() const { return folder; } |
| 62 |
|
| 63 |
std::vector<std::filesystem::path> get_files() const; |
| 64 |
|
| 65 |
// Create a symlink in this folder to target with the filename set to link_name |
| 66 |
std::filesystem::path add_symlink(std::filesystem::path const& target, std::filesystem::path const& link_name); |
| 67 |
|
| 68 |
private: |
| 69 |
bool actually_created = false; |
| 70 |
std::filesystem::path folder; |
| 71 |
std::vector<std::filesystem::path> added_files; |
| 72 |
|
| 73 |
void insert_file_to_tracking(std::filesystem::path const& name); |
| 74 |
void check_if_first_use(); |
| 75 |
}; |
| 76 |
|
| 77 |
class FileSystemManager { |
| 78 |
fs::Folder root_folder; |
| 79 |
std::unordered_map<ManifestLocation, Folder> folders; |
| 80 |
|
| 81 |
// paths that this folder appears as to the loader (ex /usr/share/vulkan/explicit_layer.d is in the ManifestLocation::icd folder |
| 82 |
std::unordered_map<std::string, ManifestLocation> redirected_paths; |
| 83 |
|
| 84 |
public: |
| 85 |
FileSystemManager(); |
| 86 |
|
| 87 |
Folder& get_folder(ManifestLocation location) noexcept; |
| 88 |
Folder const& get_folder(ManifestLocation location) const noexcept; |
| 89 |
|
| 90 |
// Gets a pointer to the folder that given_path points to. This includes redirected paths as well as the exact path of folders |
| 91 |
Folder* get_folder_for_given_path(std::string_view given_path) noexcept; |
| 92 |
|
| 93 |
bool is_folder_path(std::filesystem::path const& path) const noexcept; |
| 94 |
|
| 95 |
void add_path_redirect(std::filesystem::path const& apparent_path, ManifestLocation location); |
| 96 |
|
| 97 |
// Returns the first redirected path pointing at by a manifest location. Returns the NULL location if one isn't found |
| 98 |
std::filesystem::path get_path_redirect_by_manifest_location(ManifestLocation location) const; |
| 99 |
|
| 100 |
// Returns the real path that a redirected path points to. Returns an empty path if no redirect is found |
| 101 |
std::filesystem::path get_real_path_of_redirected_path(std::filesystem::path const& redirected_path) const; |
| 102 |
}; |
| 103 |
|
| 104 |
} // namespace fs |