Break camli/blobref into two files; add NewSimpleDirectoryFetcher to fetcher.go

This commit is contained in:
Brad Fitzpatrick 2011-01-16 21:00:36 -08:00
parent 6520da3e46
commit 5f9840a429
4 changed files with 38 additions and 23 deletions

View File

@ -2,6 +2,7 @@ include $(GOROOT)/src/Make.inc
TARG=camli/blobref
GOFILES=\
blobref.go
blobref.go\
fetcher.go\
include $(GOROOT)/src/Make.pkg

View File

@ -5,7 +5,6 @@ import (
"fmt"
"hash"
"io"
"os"
"regexp"
)
@ -28,10 +27,6 @@ type ReadSeekCloser interface {
io.Closer
}
type Fetcher interface {
Fetch(*BlobRef) (file ReadSeekCloser, size int64, err os.Error)
}
func (b *BlobRef) HashName() string {
return b.hashName
}

35
lib/go/blobref/fetcher.go Normal file
View File

@ -0,0 +1,35 @@
package blobref
import (
"fmt"
"os"
)
type Fetcher interface {
Fetch(*BlobRef) (file ReadSeekCloser, size int64, err os.Error)
}
func NewSimpleDirectoryFetcher(dir string) Fetcher {
return &dirFetcher{dir, "camli"}
}
type dirFetcher struct {
directory, extension string
}
func (df *dirFetcher) Fetch(b *BlobRef) (file ReadSeekCloser, size int64, err os.Error) {
fileName := fmt.Sprintf("%s/%s.%s", df.directory, b.String(), df.extension)
var stat *os.FileInfo
stat, err = os.Stat(fileName)
if err != nil {
return
}
file, err = os.Open(fileName, os.O_RDONLY, 0)
if err != nil {
return
}
size = stat.Size
return
}

View File

@ -16,27 +16,11 @@ var accessPassword string
var flagPubKeyDir *string = flag.String("pubkey-dir", "test/pubkey-blobs",
"Temporary development hack; directory to dig-xxxx.camli public keys.")
type pubkeyDirFetcher struct{}
func (_ *pubkeyDirFetcher) Fetch(b *blobref.BlobRef) (file blobref.ReadSeekCloser, size int64, err os.Error) {
fileName := fmt.Sprintf("%s/%s.camli", *flagPubKeyDir, b.String())
var stat *os.FileInfo
stat, err = os.Stat(fileName)
if err != nil {
return
}
file, err = os.Open(fileName, os.O_RDONLY, 0)
if err != nil {
return
}
size = stat.Size
return
}
// TODO: for now, the only implementation of the blobref.Fetcher
// interface for fetching public keys is the "local, from disk"
// implementation used for testing. In reality we'd want to be able
// to fetch these from blobservers.
var pubKeyFetcher = &pubkeyDirFetcher{}
var pubKeyFetcher = blobref.NewSimpleDirectoryFetcher(*flagPubKeyDir)
func handleRoot(conn http.ResponseWriter, req *http.Request) {
fmt.Fprintf(conn, "camsigd")