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.
|
|
|
|
*/
|
|
|
|
|
2012-08-21 18:14:47 +00:00
|
|
|
// Package schema manipulates Camlistore schema blobs.
|
|
|
|
//
|
|
|
|
// A schema blob is a JSON-encoded blob that describes other blobs.
|
|
|
|
// See documentation in Camlistore's doc/schema/ directory.
|
2010-12-30 18:17:47 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2012-03-03 20:37:54 +00:00
|
|
|
"crypto/rand"
|
2010-12-31 06:37:46 +00:00
|
|
|
"crypto/sha1"
|
2012-03-03 20:37:54 +00:00
|
|
|
"encoding/base64"
|
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
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2010-12-30 18:17:47 +00:00
|
|
|
"fmt"
|
2012-12-29 14:51:42 +00:00
|
|
|
"hash"
|
2010-12-31 06:37:46 +00:00
|
|
|
"io"
|
2010-12-30 18:17:47 +00:00
|
|
|
"os"
|
2012-12-29 14:51:42 +00:00
|
|
|
"reflect"
|
2011-01-01 10:51:15 +00:00
|
|
|
"strconv"
|
2013-01-14 04:43:55 +00:00
|
|
|
"strings"
|
2011-01-01 10:51:15 +00:00
|
|
|
"sync"
|
2010-12-31 06:37:46 +00:00
|
|
|
"time"
|
2014-03-15 04:39:08 +00:00
|
|
|
"unicode/utf8"
|
2011-05-26 23:41:49 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-02-10 20:58:51 +00:00
|
|
|
"camlistore.org/pkg/types"
|
2013-01-07 19:15:18 +00:00
|
|
|
"camlistore.org/third_party/github.com/camlistore/goexif/exif"
|
2010-12-30 18:17:47 +00:00
|
|
|
)
|
|
|
|
|
2013-01-22 17:32:40 +00:00
|
|
|
// MaxSchemaBlobSize represents the upper bound for how large
|
|
|
|
// a schema blob may be.
|
|
|
|
const MaxSchemaBlobSize = 1 << 20
|
|
|
|
|
2012-12-29 14:51:42 +00:00
|
|
|
var sha1Type = reflect.TypeOf(sha1.New())
|
|
|
|
|
2013-01-22 04:56:12 +00:00
|
|
|
var (
|
|
|
|
ErrNoCamliVersion = errors.New("schema: no camliVersion key in map")
|
|
|
|
)
|
2010-12-31 06:37:46 +00:00
|
|
|
|
2013-09-09 00:46:40 +00:00
|
|
|
var clockNow = time.Now
|
|
|
|
|
2010-12-31 06:37:46 +00:00
|
|
|
type StatHasher interface {
|
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
|
|
|
Lstat(fileName string) (os.FileInfo, error)
|
2013-08-04 02:54:30 +00:00
|
|
|
Hash(fileName string) (blob.Ref, error)
|
2010-12-30 18:17:47 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:44:03 +00:00
|
|
|
// File is the interface returned when opening a DirectoryEntry that
|
|
|
|
// is a regular file.
|
2011-07-14 17:43:30 +00:00
|
|
|
type File interface {
|
2013-01-05 00:26:14 +00:00
|
|
|
io.Closer
|
|
|
|
io.ReaderAt
|
|
|
|
io.Reader
|
2012-08-24 01:44:03 +00:00
|
|
|
Size() int64
|
2011-07-14 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
2011-07-17 15:50:55 +00:00
|
|
|
// Directory is a read-only interface to a "directory" schema blob.
|
2011-07-14 17:43:30 +00:00
|
|
|
type Directory interface {
|
2011-07-17 15:50:55 +00:00
|
|
|
// Readdir reads the contents of the directory associated with dr
|
|
|
|
// and returns an array of up to n DirectoryEntries structures.
|
|
|
|
// Subsequent calls on the same file will yield further
|
|
|
|
// DirectoryEntries.
|
|
|
|
// If n > 0, Readdir returns at most n DirectoryEntry structures. In
|
|
|
|
// this case, if Readdir returns an empty slice, it will return
|
|
|
|
// a non-nil error explaining why. At the end of a directory,
|
|
|
|
// the error is os.EOF.
|
|
|
|
// If n <= 0, Readdir returns all the DirectoryEntries from the
|
|
|
|
// directory in a single slice. In this case, if Readdir succeeds
|
|
|
|
// (reads all the way to the end of the directory), it returns the
|
|
|
|
// slice and a nil os.Error. If it encounters an error before the
|
|
|
|
// end of the directory, Readdir returns the DirectoryEntry read
|
|
|
|
// until that point and a non-nil error.
|
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
|
|
|
Readdir(count int) ([]DirectoryEntry, error)
|
2011-07-14 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Symlink interface {
|
|
|
|
// .. TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
// DirectoryEntry is a read-only interface to an entry in a (static)
|
|
|
|
// directory.
|
|
|
|
type DirectoryEntry interface {
|
|
|
|
// CamliType returns the schema blob's "camliType" field.
|
|
|
|
// This may be "file", "directory", "symlink", or other more
|
|
|
|
// obscure types added in the future.
|
|
|
|
CamliType() string
|
|
|
|
|
|
|
|
FileName() string
|
2013-08-04 02:54:30 +00:00
|
|
|
BlobRef() blob.Ref
|
2011-07-14 17:43:30 +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
|
|
|
File() (File, error) // if camliType is "file"
|
|
|
|
Directory() (Directory, error) // if camliType is "directory"
|
|
|
|
Symlink() (Symlink, error) // if camliType is "symlink"
|
2011-07-14 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// dirEntry is the default implementation of DirectoryEntry
|
|
|
|
type dirEntry struct {
|
2013-01-22 18:32:15 +00:00
|
|
|
ss superset
|
2014-03-14 19:11:08 +00:00
|
|
|
fetcher blob.Fetcher
|
2011-07-17 15:50:55 +00:00
|
|
|
fr *FileReader // or nil if not a file
|
|
|
|
dr *DirReader // or nil if not a directory
|
2011-07-14 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (de *dirEntry) CamliType() string {
|
|
|
|
return de.ss.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (de *dirEntry) FileName() string {
|
|
|
|
return de.ss.FileNameString()
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (de *dirEntry) BlobRef() blob.Ref {
|
2011-07-14 17:43:30 +00:00
|
|
|
return de.ss.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
|
|
|
func (de *dirEntry) File() (File, error) {
|
2011-07-19 02:06:56 +00:00
|
|
|
if de.fr == nil {
|
2011-07-17 15:50:55 +00:00
|
|
|
if de.ss.Type != "file" {
|
2011-07-19 02:06:56 +00:00
|
|
|
return nil, fmt.Errorf("DirectoryEntry is camliType %q, not %q", de.ss.Type, "file")
|
2011-07-17 15:50:55 +00:00
|
|
|
}
|
2011-07-19 02:06:56 +00:00
|
|
|
fr, err := NewFileReader(de.fetcher, de.ss.BlobRef)
|
2011-07-17 15:50:55 +00:00
|
|
|
if err != nil {
|
2011-07-19 02:06:56 +00:00
|
|
|
return nil, err
|
2011-07-17 15:50:55 +00:00
|
|
|
}
|
2011-07-19 02:06:56 +00:00
|
|
|
de.fr = fr
|
2011-07-17 15:50:55 +00:00
|
|
|
}
|
2011-07-19 02:06:56 +00:00
|
|
|
return de.fr, nil
|
2011-07-14 17:43:30 +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 (de *dirEntry) Directory() (Directory, error) {
|
2011-07-19 02:06:56 +00:00
|
|
|
if de.dr == nil {
|
2011-07-17 15:50:55 +00:00
|
|
|
if de.ss.Type != "directory" {
|
2011-07-19 02:06:56 +00:00
|
|
|
return nil, fmt.Errorf("DirectoryEntry is camliType %q, not %q", de.ss.Type, "directory")
|
2011-07-17 15:50:55 +00:00
|
|
|
}
|
2011-07-19 02:06:56 +00:00
|
|
|
dr, err := NewDirReader(de.fetcher, de.ss.BlobRef)
|
2011-07-17 15:50:55 +00:00
|
|
|
if err != nil {
|
2011-07-19 02:06:56 +00:00
|
|
|
return nil, err
|
2011-07-17 15:50:55 +00:00
|
|
|
}
|
2011-07-19 02:06:56 +00:00
|
|
|
de.dr = dr
|
2011-07-17 15:50:55 +00:00
|
|
|
}
|
2011-07-19 02:06:56 +00:00
|
|
|
return de.dr, nil
|
2011-07-14 17:43:30 +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 (de *dirEntry) Symlink() (Symlink, error) {
|
|
|
|
return 0, errors.New("TODO: Symlink not implemented")
|
2011-07-14 17:43:30 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
// newDirectoryEntry takes a superset and returns a DirectoryEntry if
|
2011-07-14 17:43:30 +00:00
|
|
|
// the Supserset is valid and represents an entry in a directory. It
|
|
|
|
// must by of type "file", "directory", or "symlink".
|
|
|
|
// TODO: "fifo", "socket", "char", "block", probably. later.
|
2014-03-14 19:11:08 +00:00
|
|
|
func newDirectoryEntry(fetcher blob.Fetcher, ss *superset) (DirectoryEntry, error) {
|
2011-07-14 17:43:30 +00:00
|
|
|
if ss == 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
|
|
|
return nil, errors.New("ss was nil")
|
2011-07-14 17:43:30 +00:00
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
if !ss.BlobRef.Valid() {
|
|
|
|
return nil, errors.New("ss.BlobRef was invalid")
|
2011-07-14 17:43:30 +00:00
|
|
|
}
|
|
|
|
switch ss.Type {
|
|
|
|
case "file", "directory", "symlink":
|
|
|
|
// Okay
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid DirectoryEntry camliType of %q", ss.Type)
|
|
|
|
}
|
2011-07-17 15:50:55 +00:00
|
|
|
de := &dirEntry{ss: *ss, fetcher: fetcher} // defensive copy
|
2011-07-14 17:43:30 +00:00
|
|
|
return de, nil
|
|
|
|
}
|
|
|
|
|
2011-07-17 15:50:55 +00:00
|
|
|
// NewDirectoryEntryFromBlobRef takes a BlobRef and returns a
|
|
|
|
// DirectoryEntry if the BlobRef contains a type "file", "directory"
|
|
|
|
// or "symlink".
|
|
|
|
// TODO: "fifo", "socket", "char", "block", probably. later.
|
2014-03-14 19:11:08 +00:00
|
|
|
func NewDirectoryEntryFromBlobRef(fetcher blob.Fetcher, blobRef blob.Ref) (DirectoryEntry, error) {
|
2013-01-22 18:32:15 +00:00
|
|
|
ss := new(superset)
|
2011-07-17 15:50:55 +00:00
|
|
|
err := ss.setFromBlobRef(fetcher, blobRef)
|
|
|
|
if err != nil {
|
2013-01-22 18:32:15 +00:00
|
|
|
return nil, fmt.Errorf("schema/filereader: can't fill superset: %v\n", err)
|
2011-07-17 15:50:55 +00:00
|
|
|
}
|
2013-01-22 18:32:15 +00:00
|
|
|
return newDirectoryEntry(fetcher, ss)
|
2011-07-17 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
// superset represents the superset of common Camlistore JSON schema
|
2012-08-21 18:14:47 +00:00
|
|
|
// keys as a convenient json.Unmarshal target.
|
2013-01-22 01:31:08 +00:00
|
|
|
// TODO(bradfitz): unexport this type. Getting too gross. Move to schema.Blob
|
2013-01-22 18:32:15 +00:00
|
|
|
type superset struct {
|
2013-01-02 20:55:12 +00:00
|
|
|
// BlobRef isn't for a particular metadata blob field, but included
|
|
|
|
// for convenience.
|
2013-08-04 02:54:30 +00:00
|
|
|
BlobRef blob.Ref
|
2011-03-24 05:04:50 +00:00
|
|
|
|
2011-06-30 04:36:14 +00:00
|
|
|
Version int `json:"camliVersion"`
|
|
|
|
Type string `json:"camliType"`
|
2011-03-13 18:38:53 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
Signer blob.Ref `json:"camliSigner"`
|
|
|
|
Sig string `json:"camliSig"`
|
2011-03-13 18:38:53 +00:00
|
|
|
|
2013-02-10 20:58:51 +00:00
|
|
|
ClaimType string `json:"claimType"`
|
|
|
|
ClaimDate types.Time3339 `json:"claimDate"`
|
2011-03-13 18:38:53 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
Permanode blob.Ref `json:"permaNode"`
|
|
|
|
Attribute string `json:"attribute"`
|
|
|
|
Value string `json:"value"`
|
2011-03-23 05:11:27 +00:00
|
|
|
|
2013-01-14 04:43:55 +00:00
|
|
|
// FileName and FileNameBytes represent one of the two
|
|
|
|
// representations of file names in schema blobs. They should
|
|
|
|
// not be accessed directly. Use the FileNameString accessor
|
|
|
|
// instead, which also sanitizes malicious values.
|
2011-06-30 04:36:14 +00:00
|
|
|
FileName string `json:"fileName"`
|
2014-03-07 21:54:02 +00:00
|
|
|
FileNameBytes []interface{} `json:"fileNameBytes"`
|
2011-03-26 01:36:08 +00:00
|
|
|
|
2011-06-30 04:36:14 +00:00
|
|
|
SymlinkTarget string `json:"symlinkTarget"`
|
2014-03-07 21:54:02 +00:00
|
|
|
SymlinkTargetBytes []interface{} `json:"symlinkTargetBytes"`
|
2011-03-26 01:36:08 +00:00
|
|
|
|
2011-06-30 04:36:14 +00:00
|
|
|
UnixPermission string `json:"unixPermission"`
|
|
|
|
UnixOwnerId int `json:"unixOwnerId"`
|
|
|
|
UnixOwner string `json:"unixOwner"`
|
|
|
|
UnixGroupId int `json:"unixGroupId"`
|
|
|
|
UnixGroup string `json:"unixGroup"`
|
|
|
|
UnixMtime string `json:"unixMtime"`
|
|
|
|
UnixCtime string `json:"unixCtime"`
|
|
|
|
UnixAtime string `json:"unixAtime"`
|
2011-03-23 05:11:27 +00:00
|
|
|
|
2012-08-24 01:44:03 +00:00
|
|
|
// Parts are references to the data chunks of a regular file (or a "bytes" schema blob).
|
|
|
|
// See doc/schema/bytes.txt and doc/schema/files/file.txt.
|
2011-09-08 00:51:29 +00:00
|
|
|
Parts []*BytesPart `json:"parts"`
|
2011-03-23 05:32:11 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
Entries blob.Ref `json:"entries"` // for directories, a blobref to a static-set
|
|
|
|
Members []blob.Ref `json:"members"` // for static sets (for directory static-sets: blobrefs to child dirs/files)
|
2013-01-02 20:55:12 +00:00
|
|
|
|
|
|
|
// Target is a "share" blob's target (the thing being shared)
|
2013-10-25 22:21:59 +00:00
|
|
|
// Or it is the object being deleted in a DeleteClaim claim.
|
2013-08-04 02:54:30 +00:00
|
|
|
Target blob.Ref `json:"target"`
|
2013-01-02 20:55:12 +00:00
|
|
|
// Transitive is a property of a "share" blob.
|
2013-01-02 22:50:52 +00:00
|
|
|
Transitive bool `json:"transitive"`
|
2013-01-02 20:55:12 +00:00
|
|
|
// AuthType is a "share" blob's authentication type that is required.
|
|
|
|
// Currently (2013-01-02) just "haveref" (if you know the share's blobref,
|
|
|
|
// you get access: the secret URL model)
|
2013-09-09 00:46:40 +00:00
|
|
|
AuthType string `json:"authType"`
|
|
|
|
Expires types.Time3339 `json:"expires"` // or zero for no expiration
|
2013-01-02 20:55:12 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
func parseSuperset(r io.Reader) (*superset, error) {
|
|
|
|
var ss superset
|
2013-01-14 04:43:55 +00:00
|
|
|
if err := json.NewDecoder(io.LimitReader(r, 1<<20)).Decode(&ss); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &ss, nil
|
2011-03-23 05:11:27 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 17:32:40 +00:00
|
|
|
// BlobReader returns a new Blob from the provided Reader r,
|
|
|
|
// which should be the body of the provided blobref.
|
2013-12-31 04:17:30 +00:00
|
|
|
// Note: the hash checksum is not verified.
|
2013-08-04 02:54:30 +00:00
|
|
|
func BlobFromReader(ref blob.Ref, r io.Reader) (*Blob, error) {
|
|
|
|
if !ref.Valid() {
|
|
|
|
return nil, errors.New("schema.BlobFromReader: invalid blobref")
|
2013-01-22 04:56:12 +00:00
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
2013-01-22 17:32:40 +00:00
|
|
|
tee := io.TeeReader(r, &buf)
|
2013-01-22 18:32:15 +00:00
|
|
|
ss, err := parseSuperset(tee)
|
2013-01-22 04:56:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-01-22 17:32:40 +00:00
|
|
|
var wb [16]byte
|
|
|
|
afterObj := 0
|
|
|
|
for {
|
|
|
|
n, err := tee.Read(wb[:])
|
|
|
|
afterObj += n
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if !isASCIIWhite(wb[i]) {
|
|
|
|
return nil, fmt.Errorf("invalid bytes after JSON schema blob in %v", ref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if afterObj > MaxSchemaBlobSize {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
json := buf.String()
|
|
|
|
if len(json) > MaxSchemaBlobSize {
|
|
|
|
return nil, fmt.Errorf("schema: metadata blob %v is over expected limit; size=%d", ref, len(json))
|
|
|
|
}
|
|
|
|
return &Blob{ref, json, ss}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isASCIIWhite(b byte) bool {
|
|
|
|
switch b {
|
|
|
|
case ' ', '\t', '\r', '\n':
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2013-01-22 04:56:12 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:44:03 +00:00
|
|
|
// BytesPart is the type representing one of the "parts" in a "file"
|
|
|
|
// or "bytes" JSON schema.
|
|
|
|
//
|
|
|
|
// See doc/schema/bytes.txt and doc/schema/files/file.txt.
|
2011-09-08 00:51:29 +00:00
|
|
|
type BytesPart struct {
|
2012-08-24 01:44:03 +00:00
|
|
|
// Size is the number of bytes that this part contributes to the overall segment.
|
2011-09-08 00:51:29 +00:00
|
|
|
Size uint64 `json:"size"`
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
// At most one of BlobRef or BytesRef must be non-zero
|
|
|
|
// (Valid), but it's illegal for both.
|
2012-08-24 01:44:03 +00:00
|
|
|
// If neither are set, this BytesPart represents Size zero bytes.
|
|
|
|
// BlobRef refers to raw bytes. BytesRef references a "bytes" schema blob.
|
2013-08-04 02:54:30 +00:00
|
|
|
BlobRef blob.Ref `json:"blobRef,omitempty"`
|
|
|
|
BytesRef blob.Ref `json:"bytesRef,omitempty"`
|
2011-09-08 00:51:29 +00:00
|
|
|
|
2012-08-24 01:44:03 +00:00
|
|
|
// Offset optionally specifies the offset into BlobRef to skip
|
|
|
|
// when reading Size bytes.
|
2011-09-08 00:51:29 +00:00
|
|
|
Offset uint64 `json:"offset,omitempty"`
|
2011-03-24 22:33:15 +00:00
|
|
|
}
|
|
|
|
|
2012-08-24 01:44:03 +00:00
|
|
|
// stringFromMixedArray joins a slice of either strings or float64
|
|
|
|
// values (as retrieved from JSON decoding) into a string. These are
|
|
|
|
// used for non-UTF8 filenames in "fileNameBytes" fields. The strings
|
|
|
|
// are UTF-8 segments and the float64s (actually uint8 values) are
|
|
|
|
// byte values.
|
2011-03-26 01:36:08 +00:00
|
|
|
func stringFromMixedArray(parts []interface{}) string {
|
2012-08-24 01:44:03 +00:00
|
|
|
var buf bytes.Buffer
|
2011-03-26 01:36:08 +00:00
|
|
|
for _, part := range parts {
|
|
|
|
if s, ok := part.(string); ok {
|
|
|
|
buf.WriteString(s)
|
|
|
|
continue
|
|
|
|
}
|
2011-03-26 01:45:10 +00:00
|
|
|
if num, ok := part.(float64); ok {
|
|
|
|
buf.WriteByte(byte(num))
|
2011-05-26 23:41:49 +00:00
|
|
|
continue
|
2011-03-26 01:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2014-03-15 04:39:08 +00:00
|
|
|
func mixedArrayFromString(s string) []interface{} {
|
|
|
|
buf := []byte(s)
|
|
|
|
var name []interface{}
|
|
|
|
n := 0
|
|
|
|
for n < len(buf) {
|
|
|
|
part, offset := nextStringOrByte(buf[n:])
|
|
|
|
name = append(name, part)
|
|
|
|
n += offset
|
|
|
|
}
|
|
|
|
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextStringOrByte(b []byte) (interface{}, int) {
|
|
|
|
n := 0
|
|
|
|
var s []byte
|
|
|
|
for n < len(b) {
|
|
|
|
r, size := utf8.DecodeRune(b[n:])
|
|
|
|
if r == utf8.RuneError {
|
|
|
|
// If we already have a UTF8 string segment, return it
|
|
|
|
if len(s) > 0 {
|
|
|
|
return string(s), n
|
|
|
|
}
|
|
|
|
// Return the single byte and an offset of 1
|
|
|
|
return b[n], 1
|
|
|
|
}
|
|
|
|
n += size // We have consumed size bytes
|
|
|
|
c := make([]byte, utf8.RuneLen(r))
|
|
|
|
_ = utf8.EncodeRune(c, r)
|
|
|
|
s = append(s, c...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(s), n
|
|
|
|
}
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
func (ss *superset) SumPartsSize() (size uint64) {
|
2011-09-08 00:51:29 +00:00
|
|
|
for _, part := range ss.Parts {
|
|
|
|
size += uint64(part.Size)
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
func (ss *superset) SymlinkTargetString() string {
|
2011-03-26 01:36:08 +00:00
|
|
|
if ss.SymlinkTarget != "" {
|
|
|
|
return ss.SymlinkTarget
|
|
|
|
}
|
|
|
|
return stringFromMixedArray(ss.SymlinkTargetBytes)
|
|
|
|
}
|
|
|
|
|
2013-01-14 04:43:55 +00:00
|
|
|
// FileNameString returns the schema blob's base filename.
|
|
|
|
//
|
|
|
|
// If the fileName field of the blob accidentally or maliciously
|
|
|
|
// contains a slash, this function returns an empty string instead.
|
2013-01-22 18:32:15 +00:00
|
|
|
func (ss *superset) FileNameString() string {
|
2013-01-14 04:43:55 +00:00
|
|
|
v := ss.FileName
|
|
|
|
if v == "" {
|
|
|
|
v = stringFromMixedArray(ss.FileNameBytes)
|
|
|
|
}
|
|
|
|
if v != "" {
|
|
|
|
if strings.Index(v, "/") != -1 {
|
|
|
|
// Bogus schema blob; ignore.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if strings.Index(v, "\\") != -1 {
|
|
|
|
// Bogus schema blob; ignore.
|
|
|
|
return ""
|
|
|
|
}
|
2011-03-26 01:36:08 +00:00
|
|
|
}
|
2013-01-14 04:43:55 +00:00
|
|
|
return v
|
2011-03-26 01:36:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
func (ss *superset) HasFilename(name string) bool {
|
2011-03-26 01:36:08 +00:00
|
|
|
return ss.FileNameString() == name
|
2011-03-13 18:38:53 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 17:32:40 +00:00
|
|
|
func (b *Blob) FileMode() os.FileMode {
|
|
|
|
// TODO: move this to a different type, off *Blob
|
|
|
|
return b.ss.FileMode()
|
|
|
|
}
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
func (ss *superset) FileMode() os.FileMode {
|
2012-03-05 16:04:42 +00:00
|
|
|
var mode os.FileMode
|
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
|
|
|
m64, err := strconv.ParseUint(ss.UnixPermission, 8, 64)
|
2011-03-23 05:59:45 +00:00
|
|
|
if err == nil {
|
2012-03-05 16:04:42 +00:00
|
|
|
mode = mode | os.FileMode(m64)
|
2011-03-23 05:59:45 +00:00
|
|
|
}
|
|
|
|
|
2012-03-05 16:04:42 +00:00
|
|
|
// TODO: add other types (block, char, etc)
|
2011-03-23 05:59:45 +00:00
|
|
|
switch ss.Type {
|
|
|
|
case "directory":
|
2012-03-05 16:04:42 +00:00
|
|
|
mode = mode | os.ModeDir
|
2011-03-23 05:59:45 +00:00
|
|
|
case "file":
|
2012-03-05 16:04:42 +00:00
|
|
|
// No extra bit.
|
2011-03-23 05:59:45 +00:00
|
|
|
case "symlink":
|
2012-03-05 16:04:42 +00:00
|
|
|
mode = mode | os.ModeSymlink
|
2011-03-23 05:59:45 +00:00
|
|
|
}
|
2012-03-05 16:04:42 +00:00
|
|
|
return mode
|
2011-03-23 05:59:45 +00:00
|
|
|
}
|
|
|
|
|
2012-04-29 00:03:07 +00:00
|
|
|
// MapUid returns the most appropriate mapping from this file's owner
|
|
|
|
// to the local machine's owner, trying first a match by name,
|
|
|
|
// followed by just mapping the number through directly.
|
2013-01-22 18:20:34 +00:00
|
|
|
func (b *Blob) MapUid() int { return b.ss.MapUid() }
|
|
|
|
|
|
|
|
// MapGid returns the most appropriate mapping from this file's group
|
|
|
|
// to the local machine's group, trying first a match by name,
|
|
|
|
// followed by just mapping the number through directly.
|
|
|
|
func (b *Blob) MapGid() int { return b.ss.MapGid() }
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
func (ss *superset) MapUid() int {
|
2012-04-29 00:03:07 +00:00
|
|
|
if ss.UnixOwner != "" {
|
|
|
|
uid, ok := getUidFromName(ss.UnixOwner)
|
|
|
|
if ok {
|
|
|
|
return uid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss.UnixOwnerId // TODO: will be 0 if unset, which isn't ideal
|
|
|
|
}
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
func (ss *superset) MapGid() int {
|
2012-04-29 00:03:07 +00:00
|
|
|
if ss.UnixGroup != "" {
|
|
|
|
gid, ok := getGidFromName(ss.UnixGroup)
|
|
|
|
if ok {
|
|
|
|
return gid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ss.UnixGroupId // TODO: will be 0 if unset, which isn't ideal
|
|
|
|
}
|
|
|
|
|
2013-01-22 18:32:15 +00:00
|
|
|
func (ss *superset) ModTime() time.Time {
|
2012-04-28 08:34:53 +00:00
|
|
|
if ss.UnixMtime == "" {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
2012-05-13 20:27:11 +00:00
|
|
|
t, err := time.Parse(time.RFC3339, ss.UnixMtime)
|
|
|
|
if err != nil {
|
2012-04-28 08:34:53 +00:00
|
|
|
return time.Time{}
|
|
|
|
}
|
2012-05-13 20:27:11 +00:00
|
|
|
return t
|
2012-04-28 08:34:53 +00:00
|
|
|
}
|
|
|
|
|
2010-12-31 06:37:46 +00:00
|
|
|
var DefaultStatHasher = &defaultStatHasher{}
|
|
|
|
|
|
|
|
type defaultStatHasher struct{}
|
|
|
|
|
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 (d *defaultStatHasher) Lstat(fileName string) (os.FileInfo, error) {
|
2010-12-31 06:37:46 +00:00
|
|
|
return os.Lstat(fileName)
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (d *defaultStatHasher) Hash(fileName string) (blob.Ref, error) {
|
2010-12-31 06:37:46 +00:00
|
|
|
s1 := sha1.New()
|
2011-04-07 17:58:29 +00:00
|
|
|
file, err := os.Open(fileName)
|
2010-12-31 06:37:46 +00:00
|
|
|
if err != nil {
|
2013-08-04 02:54:30 +00:00
|
|
|
return blob.Ref{}, err
|
2010-12-31 06:37:46 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
_, err = io.Copy(s1, file)
|
2011-03-13 18:39:08 +00:00
|
|
|
if err != nil {
|
2013-08-04 02:54:30 +00:00
|
|
|
return blob.Ref{}, err
|
2011-03-13 18:39:08 +00:00
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
return blob.RefFromHash(s1), nil
|
2010-12-31 06:37:46 +00:00
|
|
|
}
|
2010-12-30 18:17:47 +00:00
|
|
|
|
2011-01-01 23:44:08 +00:00
|
|
|
type StaticSet struct {
|
2011-03-13 18:39:08 +00:00
|
|
|
l sync.Mutex
|
2013-08-04 02:54:30 +00:00
|
|
|
refs []blob.Ref
|
2011-01-01 23:44:08 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func (ss *StaticSet) Add(ref blob.Ref) {
|
2011-01-01 23:44:08 +00:00
|
|
|
ss.l.Lock()
|
|
|
|
defer ss.l.Unlock()
|
|
|
|
ss.refs = append(ss.refs, ref)
|
|
|
|
}
|
|
|
|
|
2013-02-26 04:56:20 +00:00
|
|
|
func base(version int, ctype string) *Builder {
|
2013-01-22 04:56:12 +00:00
|
|
|
return &Builder{map[string]interface{}{
|
2012-08-21 18:31:48 +00:00
|
|
|
"camliVersion": version,
|
2012-08-24 03:09:31 +00:00
|
|
|
"camliType": ctype,
|
2013-01-22 04:56:12 +00:00
|
|
|
}}
|
2011-01-01 23:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 18:14:47 +00:00
|
|
|
// NewUnsignedPermanode returns a new random permanode, not yet signed.
|
2013-01-22 04:56:12 +00:00
|
|
|
func NewUnsignedPermanode() *Builder {
|
2013-02-26 04:56:20 +00:00
|
|
|
bb := base(1, "permanode")
|
2011-01-16 02:10:58 +00:00
|
|
|
chars := make([]byte, 20)
|
2012-03-03 20:37:54 +00:00
|
|
|
_, err := io.ReadFull(rand.Reader, chars)
|
|
|
|
if err != nil {
|
|
|
|
panic("error reading random bytes: " + err.Error())
|
2011-01-16 02:10:58 +00:00
|
|
|
}
|
2013-01-22 04:56:12 +00:00
|
|
|
bb.m["random"] = base64.StdEncoding.EncodeToString(chars)
|
|
|
|
return bb
|
2011-01-16 02:10:58 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 18:14:47 +00:00
|
|
|
// NewPlannedPermanode returns a permanode with a fixed key. Like
|
2013-01-22 04:56:12 +00:00
|
|
|
// NewUnsignedPermanode, this builder is also not yet signed. Callers of
|
2012-08-21 18:14:47 +00:00
|
|
|
// NewPlannedPermanode must sign the map with a fixed claimDate and
|
|
|
|
// GPG date to create consistent JSON encodings of the Map (its
|
|
|
|
// blobref), between runs.
|
2013-01-22 04:56:12 +00:00
|
|
|
func NewPlannedPermanode(key string) *Builder {
|
2013-02-26 04:56:20 +00:00
|
|
|
bb := base(1, "permanode")
|
2013-01-22 04:56:12 +00:00
|
|
|
bb.m["key"] = key
|
|
|
|
return bb
|
2012-07-28 22:41:19 +00:00
|
|
|
}
|
|
|
|
|
2012-12-29 14:51:42 +00:00
|
|
|
// NewHashPlannedPermanode returns a planned permanode with the sum
|
|
|
|
// of the hash, prefixed with "sha1-", as the key.
|
2013-01-22 04:56:12 +00:00
|
|
|
func NewHashPlannedPermanode(h hash.Hash) *Builder {
|
2012-12-29 14:51:42 +00:00
|
|
|
if reflect.TypeOf(h) != sha1Type {
|
|
|
|
panic("Hash not supported. Only sha1 for now.")
|
|
|
|
}
|
|
|
|
return NewPlannedPermanode(fmt.Sprintf("sha1-%x", h.Sum(nil)))
|
|
|
|
}
|
|
|
|
|
2011-01-01 23:44:08 +00:00
|
|
|
// Map returns a Camli map of camliType "static-set"
|
2013-01-22 04:56:12 +00:00
|
|
|
// TODO: delete this method
|
|
|
|
func (ss *StaticSet) Blob() *Blob {
|
2013-02-26 04:56:20 +00:00
|
|
|
bb := base(1, "static-set")
|
2011-01-01 23:44:08 +00:00
|
|
|
ss.l.Lock()
|
|
|
|
defer ss.l.Unlock()
|
|
|
|
|
|
|
|
members := make([]string, 0, len(ss.refs))
|
|
|
|
if ss.refs != nil {
|
|
|
|
for _, ref := range ss.refs {
|
|
|
|
members = append(members, ref.String())
|
|
|
|
}
|
|
|
|
}
|
2013-01-22 04:56:12 +00:00
|
|
|
bb.m["members"] = members
|
|
|
|
return bb.Blob()
|
2011-01-01 23:44:08 +00:00
|
|
|
}
|
|
|
|
|
2012-08-21 18:14:47 +00:00
|
|
|
// JSON returns the map m encoded as JSON in its
|
|
|
|
// recommended canonical form. The canonical form is readable with newlines and indentation,
|
|
|
|
// and always starts with the header bytes:
|
|
|
|
//
|
|
|
|
// {"camliVersion":
|
|
|
|
//
|
2013-01-22 04:56:12 +00:00
|
|
|
func mapJSON(m map[string]interface{}) (string, error) {
|
2010-12-30 18:17:47 +00:00
|
|
|
version, hasVersion := m["camliVersion"]
|
|
|
|
if !hasVersion {
|
2011-05-26 23:41:49 +00:00
|
|
|
return "", ErrNoCamliVersion
|
2010-12-30 18:17:47 +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
|
|
|
delete(m, "camliVersion")
|
2010-12-30 18:17:47 +00:00
|
|
|
jsonBytes, err := json.MarshalIndent(m, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
m["camliVersion"] = version
|
2012-08-21 18:14:47 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
fmt.Fprintf(&buf, "{\"camliVersion\": %v,\n", version)
|
2010-12-30 18:17:47 +00:00
|
|
|
buf.Write(jsonBytes[2:])
|
2012-08-21 18:14:47 +00:00
|
|
|
return buf.String(), nil
|
2010-12-30 18:17:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 04:56:12 +00:00
|
|
|
// NewFileMap returns a new builder of a type "file" schema for the provided fileName.
|
2012-08-21 18:14:47 +00:00
|
|
|
// The chunk parts of the file are not populated.
|
2013-01-22 04:56:12 +00:00
|
|
|
func NewFileMap(fileName string) *Builder {
|
|
|
|
return newCommonFilenameMap(fileName).SetType("file")
|
2011-09-08 00:51:29 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 20:14:53 +00:00
|
|
|
// NewDirMap returns a new builder of a type "directory" schema for the provided fileName.
|
|
|
|
func NewDirMap(fileName string) *Builder {
|
|
|
|
return newCommonFilenameMap(fileName).SetType("directory")
|
|
|
|
}
|
|
|
|
|
2013-01-22 04:56:12 +00:00
|
|
|
func newCommonFilenameMap(fileName string) *Builder {
|
2013-02-26 04:56:20 +00:00
|
|
|
bb := base(1, "" /* no type yet */)
|
2011-06-06 15:54:31 +00:00
|
|
|
if fileName != "" {
|
2013-01-22 04:56:12 +00:00
|
|
|
bb.SetFileName(fileName)
|
2010-12-31 06:37:46 +00:00
|
|
|
}
|
2013-01-22 04:56:12 +00:00
|
|
|
return bb
|
2011-05-29 17:39:41 +00:00
|
|
|
}
|
2011-01-01 22:44:19 +00:00
|
|
|
|
2013-01-22 04:56:12 +00:00
|
|
|
var populateSchemaStat []func(schemaMap map[string]interface{}, fi os.FileInfo)
|
2012-04-14 01:40:59 +00:00
|
|
|
|
2013-01-22 04:56:12 +00:00
|
|
|
func NewCommonFileMap(fileName string, fi os.FileInfo) *Builder {
|
|
|
|
bb := newCommonFilenameMap(fileName)
|
2011-01-01 22:44:19 +00:00
|
|
|
// Common elements (from file-common.txt)
|
2012-04-28 08:34:53 +00:00
|
|
|
if fi.Mode()&os.ModeSymlink == 0 {
|
2013-01-22 04:56:12 +00:00
|
|
|
bb.m["unixPermission"] = fmt.Sprintf("0%o", fi.Mode().Perm())
|
2011-02-03 16:19:40 +00:00
|
|
|
}
|
2012-04-14 01:40:59 +00:00
|
|
|
|
|
|
|
// OS-specific population; defined in schema_posix.go, etc. (not on App Engine)
|
|
|
|
for _, f := range populateSchemaStat {
|
2013-01-22 04:56:12 +00:00
|
|
|
f(bb.m, fi)
|
2012-04-14 01:40:59 +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
|
|
|
if mtime := fi.ModTime(); !mtime.IsZero() {
|
2013-01-22 04:56:12 +00:00
|
|
|
bb.m["unixMtime"] = RFC3339FromTime(mtime)
|
2012-04-14 01:40:59 +00:00
|
|
|
}
|
2013-01-22 04:56:12 +00:00
|
|
|
return bb
|
2010-12-31 06:37:46 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 04:56:12 +00:00
|
|
|
// PopulateParts sets the "parts" field of the blob with the provided
|
2012-10-28 23:10:31 +00:00
|
|
|
// parts. The sum of the sizes of parts must match the provided size
|
|
|
|
// or an error is returned. Also, each BytesPart may only contain either
|
|
|
|
// a BytesPart or a BlobRef, but not both.
|
2013-01-22 04:56:12 +00:00
|
|
|
func (bb *Builder) PopulateParts(size int64, parts []BytesPart) error {
|
|
|
|
return populateParts(bb.m, size, parts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func populateParts(m map[string]interface{}, size int64, parts []BytesPart) error {
|
2011-09-08 00:51:29 +00:00
|
|
|
sumSize := int64(0)
|
2013-01-22 04:56:12 +00:00
|
|
|
mparts := make([]map[string]interface{}, len(parts))
|
2011-01-01 22:44:19 +00:00
|
|
|
for idx, part := range parts {
|
2013-01-22 04:56:12 +00:00
|
|
|
mpart := make(map[string]interface{})
|
2011-01-01 22:44:19 +00:00
|
|
|
mparts[idx] = mpart
|
2011-09-08 00:51:29 +00:00
|
|
|
switch {
|
2013-08-04 02:54:30 +00:00
|
|
|
case part.BlobRef.Valid() && part.BytesRef.Valid():
|
2012-10-28 23:10:31 +00:00
|
|
|
return errors.New("schema: part contains both BlobRef and BytesRef")
|
2013-08-04 02:54:30 +00:00
|
|
|
case part.BlobRef.Valid():
|
2011-06-06 15:54:31 +00:00
|
|
|
mpart["blobRef"] = part.BlobRef.String()
|
2013-08-04 02:54:30 +00:00
|
|
|
case part.BytesRef.Valid():
|
2011-09-08 00:51:29 +00:00
|
|
|
mpart["bytesRef"] = part.BytesRef.String()
|
2012-10-28 23:10:31 +00:00
|
|
|
default:
|
|
|
|
return errors.New("schema: part must contain either a BlobRef or BytesRef")
|
2011-06-06 15:54:31 +00:00
|
|
|
}
|
2011-01-01 22:44:19 +00:00
|
|
|
mpart["size"] = part.Size
|
2011-09-08 00:51:29 +00:00
|
|
|
sumSize += int64(part.Size)
|
2011-01-01 22:44:19 +00:00
|
|
|
if part.Offset != 0 {
|
|
|
|
mpart["offset"] = part.Offset
|
|
|
|
}
|
|
|
|
}
|
2011-09-08 00:51:29 +00:00
|
|
|
if sumSize != size {
|
|
|
|
return fmt.Errorf("schema: declared size %d doesn't match sum of parts size %d", size, sumSize)
|
2010-12-30 18:17:47 +00:00
|
|
|
}
|
2011-09-08 00:51:29 +00:00
|
|
|
m["parts"] = mparts
|
2011-01-01 21:12:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-01-22 04:56:12 +00:00
|
|
|
func newBytes() *Builder {
|
2013-02-26 04:56:20 +00:00
|
|
|
return base(1, "bytes")
|
2011-09-08 00:51:29 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 22:21:59 +00:00
|
|
|
// ClaimType is one of the valid "claimType" fields in a "claim" schema blob. See doc/schema/claims/.
|
2013-02-26 04:56:20 +00:00
|
|
|
type ClaimType string
|
|
|
|
|
2012-08-21 06:11:10 +00:00
|
|
|
const (
|
2013-10-25 22:21:59 +00:00
|
|
|
SetAttributeClaim ClaimType = "set-attribute"
|
|
|
|
AddAttributeClaim ClaimType = "add-attribute"
|
|
|
|
DelAttributeClaim ClaimType = "del-attribute"
|
|
|
|
ShareClaim ClaimType = "share"
|
|
|
|
// DeleteClaim deletes a permanode or another claim.
|
|
|
|
// A delete claim can itself be deleted, and so on.
|
|
|
|
DeleteClaim ClaimType = "delete"
|
2012-08-21 06:11:10 +00:00
|
|
|
)
|
|
|
|
|
2013-06-21 20:23:37 +00:00
|
|
|
// claimParam is used to populate a claim map when building a new claim
|
|
|
|
type claimParam struct {
|
|
|
|
claimType ClaimType
|
|
|
|
|
|
|
|
// Params specific to *Attribute claims:
|
2013-08-04 02:54:30 +00:00
|
|
|
permanode blob.Ref // modified permanode
|
|
|
|
attribute string // required
|
2013-10-25 22:21:59 +00:00
|
|
|
value string // optional if Type == DelAttributeClaim
|
2013-06-21 20:23:37 +00:00
|
|
|
|
2013-10-25 22:21:59 +00:00
|
|
|
// Params specific to ShareClaim claims:
|
2013-09-09 00:46:40 +00:00
|
|
|
authType string
|
|
|
|
transitive bool
|
|
|
|
shareExpires time.Time // Zero means no expiration
|
2013-10-25 22:21:59 +00:00
|
|
|
|
|
|
|
// Params specific to ShareClaim and DeleteClaim claims.
|
|
|
|
target blob.Ref
|
2013-02-26 04:56:20 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:23:37 +00:00
|
|
|
func NewClaim(claims ...*claimParam) *Builder {
|
2013-02-26 04:56:20 +00:00
|
|
|
bb := base(1, "claim")
|
2013-09-09 00:46:40 +00:00
|
|
|
bb.SetClaimDate(clockNow())
|
2013-02-26 04:56:20 +00:00
|
|
|
if len(claims) == 1 {
|
|
|
|
cp := claims[0]
|
|
|
|
populateClaimMap(bb.m, cp)
|
|
|
|
return bb
|
|
|
|
}
|
|
|
|
var claimList []interface{}
|
|
|
|
for _, cp := range claims {
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
populateClaimMap(m, cp)
|
|
|
|
claimList = append(claimList, m)
|
|
|
|
}
|
|
|
|
bb.m["claimType"] = "multi"
|
|
|
|
bb.m["claims"] = claimList
|
2013-01-22 04:56:12 +00:00
|
|
|
return bb
|
2011-03-13 03:28:18 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:23:37 +00:00
|
|
|
func populateClaimMap(m map[string]interface{}, cp *claimParam) {
|
2013-11-04 21:57:05 +00:00
|
|
|
m["claimType"] = string(cp.claimType)
|
2013-10-25 22:21:59 +00:00
|
|
|
switch cp.claimType {
|
|
|
|
case ShareClaim:
|
|
|
|
m["authType"] = cp.authType
|
|
|
|
m["target"] = cp.target.String()
|
|
|
|
m["transitive"] = cp.transitive
|
|
|
|
case DeleteClaim:
|
|
|
|
m["target"] = cp.target.String()
|
|
|
|
default:
|
2013-06-21 20:23:37 +00:00
|
|
|
m["permaNode"] = cp.permanode.String()
|
|
|
|
m["attribute"] = cp.attribute
|
2013-10-25 22:21:59 +00:00
|
|
|
if !(cp.claimType == DelAttributeClaim && cp.value == "") {
|
2013-06-21 20:23:37 +00:00
|
|
|
m["value"] = cp.value
|
|
|
|
}
|
2013-02-26 04:56:20 +00:00
|
|
|
}
|
2011-03-13 03:28:18 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 20:23:37 +00:00
|
|
|
// NewShareRef creates a *Builder for a "share" claim.
|
2013-08-04 02:54:30 +00:00
|
|
|
func NewShareRef(authType string, target blob.Ref, transitive bool) *Builder {
|
2013-06-21 20:23:37 +00:00
|
|
|
return NewClaim(&claimParam{
|
2013-10-25 22:21:59 +00:00
|
|
|
claimType: ShareClaim,
|
2013-06-21 20:23:37 +00:00
|
|
|
authType: authType,
|
|
|
|
target: target,
|
|
|
|
transitive: transitive,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func NewSetAttributeClaim(permaNode blob.Ref, attr, value string) *Builder {
|
2013-06-21 20:23:37 +00:00
|
|
|
return NewClaim(&claimParam{
|
|
|
|
permanode: permaNode,
|
2013-10-25 22:21:59 +00:00
|
|
|
claimType: SetAttributeClaim,
|
2013-06-21 20:23:37 +00:00
|
|
|
attribute: attr,
|
|
|
|
value: value,
|
2013-02-26 04:56:20 +00:00
|
|
|
})
|
2011-03-13 03:28:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
func NewAddAttributeClaim(permaNode blob.Ref, attr, value string) *Builder {
|
2013-06-21 20:23:37 +00:00
|
|
|
return NewClaim(&claimParam{
|
|
|
|
permanode: permaNode,
|
2013-10-25 22:21:59 +00:00
|
|
|
claimType: AddAttributeClaim,
|
2013-06-21 20:23:37 +00:00
|
|
|
attribute: attr,
|
|
|
|
value: value,
|
2013-02-26 04:56:20 +00:00
|
|
|
})
|
2011-03-13 03:28:18 +00:00
|
|
|
}
|
|
|
|
|
2013-09-23 12:30:05 +00:00
|
|
|
// NewDelAttributeClaim creates a new claim to remove value from the
|
|
|
|
// values set for the attribute attr of permaNode. If value is empty then
|
|
|
|
// all the values for attribute are cleared.
|
|
|
|
func NewDelAttributeClaim(permaNode blob.Ref, attr, value string) *Builder {
|
2013-06-21 20:23:37 +00:00
|
|
|
return NewClaim(&claimParam{
|
|
|
|
permanode: permaNode,
|
2013-10-25 22:21:59 +00:00
|
|
|
claimType: DelAttributeClaim,
|
2013-06-21 20:23:37 +00:00
|
|
|
attribute: attr,
|
2013-09-23 12:30:05 +00:00
|
|
|
value: value,
|
2013-02-26 04:56:20 +00:00
|
|
|
})
|
2012-12-23 06:48:21 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 22:21:59 +00:00
|
|
|
// NewDeleteClaim creates a new claim to delete a target claim or permanode.
|
|
|
|
func NewDeleteClaim(target blob.Ref) *Builder {
|
|
|
|
return NewClaim(&claimParam{
|
|
|
|
target: target,
|
|
|
|
claimType: DeleteClaim,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-03-08 16:59:26 +00:00
|
|
|
// ShareHaveRef is the auth type specifying that if you "have the
|
2012-08-24 01:44:03 +00:00
|
|
|
// reference" (know the blobref to the haveref share blob), then you
|
|
|
|
// have access to the referenced object from that share blob.
|
|
|
|
// This is the "send a link to a friend" access model.
|
2011-01-26 06:44:03 +00:00
|
|
|
const ShareHaveRef = "haveref"
|
|
|
|
|
2012-05-13 20:27:11 +00:00
|
|
|
// RFC3339FromTime returns an RFC3339-formatted time in UTC.
|
|
|
|
// Fractional seconds are only included if the time has fractional
|
|
|
|
// seconds.
|
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 RFC3339FromTime(t time.Time) string {
|
2012-08-21 06:11:10 +00:00
|
|
|
if t.UnixNano()%1e9 == 0 {
|
2012-05-13 20:27:11 +00:00
|
|
|
return t.UTC().Format(time.RFC3339)
|
2010-12-31 20:13:33 +00:00
|
|
|
}
|
2012-05-13 20:27:11 +00:00
|
|
|
return t.UTC().Format(time.RFC3339Nano)
|
2010-12-31 20:13:33 +00:00
|
|
|
}
|
2013-01-02 22:50:52 +00:00
|
|
|
|
|
|
|
var bytesCamliVersion = []byte("camliVersion")
|
|
|
|
|
|
|
|
// LikelySchemaBlob returns quickly whether buf likely contains (or is
|
|
|
|
// the prefix of) a schema blob.
|
|
|
|
func LikelySchemaBlob(buf []byte) bool {
|
|
|
|
if len(buf) == 0 || buf[0] != '{' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return bytes.Contains(buf, bytesCamliVersion)
|
2013-01-07 19:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// findSize checks if v is an *os.File or if it has
|
|
|
|
// a Size() int64 method, to find its size.
|
|
|
|
// It returns 0, false otherwise.
|
|
|
|
func findSize(v interface{}) (size int64, ok bool) {
|
|
|
|
if fi, ok := v.(*os.File); ok {
|
|
|
|
v, _ = fi.Stat()
|
|
|
|
}
|
|
|
|
if sz, ok := v.(interface {
|
|
|
|
Size() int64
|
|
|
|
}); ok {
|
|
|
|
return sz.Size(), true
|
|
|
|
}
|
2013-02-19 05:29:19 +00:00
|
|
|
// For bytes.Reader, strings.Reader, etc:
|
|
|
|
if li, ok := v.(interface {
|
|
|
|
Len() int
|
|
|
|
}); ok {
|
|
|
|
ln := int64(li.Len()) // unread portion, typically
|
|
|
|
// If it's also a seeker, remove add any seek offset:
|
|
|
|
if sk, ok := v.(io.Seeker); ok {
|
|
|
|
if cur, err := sk.Seek(0, 1); err == nil {
|
|
|
|
ln += cur
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ln, true
|
|
|
|
}
|
2013-01-07 19:15:18 +00:00
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileTime returns the best guess of the file's creation time (or modtime).
|
|
|
|
// If the file doesn't have its own metadata indication the creation time (such as in EXIF),
|
|
|
|
// FileTime uses the modification time from the file system.
|
|
|
|
// It there was a valid EXIF but an error while trying to get a date from it,
|
|
|
|
// it logs the error and tries the other methods.
|
|
|
|
func FileTime(f io.ReaderAt) (time.Time, error) {
|
|
|
|
var ct time.Time
|
|
|
|
defaultTime := func() (time.Time, error) {
|
|
|
|
if osf, ok := f.(*os.File); ok {
|
|
|
|
fi, err := osf.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return ct, fmt.Errorf("Failed to find a modtime: lstat: %v", err)
|
|
|
|
}
|
|
|
|
return fi.ModTime(), nil
|
|
|
|
}
|
|
|
|
return ct, errors.New("All methods failed to find a creation time or modtime.")
|
|
|
|
}
|
|
|
|
|
|
|
|
size, ok := findSize(f)
|
|
|
|
if !ok {
|
|
|
|
size = 256 << 10 // enough to get the EXIF
|
|
|
|
}
|
|
|
|
r := io.NewSectionReader(f, 0, size)
|
|
|
|
ex, err := exif.Decode(r)
|
|
|
|
if err != nil {
|
|
|
|
return defaultTime()
|
|
|
|
}
|
|
|
|
ct, err = ex.DateTime()
|
|
|
|
if err != nil {
|
|
|
|
return defaultTime()
|
|
|
|
}
|
|
|
|
return ct, nil
|
|
|
|
}
|