Skip to content

Commit 581ecd7

Browse files
committed
fix(scan): reuse symlink target stat for size checks
1 parent 0e09bfa commit 581ecd7

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

internal/scanwalk.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func WalkScanFiles(input WalkScanFilesInput) error {
3636
return fmt.Errorf("stat %s: %w", input.RootPath, err)
3737
}
3838
if !info.IsDir() {
39-
exceedsLimit, err := exceedsSizeLimit(input.RootPath, input.MaxFileSize)
39+
exceedsLimit, err := exceedsSizeLimitInfo(input.RootPath, info, input.MaxFileSize)
4040
if err != nil {
4141
return fmt.Errorf("checking file size %s: %w", input.RootPath, err)
4242
}
@@ -70,6 +70,7 @@ func WalkScanFiles(input WalkScanFilesInput) error {
7070
return nil
7171
}
7272

73+
var sizeInfo os.FileInfo
7374
if d.Type()&fs.ModeSymlink != 0 {
7475
resolvedPath, resolvedInfo, ok := resolveScanSymlink(path)
7576
if !ok {
@@ -83,9 +84,10 @@ func WalkScanFiles(input WalkScanFilesInput) error {
8384
slog.Debug("skipping symlink outside scan root", "path", path, "target", resolvedPath)
8485
return nil
8586
}
87+
sizeInfo = resolvedInfo
8688
}
8789

88-
exceedsLimit, err := exceedsSizeLimit(path, input.MaxFileSize)
90+
exceedsLimit, err := exceedsSizeLimitInfo(path, sizeInfo, input.MaxFileSize)
8991
if err != nil {
9092
return fmt.Errorf("checking file size %s: %w", path, err)
9193
}
@@ -151,13 +153,16 @@ func pathWithinBoundary(path, rootBoundary string) bool {
151153
return !strings.HasPrefix(rel, ".."+string(os.PathSeparator))
152154
}
153155

154-
func exceedsSizeLimit(path string, maxFileSize int64) (bool, error) {
156+
func exceedsSizeLimitInfo(path string, info os.FileInfo, maxFileSize int64) (bool, error) {
155157
if maxFileSize <= 0 {
156158
return false, nil
157159
}
158-
info, err := os.Stat(path)
159-
if err != nil {
160-
return false, fmt.Errorf("stat %s: %w", path, err)
160+
if info == nil {
161+
statInfo, err := os.Stat(path)
162+
if err != nil {
163+
return false, fmt.Errorf("stat %s: %w", path, err)
164+
}
165+
info = statInfo
161166
}
162167
if info.Size() <= maxFileSize {
163168
return false, nil

0 commit comments

Comments
 (0)