Fix redundant stat call in addFile

Use the FileInfo already provided by Walk instead of calling Stat again.
Only stat if fi is nil (defensive, shouldn't happen in normal Walk usage).
Also fixes potential nil pointer dereference if fi was nil.
This commit is contained in:
Jeffrey Paul 2025-12-17 17:02:29 -08:00
parent d3776d7d7c
commit e480c3f677

View File

@ -156,22 +156,25 @@ func (m *manifest) addFile(p string, fi fs.FileInfo, sfsIndex int) error {
if !m.scanOptions.IncludeDotfiles && pathIsHidden(p) { if !m.scanOptions.IncludeDotfiles && pathIsHidden(p) {
return nil return nil
} }
if fi != nil && fi.IsDir() { if fi == nil {
// fi should come from Walk; if nil, stat to get info
var err error
fi, err = m.sourceFS[sfsIndex].Stat(p)
if err != nil {
return err
}
}
if fi.IsDir() {
// manifests contain only files, directories are implied. // manifests contain only files, directories are implied.
return nil return nil
} }
// FIXME test if 'fi' is already result of stat
fileinfo, staterr := m.sourceFS[sfsIndex].Stat(p)
if staterr != nil {
return staterr
}
cleanPath := p cleanPath := p
if cleanPath[0:1] == "/" { if cleanPath[0:1] == "/" {
cleanPath = cleanPath[1:] cleanPath = cleanPath[1:]
} }
nf := &manifestFile{ nf := &manifestFile{
path: cleanPath, path: cleanPath,
info: fileinfo, info: fi,
} }
m.files = append(m.files, nf) m.files = append(m.files, nf)
m.totalFileSize = m.totalFileSize + fi.Size() m.totalFileSize = m.totalFileSize + fi.Size()