glob.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package file
  2. import (
  3. "os"
  4. "path/filepath"
  5. "path"
  6. "go-common/library/log"
  7. )
  8. func (f *File) getFiles() map[string]os.FileInfo {
  9. paths := map[string]os.FileInfo{}
  10. for _, p := range f.c.Paths {
  11. // logs in docker overlay2
  12. if MergedDir := f.ctx.Value("MergedDir"); MergedDir != nil {
  13. p = path.Join(MergedDir.(string), p)
  14. }
  15. matches, err := filepath.Glob(p)
  16. if err != nil {
  17. log.Error("glob(%s) failed: %v", p, err)
  18. continue
  19. }
  20. // Check any matched files to see if we need to start a harvester
  21. for _, file := range matches {
  22. // check if the file is in the exclude_files list
  23. //if f.isFileExcluded(file) {
  24. // log.Info("input", "Exclude file: %s", file)
  25. // continue
  26. //}
  27. // Fetch Lstat File info to detected also symlinks
  28. fileInfo, err := os.Lstat(file)
  29. if err != nil {
  30. log.Warn("lstat(%s) failed: %s", file, err)
  31. continue
  32. }
  33. if fileInfo.IsDir() {
  34. log.Warn("Skipping directory: %s", file)
  35. continue
  36. }
  37. isSymlink := fileInfo.Mode()&os.ModeSymlink > 0
  38. if isSymlink && !f.c.Symlinks {
  39. log.Warn("File %s skipped as it is a symlink.", file)
  40. continue
  41. }
  42. // Fetch Stat file info which fetches the inode. In case of a symlink, the original inode is fetched
  43. fileInfo, err = os.Stat(file)
  44. if err != nil {
  45. log.Warn("stat(%s) failed: %s", file, err)
  46. continue
  47. }
  48. // If symlink is enabled, it is checked that original is not part of same input
  49. // It original is harvested by other input, states will potentially overwrite each other
  50. //if p.config.Symlinks {
  51. // for _, finfo := range paths {
  52. // if os.SameFile(finfo, fileInfo) {
  53. // log.Info("Same file found as symlink and originap. Skipping file: %s", file)
  54. // continue OUTER
  55. // }
  56. // }
  57. //}
  58. paths[file] = fileInfo
  59. }
  60. }
  61. return paths
  62. }