@@ -77,39 +77,42 @@ func setupFileWatcher(configFile string) (*fsnotify.Watcher, error) {
7777 return watcher , nil
7878}
7979
80- // handleFileEvent processes file system events
81- func handleFileEvent (event fsnotify.Event , configFile string , bp bpfprogram.Attacher ) {
82- // Check if the event is for our config file
83- if filepath .Base (event .Name ) != filepath .Base (configFile ) {
84- return
85- }
86-
87- log .Printf ("Config file changed: %s (operation: %s)" , event .Name , event .Op )
88-
89- // Small delay to handle rapid successive events
90- time .Sleep (100 * time .Millisecond )
91-
80+ func checkFileStatusAndUpdateBPF (configFile string , bp bpfprogram.Attacher ) {
9281 // Check current state and take action
9382 fileState := isFileEmptyOrMissing (configFile )
9483 switch fileState {
9584 case 1 : // File is empty
9685 log .Println ("File is empty, attaching BPF program" )
97- if err := bp .Attach (); err != nil {
86+ if err := bp .Attach (); err != nil { // No-op if already attached
9887 log .Printf ("Failed to attach BPF program: %v" , err )
9988 }
10089 case 0 : // File has content
10190 log .Println ("File has content, detaching BPF program" )
102- if err := bp .Detach (); err != nil {
91+ if err := bp .Detach (); err != nil { // No-op if already detached
10392 log .Printf ("Failed to detach BPF program: %v" , err )
10493 }
10594 case - 1 : // File is missing
10695 log .Println ("Config file was deleted, detaching BPF program" )
107- if err := bp .Detach (); err != nil {
96+ if err := bp .Detach (); err != nil { // No-op if already detached
10897 log .Printf ("Failed to detach BPF program: %v" , err )
10998 }
11099 }
111100}
112101
102+ // handleFileEvent processes file system events
103+ func handleFileEvent (event fsnotify.Event , configFile string , bp bpfprogram.Attacher ) {
104+ // Check if the event is for our config file
105+ if filepath .Base (event .Name ) != filepath .Base (configFile ) {
106+ return
107+ }
108+
109+ log .Printf ("Config file changed: %s (operation: %s)" , event .Name , event .Op )
110+
111+ // Small delay to handle rapid successive events
112+ time .Sleep (100 * time .Millisecond )
113+ checkFileStatusAndUpdateBPF (configFile , bp )
114+ }
115+
113116// run is the main application logic, separated for easier testing
114117func run (config * BlockConfig ) error {
115118 log .Printf ("Using config file: %s" , config .ConfigFile )
@@ -123,19 +126,8 @@ func run(config *BlockConfig) error {
123126 bp := config .AttacherFactory ()
124127 defer bp .Close ()
125128
126- // Initial state check
127- fileState := isFileEmptyOrMissing (config .ConfigFile )
128- switch fileState {
129- case 1 : // File is empty
130- log .Println ("File is empty, attaching BPF program" )
131- if err := bp .Attach (); err != nil {
132- return fmt .Errorf ("failed to attach BPF program: %w" , err )
133- }
134- case 0 : // File has content
135- log .Println ("Config file has content, BPF program will remain detached" )
136- case - 1 : // File is missing
137- log .Println ("Config file is missing, waiting for file to be created..." )
138- }
129+ // Check initial state of the config file
130+ checkFileStatusAndUpdateBPF (config .ConfigFile , bp )
139131
140132 // Setup file watcher
141133 watcher , err := setupFileWatcher (config .ConfigFile )
0 commit comments