func main() { flag.Parse() args := flag.Args() for _, path := range args { hash, _ := utils.HashFile(path) fmt.Println(hash) } }
func (c *WOFClone) HasChanged(local string, remote string) (bool, error) { change := true local_hash, err := utils.HashFile(local) if err != nil { c.Logger.Error("Failed to hash %s, becase %v", local, err) return change, err } return c.HasHashChanged(local_hash, remote) }
func (sink *Sync) HasChanged(source string, dest string) (ch bool, err error) { sink.Logger.Debug("HEAD s3://%s/%s", sink.Bucket, dest) // https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#HeadObjectInput // https://docs.aws.amazon.com/sdk-for-go/api/service/s3/#HeadObjectOutput params := &s3.HeadObjectInput{ Bucket: aws.String(sink.Bucket), Key: aws.String(dest), } // sink.Logger.Debug(params.GoString()) rsp, err := sink.Service.HeadObject(params) if err != nil { aws_err := err.(awserr.Error) if aws_err.Code() == "NotFound" { sink.Logger.Info("%s is 404", dest) return true, nil } sink.Logger.Error("Failed to HEAD %s because %s", dest, err) return false, err } local_hash, err := utils.HashFile(source) if err != nil { sink.Logger.Warning("Failed to hash %s, because %v", source, err) return false, err } etag := *rsp.ETag remote_hash := strings.Replace(etag, "\"", "", -1) sink.Logger.Debug("Local hash is %s remote hash is %s", local_hash, remote_hash) if local_hash == remote_hash { return false, nil } // Okay so we think that things have changed but let's just check // modification times to be extra sure (20151112/thisisaaronland) info, err := os.Stat(source) if err != nil { sink.Logger.Error("Failed to stat %s because %s", source, err) return false, err } mtime_local := info.ModTime() mtime_remote := *rsp.LastModified // Because who remembers this stuff anyway... // func (t Time) Before(u Time) bool // Before reports whether the time instant t is before u. sink.Logger.Debug("Local %s %s", mtime_local, source) sink.Logger.Debug("Remote %s %s", mtime_remote, dest) if mtime_local.Before(mtime_remote) { sink.Logger.Warning("Remote copy of %s has a more recent modification date (local: %s remote: %s)", source, mtime_local, mtime_remote) return false, nil } return true, nil }