checkpoint; still broken

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

147
main.go
View File

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