Skip to content

Commit

Permalink
Use isAbsolute()
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Jan 14, 2025
1 parent ff8e2fe commit ff9d886
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ void EvalState::checkURI(const std::string & uri)

/* If the URI is a path, then check it against allowedPaths as
well. */
if (hasPrefix(uri, "/")) {
if (isAbsolute(uri)) {
if (auto rootFS2 = rootFS.dynamic_pointer_cast<AllowListSourceAccessor>())
rootFS2->checkAccess(CanonPath(uri));
return;
Expand Down
2 changes: 1 addition & 1 deletion src/libfetchers/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct PathInputScheme : InputScheme
std::optional<std::string> isRelative(const Input & input) const
{
auto path = getStrAttr(input.attrs, "path");
if (hasPrefix(path, "/"))
if (isAbsolute(path))
return std::nullopt;
else
return path;
Expand Down
2 changes: 1 addition & 1 deletion src/libfetchers/registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static std::shared_ptr<Registry> getGlobalRegistry(const Settings & settings, re
return std::make_shared<Registry>(settings, Registry::Global); // empty registry
}

if (!hasPrefix(path, "/")) {
if (!isAbsolute(path)) {
auto storePath = downloadFile(store, path, "flake-registry.json").storePath;
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
store2->addPermRoot(storePath, getCacheDir() + "/flake-registry.json");
Expand Down
4 changes: 2 additions & 2 deletions src/libflake/flake/flakeref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ std::pair<FlakeRef, std::string> parsePathFlakeRefWithFragment(
}

} else {
if (!hasPrefix(path, "/"))
if (!isAbsolute(path))
throw BadURL("flake reference '%s' is not an absolute path", url);
path = canonPath(path + "/" + getOr(query, "dir", ""));
}
Expand Down Expand Up @@ -235,7 +235,7 @@ std::optional<std::pair<FlakeRef, std::string>> parseURLFlakeRef(
auto parsed = parseURL(url);
if (baseDir
&& (parsed.scheme == "path" || parsed.scheme == "git+file")
&& !hasPrefix(parsed.path, "/"))
&& !isAbsolute(parsed.path))
parsed.path = absPath(parsed.path, *baseDir);
return fromParsedURL(fetchSettings, std::move(parsed), isFlake);
} catch (BadURL &) {
Expand Down
7 changes: 1 addition & 6 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ namespace nix {

namespace fs { using namespace std::filesystem; }

/**
* Treat the string as possibly an absolute path, by inspecting the
* start of it. Return whether it was probably intended to be
* absolute.
*/
static bool isAbsolute(PathView path)
bool isAbsolute(PathView path)
{
return fs::path { path }.is_absolute();
}
Expand Down
5 changes: 5 additions & 0 deletions src/libutil/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ namespace nix {
struct Sink;
struct Source;

/**
* Return whether the path denotes an absolute path.
*/
bool isAbsolute(PathView path);

/**
* @return An absolutized path, resolving paths relative to the
* specified directory, or the current directory otherwise. The path
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/source-accessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ CanonPath SourceAccessor::resolveSymlinks(
throw Error("infinite symlink recursion in path '%s'", showPath(path));
auto target = readLink(res);
res.pop();
if (hasPrefix(target, "/"))
if (isAbsolute(target))
res = CanonPath::root;
todo.splice(todo.begin(), tokenizeString<std::list<std::string>>(target, "/"));
}
Expand Down

0 comments on commit ff9d886

Please sign in to comment.