From aa6273d315a8b4d358a94174e6131f196351f094 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Tue, 16 Sep 2014 17:12:52 -0400 Subject: [PATCH] filling out base functionality --- base/base.go | 33 ++++++++++++++++++++++++-------- crypto.go => cryptomap/crypto.go | 6 +++++- main.go | 11 +++++++---- 3 files changed, 37 insertions(+), 13 deletions(-) rename crypto.go => cryptomap/crypto.go (85%) diff --git a/base/base.go b/base/base.go index a22d27c..7818aad 100644 --- a/base/base.go +++ b/base/base.go @@ -1,24 +1,41 @@ package base import ( - "crypto" + "io" "os" "path/filepath" ) -func InitVarBase(base string) error { - for _, path := range []string{"dedup/blobs", "dedup/state"} { - if err := os.MkdirAll(filepath.Join(base, path), 0755); err != nil { - return err +func NewBase(path string, hashName string) (*Base, error) { + for _, p := range []string{"dedup/blobs" + hashName, "dedup/state"} { + if err := os.MkdirAll(filepath.Join(path, p), 0755); err != nil { + return nil, err } } - return nil + return &Base{Path: path, HashName: hashName}, nil } type Base struct { - Path string + Path string + HashName string } -func (b Base) HasBlob(hashType crypto.Hash, sum string) bool { +// GetBlob store the content from src, for the sum and hashType +func (b Base) GetBlob(sum string) (io.Reader, error) { + // XXX + return nil, nil +} + +// PutBlob store the content from src, for the sum and hashType +// +// we take the sum up front to avoid recalculation and tempfiles +func (b Base) PutBlob(sum string, src io.Reader) error { + // XXX + return nil +} + +// HasBlob tests whether a blob with this sum exists +func (b Base) HasBlob(sum string) bool { + // XXX return true } diff --git a/crypto.go b/cryptomap/crypto.go similarity index 85% rename from crypto.go rename to cryptomap/crypto.go index feaf008..b5bb665 100644 --- a/crypto.go +++ b/cryptomap/crypto.go @@ -1,4 +1,4 @@ -package main +package cryptomap import ( "crypto" @@ -10,6 +10,10 @@ import ( "strings" ) +var knownCiphers = map[string]crypto.Hash{ + "md5": crypto.MD5, +} + func DetermineHash(str string) (h crypto.Hash) { switch strings.ToLower(str) { case "md5": diff --git a/main.go b/main.go index 910dfcf..1fe6d41 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "runtime" "./base" + "./cryptomap" "./file" ) @@ -27,13 +28,15 @@ func init() { func main() { flag.Parse() - if err := base.InitVarBase(*flVarBase); err != nil { + + // TODO the *flCipher has not been checked yet, and would cause the directory to get created + ourbase, err := base.NewBase(*flVarBase, *flCipher) + if err != nil { log.Fatal(err) } var ( - hash = DetermineHash(*flCipher) - ourbase = base.Base{Path: *flVarBase} + hash = cryptomap.DetermineHash(*flCipher) //infos = []*file.FileHashInfo{} //mu = sync.Mutex{} //results := make(chan file.FileHashInfo, 2) @@ -55,7 +58,7 @@ func main() { done <- struct{}{} } fmt.Printf("%s %s\n", fi.Hash, fi.Path) - if ourbase.HasBlob(fi.HashType, fi.Hash) { + if ourbase.HasBlob(fi.Hash) { // TODO check if they have the same Inode // if not, then clobber } else {