mirror of
https://github.com/NixOS/nix.git
synced 2025-11-15 15:02:42 +01:00
Create a second Store::getFSAccessor for a single store object
This is sometimes easier / more performant to implement, and independently it is also a more convenient interface for many callers. The existing store-wide `getFSAccessor` is only used for - `nix why-depends` - the evaluator I hope we can get rid of it for those, too, and then we have the option of getting rid of the store-wide method. Co-authored-by: Sergei Zimmerman <sergei@zimmerman.foo>
This commit is contained in:
parent
0175f7e836
commit
a97d6d89d8
26 changed files with 125 additions and 57 deletions
|
|
@ -539,11 +539,21 @@ void BinaryCacheStore::registerDrvOutput(const Realisation & info)
|
|||
upsertFile(filePath, static_cast<nlohmann::json>(info).dump(), "application/json");
|
||||
}
|
||||
|
||||
ref<SourceAccessor> BinaryCacheStore::getFSAccessor(bool requireValidPath)
|
||||
ref<RemoteFSAccessor> BinaryCacheStore::getRemoteFSAccessor(bool requireValidPath)
|
||||
{
|
||||
return make_ref<RemoteFSAccessor>(ref<Store>(shared_from_this()), requireValidPath, config.localNarCache);
|
||||
}
|
||||
|
||||
ref<SourceAccessor> BinaryCacheStore::getFSAccessor(bool requireValidPath)
|
||||
{
|
||||
return getRemoteFSAccessor(requireValidPath);
|
||||
}
|
||||
|
||||
std::shared_ptr<SourceAccessor> BinaryCacheStore::getFSAccessor(const StorePath & storePath, bool requireValidPath)
|
||||
{
|
||||
return getRemoteFSAccessor(requireValidPath)->accessObject(storePath);
|
||||
}
|
||||
|
||||
void BinaryCacheStore::addSignatures(const StorePath & storePath, const StringSet & sigs)
|
||||
{
|
||||
/* Note: this is inherently racy since there is no locking on
|
||||
|
|
|
|||
|
|
@ -276,7 +276,14 @@ struct DummyStore : virtual Store
|
|||
callback(nullptr);
|
||||
}
|
||||
|
||||
virtual ref<SourceAccessor> getFSAccessor(bool requireValidPath) override
|
||||
std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath) override
|
||||
{
|
||||
std::shared_ptr<SourceAccessor> res;
|
||||
contents.cvisit(path, [&](const auto & kv) { res = kv.second.contents.get_ptr(); });
|
||||
return res;
|
||||
}
|
||||
|
||||
ref<SourceAccessor> getFSAccessor(bool requireValidPath) override
|
||||
{
|
||||
return wholeStoreView;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
namespace nix {
|
||||
|
||||
struct NarInfo;
|
||||
class RemoteFSAccessor;
|
||||
|
||||
struct BinaryCacheStoreConfig : virtual StoreConfig
|
||||
{
|
||||
|
|
@ -136,6 +137,11 @@ private:
|
|||
CheckSigsFlag checkSigs,
|
||||
std::function<ValidPathInfo(HashResult)> mkInfo);
|
||||
|
||||
/**
|
||||
* Same as `getFSAccessor`, but with a more preceise return type.
|
||||
*/
|
||||
ref<RemoteFSAccessor> getRemoteFSAccessor(bool requireValidPath = true);
|
||||
|
||||
public:
|
||||
|
||||
bool isValidPathUncached(const StorePath & path) override;
|
||||
|
|
@ -175,6 +181,8 @@ public:
|
|||
|
||||
ref<SourceAccessor> getFSAccessor(bool requireValidPath = true) override;
|
||||
|
||||
std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath &, bool requireValidPath = true) override;
|
||||
|
||||
void addSignatures(const StorePath & storePath, const StringSet & sigs) override;
|
||||
|
||||
std::optional<std::string> getBuildLogExact(const StorePath & path) override;
|
||||
|
|
|
|||
|
|
@ -142,7 +142,12 @@ public:
|
|||
unsupported("ensurePath");
|
||||
}
|
||||
|
||||
virtual ref<SourceAccessor> getFSAccessor(bool requireValidPath) override
|
||||
ref<SourceAccessor> getFSAccessor(bool requireValidPath) override
|
||||
{
|
||||
unsupported("getFSAccessor");
|
||||
}
|
||||
|
||||
std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath) override
|
||||
{
|
||||
unsupported("getFSAccessor");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,6 +68,7 @@ struct LocalFSStore : virtual Store, virtual GcStore, virtual LogStore
|
|||
|
||||
void narFromPath(const StorePath & path, Sink & sink) override;
|
||||
ref<SourceAccessor> getFSAccessor(bool requireValidPath = true) override;
|
||||
std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath = true) override;
|
||||
|
||||
/**
|
||||
* Creates symlink from the `gcRoot` to the `storePath` and
|
||||
|
|
|
|||
|
|
@ -27,6 +27,11 @@ class RemoteFSAccessor : public SourceAccessor
|
|||
|
||||
public:
|
||||
|
||||
/**
|
||||
* @return nullptr if the store does not contain any object at that path.
|
||||
*/
|
||||
std::shared_ptr<SourceAccessor> accessObject(const StorePath & path);
|
||||
|
||||
RemoteFSAccessor(
|
||||
ref<Store> store, bool requireValidPath = true, const /* FIXME: use std::optional */ Path & cacheDir = "");
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ struct FdSink;
|
|||
struct FdSource;
|
||||
template<typename T>
|
||||
class Pool;
|
||||
class RemoteFSAccessor;
|
||||
|
||||
struct RemoteStoreConfig : virtual StoreConfig
|
||||
{
|
||||
|
|
@ -176,10 +177,18 @@ protected:
|
|||
|
||||
virtual ref<SourceAccessor> getFSAccessor(bool requireValidPath = true) override;
|
||||
|
||||
virtual std::shared_ptr<SourceAccessor>
|
||||
getFSAccessor(const StorePath & path, bool requireValidPath = true) override;
|
||||
|
||||
virtual void narFromPath(const StorePath & path, Sink & sink) override;
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Same as the default implemenation of `RemoteStore::getFSAccessor`, but with a more preceise return type.
|
||||
*/
|
||||
ref<RemoteFSAccessor> getRemoteFSAccessor(bool requireValidPath = true);
|
||||
|
||||
std::atomic_bool failed{false};
|
||||
|
||||
void copyDrvsFromEvalStore(const std::vector<DerivedPath> & paths, std::shared_ptr<Store> evalStore);
|
||||
|
|
|
|||
|
|
@ -717,10 +717,20 @@ public:
|
|||
};
|
||||
|
||||
/**
|
||||
* @return An object to access files in the Nix store.
|
||||
* @return An object to access files in the Nix store, across all
|
||||
* store objects.
|
||||
*/
|
||||
virtual ref<SourceAccessor> getFSAccessor(bool requireValidPath = true) = 0;
|
||||
|
||||
/**
|
||||
* @return An object to access files for a specific store object in
|
||||
* the Nix store.
|
||||
*
|
||||
* @return nullptr if the store doesn't contain an object at the
|
||||
* givine path.
|
||||
*/
|
||||
virtual std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath = true) = 0;
|
||||
|
||||
/**
|
||||
* Repair the contents of the given path by redownloading it using
|
||||
* a substituter (if available).
|
||||
|
|
|
|||
|
|
@ -61,6 +61,11 @@ struct UDSRemoteStore : virtual IndirectRootStore, virtual RemoteStore
|
|||
return LocalFSStore::getFSAccessor(requireValidPath);
|
||||
}
|
||||
|
||||
std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath = true) override
|
||||
{
|
||||
return LocalFSStore::getFSAccessor(path, requireValidPath);
|
||||
}
|
||||
|
||||
void narFromPath(const StorePath & path, Sink & sink) override
|
||||
{
|
||||
LocalFSStore::narFromPath(path, sink);
|
||||
|
|
|
|||
|
|
@ -91,6 +91,23 @@ ref<SourceAccessor> LocalFSStore::getFSAccessor(bool requireValidPath)
|
|||
ref<LocalFSStore>(std::dynamic_pointer_cast<LocalFSStore>(shared_from_this())), requireValidPath);
|
||||
}
|
||||
|
||||
std::shared_ptr<SourceAccessor> LocalFSStore::getFSAccessor(const StorePath & path, bool requireValidPath)
|
||||
{
|
||||
auto absPath = std::filesystem::path{config.realStoreDir.get()} / path.to_string();
|
||||
if (requireValidPath) {
|
||||
/* Only return non-null if the store object is a fully-valid
|
||||
member of the store. */
|
||||
if (!isValidPath(path))
|
||||
return nullptr;
|
||||
} else {
|
||||
/* Return non-null as long as the some file system data exists,
|
||||
even if the store object is not fully registered. */
|
||||
if (!pathExists(absPath))
|
||||
return nullptr;
|
||||
}
|
||||
return std::make_shared<PosixSourceAccessor>(std::move(absPath));
|
||||
}
|
||||
|
||||
void LocalFSStore::narFromPath(const StorePath & path, Sink & sink)
|
||||
{
|
||||
if (!isValidPath(path))
|
||||
|
|
|
|||
|
|
@ -51,15 +51,17 @@ ref<SourceAccessor> RemoteFSAccessor::addToCache(std::string_view hashPart, std:
|
|||
|
||||
std::pair<ref<SourceAccessor>, CanonPath> RemoteFSAccessor::fetch(const CanonPath & path)
|
||||
{
|
||||
auto [storePath, restPath_] = store->toStorePath(store->storeDir + path.abs());
|
||||
auto restPath = CanonPath(restPath_);
|
||||
|
||||
auto [storePath, restPath] = store->toStorePath(store->storeDir + path.abs());
|
||||
if (requireValidPath && !store->isValidPath(storePath))
|
||||
throw InvalidPath("path '%1%' is not a valid store path", store->printStorePath(storePath));
|
||||
return {ref{accessObject(storePath)}, CanonPath{restPath}};
|
||||
}
|
||||
|
||||
std::shared_ptr<SourceAccessor> RemoteFSAccessor::accessObject(const StorePath & storePath)
|
||||
{
|
||||
auto i = nars.find(std::string(storePath.hashPart()));
|
||||
if (i != nars.end())
|
||||
return {i->second, restPath};
|
||||
return i->second;
|
||||
|
||||
std::string listing;
|
||||
Path cacheFile;
|
||||
|
|
@ -90,7 +92,7 @@ std::pair<ref<SourceAccessor>, CanonPath> RemoteFSAccessor::fetch(const CanonPat
|
|||
});
|
||||
|
||||
nars.emplace(storePath.hashPart(), narAccessor);
|
||||
return {narAccessor, restPath};
|
||||
return narAccessor;
|
||||
|
||||
} catch (SystemError &) {
|
||||
}
|
||||
|
|
@ -98,14 +100,14 @@ std::pair<ref<SourceAccessor>, CanonPath> RemoteFSAccessor::fetch(const CanonPat
|
|||
try {
|
||||
auto narAccessor = makeNarAccessor(nix::readFile(cacheFile));
|
||||
nars.emplace(storePath.hashPart(), narAccessor);
|
||||
return {narAccessor, restPath};
|
||||
return narAccessor;
|
||||
} catch (SystemError &) {
|
||||
}
|
||||
}
|
||||
|
||||
StringSink sink;
|
||||
store->narFromPath(storePath, sink);
|
||||
return {addToCache(storePath.hashPart(), std::move(sink.s)), restPath};
|
||||
return addToCache(storePath.hashPart(), std::move(sink.s));
|
||||
}
|
||||
|
||||
std::optional<SourceAccessor::Stat> RemoteFSAccessor::maybeLstat(const CanonPath & path)
|
||||
|
|
|
|||
|
|
@ -794,9 +794,19 @@ void RemoteStore::narFromPath(const StorePath & path, Sink & sink)
|
|||
conn->narFromPath(*this, &conn.daemonException, path, [&](Source & source) { copyNAR(conn->from, sink); });
|
||||
}
|
||||
|
||||
ref<RemoteFSAccessor> RemoteStore::getRemoteFSAccessor(bool requireValidPath)
|
||||
{
|
||||
return make_ref<RemoteFSAccessor>(ref<Store>(shared_from_this()), requireValidPath);
|
||||
}
|
||||
|
||||
ref<SourceAccessor> RemoteStore::getFSAccessor(bool requireValidPath)
|
||||
{
|
||||
return make_ref<RemoteFSAccessor>(ref<Store>(shared_from_this()));
|
||||
return getRemoteFSAccessor(requireValidPath);
|
||||
}
|
||||
|
||||
std::shared_ptr<SourceAccessor> RemoteStore::getFSAccessor(const StorePath & path, bool requireValidPath)
|
||||
{
|
||||
return getRemoteFSAccessor(requireValidPath)->accessObject(path);
|
||||
}
|
||||
|
||||
void RemoteStore::ConnectionHandle::withFramedSink(std::function<void(Sink & sink)> fun)
|
||||
|
|
|
|||
|
|
@ -151,6 +151,11 @@ struct MountedSSHStore : virtual SSHStore, virtual LocalFSStore
|
|||
return LocalFSStore::getFSAccessor(requireValidPath);
|
||||
}
|
||||
|
||||
std::shared_ptr<SourceAccessor> getFSAccessor(const StorePath & path, bool requireValidPath) override
|
||||
{
|
||||
return LocalFSStore::getFSAccessor(path, requireValidPath);
|
||||
}
|
||||
|
||||
std::optional<std::string> getBuildLogExact(const StorePath & path) override
|
||||
{
|
||||
return LocalFSStore::getBuildLogExact(path);
|
||||
|
|
|
|||
|
|
@ -1120,10 +1120,9 @@ Derivation Store::derivationFromPath(const StorePath & drvPath)
|
|||
|
||||
static Derivation readDerivationCommon(Store & store, const StorePath & drvPath, bool requireValidPath)
|
||||
{
|
||||
auto accessor = store.getFSAccessor(requireValidPath);
|
||||
auto accessor = store.getFSAccessor(drvPath, requireValidPath);
|
||||
try {
|
||||
return parseDerivation(
|
||||
store, accessor->readFile(CanonPath(drvPath.to_string())), Derivation::nameFromPath(drvPath));
|
||||
return parseDerivation(store, accessor->readFile(CanonPath::root), Derivation::nameFromPath(drvPath));
|
||||
} catch (FormatError & e) {
|
||||
throw Error("error parsing derivation '%s': %s", store.printStorePath(drvPath), e.msg());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue