2023-02-18 23:37:11 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
// Watches for changes to a directory. Works cross-platform
|
|
|
|
|
|
|
|
import (
|
2023-12-16 09:02:59 -06:00
|
|
|
"log"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2023-02-18 23:37:11 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// This would be a really dumb way to watch for new network interfaces
|
|
|
|
// since it then watches a linux only directory /sys/class/net for changes
|
|
|
|
|
|
|
|
func watchSysClassNet() {
|
|
|
|
// Create new watcher.
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
2023-12-20 03:13:43 -06:00
|
|
|
debug(LogError, "watchSysClassNet() failed:", err)
|
2023-03-28 08:53:51 -05:00
|
|
|
return
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
// Start listening for events.
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-watcher.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2023-12-16 09:02:59 -06:00
|
|
|
log.Println("event:", event)
|
2023-02-18 23:37:11 -06:00
|
|
|
if event.Has(fsnotify.Write) {
|
2023-12-16 09:02:59 -06:00
|
|
|
log.Println("modified file:", event.Name)
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
case err, ok := <-watcher.Errors:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2023-12-16 09:02:59 -06:00
|
|
|
log.Println("error:", err)
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Add a path.
|
|
|
|
err = watcher.Add("/tmp")
|
|
|
|
if err != nil {
|
2023-12-20 03:13:43 -06:00
|
|
|
debug(LogError, "watchSysClassNet() watcher.Add() failed:", err)
|
2023-03-28 08:53:51 -05:00
|
|
|
return
|
2023-02-18 23:37:11 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Block main goroutine forever.
|
|
|
|
<-make(chan struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func fsnotifyNetworkInterfaceChanges() error {
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer watcher.Close()
|
|
|
|
|
|
|
|
// Watch for network interface changes
|
|
|
|
err = watcher.Add("/sys/class/net")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-watcher.Events:
|
2023-12-16 09:02:59 -06:00
|
|
|
log.Println("fsnotifyNetworkInterfaceChanges() event =", event)
|
2023-02-18 23:37:11 -06:00
|
|
|
if event.Op&fsnotify.Create == fsnotify.Create {
|
|
|
|
// Do something on network interface creation
|
|
|
|
}
|
|
|
|
case err := <-watcher.Errors:
|
2023-12-16 09:02:59 -06:00
|
|
|
log.Println("fsnotifyNetworkInterfaceChanges() event err =", err)
|
2023-02-18 23:37:11 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|