fix(inputs.filecount): Respect symlink files with FollowSymLinks (#14669)
This commit is contained in:
parent
c981bb7819
commit
1d513e6379
|
|
@ -130,7 +130,7 @@ func (fc *FileCount) count(acc telegraf.Accumulator, basedir string, glob globpa
|
||||||
if err == nil && rel == "." {
|
if err == nil && rel == "." {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
file, err := fc.Fs.Stat(path)
|
file, err := fc.resolveLink(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -244,6 +244,21 @@ func (fc *FileCount) Gather(acc telegraf.Accumulator) error {
|
||||||
return nil
|
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 {
|
func (fc *FileCount) onlyDirectories(directories []string) []string {
|
||||||
out := make([]string, 0)
|
out := make([]string, 0)
|
||||||
for _, path := range directories {
|
for _, path := range directories {
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ import (
|
||||||
type fileSystem interface {
|
type fileSystem interface {
|
||||||
Open(name string) (file, error)
|
Open(name string) (file, error)
|
||||||
Stat(name string) (os.FileInfo, error)
|
Stat(name string) (os.FileInfo, error)
|
||||||
|
Lstat(name string) (os.FileInfo, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type file interface {
|
type file interface {
|
||||||
|
|
@ -27,5 +28,6 @@ type file interface {
|
||||||
// osFS implements fileSystem using the local disk
|
// osFS implements fileSystem using the local disk
|
||||||
type osFS struct{}
|
type osFS struct{}
|
||||||
|
|
||||||
func (osFS) Open(name string) (file, error) { return os.Open(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) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
|
||||||
|
func (osFS) Lstat(name string) (os.FileInfo, error) { return os.Lstat(name) }
|
||||||
|
|
|
||||||
|
|
@ -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")}
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue