Skip to content

Commit

Permalink
List files even when lstat fails
Browse files Browse the repository at this point in the history
Implements a `fakeStat` struct with dummy values and stores the
error in `file.err`, then shows the error instead of FileInfo, if
the file is inaccesible.

Fixes gokcehan#1359
  • Loading branch information
jantatje committed Aug 12, 2023
1 parent dd82949 commit a9d48ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
25 changes: 25 additions & 0 deletions nav.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type file struct {
accessTime time.Time
changeTime time.Time
ext string
err error
}

func (file *file) TotalSize() int64 {
Expand All @@ -49,6 +50,17 @@ func (file *file) TotalSize() int64 {
return file.Size()
}

type fakeStat struct {
name string
}

func (fs *fakeStat) Name() string { return fs.name }
func (fs *fakeStat) Size() int64 { return 0 }
func (fs *fakeStat) Mode() os.FileMode { return os.FileMode(0000) }
func (fs *fakeStat) ModTime() time.Time { return time.Unix(0, 0) }
func (fs *fakeStat) IsDir() bool { return false }
func (fs *fakeStat) Sys() any { return nil }

func readdir(path string) ([]*file, error) {
f, err := os.Open(path)
if err != nil {
Expand All @@ -68,6 +80,18 @@ func readdir(path string) ([]*file, error) {
}
if err != nil {
log.Printf("getting file information: %s", err)
files = append(files, &file{
FileInfo: &fakeStat{name: fname},
linkState: notLink,
linkTarget: "",
path: fpath,
dirCount: -1,
dirSize: -1,
accessTime: time.Unix(0, 0),
changeTime: time.Unix(0, 0),
ext: filepath.Ext(fpath),
err: err,
})
continue
}

Expand Down Expand Up @@ -130,6 +154,7 @@ func readdir(path string) ([]*file, error) {
accessTime: at,
changeTime: ct,
ext: ext,
err: nil,
})
}

Expand Down
5 changes: 5 additions & 0 deletions ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,11 @@ func (ui *ui) loadFileInfo(nav *nav) {
return
}

if curr.err != nil {
ui.echoerrf("stat: %s", curr.err)
return
}

statfmt := strings.ReplaceAll(gOpts.statfmt, "|", "\x1f")
replace := func(s string, val string) {
if val == "" {
Expand Down

0 comments on commit a9d48ea

Please sign in to comment.