Skip to content

Commit

Permalink
Merge pull request #9759 from obsidiansystems/abs-path-string-view
Browse files Browse the repository at this point in the history
`absPath`: just take a `std::string_view`
  • Loading branch information
Ericson2314 authored Jan 15, 2024
2 parents eeaa188 + beed00c commit 8a2da82
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/libutil/canon-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace nix {
CanonPath CanonPath::root = CanonPath("/");

CanonPath::CanonPath(std::string_view raw)
: path(absPath((Path) raw, "/"))
: path(absPath(raw, "/"))
{ }

CanonPath::CanonPath(std::string_view raw, const CanonPath & root)
: path(absPath((Path) raw, root.abs()))
: path(absPath(raw, root.abs()))
{ }

CanonPath::CanonPath(const std::vector<std::string> & elems)
Expand All @@ -22,7 +22,7 @@ CanonPath::CanonPath(const std::vector<std::string> & elems)

CanonPath CanonPath::fromCwd(std::string_view path)
{
return CanonPath(unchecked_t(), absPath((Path) path));
return CanonPath(unchecked_t(), absPath(path));
}

std::optional<CanonPath> CanonPath::parent() const
Expand Down
14 changes: 11 additions & 3 deletions src/libutil/file-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ namespace fs = std::filesystem;

namespace nix {

Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
Path absPath(PathView path, std::optional<PathView> dir, bool resolveSymlinks)
{
std::string scratch;

if (path[0] != '/') {
// In this case we need to call `canonPath` on a newly-created
// string. We set `scratch` to that string first, and then set
// `path` to `scratch`. This ensures the newly-created string
// lives long enough for the call to `canonPath`, and allows us
// to just accept a `std::string_view`.
if (!dir) {
#ifdef __GNU__
/* GNU (aka. GNU/Hurd) doesn't have any limitation on path
Expand All @@ -35,12 +42,13 @@ Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
if (!getcwd(buf, sizeof(buf)))
#endif
throw SysError("cannot get cwd");
path = concatStrings(buf, "/", path);
scratch = concatStrings(buf, "/", path);
#ifdef __GNU__
free(buf);
#endif
} else
path = concatStrings(*dir, "/", path);
scratch = concatStrings(*dir, "/", path);
path = scratch;
}
return canonPath(path, resolveSymlinks);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/file-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Source;
* specified directory, or the current directory otherwise. The path
* is also canonicalised.
*/
Path absPath(Path path,
Path absPath(PathView path,
std::optional<PathView> dir = {},
bool resolveSymlinks = false);

Expand Down

0 comments on commit 8a2da82

Please sign in to comment.