2013-08-04 02:08:17 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package blob
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-01-28 20:46:52 +00:00
|
|
|
"math"
|
2013-08-04 02:08:17 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
// Fetcher is the minimal interface for retrieving a blob from storage.
|
2014-02-21 14:59:16 +00:00
|
|
|
// The full storage interface is blobserver.Storage.
|
2014-03-14 19:11:08 +00:00
|
|
|
type Fetcher interface {
|
2013-08-04 02:08:17 +00:00
|
|
|
// Fetch returns a blob. If the blob is not found then
|
|
|
|
// os.ErrNotExist should be returned for the error (not a wrapped
|
|
|
|
// error with a ErrNotExist inside)
|
|
|
|
//
|
2014-03-14 19:11:08 +00:00
|
|
|
// The contents are not guaranteed to match the digest of the
|
|
|
|
// provided Ref (e.g. when streamed over HTTP). Paranoid
|
|
|
|
// callers should verify them.
|
2013-08-04 02:08:17 +00:00
|
|
|
//
|
2014-03-14 19:11:08 +00:00
|
|
|
// The caller must close blob.
|
|
|
|
Fetch(Ref) (blob io.ReadCloser, size uint32, err error)
|
2013-08-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func NewSerialFetcher(fetchers ...Fetcher) Fetcher {
|
2013-08-04 02:08:17 +00:00
|
|
|
return &serialFetcher{fetchers}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSimpleDirectoryFetcher(dir string) *DirFetcher {
|
|
|
|
return &DirFetcher{dir, "camli"}
|
|
|
|
}
|
|
|
|
|
|
|
|
type serialFetcher struct {
|
2014-03-14 19:11:08 +00:00
|
|
|
fetchers []Fetcher
|
2013-08-04 02:08:17 +00:00
|
|
|
}
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func (sf *serialFetcher) Fetch(r Ref) (file io.ReadCloser, size uint32, err error) {
|
2013-08-04 02:08:17 +00:00
|
|
|
for _, fetcher := range sf.fetchers {
|
|
|
|
file, size, err = fetcher.Fetch(r)
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type DirFetcher struct {
|
|
|
|
directory, extension string
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func (df *DirFetcher) Fetch(r Ref) (file io.ReadCloser, size uint32, err error) {
|
2013-08-04 02:08:17 +00:00
|
|
|
fileName := fmt.Sprintf("%s/%s.%s", df.directory, r.String(), df.extension)
|
|
|
|
var stat os.FileInfo
|
|
|
|
stat, err = os.Stat(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
if stat.Size() > math.MaxUint32 {
|
|
|
|
err = errors.New("file size too big")
|
|
|
|
return
|
|
|
|
}
|
2013-08-04 02:08:17 +00:00
|
|
|
file, err = os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
size = uint32(stat.Size())
|
2013-08-04 02:08:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// MemoryStore stores blobs in memory and is a Fetcher and
|
2014-03-14 19:11:08 +00:00
|
|
|
// Fetcher. Its zero value is usable.
|
2013-08-04 02:08:17 +00:00
|
|
|
type MemoryStore struct {
|
|
|
|
lk sync.Mutex
|
|
|
|
m map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MemoryStore) AddBlob(hashtype crypto.Hash, data string) (Ref, error) {
|
|
|
|
if hashtype != crypto.SHA1 {
|
|
|
|
return Ref{}, errors.New("blobref: unsupported hash type")
|
|
|
|
}
|
|
|
|
hash := hashtype.New()
|
|
|
|
hash.Write([]byte(data))
|
|
|
|
bstr := fmt.Sprintf("sha1-%x", hash.Sum(nil))
|
|
|
|
s.lk.Lock()
|
|
|
|
defer s.lk.Unlock()
|
|
|
|
if s.m == nil {
|
|
|
|
s.m = make(map[string]string)
|
|
|
|
}
|
|
|
|
s.m[bstr] = data
|
|
|
|
return MustParse(bstr), nil
|
|
|
|
}
|
|
|
|
|
2014-03-14 19:11:08 +00:00
|
|
|
func (s *MemoryStore) Fetch(b Ref) (file io.ReadCloser, size uint32, err error) {
|
2013-08-04 02:08:17 +00:00
|
|
|
s.lk.Lock()
|
|
|
|
defer s.lk.Unlock()
|
|
|
|
if s.m == nil {
|
|
|
|
return nil, 0, os.ErrNotExist
|
|
|
|
}
|
|
|
|
str, ok := s.m[b.String()]
|
|
|
|
if !ok {
|
|
|
|
return nil, 0, os.ErrNotExist
|
|
|
|
}
|
2014-01-28 20:46:52 +00:00
|
|
|
return ioutil.NopCloser(strings.NewReader(str)), uint32(len(str)), nil
|
2013-08-04 02:08:17 +00:00
|
|
|
}
|