List files even when lstat fails (#1382)

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 #1359
This commit is contained in:
Jan Tatje authored and GitHub committed 2023-08-26 15:58:48 +03:00
1 parent a280b6c7d3
commit e0abb29a00
2 files changed
+30

No files matched your search

+25
View File
@@ -37,6 +37,7 @@ type file struct {
accessTime time.Time accessTime time.Time
changeTime time.Time changeTime time.Time
ext string ext string
err error
} }
func (file *file) TotalSize() int64 { func (file *file) TotalSize() int64 {
@@ -49,6 +50,17 @@ func (file *file) TotalSize() int64 {
return file.Size() 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) { func readdir(path string) ([]*file, error) {
f, err := os.Open(path) f, err := os.Open(path)
if err != nil { if err != nil {
@@ -68,6 +80,18 @@ func readdir(path string) ([]*file, error) {
} }
if err != nil { if err != nil {
log.Printf("getting file information: %s", err) 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 continue
} }
@@ -130,6 +154,7 @@ func readdir(path string) ([]*file, error) {
accessTime: at, accessTime: at,
changeTime: ct, changeTime: ct,
ext: ext, ext: ext,
err: nil,
}) })
} }
+5
View File
@@ -726,6 +726,11 @@ func (ui *ui) loadFileInfo(nav *nav) {
return return
} }
if curr.err != nil {
ui.echoerrf("stat: %s", curr.err)
return
}
statfmt := strings.ReplaceAll(gOpts.statfmt, "|", "\x1f") statfmt := strings.ReplaceAll(gOpts.statfmt, "|", "\x1f")
replace := func(s string, val string) { replace := func(s string, val string) {
if val == "" { if val == "" {