2011-01-28 07:07:18 +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.
|
|
|
|
*/
|
|
|
|
|
2010-12-03 15:34:24 +00:00
|
|
|
package blobref
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha1"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
2010-12-09 03:24:48 +00:00
|
|
|
"io"
|
2010-12-03 15:34:24 +00:00
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
2013-01-02 20:55:12 +00:00
|
|
|
// Pattern is the regular expression which matches a blobref.
|
|
|
|
// It does not contain ^ or $.
|
2013-01-02 22:50:52 +00:00
|
|
|
const Pattern = `\b([a-z][a-z0-9]*)-([a-f0-9]+)\b`
|
2013-01-02 20:55:12 +00:00
|
|
|
|
2013-01-02 22:50:52 +00:00
|
|
|
// whole blobref pattern
|
2013-01-02 20:55:12 +00:00
|
|
|
var kBlobRefPattern = regexp.MustCompile("^" + Pattern + "$")
|
2010-12-03 15:34:24 +00:00
|
|
|
|
2011-03-02 18:18:45 +00:00
|
|
|
var supportedDigests = map[string]func() hash.Hash{
|
2010-12-03 15:34:24 +00:00
|
|
|
"sha1": func() hash.Hash {
|
|
|
|
return sha1.New()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2011-02-04 01:06:29 +00:00
|
|
|
// BlobRef is an immutable reference to a blob.
|
2010-12-17 17:59:03 +00:00
|
|
|
type BlobRef struct {
|
2011-03-02 18:18:45 +00:00
|
|
|
hashName string
|
|
|
|
digest string
|
|
|
|
|
|
|
|
strValue string // "<hashname>-<digest>"
|
2010-12-03 15:34:24 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (br *BlobRef) GobEncode() ([]byte, error) {
|
2011-09-17 00:59:15 +00:00
|
|
|
return []byte(br.String()), nil
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (br *BlobRef) GobDecode(b []byte) error {
|
2011-09-17 00:59:15 +00:00
|
|
|
dec := Parse(string(b))
|
|
|
|
if dec == nil {
|
|
|
|
return fmt.Errorf("invalid blobref %q", string(b))
|
|
|
|
}
|
|
|
|
*br = *dec
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-05-10 23:13:37 +00:00
|
|
|
// SizedBlobRef is like a BlobRef but includes because it includes a
|
|
|
|
// potentially mutable 'Size', this should be used as a stack value,
|
|
|
|
// not a *SizedBlobRef.
|
2011-02-04 01:06:29 +00:00
|
|
|
type SizedBlobRef struct {
|
|
|
|
*BlobRef
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
2011-05-29 17:38:21 +00:00
|
|
|
func (sb *SizedBlobRef) Equal(o SizedBlobRef) bool {
|
|
|
|
return sb.Size == o.Size && sb.BlobRef.String() == o.BlobRef.String()
|
|
|
|
}
|
|
|
|
|
2011-09-28 18:05:02 +00:00
|
|
|
func (sb SizedBlobRef) String() string {
|
|
|
|
return fmt.Sprintf("[%s; %d bytes]", sb.BlobRef.String(), sb.Size)
|
2011-05-29 17:38:21 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 02:20:31 +00:00
|
|
|
type ReadSeekCloser interface {
|
|
|
|
io.Reader
|
|
|
|
io.Seeker
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func (b *BlobRef) HashName() string {
|
2010-12-03 15:34:24 +00:00
|
|
|
return b.hashName
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func (b *BlobRef) Digest() string {
|
2010-12-03 15:34:24 +00:00
|
|
|
return b.digest
|
|
|
|
}
|
|
|
|
|
2011-07-06 00:27:10 +00:00
|
|
|
func (b *BlobRef) DigestPrefix(digits int) string {
|
|
|
|
if len(b.digest) < digits {
|
|
|
|
return b.digest
|
|
|
|
}
|
|
|
|
return b.digest[:digits]
|
|
|
|
}
|
|
|
|
|
2011-03-02 18:18:45 +00:00
|
|
|
func (b *BlobRef) String() string {
|
2011-03-24 02:57:40 +00:00
|
|
|
if b == nil {
|
|
|
|
return "<nil-BlobRef>"
|
|
|
|
}
|
2011-03-02 18:18:45 +00:00
|
|
|
return b.strValue
|
2010-12-03 15:34:24 +00:00
|
|
|
}
|
|
|
|
|
2011-07-06 00:27:10 +00:00
|
|
|
func (b *BlobRef) DomID() string {
|
|
|
|
if b == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2011-07-09 01:00:46 +00:00
|
|
|
return "camli-" + b.String()
|
2011-07-06 00:27:10 +00:00
|
|
|
}
|
|
|
|
|
2011-12-02 01:13:42 +00:00
|
|
|
func (o *BlobRef) Equal(other *BlobRef) bool {
|
2011-12-02 01:28:05 +00:00
|
|
|
if (o == nil) != (other == nil) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if o == nil {
|
|
|
|
return true
|
|
|
|
}
|
2011-02-26 20:22:26 +00:00
|
|
|
return o.hashName == other.hashName && o.digest == other.digest
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func (o *BlobRef) Hash() hash.Hash {
|
2010-12-03 15:34:24 +00:00
|
|
|
fn, ok := supportedDigests[o.hashName]
|
|
|
|
if !ok {
|
2011-06-10 01:33:26 +00:00
|
|
|
return nil // TODO: return an error here, not nil
|
2010-12-03 15:34:24 +00:00
|
|
|
}
|
|
|
|
return fn()
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func (o *BlobRef) HashMatches(h hash.Hash) bool {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return fmt.Sprintf("%x", h.Sum(nil)) == o.digest
|
2010-12-17 17:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *BlobRef) IsSupported() bool {
|
2010-12-03 15:34:24 +00:00
|
|
|
_, ok := supportedDigests[o.hashName]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2011-05-21 21:32:25 +00:00
|
|
|
func (o *BlobRef) Sum32() uint32 {
|
|
|
|
var h32 uint32
|
|
|
|
n, err := fmt.Sscanf(o.digest[len(o.digest)-8:], "%8x", &h32)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if n != 1 {
|
|
|
|
panic("sum32")
|
|
|
|
}
|
|
|
|
return h32
|
|
|
|
}
|
|
|
|
|
2010-12-03 15:34:24 +00:00
|
|
|
var kExpectedDigestSize = map[string]int{
|
|
|
|
"md5": 32,
|
|
|
|
"sha1": 40,
|
|
|
|
}
|
|
|
|
|
2011-03-02 18:18:45 +00:00
|
|
|
func newBlob(hashName, digest string) *BlobRef {
|
|
|
|
strValue := fmt.Sprintf("%s-%s", hashName, digest)
|
|
|
|
return &BlobRef{strValue[0:len(hashName)],
|
|
|
|
strValue[len(hashName)+1:],
|
|
|
|
strValue}
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func blobIfValid(hashname, digest string) *BlobRef {
|
2010-12-03 15:34:24 +00:00
|
|
|
expectedSize := kExpectedDigestSize[hashname]
|
|
|
|
if expectedSize != 0 && len(digest) != expectedSize {
|
|
|
|
return nil
|
|
|
|
}
|
2011-03-02 18:18:45 +00:00
|
|
|
return newBlob(hashname, digest)
|
2010-12-03 15:34:24 +00:00
|
|
|
}
|
|
|
|
|
2011-05-29 17:38:21 +00:00
|
|
|
func FromHash(hashfunc string, h hash.Hash) *BlobRef {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
return newBlob(hashfunc, fmt.Sprintf("%x", h.Sum(nil)))
|
2011-05-29 17:38:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-27 19:06:12 +00:00
|
|
|
func SHA1FromString(s string) *BlobRef {
|
2011-05-29 17:38:21 +00:00
|
|
|
s1 := sha1.New()
|
|
|
|
s1.Write([]byte(s))
|
|
|
|
return FromHash("sha1", s1)
|
2010-12-31 06:37:46 +00:00
|
|
|
}
|
|
|
|
|
2010-12-03 15:34:24 +00:00
|
|
|
// FromPattern takes a pattern and if it matches 's' with two exactly two valid
|
|
|
|
// submatches, returns a BlobRef, else returns nil.
|
2010-12-17 17:59:03 +00:00
|
|
|
func FromPattern(r *regexp.Regexp, s string) *BlobRef {
|
2010-12-03 15:34:24 +00:00
|
|
|
matches := r.FindStringSubmatch(s)
|
|
|
|
if len(matches) != 3 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return blobIfValid(matches[1], matches[2])
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func Parse(ref string) *BlobRef {
|
2010-12-03 15:34:24 +00:00
|
|
|
return FromPattern(kBlobRefPattern, ref)
|
|
|
|
}
|
2011-03-05 21:43:24 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (br *BlobRef) UnmarshalJSON(d []byte) error {
|
2011-06-08 00:00:21 +00:00
|
|
|
if len(d) < 2 || d[0] != '"' || d[len(d)-1] != '"' {
|
|
|
|
return fmt.Errorf("blobref: expecting a JSON string to unmarshal, got %q", d)
|
|
|
|
}
|
2011-07-02 16:09:50 +00:00
|
|
|
refStr := string(d[1 : len(d)-1])
|
2011-06-08 00:00:21 +00:00
|
|
|
p := Parse(refStr)
|
|
|
|
if p == nil {
|
|
|
|
return fmt.Errorf("blobref: invalid blobref %q (%d)", refStr, len(refStr))
|
|
|
|
}
|
|
|
|
*br = *p
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (br *BlobRef) MarshalJSON() ([]byte, error) {
|
2011-06-08 00:00:21 +00:00
|
|
|
return []byte(fmt.Sprintf("%q", br.String())), nil
|
|
|
|
}
|
|
|
|
|
2011-03-16 06:16:24 +00:00
|
|
|
func MustParse(ref string) *BlobRef {
|
|
|
|
br := Parse(ref)
|
|
|
|
if br == nil {
|
|
|
|
panic("Failed to parse blobref: " + ref)
|
|
|
|
}
|
|
|
|
return br
|
|
|
|
}
|
|
|
|
|
2011-03-05 21:43:24 +00:00
|
|
|
// May return nil in list positions where the blobref could not be parsed.
|
|
|
|
func ParseMulti(refs []string) (parsed []*BlobRef) {
|
|
|
|
parsed = make([]*BlobRef, 0, len(refs))
|
|
|
|
for _, ref := range refs {
|
|
|
|
parsed = append(parsed, Parse(ref))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|