checkpoint; still broken

This commit is contained in:
Jeffrey Paul 2019-09-24 13:08:08 -07:00
parent 87d134a877
commit 5b1ba9a3f2

151
main.go
View File

@ -8,6 +8,8 @@ import "github.com/sirupsen/logrus"
import "github.com/multiformats/go-multihash"
import "github.com/mr-tron/base58"
import "github.com/pkg/xattr"
//import "errors"
import "os"
import "io"
import "flag"
@ -58,13 +60,33 @@ func xsum() int {
}
switch flag.Arg(0) {
case "cron":
return xsfCheckAndUpdate(paths)
x := xsfCheckAndUpdate(paths)
if x != nil {
return -1
} else {
return 0
}
case "check-and-update":
return xsfCheckAndUpdate(paths)
x := xsfCheckAndUpdate(paths)
if x != nil {
return -1
} else {
return 0
}
case "check":
return xsfCheck(paths)
x := xsfCheck(paths)
if x != nil {
return -1
} else {
return 0
}
case "update":
return xsfUpdate(paths)
x := xsfUpdate(paths)
if x != nil {
return -1
} else {
return 0
}
default:
usage()
return -1
@ -76,33 +98,40 @@ func usage() {
flag.PrintDefaults()
}
func xsfCheck(paths []string) int {
func xsfCheck(paths []string) error {
log.Debugf("check")
log.Fatalf("not implemented")
return 0
for _, path := range paths {
x := newXsf(path)
err := x.Check()
if err != nil {
return err
}
}
return nil
}
func showError(e error) {
fmt.Fprintf(os.Stderr, "error: %s\n", e)
}
func xsfUpdate(paths []string) int {
func xsfUpdate(paths []string) error {
log.Debugf("update")
for _, path := range paths {
x := newXsf(path)
err := x.Update()
if err != nil {
showError(err)
return -1
return err
}
}
return 0
return nil
}
func xsfCheckAndUpdate(paths []string) int {
func xsfCheckAndUpdate(paths []string) error {
log.Debugf("check-and-update")
r := xsfCheck(paths)
if r != 0 {
if r != nil {
return r
}
return xsfUpdate(paths)
@ -131,7 +160,7 @@ func HashFile(fp *os.File) (string, error) {
type xsf struct {
fi *os.FileInfo
fp *os.File
hash string
multihash string
mtime string
path string
size int64
@ -141,21 +170,30 @@ type xsf struct {
// constructor
/////////////////////////////////////////////////////////////////////////////////
type xsf struct {
fi *os.FileInfo
fp *os.File
hash string
mtime string
path string
size int64
}
func newXsf(path string) *xsf {
x := xsf{}
x.path = path
return &x
}
func (x *xsf) readXattrs(fp *os.File) error {
log.Infof("reading xattrs")
var xn string
var err error
var v []byte
xn = fmt.Sprintf("%s.%s", namespacePrefix, "mtime")
v, err = xattr.FGet(x.fp, xn)
xn = fmt.Sprintf("%s.%s", namespacePrefix, "size")
v, err = xattr.FGet(x.fp, xn)
xn = fmt.Sprintf("%s.%s", namespacePrefix, "multihash")
v, err = xattr.FGet(x.fp, xn)
return nil //FIXME
}
func (x *xsf) writeXattrs(fp *os.File) error {
log.Infof("writing xattrs")
@ -177,8 +215,8 @@ func (x *xsf) writeXattrs(fp *os.File) error {
}
xn = fmt.Sprintf("%s.%s", namespacePrefix, "multihash")
log.Infof("writing xattr %s=%s", xn, x.hash)
err = xattr.FSet(fp, xn, []byte(x.hash))
log.Infof("writing xattr %s=%s", xn, x.multihash)
err = xattr.FSet(fp, xn, []byte(x.multihash))
if err != nil {
return err
}
@ -186,37 +224,60 @@ func (x *xsf) writeXattrs(fp *os.File) error {
return nil
}
func (x *xsf) Update() error {
fp, e1 := os.Open(x.path)
defer fp.Close()
log.Infof("updating file")
log.Debugf("path: %s", x.path)
if e1 != nil {
return e1
}
fi, e2 := fp.Stat()
if e2 != nil {
return e2
func (x *xsf) stat(fp *os.File) error {
fi, err := fp.Stat()
if err != nil {
return err
}
x.size = fi.Size()
log.Debugf("size: %d", x.size)
t := fi.ModTime().UTC().Format(time.RFC3339)
log.Debugf("modtime: %s", t)
x.mtime = t
log.Debugf("hashing...")
h, e3 := HashFile(fp)
if e3 != nil {
return e3
return nil
}
x.hash = h
log.Debugf("hash: %s", h)
e4 := x.writeXattrs(fp)
func (x *xsf) hash(fp *os.File) error {
log.Debugf("hashing...")
var err error
if x.multihash, err = HashFile(fp); err != nil {
return err
}
log.Debugf("hash: %s", x.multihash)
return nil
}
if e4 != nil {
return e4
func (x *xsf) Check() error {
fp, err := os.Open(x.path)
defer fp.Close()
if err != nil {
return err
}
x.stat(x.fp)
return nil
}
func (x *xsf) Update() error {
fp, err := os.Open(x.path)
defer fp.Close()
log.Debugf("updating file (path: %s)", x.path)
if err != nil {
return err
}
//FIXME check if size/mtime are defined first
//and skip update if match (and key 'multihash' exists)
if err = x.stat(fp); err != nil {
return err
}
if err = x.hash(fp); err != nil {
return err
}
if err = x.writeXattrs(fp); err != nil {
return err
}
return nil