xsum/main.go

129 lines
2.1 KiB
Go

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] <update|check|check-and-update|cron> <path> [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
}