Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the fs accessor for readInvalidDerivation #4366

Merged
merged 1 commit into from Dec 23, 2020

Conversation

thufschmitt
Copy link
Member

Extend FSAccessor::readFile to allow not checking that the path is a
valid one, and rewrite readInvalidDerivation using this extended
readFile.

Several places in the code use readInvalidDerivation, either because
they need to read a derivation that has been written in the store but
not registered yet, or more generally to prevent a deadlock because
readDerivation tries to lock the state, so can't be called from a
place where the lock is already held.
However, readInvalidDerivation implicitely assumes that the store is a
LocalFSStore, which isn't always the case.

The concrete motivation for this is that it's required for nix copy --from someBinaryCache to work, which is tremendously useful for the
tests.

Extend `FSAccessor::readFile` to allow not checking that the path is a
valid one, and rewrite `readInvalidDerivation` using this extended
`readFile`.

Several places in the code use `readInvalidDerivation`, either because
they need to read a derivation that has been written in the store but
not registered yet, or more generally to prevent a deadlock because
`readDerivation` tries to lock the state, so can't be called from a
place where the lock is already held.
However, `readInvalidDerivation` implicitely assumes that the store is a
`LocalFSStore`, which isn't always the case.

The concrete motivation for this is that it's required for `nix copy
--from someBinaryCache` to work, which is tremendously useful for the
tests.
@@ -61,9 +61,9 @@ struct LocalStoreAccessor : public FSAccessor
return res;
}

std::string readFile(const Path & path) override
std::string readFile(const Path & path, bool requireValidPath = true) override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the overrides shouldn't have the default args? Elsewhere in the codebase they at least are only part of the declarations, IIRC.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. Triggered me to read https://stackoverflow.com/questions/6464404/virtual-function-default-arguments-behaviour which is… interesting.

The gist of it seems to that if we only call accessor.readFile(path) where accessor is declared of type FSAccessor (which is probably the case) then we don't need the default arguments for the overrides. But that looks quite fragile. Maybe I could just split the interface as:

virtual bool isValidPath(const Path& path);
virtual std::string readFileUnchecked(const Path& path);
std::string readFile(const Path& path) {
  if (!isValidPath(path)) throw InvalidPath(...);
  return readFileUnchecked(path);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I read it the other way around, where up casting certainly gives you the outer default arg, but no upcasting would also give you the outer default arg if there is no inner default arg.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, just checked:

struct Foo {
  virtual void foo(int n = 1) { }
};

struct Bar : Foo {
  void foo(int n) override { }
};

int main() {
  Foo x;
  x.foo();
  Bar y;
  y.foo();
}

x.foo() typechecks fine, but y.foo() doesn't

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to have misled you with that wrong interpretation then.

Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!

@thufschmitt thufschmitt added this to the ca-derivations-mvp milestone Dec 16, 2020
@edolstra edolstra merged commit 8927cba into master Dec 23, 2020
@edolstra edolstra deleted the readInvalidDerivation-on-remote-caches branch December 23, 2020 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants