2010-07-11 04:18:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2010-12-03 15:34:24 +00:00
|
|
|
"camli/blobref"
|
2010-07-11 04:18:16 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2010-12-14 02:20:31 +00:00
|
|
|
"os"
|
2010-07-11 04:18:16 +00:00
|
|
|
)
|
|
|
|
|
2010-12-14 02:20:31 +00:00
|
|
|
type diskStorage struct {
|
|
|
|
Root string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ds *diskStorage) Fetch(blob blobref.BlobRef) (blobref.ReadSeekCloser, int64, os.Error) {
|
|
|
|
fileName := BlobFileName(blob)
|
|
|
|
stat, err := os.Stat(fileName)
|
|
|
|
if err == os.ENOENT {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
file, err := os.Open(fileName, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return file, stat.Size, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDiskStorage(root string) *diskStorage {
|
|
|
|
return &diskStorage{Root: root}
|
|
|
|
}
|
|
|
|
|
2010-07-11 04:18:16 +00:00
|
|
|
var kGetPutPattern *regexp.Regexp = regexp.MustCompile(`^/camli/([a-z0-9]+)-([a-f0-9]+)$`)
|
|
|
|
|
2010-12-03 15:34:24 +00:00
|
|
|
func BlobFileBaseName(b blobref.BlobRef) string {
|
|
|
|
return fmt.Sprintf("%s-%s.dat", b.HashName(), b.Digest())
|
2010-07-11 04:18:16 +00:00
|
|
|
}
|
|
|
|
|
2010-12-03 15:34:24 +00:00
|
|
|
func BlobDirectoryName(b blobref.BlobRef) string {
|
|
|
|
d := b.Digest()
|
|
|
|
return fmt.Sprintf("%s/%s/%s/%s", *flagStorageRoot, b.HashName(), d[0:3], d[3:6])
|
2010-07-11 04:18:16 +00:00
|
|
|
}
|
|
|
|
|
2010-12-03 15:34:24 +00:00
|
|
|
func BlobFileName(b blobref.BlobRef) string {
|
|
|
|
return fmt.Sprintf("%s/%s-%s.dat", BlobDirectoryName(b), b.HashName(), b.Digest())
|
2010-07-11 04:18:16 +00:00
|
|
|
}
|
|
|
|
|
2010-12-03 15:34:24 +00:00
|
|
|
func BlobFromUrlPath(path string) blobref.BlobRef {
|
|
|
|
return blobref.FromPattern(kGetPutPattern, path)
|
2010-07-11 04:18:16 +00:00
|
|
|
}
|
|
|
|
|