Cleanup previous PR

Require ModeSymlink when checking the SymlinkToDir marker as it shares a
bit with the other-execute permission, use a single helper for the check,
avoid stat() on hidden entries that will be skipped and treat symlinks to
directories as directories in save file path completion.
This commit is contained in:
Kovid Goyal 2026-10-12 08:12:22 +05:30
parent f390eec17d
commit 4fa2cf249d
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
5 changed files with 53 additions and 8 deletions

View file

@ -325,7 +325,7 @@ func (pm *PreviewManager) preview_for(abspath string, ftype fs.FileMode) (ans Pr
if s.IsDir() {
return NewDirectoryPreview(abspath, s)
}
if ftype&fs.ModeSymlink != 0 && ftype&SymlinkToDir != 0 {
if is_symlink_to_dir(ftype) {
s, err = os.Stat(abspath)
if err != nil {
return NewErrorPreview(err)

View file

@ -130,7 +130,7 @@ func icon_for(path string, x os.FileMode) string {
return ans
}
var ans string
if x&fs.ModeSymlink != 0 && x&SymlinkToDir != 0 {
if is_symlink_to_dir(x) {
ans = string(icons.SYMLINK_TO_DIR)
} else {
ans = icons.IconForFileWithMode(path, x, true)

View file

@ -68,7 +68,7 @@ func FilePromptCompleter(getcwd func() string) func(string, string) *cli.Complet
for _, e := range entries {
word := e.Name()
if leading == "" || strings.HasPrefix(word, leading) {
collection := utils.IfElse(e.Type().IsDir(), dirs, files)
collection := utils.IfElse(is_directory(resolve_symlink_to_dir(filepath.Join(dir, e.Name()), e.Type())), dirs, files)
if prefix != "" {
word = prefix + word
}

View file

@ -164,8 +164,24 @@ type sortable_dir_entry struct {
const SymlinkToDir = 1
// SymlinkToDir shares its bit with the other-execute permission bit, so it is
// only meaningful when combined with fs.ModeSymlink
func is_symlink_to_dir(ftype fs.FileMode) bool {
return ftype&fs.ModeSymlink != 0 && ftype&SymlinkToDir != 0
}
func is_directory(ftype fs.FileMode) bool {
return ftype.IsDir() || ftype&SymlinkToDir != 0
return ftype.IsDir() || is_symlink_to_dir(ftype)
}
// Mark ftype with SymlinkToDir if it is a symlink pointing to a directory
func resolve_symlink_to_dir(path string, ftype fs.FileMode) fs.FileMode {
if ftype&fs.ModeSymlink != 0 {
if st, err := os.Stat(path); err == nil && st.IsDir() {
ftype |= SymlinkToDir
}
}
return ftype
}
// lowercase a string into a pre-existing byte buffer with speedups for ASCII
@ -305,10 +321,9 @@ func (fss *FileSystemScanner) worker() {
for i, e := range entries {
name := e.Name()
ftype := e.Type()
if ftype&fs.ModeSymlink != 0 {
if st, serr := os.Stat(dir + name); serr == nil && st.IsDir() {
ftype |= SymlinkToDir
}
// avoid stat() on hidden entries that will be skipped anyway
if fss.show_hidden || name[0] != '.' || name == dot_git {
ftype = resolve_symlink_to_dir(dir+name, ftype)
}
is_dir := is_directory(ftype)
if !is_dir {

View file

@ -315,6 +315,36 @@ func TestChooseFilesDirectorySymlinks(t *testing.T) {
}
}
func TestIsDirectory(t *testing.T) {
for _, tc := range []struct {
ftype fs.FileMode
expected bool
}{
{0, false},
{0o755, false},
{fs.ModeDir, true},
{fs.ModeSymlink, false},
{fs.ModeSymlink | SymlinkToDir, true},
} {
if actual := is_directory(tc.ftype); actual != tc.expected {
t.Fatalf("is_directory(%s) = %v, expected %v", tc.ftype, actual, tc.expected)
}
}
tdir := t.TempDir()
if err := os.Mkdir(filepath.Join(tdir, "dir"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink("dir", filepath.Join(tdir, "link")); err != nil {
t.Fatal(err)
}
if ftype := resolve_symlink_to_dir(filepath.Join(tdir, "link"), fs.ModeSymlink); !is_symlink_to_dir(ftype) {
t.Fatalf("symlink to directory not resolved: %s", ftype)
}
if ftype := resolve_symlink_to_dir(filepath.Join(tdir, "missing"), fs.ModeSymlink); is_directory(ftype) {
t.Fatalf("dangling symlink resolved as directory: %s", ftype)
}
}
func TestSortedResults(t *testing.T) {
r := NewSortedResults()
idx := CollectionIndex{}