2016-03-21 18:55:57 +02:00
|
|
|
|
#include "archive.hh"
|
2016-02-25 18:43:19 +02:00
|
|
|
|
#include "fs-accessor.hh"
|
|
|
|
|
#include "store-api.hh"
|
2016-06-02 14:33:49 +03:00
|
|
|
|
#include "globals.hh"
|
2016-02-25 18:43:19 +02:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
2016-06-02 14:33:49 +03:00
|
|
|
|
LocalFSStore::LocalFSStore(const Params & params)
|
|
|
|
|
: Store(params)
|
2016-07-27 18:47:18 +03:00
|
|
|
|
, rootDir(get(params, "root"))
|
|
|
|
|
, stateDir(canonPath(get(params, "state", rootDir != "" ? rootDir + "/nix/var/nix" : settings.nixStateDir)))
|
|
|
|
|
, logDir(canonPath(get(params, "log", rootDir != "" ? rootDir + "/nix/var/log/nix" : settings.nixLogDir)))
|
2016-06-02 14:33:49 +03:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 18:43:19 +02:00
|
|
|
|
struct LocalStoreAccessor : public FSAccessor
|
|
|
|
|
{
|
2016-06-02 19:43:36 +03:00
|
|
|
|
ref<LocalFSStore> store;
|
2016-02-25 18:43:19 +02:00
|
|
|
|
|
2016-06-02 19:43:36 +03:00
|
|
|
|
LocalStoreAccessor(ref<LocalFSStore> store) : store(store) { }
|
2016-02-25 18:43:19 +02:00
|
|
|
|
|
2016-06-02 19:43:36 +03:00
|
|
|
|
Path toRealPath(const Path & path)
|
2016-02-25 18:43:19 +02:00
|
|
|
|
{
|
2016-06-01 15:49:12 +03:00
|
|
|
|
Path storePath = store->toStorePath(path);
|
2016-02-25 18:43:19 +02:00
|
|
|
|
if (!store->isValidPath(storePath))
|
|
|
|
|
throw Error(format("path ‘%1%’ is not a valid store path") % storePath);
|
2016-06-02 19:43:36 +03:00
|
|
|
|
return store->getRealStoreDir() + std::string(path, store->storeDir.size());
|
2016-02-25 18:43:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FSAccessor::Stat stat(const Path & path) override
|
|
|
|
|
{
|
2016-06-02 19:43:36 +03:00
|
|
|
|
auto realPath = toRealPath(path);
|
2016-02-25 18:43:19 +02:00
|
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(path.c_str(), &st)) {
|
2016-03-23 12:17:46 +02:00
|
|
|
|
if (errno == ENOENT || errno == ENOTDIR) return {Type::tMissing, 0, false};
|
2016-02-25 18:43:19 +02:00
|
|
|
|
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
|
|
|
|
|
{
|
2016-06-02 19:43:36 +03:00
|
|
|
|
auto realPath = toRealPath(path);
|
2016-02-25 18:43:19 +02:00
|
|
|
|
|
|
|
|
|
auto entries = nix::readDirectory(path);
|
|
|
|
|
|
|
|
|
|
StringSet res;
|
|
|
|
|
for (auto & entry : entries)
|
|
|
|
|
res.insert(entry.name);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string readFile(const Path & path) override
|
|
|
|
|
{
|
2016-06-02 19:43:36 +03:00
|
|
|
|
return nix::readFile(toRealPath(path));
|
2016-02-25 18:43:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string readLink(const Path & path) override
|
|
|
|
|
{
|
2016-06-02 19:43:36 +03:00
|
|
|
|
return nix::readLink(toRealPath(path));
|
2016-02-25 18:43:19 +02:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ref<FSAccessor> LocalFSStore::getFSAccessor()
|
|
|
|
|
{
|
2016-06-02 19:43:36 +03:00
|
|
|
|
return make_ref<LocalStoreAccessor>(ref<LocalFSStore>(std::dynamic_pointer_cast<LocalFSStore>(shared_from_this())));
|
2016-02-25 18:43:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 15:21:45 +02:00
|
|
|
|
void LocalFSStore::narFromPath(const Path & path, Sink & sink)
|
2016-03-21 18:55:57 +02:00
|
|
|
|
{
|
2016-03-22 15:21:45 +02:00
|
|
|
|
if (!isValidPath(path))
|
|
|
|
|
throw Error(format("path ‘%s’ is not valid") % path);
|
2016-07-28 16:14:59 +03:00
|
|
|
|
dumpPath(getRealStoreDir() + std::string(path, storeDir.size()), sink);
|
2016-03-21 18:55:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 18:43:19 +02:00
|
|
|
|
}
|