Fix the "vim swaps files" bug

This commit is contained in:
Aaron Fischer 2016-07-21 23:51:25 +02:00
parent 145ecf4c4c
commit 67462dcf04

View file

@ -58,7 +58,10 @@ func watchForConfigChanges(configFile string) {
// The file is changed. This is the only event we are interested
// in. If the file is renamed, removed or something else, we drop
// an error to the user.
if event.Op&fsnotify.Write == fsnotify.Write {
if event.Op&fsnotify.Write == fsnotify.Write ||
event.Op&fsnotify.Remove == fsnotify.Remove {
if _, err := os.Stat(event.Name); err == nil {
log.Println("Reload the config file ...")
// TODO: Parse the config file
// TODO: Handle errors in the config file
@ -67,16 +70,21 @@ func watchForConfigChanges(configFile string) {
// TODO: Transfer the new key mappings
// TODO: Close the connection?
}
}
// TODO: vim has a very strange behaviour to save a file. There is
// no WRITE event triggered. Instead: RENAME -> CHMOD -> REMOVE is
// called on that file and after that, this mechanism lost track of
// the file. Why is that tha case? Cant find a valid answer to that.
if event.Op&fsnotify.Rename == fsnotify.Rename ||
event.Op&fsnotify.Remove == fsnotify.Remove {
log.Println("The configuration file is renamed or removed. We cant do any further changes without the config file. So create the file or rename it back and restart the agent.")
// Some editors have a feature calles "swap save" (like vim), which
// will trigger a simple WRITE event, instead it wil trigger RENAME
// -> CHMOD -> REMOVE in that order. So we lost track of the file.
// To prevent this, we add the file again, if it exist after a
// REMOVE event.
if event.Op&fsnotify.Remove == fsnotify.Remove {
if _, err := os.Stat(event.Name); err == nil {
watcher.Add(event.Name)
} else {
log.Fatal("Lost the configuration file. We stop now.")
stop()
}
}
case err := <-watcher.Errors:
log.Println("We have the following problem with the configuration file: ", err)
log.Println("Try the fix this problem by yourself and restart the agent.")