2016-03-21 16:55:57 +00:00
|
|
|
|
#include "archive.hh"
|
2016-02-25 16:43:19 +00:00
|
|
|
|
#include "fs-accessor.hh"
|
|
|
|
|
#include "store-api.hh"
|
2016-06-02 11:33:49 +00:00
|
|
|
|
#include "globals.hh"
|
2017-03-13 11:07:50 +00:00
|
|
|
|
#include "compression.hh"
|
|
|
|
|
#include "derivations.hh"
|
2016-02-25 16:43:19 +00:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
2016-06-02 11:33:49 +00:00
|
|
|
|
LocalFSStore::LocalFSStore(const Params & params)
|
|
|
|
|
: Store(params)
|
2016-07-27 15:47:18 +00: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 11:33:49 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 16:43:19 +00:00
|
|
|
|
struct LocalStoreAccessor : public FSAccessor
|
|
|
|
|
{
|
2016-06-02 16:43:36 +00:00
|
|
|
|
ref<LocalFSStore> store;
|
2016-02-25 16:43:19 +00:00
|
|
|
|
|
2016-06-02 16:43:36 +00:00
|
|
|
|
LocalStoreAccessor(ref<LocalFSStore> store) : store(store) { }
|
2016-02-25 16:43:19 +00:00
|
|
|
|
|
2016-06-02 16:43:36 +00:00
|
|
|
|
Path toRealPath(const Path & path)
|
2016-02-25 16:43:19 +00:00
|
|
|
|
{
|
2016-06-01 12:49:12 +00:00
|
|
|
|
Path storePath = store->toStorePath(path);
|
2016-02-25 16:43:19 +00:00
|
|
|
|
if (!store->isValidPath(storePath))
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw InvalidPath(format("path ‘%1%’ is not a valid store path") % storePath);
|
2016-06-02 16:43:36 +00:00
|
|
|
|
return store->getRealStoreDir() + std::string(path, store->storeDir.size());
|
2016-02-25 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FSAccessor::Stat stat(const Path & path) override
|
|
|
|
|
{
|
2016-06-02 16:43:36 +00:00
|
|
|
|
auto realPath = toRealPath(path);
|
2016-02-25 16:43:19 +00:00
|
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
if (lstat(path.c_str(), &st)) {
|
2016-03-23 10:17:46 +00:00
|
|
|
|
if (errno == ENOENT || errno == ENOTDIR) return {Type::tMissing, 0, false};
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw SysError(format("getting status of ‘%1%’") % path);
|
2016-02-25 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode))
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error(format("file ‘%1%’ has unsupported type") % path);
|
2016-02-25 16:43:19 +00:00
|
|
|
|
|
|
|
|
|
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 16:43:36 +00:00
|
|
|
|
auto realPath = toRealPath(path);
|
2016-02-25 16:43:19 +00: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 16:43:36 +00:00
|
|
|
|
return nix::readFile(toRealPath(path));
|
2016-02-25 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string readLink(const Path & path) override
|
|
|
|
|
{
|
2016-06-02 16:43:36 +00:00
|
|
|
|
return nix::readLink(toRealPath(path));
|
2016-02-25 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ref<FSAccessor> LocalFSStore::getFSAccessor()
|
|
|
|
|
{
|
2016-06-02 16:43:36 +00:00
|
|
|
|
return make_ref<LocalStoreAccessor>(ref<LocalFSStore>(std::dynamic_pointer_cast<LocalFSStore>(shared_from_this())));
|
2016-02-25 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 13:21:45 +00:00
|
|
|
|
void LocalFSStore::narFromPath(const Path & path, Sink & sink)
|
2016-03-21 16:55:57 +00:00
|
|
|
|
{
|
2016-03-22 13:21:45 +00:00
|
|
|
|
if (!isValidPath(path))
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error(format("path ‘%s’ is not valid") % path);
|
2016-07-28 13:14:59 +00:00
|
|
|
|
dumpPath(getRealStoreDir() + std::string(path, storeDir.size()), sink);
|
2016-03-21 16:55:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-13 11:07:50 +00:00
|
|
|
|
const string LocalFSStore::drvsLogDir = "drvs";
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<std::string> LocalFSStore::getBuildLog(const Path & path_)
|
|
|
|
|
{
|
|
|
|
|
auto path(path_);
|
|
|
|
|
|
|
|
|
|
assertStorePath(path);
|
|
|
|
|
|
|
|
|
|
if (!isDerivation(path)) {
|
|
|
|
|
path = queryPathInfo(path)->deriver;
|
|
|
|
|
if (path == "") return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string baseName = baseNameOf(path);
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
|
|
|
|
|
|
Path logPath =
|
|
|
|
|
j == 0
|
|
|
|
|
? (format("%1%/%2%/%3%/%4%") % logDir % drvsLogDir % string(baseName, 0, 2) % string(baseName, 2)).str()
|
|
|
|
|
: (format("%1%/%2%/%3%") % logDir % drvsLogDir % baseName).str();
|
|
|
|
|
Path logBz2Path = logPath + ".bz2";
|
|
|
|
|
|
|
|
|
|
if (pathExists(logPath))
|
|
|
|
|
return std::make_shared<std::string>(readFile(logPath));
|
|
|
|
|
|
|
|
|
|
else if (pathExists(logBz2Path))
|
|
|
|
|
return decompress("bzip2", readFile(logBz2Path));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 16:43:19 +00:00
|
|
|
|
}
|