2016-03-21 18:55:57 +02:00
|
|
|
#include "archive.hh"
|
2023-11-01 18:22:25 +02:00
|
|
|
#include "posix-source-accessor.hh"
|
2016-02-25 18:43:19 +02:00
|
|
|
#include "store-api.hh"
|
2020-10-09 23:18:08 +03:00
|
|
|
#include "local-fs-store.hh"
|
2016-06-02 14:33:49 +03:00
|
|
|
#include "globals.hh"
|
2017-03-13 13:07:50 +02:00
|
|
|
#include "compression.hh"
|
|
|
|
#include "derivations.hh"
|
2016-02-25 18:43:19 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2024-07-18 06:32:27 +03:00
|
|
|
LocalFSStoreConfig::LocalFSStoreConfig(PathView rootDir, const Params & params)
|
|
|
|
: StoreConfig(params)
|
|
|
|
// Default `?root` from `rootDir` if non set
|
|
|
|
// FIXME don't duplicate description once we don't have root setting
|
|
|
|
, rootDir{
|
|
|
|
this,
|
|
|
|
!rootDir.empty() && params.count("root") == 0
|
|
|
|
? (std::optional<Path>{rootDir})
|
|
|
|
: std::nullopt,
|
|
|
|
"root",
|
|
|
|
"Directory prefixed to all other paths."}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-02 14:33:49 +03:00
|
|
|
LocalFSStore::LocalFSStore(const Params & params)
|
|
|
|
: Store(params)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:22:25 +02:00
|
|
|
struct LocalStoreAccessor : PosixSourceAccessor
|
2016-02-25 18:43:19 +02:00
|
|
|
{
|
2016-06-02 19:43:36 +03:00
|
|
|
ref<LocalFSStore> store;
|
2023-11-01 18:09:28 +02:00
|
|
|
bool requireValidPath;
|
2016-02-25 18:43:19 +02:00
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
LocalStoreAccessor(ref<LocalFSStore> store, bool requireValidPath)
|
|
|
|
: store(store)
|
|
|
|
, requireValidPath(requireValidPath)
|
|
|
|
{ }
|
2016-02-25 18:43:19 +02:00
|
|
|
|
2023-11-01 18:22:25 +02:00
|
|
|
CanonPath toRealPath(const CanonPath & path)
|
2016-02-25 18:43:19 +02:00
|
|
|
{
|
2023-11-01 18:22:25 +02:00
|
|
|
auto [storePath, rest] = store->toStorePath(path.abs());
|
2020-12-15 11:54:24 +02:00
|
|
|
if (requireValidPath && !store->isValidPath(storePath))
|
2020-07-13 17:19:37 +03:00
|
|
|
throw InvalidPath("path '%1%' is not a valid store path", store->printStorePath(storePath));
|
2024-02-05 16:13:11 +02:00
|
|
|
return CanonPath(store->getRealStoreDir()) / storePath.to_string() / CanonPath(rest);
|
2016-02-25 18:43:19 +02:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
std::optional<Stat> maybeLstat(const CanonPath & path) override
|
2016-02-25 18:43:19 +02:00
|
|
|
{
|
2024-04-11 00:46:19 +03:00
|
|
|
/* Handle the case where `path` is (a parent of) the store. */
|
|
|
|
if (isDirOrInDir(store->storeDir, path.abs()))
|
|
|
|
return Stat{ .type = tDirectory };
|
|
|
|
|
2023-11-01 18:22:25 +02:00
|
|
|
return PosixSourceAccessor::maybeLstat(toRealPath(path));
|
2016-02-25 18:43:19 +02:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
DirEntries readDirectory(const CanonPath & path) override
|
2016-02-25 18:43:19 +02:00
|
|
|
{
|
2023-11-01 18:22:25 +02:00
|
|
|
return PosixSourceAccessor::readDirectory(toRealPath(path));
|
2016-02-25 18:43:19 +02:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:22:25 +02:00
|
|
|
void readFile(
|
|
|
|
const CanonPath & path,
|
|
|
|
Sink & sink,
|
|
|
|
std::function<void(uint64_t)> sizeCallback) override
|
2016-02-25 18:43:19 +02:00
|
|
|
{
|
2023-11-01 18:22:25 +02:00
|
|
|
return PosixSourceAccessor::readFile(toRealPath(path), sink, sizeCallback);
|
2016-02-25 18:43:19 +02:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
std::string readLink(const CanonPath & path) override
|
2016-02-25 18:43:19 +02:00
|
|
|
{
|
2023-11-01 18:22:25 +02:00
|
|
|
return PosixSourceAccessor::readLink(toRealPath(path));
|
2016-02-25 18:43:19 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-11-01 18:09:28 +02:00
|
|
|
ref<SourceAccessor> LocalFSStore::getFSAccessor(bool requireValidPath)
|
2016-02-25 18:43:19 +02:00
|
|
|
{
|
2017-05-02 16:07:11 +03:00
|
|
|
return make_ref<LocalStoreAccessor>(ref<LocalFSStore>(
|
2023-11-01 18:09:28 +02:00
|
|
|
std::dynamic_pointer_cast<LocalFSStore>(shared_from_this())),
|
|
|
|
requireValidPath);
|
2016-02-25 18:43:19 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
void LocalFSStore::narFromPath(const StorePath & path, Sink & sink)
|
2016-03-21 18:55:57 +02:00
|
|
|
{
|
2016-03-22 15:21:45 +02:00
|
|
|
if (!isValidPath(path))
|
2019-12-05 20:11:09 +02:00
|
|
|
throw Error("path '%s' is not valid", printStorePath(path));
|
|
|
|
dumpPath(getRealStoreDir() + std::string(printStorePath(path), storeDir.size()), sink);
|
2016-03-21 18:55:57 +02:00
|
|
|
}
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
const std::string LocalFSStore::drvsLogDir = "drvs";
|
2017-03-13 13:07:50 +02:00
|
|
|
|
2022-12-15 22:58:54 +02:00
|
|
|
std::optional<std::string> LocalFSStore::getBuildLogExact(const StorePath & path)
|
2017-03-13 13:07:50 +02:00
|
|
|
{
|
2022-01-17 23:20:05 +02:00
|
|
|
auto baseName = path.to_string();
|
2017-03-13 13:07:50 +02:00
|
|
|
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
|
|
|
|
Path logPath =
|
|
|
|
j == 0
|
2022-01-17 23:20:05 +02:00
|
|
|
? fmt("%s/%s/%s/%s", logDir, drvsLogDir, baseName.substr(0, 2), baseName.substr(2))
|
2017-04-13 16:55:38 +03:00
|
|
|
: fmt("%s/%s/%s", logDir, drvsLogDir, baseName);
|
2017-03-13 13:07:50 +02:00
|
|
|
Path logBz2Path = logPath + ".bz2";
|
|
|
|
|
|
|
|
if (pathExists(logPath))
|
2022-01-17 23:20:05 +02:00
|
|
|
return readFile(logPath);
|
2017-03-13 13:07:50 +02:00
|
|
|
|
2017-03-21 20:23:07 +02:00
|
|
|
else if (pathExists(logBz2Path)) {
|
|
|
|
try {
|
|
|
|
return decompress("bzip2", readFile(logBz2Path));
|
|
|
|
} catch (Error &) { }
|
|
|
|
}
|
|
|
|
|
2017-03-13 13:07:50 +02:00
|
|
|
}
|
|
|
|
|
2022-01-17 23:20:05 +02:00
|
|
|
return std::nullopt;
|
2017-03-13 13:07:50 +02:00
|
|
|
}
|
|
|
|
|
2016-02-25 18:43:19 +02:00
|
|
|
}
|