remote-challenge-sync-airbus: Add inotify watcher
This commit is contained in:
parent
367e686e8a
commit
cc1b212cca
3 changed files with 148 additions and 70 deletions
|
|
@ -1,50 +1,42 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func loadTS(tspath string) (timestamp *time.Time, err error) {
|
||||
func loadTS(tspath string) (timestamp map[AirbusUserId]time.Time, err error) {
|
||||
var fd *os.File
|
||||
if _, err = os.Stat(tspath); os.IsNotExist(err) {
|
||||
init := time.Unix(0, 0)
|
||||
timestamp = &init
|
||||
|
||||
timestamp = map[AirbusUserId]time.Time{}
|
||||
err = saveTS(tspath, timestamp)
|
||||
return
|
||||
}
|
||||
} else if fd, err = os.Open(tspath); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer fd.Close()
|
||||
jdec := json.NewDecoder(fd)
|
||||
|
||||
if err = jdec.Decode(×tamp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var fd *os.File
|
||||
fd, err = os.Open(tspath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
var init int64
|
||||
_, err = fmt.Fscanf(fd, "%d", &init)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tmp := time.Unix(init, 0)
|
||||
timestamp = &tmp
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func saveTS(tspath string, ts *time.Time) error {
|
||||
fd, err := os.Create(tspath)
|
||||
if err != nil {
|
||||
func saveTS(tspath string, ts map[AirbusUserId]time.Time) error {
|
||||
if fd, err := os.Create(tspath); err != nil {
|
||||
return err
|
||||
}
|
||||
defer fd.Close()
|
||||
} else {
|
||||
defer fd.Close()
|
||||
jenc := json.NewEncoder(fd)
|
||||
|
||||
_, err = fmt.Fprintf(fd, "%d", ts.Unix())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := jenc.Encode(ts); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue