2013-08-21 20:57:28 +00:00
|
|
|
/*
|
2018-01-04 00:52:49 +00:00
|
|
|
Copyright 2013 The Perkeep Authors
|
2013-08-21 20:57:28 +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 blobserver
|
|
|
|
|
|
|
|
import (
|
2018-01-16 23:03:16 +00:00
|
|
|
"context"
|
2018-01-02 21:33:42 +00:00
|
|
|
"errors"
|
2018-01-08 18:38:20 +00:00
|
|
|
"fmt"
|
2013-08-21 20:57:28 +00:00
|
|
|
"hash"
|
|
|
|
"io"
|
2013-10-25 07:22:29 +00:00
|
|
|
"strings"
|
2013-08-21 20:57:28 +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
|
|
|
"perkeep.org/pkg/blob"
|
2013-08-21 20:57:28 +00:00
|
|
|
)
|
|
|
|
|
2018-01-02 21:33:42 +00:00
|
|
|
// ErrReadonly is the error value returned by read-only blobservers.
|
|
|
|
var ErrReadonly = errors.New("this blobserver is read only")
|
|
|
|
|
2013-10-25 07:22:29 +00:00
|
|
|
// ReceiveString uploads the blob given by the string s to dst
|
|
|
|
// and returns its blobref and size.
|
2018-01-16 23:03:16 +00:00
|
|
|
func ReceiveString(ctx context.Context, dst BlobReceiver, s string) (blob.SizedRef, error) {
|
|
|
|
return Receive(ctx, dst, blob.RefFromString(s), strings.NewReader(s))
|
2013-10-25 07:22:29 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 20:57:28 +00:00
|
|
|
// Receive wraps calling a BlobReceiver's ReceiveBlob method,
|
|
|
|
// additionally providing verification of the src digest, and also
|
|
|
|
// notifying the blob hub on success.
|
2014-01-05 01:32:08 +00:00
|
|
|
// The error will be ErrCorruptBlob if the blobref didn't match.
|
2018-01-16 23:03:16 +00:00
|
|
|
func Receive(ctx context.Context, dst BlobReceiver, br blob.Ref, src io.Reader) (blob.SizedRef, error) {
|
|
|
|
return receive(ctx, dst, br, src, true)
|
2013-08-21 20:57:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 23:03:16 +00:00
|
|
|
func ReceiveNoHash(ctx context.Context, dst BlobReceiver, br blob.Ref, src io.Reader) (blob.SizedRef, error) {
|
|
|
|
return receive(ctx, dst, br, src, false)
|
2013-08-21 20:57:28 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 23:03:16 +00:00
|
|
|
func receive(ctx context.Context, dst BlobReceiver, br blob.Ref, src io.Reader, checkHash bool) (sb blob.SizedRef, err error) {
|
2013-11-23 19:09:06 +00:00
|
|
|
src = io.LimitReader(src, MaxBlobSize)
|
2013-08-21 20:57:28 +00:00
|
|
|
if checkHash {
|
2018-01-08 18:38:20 +00:00
|
|
|
h := br.Hash()
|
|
|
|
if h == nil {
|
|
|
|
return sb, fmt.Errorf("invalid blob type %v; no registered hash function", br)
|
|
|
|
}
|
|
|
|
src = &checkHashReader{h, br, src, false}
|
2013-08-21 20:57:28 +00:00
|
|
|
}
|
2018-01-16 23:03:16 +00:00
|
|
|
sb, err = dst.ReceiveBlob(ctx, br, src)
|
2013-08-21 20:57:28 +00:00
|
|
|
if err != nil {
|
2014-01-05 01:32:08 +00:00
|
|
|
if checkHash && src.(*checkHashReader).corrupt {
|
|
|
|
err = ErrCorruptBlob
|
|
|
|
}
|
2013-08-21 20:57:28 +00:00
|
|
|
return
|
|
|
|
}
|
2013-11-25 00:20:11 +00:00
|
|
|
err = GetHub(dst).NotifyBlobReceived(sb)
|
2013-08-21 20:57:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkHashReader is an io.Reader that wraps the src Reader but turns
|
|
|
|
// an io.EOF into an ErrCorruptBlob if the data read doesn't match the
|
|
|
|
// hash of br.
|
|
|
|
type checkHashReader struct {
|
2014-01-05 01:32:08 +00:00
|
|
|
h hash.Hash
|
|
|
|
br blob.Ref
|
|
|
|
src io.Reader
|
|
|
|
corrupt bool
|
2013-08-21 20:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checkHashReader) Read(p []byte) (n int, err error) {
|
|
|
|
n, err = c.src.Read(p)
|
|
|
|
c.h.Write(p[:n])
|
|
|
|
if err == io.EOF && !c.br.HashMatches(c.h) {
|
|
|
|
err = ErrCorruptBlob
|
2014-01-05 01:32:08 +00:00
|
|
|
c.corrupt = true
|
2013-08-21 20:57:28 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|