diff --git a/pkg/cgfsmon/private.go b/pkg/cgfsmon/private.go index 9cb9ce2..cacad9e 100644 --- a/pkg/cgfsmon/private.go +++ b/pkg/cgfsmon/private.go @@ -3,12 +3,86 @@ package cgfsmon import ( "context" "errors" + "io/fs" "os" + "path/filepath" + "strings" "github.com/black-desk/cgtproxy/pkg/types" fsevents "github.com/tywkeene/go-fsevents" ) +func (m *CGroupFSMonitor) walkFn(ctx context.Context) func(path string, d fs.DirEntry, err error) error { + return func(path string, d fs.DirEntry, err error) error { + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + m.log.Debug( + "Cgroup had been removed.", + "path", path, + ) + err = nil + } + m.log.Errorw( + "Errors occurred while first time going through cgroupfs.", + "path", path, + "error", err, + ) + err = nil + } + + if !d.IsDir() { + return nil + } + + cgEvent := &types.CGroupEvent{ + Path: path, + EventType: types.CgroupEventTypeNew, + } + + err = m.send(ctx, cgEvent) + if err != nil { + return err + } + + return nil + } +} + +func (m *CGroupFSMonitor) walk(ctx context.Context, path string) { + err := filepath.WalkDir(path, m.walkFn(ctx)) + if err == nil { + return + } + + return +} + +func (m *CGroupFSMonitor) send(ctx context.Context, cgEvent *types.CGroupEvent) (err error) { + path := strings.TrimRight(cgEvent.Path, "/") + cgEvent.Path = path + + if cgEvent.Path == string(m.root) { + // NOTE: Ignore cgroup root. + return nil + } + + m.log.Debugw("New cgroup envent.", + "event", cgEvent, + ) + + select { + case <-ctx.Done(): + err = ctx.Err() + return + case m.events <- *cgEvent: + m.log.Debugw("Cgroup event sent.", + "path", path, + ) + } + + return +} + type handle struct { ctx context.Context mon *CGroupFSMonitor diff --git a/pkg/cgfsmon/public.go b/pkg/cgfsmon/public.go index 70c150a..9820074 100644 --- a/pkg/cgfsmon/public.go +++ b/pkg/cgfsmon/public.go @@ -2,10 +2,6 @@ package cgfsmon import ( "context" - "errors" - "io/fs" - "path/filepath" - "strings" "github.com/black-desk/cgtproxy/pkg/types" . "github.com/black-desk/lib/go/errwrap" @@ -16,77 +12,6 @@ func (w *CGroupFSMonitor) Events() <-chan types.CGroupEvent { return w.events } -func (m *CGroupFSMonitor) walkFn(ctx context.Context) func(path string, d fs.DirEntry, err error) error { - return func(path string, d fs.DirEntry, err error) error { - if err != nil { - if errors.Is(err, fs.ErrNotExist) { - m.log.Debug( - "Cgroup had been removed.", - "path", path, - ) - err = nil - } - m.log.Errorw( - "Errors occurred while first time going through cgroupfs.", - "path", path, - "error", err, - ) - err = nil - } - - if !d.IsDir() { - return nil - } - - cgEvent := &types.CGroupEvent{ - Path: path, - EventType: types.CgroupEventTypeNew, - } - - err = m.send(ctx, cgEvent) - if err != nil { - return err - } - - return nil - } -} - -func (m *CGroupFSMonitor) walk(ctx context.Context, path string) { - err := filepath.WalkDir(path, m.walkFn(ctx)) - if err == nil { - return - } - - return -} - -func (m *CGroupFSMonitor) send(ctx context.Context, cgEvent *types.CGroupEvent) (err error) { - path := strings.TrimRight(cgEvent.Path, "/") - cgEvent.Path = path - - if cgEvent.Path == string(m.root) { - // NOTE: Ignore cgroup root. - return nil - } - - m.log.Debugw("New cgroup envent.", - "event", cgEvent, - ) - - select { - case <-ctx.Done(): - err = ctx.Err() - return - case m.events <- *cgEvent: - m.log.Debugw("Cgroup event sent.", - "path", path, - ) - } - - return -} - func (w *CGroupFSMonitor) Run(ctx context.Context) (err error) { defer Wrap(&err, "running filesystem watcher") defer close(w.events)