func getFiles()

in agent/inventory/gatherers/file/dataProvider.go [92:155]


func getFiles(path string, pattern []string, recursive bool, fileLimit int, dirLimit int) (validFiles []string, err error) {
	var ex bool
	ex, err = existsPath(path)
	if err != nil {
		log.GetLogger().Error(err)
		return
	}
	if !ex {
		log.GetLogger().Error(fmt.Errorf("Path %v does not exist!", path))
		return
	}
	dirScanCount := 0
	if recursive {
		err = filepathWalk(path, func(fp string, fi os.FileInfo, err error) error {
			if err != nil {
				log.GetLogger().Error(err)
				return nil
			}
			if fi.IsDir() {
				dirScanCount++
				if dirScanCount > dirLimit {
					log.GetLogger().Errorf("Scanned maximum allowed directories. Returning collected files")
					return DirScanLimitError
				}
				return nil

			}
			if fileMatchesAnyPattern(pattern, fi.Name()) {
				validFiles = append(validFiles, fp)
				if len(validFiles) > fileLimit {
					log.GetLogger().Errorf("Found more than limit of %d files", FileCountLimit)
					return FileCountLimitError
				}
			}
			return nil
		})
	} else {
		files, readDirErr := readDirFunc(path)
		if readDirErr != nil {
			log.GetLogger().Error(readDirErr)
			err = readDirErr
			return
		}

		dirScanCount++
		for _, fi := range files {
			if fi.IsDir() {
				continue
			}
			if fileMatchesAnyPattern(pattern, fi.Name()) {
				validFiles = append(validFiles, filepath.Join(path, fi.Name()))
				if len(validFiles) > fileLimit {
					log.GetLogger().Errorf("Found more than limit of %d files", FileCountLimit)
					err = FileCountLimitError
					return
				}
			}
		}

	}

	log.GetLogger().Debugf("DirScanned %d", dirScanCount)
	return
}