Skip to content

Determine if directory as part of C code#611

Open
zapdos26 wants to merge 2 commits intoenvmodules:mainfrom
zapdos26:fix/lstat-filetype
Open

Determine if directory as part of C code#611
zapdos26 wants to merge 2 commits intoenvmodules:mainfrom
zapdos26:fix/lstat-filetype

Conversation

@zapdos26
Copy link

This pull request improves the detection and handling of directories, including symlinked directories, in both the C and Tcl code for module file discovery. The changes ensure that directories are correctly identified even when directory entries have ambiguous types (such as symlinks or NFS mounts), and that this information is consistently propagated through the Tcl logic. Additionally, new tests are added to verify correct behavior with symlinked directories.

Directory detection improvements

  • In lib/envmodules.c, the logic in Envmodules_GetFilesInDirectoryObjCmd now determines whether a directory entry is a directory by first checking d_type (when available), and falling back to stat() for ambiguous or symlinked entries. This ensures accurate identification of directories, including symlinks to directories.
  • The C extension now appends an additional field to each result indicating whether the entry is a directory, and this is also done for .modulerc and .version files.

Tcl logic updates

  • The Tcl function __getFilesInDirectory is updated to include directory status in its output using file isdirectory ... for each entry, and this is propagated through the code.
  • The findModulesFromDirsAndFiles and findModules procedures are updated to consume and record this directory status, avoiding redundant stat calls and improving efficiency and correctness.

Test improvements

  • A new test (testsuite/modules.80-deep/025-symlink-deep.exp) verifies that symlinked directories are correctly identified and traversed as directories.
  • A new test resource (testsuite/modulefiles.deep/plainlink) is added to support the symlink-directory test.

Extend getFilesInDirectory (C extension) to return triplets
{path hidden is_dir} instead of pairs {path hidden}. The is_dir flag
is determined from d_type when available, falling back to stat() for
DT_UNKNOWN (NFS v3) and DT_LNK (symlinks to directories).

This feeds directly into the existing fknown_arr/dknown_arr mechanism
in findModulesFromDirsAndFiles, eliminating redundant per-file
'file isdirectory' stat calls.

Also update the Tcl fallback __getFilesInDirectory to return
the same triplet format using 'file isdirectory' (which follows
symlinks).

Signed-off-by: zapdos26 <zapdoskid@gmail.com>
Verify that symlinked directories are correctly identified as
directories and their modules are discoverable via avail and load.
This exercises the d_type/DT_LNK handling in the C extension.

Signed-off-by: zapdos26 <zapdoskid@gmail.com>
@zapdos26 zapdos26 changed the title Fix/lstat filetype Determine if directory as part of C code Feb 17, 2026
@xdelaruelle
Copy link
Collaborator

@zapdos26 Thanks for your interest in the project. Could you demonstrate by adding a new test the issue with the existing code this pull request is trying to fix.

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.

2 participants