File: walk.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (49 lines) | stat: -rw-r--r-- 1,324 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package pin

import (
	"fmt"
	"io/fs"
	"os"
	"path/filepath"

	"github.com/cilium/ebpf/internal/linux"
	"github.com/cilium/ebpf/internal/unix"
)

// WalkDirFunc is the type of the function called for each object visited by
// [WalkDir]. It's identical to [fs.WalkDirFunc], but with an extra [Pinner]
// argument. If the visited node is a directory, obj is nil.
//
// err contains any errors encountered during bpffs traversal or object loading.
type WalkDirFunc func(path string, d fs.DirEntry, obj Pinner, err error) error

// WalkDir walks the file tree rooted at path, calling bpffn for each node in
// the tree, including directories. Running WalkDir on a non-bpf filesystem is
// an error. Otherwise identical in behavior to [fs.WalkDir].
//
// See the [WalkDirFunc] for more information.
func WalkDir(root string, bpffn WalkDirFunc) error {
	fsType, err := linux.FSType(root)
	if err != nil {
		return err
	}
	if fsType != unix.BPF_FS_MAGIC {
		return fmt.Errorf("%s is not on a bpf filesystem", root)
	}

	fn := func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return bpffn(path, nil, nil, err)
		}

		if d.IsDir() {
			return bpffn(path, d, nil, err)
		}

		obj, err := Load(filepath.Join(root, path), nil)

		return bpffn(path, d, obj, err)
	}

	return fs.WalkDir(os.DirFS(root), ".", fn)
}