2014-03-14 18:47:01 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2014 The Perkeep Authors
|
2014-03-14 18:47:01 +00:00
|
|
|
|
|
|
|
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 (
|
|
|
|
"bytes"
|
2018-01-16 23:03:16 +00:00
|
|
|
"context"
|
2014-03-14 18:47:01 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2018-01-16 23:03:16 +00:00
|
|
|
"sync/atomic"
|
2014-03-14 18:47:01 +00:00
|
|
|
"unicode/utf8"
|
|
|
|
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/constants"
|
2014-03-14 18:47:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Blob represents a blob. Use the methods Size, SizedRef and
|
2018-01-16 23:03:16 +00:00
|
|
|
// ReadAll to query and get data from Blob.
|
2014-03-14 18:47:01 +00:00
|
|
|
type Blob struct {
|
2018-01-16 23:03:16 +00:00
|
|
|
ref Ref
|
|
|
|
size uint32
|
|
|
|
readAll func(context.Context) ([]byte, error)
|
|
|
|
mem atomic.Value // of []byte, if in memory
|
2014-03-14 18:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlob constructs a Blob from its Ref, size and a function that
|
|
|
|
// returns an io.ReadCloser from which the blob can be read. Any error
|
|
|
|
// in the function newReader when constructing the io.ReadCloser should
|
|
|
|
// be returned upon the first call to Read or Close.
|
2018-01-16 23:03:16 +00:00
|
|
|
func NewBlob(ref Ref, size uint32, readAll func(ctx context.Context) ([]byte, error)) *Blob {
|
2014-03-14 18:47:01 +00:00
|
|
|
return &Blob{
|
2018-01-16 23:03:16 +00:00
|
|
|
ref: ref,
|
|
|
|
size: size,
|
|
|
|
readAll: readAll,
|
2014-03-14 18:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the size of the blob (in bytes).
|
|
|
|
func (b *Blob) Size() uint32 {
|
|
|
|
return b.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// SizedRef returns the SizedRef corresponding to the blob.
|
|
|
|
func (b *Blob) SizedRef() SizedRef {
|
|
|
|
return SizedRef{b.ref, b.size}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ref returns the blob's reference.
|
|
|
|
func (b *Blob) Ref() Ref { return b.ref }
|
|
|
|
|
2018-01-16 23:03:16 +00:00
|
|
|
// ReadAll reads the blob completely to memory, using the provided
|
|
|
|
// context, and then returns a reader over it. The Reader will not
|
|
|
|
// have errors except EOF.
|
|
|
|
//
|
|
|
|
// The provided is only ctx is used until ReadAll returns.
|
|
|
|
func (b *Blob) ReadAll(ctx context.Context) (*bytes.Reader, error) {
|
|
|
|
mem, err := b.getMem(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bytes.NewReader(mem), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Blob) getMem(ctx context.Context) ([]byte, error) {
|
|
|
|
mem, ok := b.mem.Load().([]byte)
|
|
|
|
if ok {
|
|
|
|
return mem, nil
|
|
|
|
}
|
|
|
|
mem, err := b.readAll(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if uint32(len(mem)) != b.size {
|
|
|
|
return nil, fmt.Errorf("Blob.ReadAll read %d bytes of %v; expected %d", len(mem), b.ref, b.size)
|
|
|
|
}
|
|
|
|
b.mem.Store(mem)
|
|
|
|
return mem, nil
|
2014-03-14 18:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidContents reports whether the hash of blob's content matches
|
|
|
|
// its reference.
|
2018-01-16 23:03:16 +00:00
|
|
|
func (b *Blob) ValidContents(ctx context.Context) error {
|
|
|
|
mem, err := b.getMem(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-14 18:47:01 +00:00
|
|
|
h := b.ref.Hash()
|
2018-01-16 23:03:16 +00:00
|
|
|
h.Write(mem)
|
|
|
|
if !b.ref.HashMatches(h) {
|
|
|
|
return fmt.Errorf("blob contents don't match digest for ref %v", b.ref)
|
2014-03-14 18:47:01 +00:00
|
|
|
}
|
2018-01-16 23:03:16 +00:00
|
|
|
return nil
|
2014-03-14 18:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsUTF8 reports whether the blob is entirely UTF-8.
|
2018-01-16 23:03:16 +00:00
|
|
|
func (b *Blob) IsUTF8(ctx context.Context) (bool, error) {
|
|
|
|
mem, err := b.getMem(ctx)
|
2014-03-14 18:47:01 +00:00
|
|
|
if err != nil {
|
2018-01-16 23:03:16 +00:00
|
|
|
return false, err
|
2014-03-14 18:47:01 +00:00
|
|
|
}
|
2018-01-16 23:03:16 +00:00
|
|
|
return utf8.Valid(mem), nil
|
2014-03-14 18:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A reader reads a blob's contents.
|
|
|
|
// It adds a no-op Close method to a *bytes.Reader.
|
|
|
|
type reader struct {
|
|
|
|
*bytes.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (reader) Close() error { return nil }
|
|
|
|
|
|
|
|
// FromFetcher fetches br from fetcher and slurps its contents to
|
|
|
|
// memory. It does not validate the blob's digest. Use the
|
|
|
|
// Blob.ValidContents method for that.
|
2018-01-16 23:03:16 +00:00
|
|
|
func FromFetcher(ctx context.Context, fetcher Fetcher, br Ref) (*Blob, error) {
|
|
|
|
rc, size, err := fetcher.Fetch(ctx, br)
|
2014-03-14 18:47:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
2018-01-16 23:03:16 +00:00
|
|
|
return FromReader(ctx, br, rc, size)
|
2014-03-14 18:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromReader slurps the given blob from r to memory.
|
|
|
|
// It does not validate the blob's digest. Use the
|
|
|
|
// Blob.ValidContents method for that.
|
2018-01-16 23:03:16 +00:00
|
|
|
func FromReader(ctx context.Context, br Ref, r io.Reader, size uint32) (*Blob, error) {
|
2014-03-14 18:47:01 +00:00
|
|
|
if size > constants.MaxBlobSize {
|
|
|
|
return nil, fmt.Errorf("blob: %v with reported size %d is over max size of %d", br, size, constants.MaxBlobSize)
|
|
|
|
}
|
|
|
|
buf := make([]byte, size)
|
2018-01-16 23:03:16 +00:00
|
|
|
// TODO: use ctx here during ReadFull? add context-checking Reader wrapper?
|
2014-03-14 18:47:01 +00:00
|
|
|
if n, err := io.ReadFull(r, buf); err != nil {
|
|
|
|
return nil, fmt.Errorf("blob: after reading %d bytes of %v: %v", n, br, err)
|
|
|
|
}
|
|
|
|
n, _ := io.CopyN(ioutil.Discard, r, 1)
|
|
|
|
if n > 0 {
|
|
|
|
return nil, fmt.Errorf("blob: %v had more than reported %d bytes", br, size)
|
|
|
|
}
|
2018-01-16 23:03:16 +00:00
|
|
|
b := NewBlob(br, uint32(size), func(context.Context) ([]byte, error) {
|
|
|
|
return buf, nil
|
|
|
|
})
|
|
|
|
b.mem.Store(buf)
|
2014-03-14 18:47:01 +00:00
|
|
|
return b, nil
|
|
|
|
}
|