aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/local-fs-store.cc
blob: 303c3af27b8decde097af0ab27cc55407b026c4e (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
#include "archive.hh"
#include "fs-accessor.hh"
#include "store-api.hh"

namespace nix {

struct LocalStoreAccessor : public FSAccessor
{
    ref<Store> store;

    LocalStoreAccessor(ref<Store> store) : store(store) { }

    void assertStore(const Path & path)
    {
        Path storePath = toStorePath(path);
        if (!store->isValidPath(storePath))
            throw Error(format("path ‘%1%’ is not a valid store path") % storePath);
    }

    FSAccessor::Stat stat(const Path & path) override
    {
        assertStore(path);

        struct stat st;
        if (lstat(path.c_str(), &st)) {
            if (errno == ENOENT || errno == ENOTDIR) return {Type::tMissing, 0, false};
            throw SysError(format("getting status of ‘%1%’") % path);
        }

        if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode))
            throw Error(format("file ‘%1%’ has unsupported type") % path);

        return {
            S_ISREG(st.st_mode) ? Type::tRegular :
            S_ISLNK(st.st_mode) ? Type::tSymlink :
            Type::tDirectory,
            S_ISREG(st.st_mode) ? (uint64_t) st.st_size : 0,
            S_ISREG(st.st_mode) && st.st_mode & S_IXUSR};
    }

    StringSet readDirectory(const Path & path) override
    {
        assertStore(path);

        auto entries = nix::readDirectory(path);

        StringSet res;
        for (auto & entry : entries)
            res.insert(entry.name);

        return res;
    }

    std::string readFile(const Path & path) override
    {
        assertStore(path);
        return nix::readFile(path);
    }

    std::string readLink(const Path & path) override
    {
        assertStore(path);
        return nix::readLink(path);
    }
};

ref<FSAccessor> LocalFSStore::getFSAccessor()
{
    return make_ref<LocalStoreAccessor>(ref<Store>(shared_from_this()));
}

void LocalFSStore::narFromPath(const Path & path, Sink & sink)
{
    if (!isValidPath(path))
        throw Error(format("path ‘%s’ is not valid") % path);
    dumpPath(path, sink);
}

}