aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/path.hh
blob: f3ed6ca39cfda2f9d22a8d95066ab8629648d828 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once

#include "rust-ffi.hh"

namespace nix {

/* See path.rs. */
struct StorePath;

class Store;

extern "C" {
    void ffi_StorePath_drop(void *);
    bool ffi_StorePath_less_than(const StorePath & a, const StorePath & b);
    bool ffi_StorePath_eq(const StorePath & a, const StorePath & b);
    void ffi_StorePath_clone_to(const StorePath & _other, StorePath & _this);
    unsigned char * ffi_StorePath_hash_data(const StorePath & p);
}

struct StorePath : rust::Value<3 * sizeof(void *) + 24, ffi_StorePath_drop>
{
    StorePath() = delete;

    static StorePath make(std::string_view path, std::string_view storeDir);

    static StorePath make(unsigned char hash[20], std::string_view name);

    static StorePath fromBaseName(std::string_view baseName);

    rust::String to_string() const;

    bool operator < (const StorePath & other) const
    {
        return ffi_StorePath_less_than(*this, other);
    }

    bool operator == (const StorePath & other) const
    {
        return ffi_StorePath_eq(*this, other);
    }

    bool operator != (const StorePath & other) const
    {
        return !(*this == other);
    }

    StorePath(StorePath && that) = default;

    StorePath(const StorePath & that)
    {
        ffi_StorePath_clone_to(that, *this);
    }

    void operator = (const StorePath & that)
    {
        (rust::Value<3 * sizeof(void *) + 24, ffi_StorePath_drop>::operator = (that));
        ffi_StorePath_clone_to(that, *this);
    }

    StorePath clone() const;

    /* Check whether a file name ends with the extension for
       derivations. */
    bool isDerivation() const;

    std::string_view name() const;

    unsigned char * hashData() const
    {
        return ffi_StorePath_hash_data(*this);
    }

    static StorePath dummy;
};

typedef std::set<StorePath> StorePathSet;
typedef std::vector<StorePath> StorePaths;

StorePathSet cloneStorePathSet(const StorePathSet & paths);
StorePathSet storePathsToSet(const StorePaths & paths);

StorePathSet singleton(const StorePath & path);

/* Size of the hash part of store paths, in base-32 characters. */
const size_t storePathHashLen = 32; // i.e. 160 bits

/* Extension of derivations in the Nix store. */
const std::string drvExtension = ".drv";

enum struct FileIngestionMethod : uint8_t {
    Flat = false,
    Recursive = true
};

struct StorePathWithOutputs
{
    StorePath path;
    std::set<std::string> outputs;

    StorePathWithOutputs(const StorePath & path, const std::set<std::string> & outputs = {})
        : path(path.clone()), outputs(outputs)
    { }

    StorePathWithOutputs(StorePath && path, std::set<std::string> && outputs)
        : path(std::move(path)), outputs(std::move(outputs))
    { }

    StorePathWithOutputs(const StorePathWithOutputs & other)
        : path(other.path.clone()), outputs(other.outputs)
    { }

    std::string to_string(const Store & store) const;
};

std::pair<std::string_view, StringSet> parsePathWithOutputs(std::string_view s);

}

namespace std {

template<> struct hash<nix::StorePath> {
    std::size_t operator()(const nix::StorePath & path) const noexcept
    {
        return * (std::size_t *) path.hashData();
    }
};

}