| 1 |
// Package file implements a koanf.Provider that reads raw bytes |
| 2 |
// from files on disk to be used with a koanf.Parser to parse |
| 3 |
// into conf maps. |
| 4 |
package file |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"os" |
| 10 |
"path/filepath" |
| 11 |
"sync" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/fsnotify/fsnotify" |
| 15 |
) |
| 16 |
|
| 17 |
// File implements a File provider. |
| 18 |
type File struct { |
| 19 |
path string |
| 20 |
w *fsnotify.Watcher |
| 21 |
|
| 22 |
// Mutex to protect concurrent access to watcher state |
| 23 |
mu sync.Mutex |
| 24 |
isWatching bool |
| 25 |
} |
| 26 |
|
| 27 |
// Provider returns a file provider. |
| 28 |
func Provider(path string) *File { |
| 29 |
return &File{path: filepath.Clean(path)} |
| 30 |
} |
| 31 |
|
| 32 |
// ReadBytes reads the contents of a file on disk and returns the bytes. |
| 33 |
func (f *File) ReadBytes() ([]byte, error) { |
| 34 |
return os.ReadFile(f.path) |
| 35 |
} |
| 36 |
|
| 37 |
// Read is not supported by the file provider. |
| 38 |
func (f *File) Read() (map[string]any, error) { |
| 39 |
return nil, errors.New("file provider does not support this method") |
| 40 |
} |
| 41 |
|
| 42 |
// Watch watches the file and triggers a callback when it changes. It is a |
| 43 |
// blocking function that internally spawns a goroutine to watch for changes. |
| 44 |
func (f *File) Watch(cb func(event any, err error)) error { |
| 45 |
f.mu.Lock() |
| 46 |
|
| 47 |
// If a watcher already exists, return an error. |
| 48 |
if f.isWatching { |
| 49 |
f.mu.Unlock() |
| 50 |
return errors.New("file is already being watched") |
| 51 |
} |
| 52 |
|
| 53 |
// Resolve symlinks and save the original path so that changes to symlinks |
| 54 |
// can be detected. |
| 55 |
realPath, err := filepath.EvalSymlinks(f.path) |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
realPath = filepath.Clean(realPath) |
| 60 |
|
| 61 |
// Although only a single file is being watched, fsnotify has to watch |
| 62 |
// the whole parent directory to pick up all events such as symlink changes. |
| 63 |
fDir, _ := filepath.Split(f.path) |
| 64 |
|
| 65 |
f.w, err = fsnotify.NewWatcher() |
| 66 |
if err != nil { |
| 67 |
f.mu.Unlock() |
| 68 |
return err |
| 69 |
} |
| 70 |
|
| 71 |
f.isWatching = true |
| 72 |
|
| 73 |
// Set up the directory watch before releasing the lock |
| 74 |
err = f.w.Add(fDir) |
| 75 |
if err != nil { |
| 76 |
f.w.Close() |
| 77 |
f.w = nil |
| 78 |
f.isWatching = false |
| 79 |
f.mu.Unlock() |
| 80 |
return err |
| 81 |
} |
| 82 |
|
| 83 |
// Release the lock before spawning goroutine |
| 84 |
f.mu.Unlock() |
| 85 |
|
| 86 |
var ( |
| 87 |
lastEvent string |
| 88 |
lastEventTime time.Time |
| 89 |
) |
| 90 |
|
| 91 |
go func() { |
| 92 |
loop: |
| 93 |
for { |
| 94 |
select { |
| 95 |
case event, ok := <-f.w.Events: |
| 96 |
if !ok { |
| 97 |
// Only throw an error if we were still supposed to be watching. |
| 98 |
f.mu.Lock() |
| 99 |
stillWatching := f.isWatching |
| 100 |
f.mu.Unlock() |
| 101 |
|
| 102 |
if stillWatching { |
| 103 |
cb(nil, errors.New("fsnotify watch channel closed")) |
| 104 |
} |
| 105 |
|
| 106 |
break loop |
| 107 |
} |
| 108 |
|
| 109 |
// Use a simple timer to buffer events as certain events fire |
| 110 |
// multiple times on some platforms. |
| 111 |
if event.String() == lastEvent && time.Since(lastEventTime) < time.Millisecond*5 { |
| 112 |
continue |
| 113 |
} |
| 114 |
lastEvent = event.String() |
| 115 |
lastEventTime = time.Now() |
| 116 |
|
| 117 |
evFile := filepath.Clean(event.Name) |
| 118 |
|
| 119 |
// Resolve symlink to get the real path, in case the symlink's |
| 120 |
// target has changed. |
| 121 |
curPath, err := filepath.EvalSymlinks(f.path) |
| 122 |
if err != nil { |
| 123 |
cb(nil, err) |
| 124 |
break loop |
| 125 |
} |
| 126 |
curPath = filepath.Clean(curPath) |
| 127 |
|
| 128 |
onWatchedFile := evFile == realPath || evFile == f.path |
| 129 |
|
| 130 |
// Since the event is triggered on a directory, is this |
| 131 |
// a create or write on the file being watched? |
| 132 |
// |
| 133 |
// Or has the real path of the file being watched changed? |
| 134 |
// |
| 135 |
// If either of the above are true, trigger the callback. |
| 136 |
if event.Has(fsnotify.Create|fsnotify.Write) && (onWatchedFile || |
| 137 |
(curPath != "" && curPath != realPath)) { |
| 138 |
realPath = curPath |
| 139 |
|
| 140 |
// Trigger event. |
| 141 |
cb(event, nil) |
| 142 |
} else if onWatchedFile && event.Has(fsnotify.Remove) { |
| 143 |
cb(nil, fmt.Errorf("file %s was removed", event.Name)) |
| 144 |
break loop |
| 145 |
} |
| 146 |
|
| 147 |
// There's an error. |
| 148 |
case err, ok := <-f.w.Errors: |
| 149 |
if !ok { |
| 150 |
// Only throw an error if we were still supposed to be watching. |
| 151 |
f.mu.Lock() |
| 152 |
stillWatching := f.isWatching |
| 153 |
f.mu.Unlock() |
| 154 |
|
| 155 |
if stillWatching { |
| 156 |
cb(nil, errors.New("fsnotify err channel closed")) |
| 157 |
} |
| 158 |
|
| 159 |
break loop |
| 160 |
} |
| 161 |
|
| 162 |
// Pass the error to the callback. |
| 163 |
cb(nil, err) |
| 164 |
break loop |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
f.mu.Lock() |
| 169 |
f.isWatching = false |
| 170 |
if f.w != nil { |
| 171 |
f.w.Close() |
| 172 |
f.w = nil |
| 173 |
} |
| 174 |
f.mu.Unlock() |
| 175 |
}() |
| 176 |
|
| 177 |
return nil |
| 178 |
} |
| 179 |
|
| 180 |
// Unwatch stops watching the files and closes fsnotify watcher. |
| 181 |
func (f *File) Unwatch() error { |
| 182 |
f.mu.Lock() |
| 183 |
defer f.mu.Unlock() |
| 184 |
|
| 185 |
if !f.isWatching { |
| 186 |
return nil // Already unwatched |
| 187 |
} |
| 188 |
|
| 189 |
f.isWatching = false |
| 190 |
if f.w != nil { |
| 191 |
// Close the watcher to signal the goroutine to stop |
| 192 |
// The goroutine will handle setting f.w = nil |
| 193 |
return f.w.Close() |
| 194 |
} |
| 195 |
// This state should ideally never be reached - it indicates a bug in the synchronization logic |
| 196 |
return errors.New("file watcher is in an inconsistent state: isWatching is true but watcher is nil") |
| 197 |
} |