Skip to content

Commit

Permalink
Merge pull request #4679 from ony/feature/one-pass-canon-path
Browse files Browse the repository at this point in the history
Optimize canonPath to resolve relative symlinks in one pass
  • Loading branch information
edolstra committed Apr 15, 2021
2 parents 4cd9bd1 + f3f2287 commit 3ee0ecd
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/libutil/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,18 @@ Path canonPath(const Path & path, bool resolveSymlinks)
s += '/';
while (i != end && *i != '/') s += *i++;

/* If s points to a symlink, resolve it and restart (since
the symlink target might contain new symlinks). */
/* If s points to a symlink, resolve it and continue from there */
if (resolveSymlinks && isLink(s)) {
if (++followCount >= maxFollow)
throw Error("infinite symlink recursion in path '%1%'", path);
temp = absPath(readLink(s), dirOf(s))
+ string(i, end);
i = temp.begin(); /* restart */
temp = readLink(s) + string(i, end);
i = temp.begin();
end = temp.end();
s = "";
if (!temp.empty() && temp[0] == '/') {
s.clear(); /* restart for symlinks pointing to absolute path */
} else {
s = dirOf(s);
}
}
}
}
Expand Down

0 comments on commit 3ee0ecd

Please sign in to comment.