aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/path.cc
blob: cda5f9968180a4a4b54311ffa0b4a731806f8440 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#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;
}

std::pair<std::string_view, StringSet> parsePathWithOutputs(std::string_view s)
{
    size_t n = s.find("!");
    return n == s.npos
        ? std::make_pair(s, std::set<string>())
        : std::make_pair(((std::string_view) s).substr(0, n),
            tokenizeString<std::set<string>>(((std::string_view) s).substr(n + 1), ","));
}

StorePathWithOutputs Store::parsePathWithOutputs(const std::string & s)
{
    auto [path, outputs] = nix::parsePathWithOutputs(s);
    return {parseStorePath(path), std::move(outputs)};
}

}