aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/path.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2019-12-05 19:11:09 +0100
committerEelco Dolstra <edolstra@gmail.com>2019-12-10 22:06:05 +0100
commitbbe97dff8b3054d96e758f486f9ce3fa09e64de3 (patch)
tree9dd575bc61ec93de4bc693f00a84fc8686a613f9 /src/libstore/path.cc
parentebd89999c28e25e71048d523f9c4f8b856dee9a9 (diff)
Make the Store API more type-safe
Most functions now take a StorePath argument rather than a Path (which is just an alias for std::string). The StorePath constructor ensures that the path is syntactically correct (i.e. it looks like <store-dir>/<base32-hash>-<name>). Similarly, functions like buildPaths() now take a StorePathWithOutputs, rather than abusing Path by adding a '!<outputs>' suffix. Note that the StorePath type is implemented in Rust. This involves some hackery to allow Rust values to be used directly in C++, via a helper type whose destructor calls the Rust type's drop() function. The main issue is the dynamic nature of C++ move semantics: after we have moved a Rust value, we should not call the drop function on the original value. So when we move a value, we set the original value to bitwise zero, and the destructor only calls drop() if the value is not bitwise zero. This should be sufficient for most types. Also lots of minor cleanups to the C++ API to make it more modern (e.g. using std::optional and std::string_view in some places).
Diffstat (limited to 'src/libstore/path.cc')
-rw-r--r--src/libstore/path.cc99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/libstore/path.cc b/src/libstore/path.cc
new file mode 100644
index 000000000..81ae495a1
--- /dev/null
+++ b/src/libstore/path.cc
@@ -0,0 +1,99 @@
+#include "store-api.hh"
+
+namespace nix {
+
+extern "C" {
+ rust::Result<StorePath> ffi_StorePath_new(rust::StringSlice path, rust::StringSlice storeDir);
+ rust::Result<StorePath> ffi_StorePath_new2(unsigned char hash[20], rust::StringSlice storeDir);
+ rust::Result<StorePath> ffi_StorePath_fromBaseName(rust::StringSlice baseName);
+ rust::String ffi_StorePath_to_string(const StorePath & _this);
+ StorePath ffi_StorePath_clone(const StorePath & _this);
+ rust::StringSlice ffi_StorePath_name(const StorePath & _this);
+}
+
+StorePath StorePath::make(std::string_view path, std::string_view storeDir)
+{
+ return ffi_StorePath_new((rust::StringSlice) path, (rust::StringSlice) storeDir).unwrap();
+}
+
+StorePath StorePath::make(unsigned char hash[20], std::string_view name)
+{
+ return ffi_StorePath_new2(hash, (rust::StringSlice) name).unwrap();
+}
+
+StorePath StorePath::fromBaseName(std::string_view baseName)
+{
+ return ffi_StorePath_fromBaseName((rust::StringSlice) baseName).unwrap();
+}
+
+rust::String StorePath::to_string() const
+{
+ return ffi_StorePath_to_string(*this);
+}
+
+StorePath StorePath::clone() const
+{
+ return ffi_StorePath_clone(*this);
+}
+
+bool StorePath::isDerivation() const
+{
+ return hasSuffix(name(), drvExtension);
+}
+
+std::string_view StorePath::name() const
+{
+ return ffi_StorePath_name(*this);
+}
+
+StorePath Store::parseStorePath(std::string_view path) const
+{
+ return StorePath::make(path, storeDir);
+}
+
+
+StorePathSet Store::parseStorePathSet(const PathSet & paths) const
+{
+ StorePathSet res;
+ for (auto & i : paths) res.insert(parseStorePath(i));
+ return res;
+}
+
+std::string Store::printStorePath(const StorePath & path) const
+{
+ auto s = storeDir + "/";
+ s += (std::string_view) path.to_string();
+ return s;
+}
+
+PathSet Store::printStorePathSet(const StorePathSet & paths) const
+{
+ PathSet res;
+ for (auto & i : paths) res.insert(printStorePath(i));
+ return res;
+}
+
+StorePathSet cloneStorePathSet(const StorePathSet & paths)
+{
+ StorePathSet res;
+ for (auto & p : paths)
+ res.insert(p.clone());
+ return res;
+}
+
+StorePathSet storePathsToSet(const StorePaths & paths)
+{
+ StorePathSet res;
+ for (auto & p : paths)
+ res.insert(p.clone());
+ return res;
+}
+
+StorePathSet singleton(const StorePath & path)
+{
+ StorePathSet res;
+ res.insert(path.clone());
+ return res;
+}
+
+}