fix(inputs.filecount): Respect symlink files with FollowSymLinks (#14669)

This commit is contained in:
Dane Strandboge 2024-02-06 15:11:18 -06:00 committed by GitHub
parent c981bb7819
commit 1d513e6379
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 3 deletions

View File

@ -130,7 +130,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
if err == nil && rel == "." {
return nil
}
file, err := fc.Fs.Stat(path)
file, err := fc.resolveLink(path)
if err != nil {
if os.IsNotExist(err) {
return nil
@ -244,6 +244,21 @@ func (fc *FileCount) Gather(acc telegraf.Accumulator) error {
return nil
}
func (fc *FileCount) resolveLink(path string) (os.FileInfo, error) {
if fc.FollowSymlinks {
return fc.Fs.Stat(path)
}
fi, err := fc.Fs.Lstat(path)
if err != nil {
return fi, err
}
if fi.Mode()&os.ModeSymlink != 0 {
// if this file is a symlink, skip it
return nil, godirwalk.SkipThis
}
return fi, nil
}
func (fc *FileCount) onlyDirectories(directories []string) []string {
out := make([]string, 0)
for _, path := range directories {

View File

@ -14,6 +14,7 @@ import (
type fileSystem interface {
Open(name string) (file, error)
Stat(name string) (os.FileInfo, error)
Lstat(name string) (os.FileInfo, error)
}
type file interface {
@ -27,5 +28,6 @@ type file interface {
// osFS implements fileSystem using the local disk
type osFS struct{}
func (osFS) Open(name string) (file, error) { return os.Open(name) }
func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
func (osFS) Open(name string) (file, error) { return os.Open(name) }
func (osFS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
func (osFS) Lstat(name string) (os.FileInfo, error) { return os.Lstat(name) }

View File

@ -50,3 +50,8 @@ func (f fakeFileSystem) Stat(name string) (os.FileInfo, error) {
}
return nil, &os.PathError{Op: "Stat", Path: name, Err: errors.New("no such file or directory")}
}
func (f fakeFileSystem) Lstat(name string) (os.FileInfo, error) {
// not able to test with symlinks currently
return f.Stat(name)
}