diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d616c8c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +xsum diff --git a/Makefile b/Makefile index a1ba2b8..c099c92 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ GOFLAGS = -ldflags "$(GOLDFLAGS)" default: rundebug rundebug: build - DEBUG=1 ./xsum + ./xsum -d run: build ./xsum diff --git a/main.go b/main.go new file mode 100644 index 0000000..f19164c --- /dev/null +++ b/main.go @@ -0,0 +1,128 @@ +package main + +//import "crypto/sha256" +import "fmt" +import "github.com/sirupsen/logrus" + +//import "github.com/pkg/xattr" +import "hash" +import "os" +import "flag" +import "time" + +var Version string +var Buildtime string +var Builduser string +var Buildarch string +var log *logrus.Logger + +func main() { + os.Exit(xsum()) +} + +func xsum() int { + log = logrus.New() + log.SetLevel(logrus.ErrorLevel) + log.SetReportCaller(false) + debugPtr := flag.Bool("d", false, "debug mode") + flag.Parse() + if *debugPtr == true { + log.SetReportCaller(true) + log.SetLevel(logrus.DebugLevel) + } + + log.Debugf( + "xsum version %s (%s) built %s by %s", + Version, + Buildarch, + Buildtime, + Builduser, + ) + + paths := flag.Args() + fmt.Printf("%+v\n", paths) + if len(paths) > 1 { + paths = paths[1:] + } + fmt.Printf("%+v\n", paths) + switch flag.Arg(0) { + case "cron": + return xsfCheckAndUpdate(paths) + case "check-and-update": + return xsfCheckAndUpdate(paths) + case "check": + return xsfCheck(paths) + case "update": + return xsfUpdate(paths) + default: + usage() + return -1 + } +} + +func usage() { + fmt.Fprintf(os.Stderr, "usage: %s [-d] [path2] [...]\n", os.Args[0]) + flag.PrintDefaults() +} + +func xsfCheck(paths []string) int { + log.Debugf("check") + return 0 +} + +func showError(e *error) { + fmt.Fprintf(os.Stderr, "error: %s\n", e) +} + +func xsfUpdate(paths []string) int { + log.Debugf("update") + for _, path := range paths { + x := NewXsf(path) + err := x.Update() + if err != nil { + showError(&err) + return -1 + } + } + return 0 +} + +func xsfCheckAndUpdate(paths []string) int { + log.Debugf("check-and-update") + r := xsfCheck(paths) + if r != 0 { + return r + } + return xsfUpdate(paths) +} + +type xsf struct { + path string + mtime *time.Time + size int64 + sum *hash.Hash + fi *os.FileInfo +} + +func (x *xsf) Update() error { + fp, e1 := os.Open(x.path) + defer fp.Close() + if e1 != nil { + return e1 + } + + fi, e2 := fp.Stat() + if e2 != nil { + return e2 + } + x.size = fi.Size() + t := fi.ModTime() + x.mtime = &t + return nil +} + +func NewXsf(path string) *xsf { + x := xsf{} + x.path = path + return &x +}