Skip to content

Commit

Permalink
Resolve intermediate symlinks in resolveExprPath
Browse files Browse the repository at this point in the history
This addresses some cases of NixOS#2109
  • Loading branch information
ony committed Mar 31, 2021
1 parent 3c82d9d commit 8281792
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
18 changes: 4 additions & 14 deletions src/libexpr/parser.y
Expand Up @@ -605,23 +605,13 @@ Path resolveExprPath(Path path)
{
assert(path[0] == '/');

unsigned int followCount = 0, maxFollow = 1024;

/* If `path' is a symlink, follow it. This is so that relative
/* Expand all symlinks in `path`. This is so that relative
path references work. */
struct stat st;
while (true) {
// Basic cycle/depth limit to avoid infinite loops.
if (++followCount >= maxFollow)
throw Error("too many symbolic links encountered while traversing the path '%s'", path);
st = lstat(path);
if (!S_ISLNK(st.st_mode)) break;
path = absPath(readLink(path), dirOf(path));
}
path = absPath(path, {}, true);

/* If `path' refers to a directory, append `/default.nix'. */
if (S_ISDIR(st.st_mode))
path = canonPath(path + "/default.nix");
if (getFileType(path) == DT_DIR)
path = canonPath(path + "/default.nix", true);

return path;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/check-imports.sh
Expand Up @@ -47,10 +47,10 @@ testImportFile $TEST_ROOT/example/dir/subdir/alt-good.nix
testImportFile $TEST_ROOT/example/dir/subdir

# two-level relative import from path with symlink dir
# FIXME: testImportFile $TEST_ROOT/example/dir/subdir/import-good.nix
testImportFile $TEST_ROOT/example/dir/subdir/import-good.nix

# relative traverse of symlink dir
# FIXME: testImportFile $TEST_ROOT/example/dir/subdir/../good.nix

# dir import with symlinked default.nix
# FIXME: testImportFile $TEST_ROOT/example/alt-dir
testImportFile $TEST_ROOT/example/alt-dir

0 comments on commit 8281792

Please sign in to comment.