Skip to content

Commit 9e53e3a

Browse files
committed
pkg/manager: remove a dependency on grep
Instead of calling grep (the implementations of which may differ in different environments), traverse the directory and grep files with a special pkg/osutil helper functionality.
1 parent 1458b36 commit 9e53e3a

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

pkg/manager/diff.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -816,25 +816,21 @@ func affectedFiles(cfg *mgrconfig.Config, gitPatches [][]byte) (direct, transiti
816816
for _, file := range allFiles {
817817
directMap[file] = struct{}{}
818818
if strings.HasSuffix(file, ".h") && cfg.KernelSrc != "" {
819+
// For .h files, we want to determine all the .c files that include them.
819820
// Ideally, we should combine this with the recompilation process - then we know
820821
// exactly which files were affected by the patch.
821-
out, err := osutil.RunCmd(time.Minute, cfg.KernelSrc, "/usr/bin/grep",
822-
"-rl", "--include", `*.c`, `<`+strings.TrimPrefix(file, "include/")+`>`)
822+
matching, err := osutil.GrepFiles(cfg.KernelSrc, `.c`,
823+
[]byte(`<`+strings.TrimPrefix(file, "include/")+`>`))
823824
if err != nil {
824-
log.Logf(0, "failed to grep for the header usages: %v", err)
825+
log.Logf(0, "failed to grep for includes: %s", err)
825826
continue
826827
}
827-
lines := strings.Split(string(out), "\n")
828-
if len(lines) >= maxAffectedByHeader {
828+
if len(matching) >= maxAffectedByHeader {
829829
// It's too widespread. It won't help us focus on anything.
830-
log.Logf(0, "the header %q is included in too many files (%d)", file, len(lines))
830+
log.Logf(0, "the header %q is included in too many files (%d)", file, len(matching))
831831
continue
832832
}
833-
for _, name := range lines {
834-
name = strings.TrimSpace(name)
835-
if name == "" {
836-
continue
837-
}
833+
for _, name := range matching {
838834
transitiveMap[name] = struct{}{}
839835
}
840836
}

pkg/osutil/fileutil.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package osutil
55

66
import (
7+
"bytes"
78
"fmt"
89
"io"
10+
"io/fs"
911
"os"
1012
"path/filepath"
1113
)
@@ -81,3 +83,28 @@ func WriteTempFile(data []byte) (string, error) {
8183
f.Close()
8284
return f.Name(), nil
8385
}
86+
87+
// GrepFiles returns the list of files (relative to root) that include target.
88+
// If ext is not empty, the files will be filtered by the extension.
89+
// The function assumes that the files are not too big and may fit in memory.
90+
func GrepFiles(root, ext string, target []byte) ([]string, error) {
91+
var ret []string
92+
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
93+
if err != nil {
94+
return err
95+
}
96+
if d.IsDir() || filepath.Ext(path) != ext {
97+
return nil
98+
}
99+
content, err := os.ReadFile(path)
100+
if err != nil {
101+
return fmt.Errorf("failed to open %s: %w", path, err)
102+
}
103+
if bytes.Contains(content, target) {
104+
rel, _ := filepath.Rel(root, path)
105+
ret = append(ret, rel)
106+
}
107+
return nil
108+
})
109+
return ret, err
110+
}

pkg/osutil/fileutil_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"strconv"
1010
"sync"
1111
"testing"
12+
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1215
)
1316

1417
func TestProcessTempDir(t *testing.T) {
@@ -60,3 +63,17 @@ func TestProcessTempDir(t *testing.T) {
6063
}()
6164
}
6265
}
66+
67+
func TestGrepFiles(t *testing.T) {
68+
dir := t.TempDir()
69+
require.NoError(t, FillDirectory(dir, map[string]string{
70+
"a.c": `must be found`,
71+
"a/b.c": `nested`,
72+
"a/b/c.c": `even more nested, must be found`,
73+
"a.txt": `must be found, but has a different extension`,
74+
}))
75+
76+
ret, err := GrepFiles(dir, ".c", []byte(`must be found`))
77+
require.NoError(t, err)
78+
assert.ElementsMatch(t, []string{"a.c", "a/b/c.c"}, ret)
79+
}

0 commit comments

Comments
 (0)