summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-09-23 12:09:44 +0200
committerPaul Cristian Sarbu <paul.cristian.sarbu@huawei.com>2024-10-25 13:00:43 +0200
commit7571f74dbc53e9b4fe3b9000662ca686960db78e (patch)
treeba1adfd9bd5a2024de2c0ddb0e8c9e76e947bdc2 /src
parentbfefe45f8731e10a92f739d630a3c027fc72bc5b (diff)
downloadjustbuild-7571f74dbc53e9b4fe3b9000662ca686960db78e.tar.gz
StorageUtils: Add generation-aware rehashing ID file map
Such a file could be used to store mappings of digests from CAS or Git cache to digests of different hash type that represent same content.
Diffstat (limited to 'src')
-rw-r--r--src/buildtool/crypto/hash_function.hpp11
-rw-r--r--src/buildtool/crypto/hash_info.cpp14
-rw-r--r--src/buildtool/storage/TARGETS1
-rw-r--r--src/buildtool/storage/fs_utils.cpp10
-rw-r--r--src/buildtool/storage/fs_utils.hpp15
5 files changed, 39 insertions, 12 deletions
diff --git a/src/buildtool/crypto/hash_function.hpp b/src/buildtool/crypto/hash_function.hpp
index 9e5a9c0b..530e0b2b 100644
--- a/src/buildtool/crypto/hash_function.hpp
+++ b/src/buildtool/crypto/hash_function.hpp
@@ -95,4 +95,15 @@ class HashFunction {
-> std::optional<std::pair<Hasher::HashDigest, std::uintmax_t>>;
};
+[[nodiscard]] constexpr auto ToString(HashFunction::Type type) noexcept -> const
+ char* {
+ switch (type) {
+ case HashFunction::Type::GitSHA1:
+ return "git-SHA1";
+ case HashFunction::Type::PlainSHA256:
+ return "plain-SHA256";
+ }
+ Ensures(false); // unreachable
+}
+
#endif // INCLUDED_SRC_BUILDTOOL_CRYPTO_HASH_FUNCTION_HPP
diff --git a/src/buildtool/crypto/hash_info.cpp b/src/buildtool/crypto/hash_info.cpp
index 4fb87161..e185d214 100644
--- a/src/buildtool/crypto/hash_info.cpp
+++ b/src/buildtool/crypto/hash_info.cpp
@@ -21,16 +21,6 @@
#include "src/utils/cpp/hex_string.hpp"
namespace {
-[[nodiscard]] inline auto GetHashTypeName(HashFunction::Type type) noexcept
- -> std::string {
- switch (type) {
- case HashFunction::Type::GitSHA1:
- return "GitSHA1";
- case HashFunction::Type::PlainSHA256:
- return "PlainSHA256";
- }
- Ensures(false);
-}
inline constexpr auto kSHA1EmptyGitBlobHash =
"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391";
@@ -91,7 +81,7 @@ auto HashInfo::ValidateInput(HashFunction::Type type,
"HashInfo: hash {} is expected to be {}.\nTrees are "
"not allowed in this mode.",
hash,
- GetHashTypeName(type));
+ ToString(type));
}
if (auto const exp_size = HashFunction{type}.MakeHasher().GetHashLength();
@@ -100,7 +90,7 @@ auto HashInfo::ValidateInput(HashFunction::Type type,
"HashInfo: hash {} is expected to be {}.\n It must have a length "
"of {}, but its length is {}.",
hash,
- GetHashTypeName(type),
+ ToString(type),
exp_size,
hash.size());
}
diff --git a/src/buildtool/storage/TARGETS b/src/buildtool/storage/TARGETS
index 79af1305..73356708 100644
--- a/src/buildtool/storage/TARGETS
+++ b/src/buildtool/storage/TARGETS
@@ -103,6 +103,7 @@
[ "config"
, "storage"
, ["src/buildtool/common", "user_structs"]
+ , ["src/buildtool/crypto", "hash_function"]
, ["src/buildtool/file_system/symlinks_map", "pragma_special"]
]
, "stage": ["src", "buildtool", "storage"]
diff --git a/src/buildtool/storage/fs_utils.cpp b/src/buildtool/storage/fs_utils.cpp
index 171a5d1c..05883814 100644
--- a/src/buildtool/storage/fs_utils.cpp
+++ b/src/buildtool/storage/fs_utils.cpp
@@ -104,6 +104,16 @@ auto GetResolvedTreeIDFile(StorageConfig const& storage_config,
tree_hash;
}
+auto GetRehashIDFile(StorageConfig const& storage_config,
+ HashFunction::Type target_hash_type,
+ std::string const& hash,
+ bool from_git,
+ std::size_t generation) noexcept -> std::filesystem::path {
+ return storage_config.GenerationCacheRoot(generation) /
+ fmt::format("to-{}", ToString(target_hash_type)) /
+ (from_git ? "from-git" : "from-cas") / hash;
+}
+
auto WriteTreeIDFile(std::filesystem::path const& tree_id_file,
std::string const& tree_id) noexcept -> bool {
// needs to be done safely, so use the rename trick
diff --git a/src/buildtool/storage/fs_utils.hpp b/src/buildtool/storage/fs_utils.hpp
index 7482457b..25392b91 100644
--- a/src/buildtool/storage/fs_utils.hpp
+++ b/src/buildtool/storage/fs_utils.hpp
@@ -20,6 +20,7 @@
#include <string>
#include "src/buildtool/common/user_structs.hpp"
+#include "src/buildtool/crypto/hash_function.hpp"
#include "src/buildtool/file_system/symlinks_map/pragma_special.hpp"
#include "src/buildtool/storage/config.hpp"
#include "src/buildtool/storage/storage.hpp"
@@ -73,6 +74,20 @@ namespace StorageUtils {
std::size_t generation = 0) noexcept
-> std::filesystem::path;
+/// \brief Get the path to the file storing the corresponding artifact hashed by
+/// a different hash function.
+/// \param storage_config Storage under which the file is to be found.
+/// \param target_hash_type Hash type to identify mapping target.
+/// \param hash Hash to identify mapping source.
+/// \param from_git Flag to distinguish further mapping source (CAS / GitCAS)
+/// \param generation Further specificity in location of the file.
+[[nodiscard]] auto GetRehashIDFile(StorageConfig const& storage_config,
+ HashFunction::Type target_hash_type,
+ std::string const& hash,
+ bool from_git,
+ std::size_t generation = 0) noexcept
+ -> std::filesystem::path;
+
/// \brief Write a tree id to file. The parent folder of the file must exist!
[[nodiscard]] auto WriteTreeIDFile(std::filesystem::path const& tree_id_file,
std::string const& tree_id) noexcept -> bool;