filling out base functionality

This commit is contained in:
Vincent Batts 2014-09-16 17:12:52 -04:00
parent 842fa03b61
commit aa6273d315
3 changed files with 37 additions and 13 deletions

View File

@ -1,24 +1,41 @@
package base package base
import ( import (
"crypto" "io"
"os" "os"
"path/filepath" "path/filepath"
) )
func InitVarBase(base string) error { func NewBase(path string, hashName string) (*Base, error) {
for _, path := range []string{"dedup/blobs", "dedup/state"} { for _, p := range []string{"dedup/blobs" + hashName, "dedup/state"} {
if err := os.MkdirAll(filepath.Join(base, path), 0755); err != nil { if err := os.MkdirAll(filepath.Join(path, p), 0755); err != nil {
return err return nil, err
} }
} }
return nil return &Base{Path: path, HashName: hashName}, nil
} }
type Base struct { 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 return true
} }

View File

@ -1,4 +1,4 @@
package main package cryptomap
import ( import (
"crypto" "crypto"
@ -10,6 +10,10 @@ import (
"strings" "strings"
) )
var knownCiphers = map[string]crypto.Hash{
"md5": crypto.MD5,
}
func DetermineHash(str string) (h crypto.Hash) { func DetermineHash(str string) (h crypto.Hash) {
switch strings.ToLower(str) { switch strings.ToLower(str) {
case "md5": case "md5":

11
main.go
View File

@ -9,6 +9,7 @@ import (
"runtime" "runtime"
"./base" "./base"
"./cryptomap"
"./file" "./file"
) )
@ -27,13 +28,15 @@ func init() {
func main() { func main() {
flag.Parse() 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) log.Fatal(err)
} }
var ( var (
hash = DetermineHash(*flCipher) hash = cryptomap.DetermineHash(*flCipher)
ourbase = base.Base{Path: *flVarBase}
//infos = []*file.FileHashInfo{} //infos = []*file.FileHashInfo{}
//mu = sync.Mutex{} //mu = sync.Mutex{}
//results := make(chan file.FileHashInfo, 2) //results := make(chan file.FileHashInfo, 2)
@ -55,7 +58,7 @@ func main() {
done <- struct{}{} done <- struct{}{}
} }
fmt.Printf("%s %s\n", fi.Hash, fi.Path) 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 // TODO check if they have the same Inode
// if not, then clobber // if not, then clobber
} else { } else {