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