Pull request: 3906 filewalker read limit

Merge in DNS/adguard-home from 3906-filewalker-constraint to master

Updates #3906.

Squashed commit of the following:

commit e1b654420a5031dbfe69d86c3fcf16a95a9ca935
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Dec 1 15:45:26 2021 +0300

    aghos: rm filewalker limit
This commit is contained in:
Eugene Burkov 2021-12-01 16:26:46 +03:00
parent b92b5fb46a
commit 9c6d139913
1 changed files with 6 additions and 18 deletions

View File

@ -5,7 +5,6 @@ import (
"io" "io"
"io/fs" "io/fs"
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
"github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/stringutil" "github.com/AdguardTeam/golibs/stringutil"
) )
@ -13,18 +12,15 @@ import (
// FileWalker is the signature of a function called for files in the file tree. // FileWalker is the signature of a function called for files in the file tree.
// As opposed to filepath.Walk it only walk the files (not directories) matching // As opposed to filepath.Walk it only walk the files (not directories) matching
// the provided pattern and those returned by function itself. All patterns // the provided pattern and those returned by function itself. All patterns
// should be valid for fs.Glob. If cont is false, the walking terminates. Each // should be valid for fs.Glob. If FileWalker returns false for cont then
// opened file is also limited for reading to MaxWalkedFileSize. // walking terminates. Prefer using bufio.Scanner to read the r since the input
// is not limited.
// //
// TODO(e.burkov, a.garipov): Move into another package like aghfs. // TODO(e.burkov, a.garipov): Move into another package like aghfs.
// //
// TODO(e.burkov): Think about passing filename or any additional data. // TODO(e.burkov): Think about passing filename or any additional data.
type FileWalker func(r io.Reader) (patterns []string, cont bool, err error) type FileWalker func(r io.Reader) (patterns []string, cont bool, err error)
// MaxWalkedFileSize is the maximum length of the file that FileWalker can
// check.
const MaxWalkedFileSize = 1024 * 1024
// checkFile tries to open and process a single file located on sourcePath in // checkFile tries to open and process a single file located on sourcePath in
// the specified fsys. The path is skipped if it's a directory. // the specified fsys. The path is skipped if it's a directory.
func checkFile( func checkFile(
@ -48,20 +44,12 @@ func checkFile(
var fi fs.FileInfo var fi fs.FileInfo
if fi, err = f.Stat(); err != nil { if fi, err = f.Stat(); err != nil {
return nil, true, err return nil, true, err
} } else if fi.IsDir() {
if fi.IsDir() {
// Skip the directories. // Skip the directories.
return nil, true, nil return nil, true, nil
} }
var r io.Reader return c(f)
// Ignore the error since LimitReader function returns error only if passed
// limit value is less than zero, but the constant used.
//
// TODO(e.burkov): Make variable.
r, _ = aghio.LimitReader(f, MaxWalkedFileSize)
return c(r)
} }
// handlePatterns parses the patterns in fsys and ignores duplicates using // handlePatterns parses the patterns in fsys and ignores duplicates using