2011-09-17 17:26:32 +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 main
|
|
|
|
|
|
|
|
import (
|
2012-04-23 00:18:19 +00:00
|
|
|
"bufio"
|
2012-12-28 18:25:03 +00:00
|
|
|
"bytes"
|
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
|
|
|
"errors"
|
2012-04-23 00:18:19 +00:00
|
|
|
"fmt"
|
2012-12-28 18:25:03 +00:00
|
|
|
"hash/crc32"
|
2012-04-21 15:54:26 +00:00
|
|
|
"io"
|
2011-09-17 17:26:32 +00:00
|
|
|
"log"
|
2012-12-31 21:45:13 +00:00
|
|
|
"net/url"
|
2013-01-02 04:23:44 +00:00
|
|
|
"os"
|
2011-09-17 17:26:32 +00:00
|
|
|
"path/filepath"
|
2012-04-23 00:18:19 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2011-09-17 17:26:32 +00:00
|
|
|
"sync"
|
|
|
|
|
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
|
|
|
"camlistore.org/pkg/blobref"
|
|
|
|
"camlistore.org/pkg/client"
|
|
|
|
"camlistore.org/pkg/osutil"
|
2011-09-17 17:26:32 +00:00
|
|
|
)
|
|
|
|
|
2012-04-23 00:18:19 +00:00
|
|
|
type statFingerprint string
|
2012-04-20 21:27:52 +00:00
|
|
|
|
2013-01-02 05:05:02 +00:00
|
|
|
var cleanSysStat func(v interface{}) interface{}
|
|
|
|
|
2012-04-23 00:18:19 +00:00
|
|
|
func fileInfoToFingerprint(fi os.FileInfo) statFingerprint {
|
2012-12-28 18:25:03 +00:00
|
|
|
// We calculate the CRC32 of the underlying system stat structure to get
|
|
|
|
// ctime, owner, group, etc. This is overkill (e.g. we don't care about
|
|
|
|
// the inode or device number probably), but works.
|
|
|
|
sysHash := uint32(0)
|
|
|
|
if sys := fi.Sys(); sys != nil {
|
2013-01-02 05:05:02 +00:00
|
|
|
if clean := cleanSysStat; clean != nil {
|
|
|
|
// TODO: don't clean bad fields, but provide a
|
|
|
|
// portable way to extract all good fields.
|
|
|
|
// This is a Linux+Mac-specific hack for now.
|
|
|
|
sys = clean(sys)
|
|
|
|
}
|
2012-12-28 18:25:03 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
fmt.Fprintf(&buf, "%#v", sys)
|
|
|
|
sysHash = crc32.ChecksumIEEE(buf.Bytes())
|
|
|
|
}
|
|
|
|
return statFingerprint(fmt.Sprintf("%dB/%dMOD/sys-%d", fi.Size(), fi.ModTime().UnixNano(), sysHash))
|
2012-04-20 21:27:52 +00:00
|
|
|
}
|
|
|
|
|
2011-09-17 17:26:32 +00:00
|
|
|
type fileInfoPutRes struct {
|
2012-04-20 21:27:52 +00:00
|
|
|
Fingerprint statFingerprint
|
|
|
|
Result client.PutResult
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
2011-09-17 23:59:04 +00:00
|
|
|
// FlatStatCache is an ugly hack, until leveldb-go is ready
|
2011-09-17 17:26:32 +00:00
|
|
|
// (http://code.google.com/p/leveldb-go/)
|
2011-09-17 23:59:04 +00:00
|
|
|
type FlatStatCache struct {
|
2012-04-23 00:18:19 +00:00
|
|
|
mu sync.RWMutex
|
2011-09-17 17:26:32 +00:00
|
|
|
filename string
|
|
|
|
m map[string]fileInfoPutRes
|
2012-04-23 00:18:19 +00:00
|
|
|
af *os.File // for appending
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 21:45:13 +00:00
|
|
|
func escapeGen(gen string) string {
|
|
|
|
// Good enough:
|
|
|
|
return url.QueryEscape(gen)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFlatStatCache(gen string) *FlatStatCache {
|
2013-02-18 23:35:43 +00:00
|
|
|
filename := filepath.Join(osutil.CacheDir(), "camput.statcache."+escapeGen(gen))
|
2011-09-17 23:59:04 +00:00
|
|
|
fc := &FlatStatCache{
|
2011-09-17 17:26:32 +00:00
|
|
|
filename: filename,
|
|
|
|
m: make(map[string]fileInfoPutRes),
|
|
|
|
}
|
|
|
|
|
2012-04-23 00:18:19 +00:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return fc
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("opening camput stat cache: %v", filename, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
br := bufio.NewReader(f)
|
|
|
|
for {
|
|
|
|
ln, err := br.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Warning: (ignoring) reading stat cache: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ln = strings.TrimSpace(ln)
|
|
|
|
f := strings.Split(ln, "\t")
|
|
|
|
if len(f) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filename, fp, putres := f[0], statFingerprint(f[1]), f[2]
|
|
|
|
f = strings.Split(putres, "/")
|
|
|
|
if len(f) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
blobrefStr := f[0]
|
|
|
|
blobSize, err := strconv.ParseInt(f[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fc.m[filename] = fileInfoPutRes{
|
|
|
|
Fingerprint: fp,
|
|
|
|
Result: client.PutResult{
|
|
|
|
BlobRef: blobref.Parse(blobrefStr),
|
|
|
|
Size: blobSize,
|
|
|
|
Skipped: true, // is this used?
|
|
|
|
},
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-23 00:18:19 +00:00
|
|
|
vlog.Printf("Flatcache read %d entries from %s", len(fc.m), filename)
|
2011-09-17 17:26:32 +00:00
|
|
|
return fc
|
|
|
|
}
|
|
|
|
|
2011-09-17 23:59:04 +00:00
|
|
|
var _ UploadCache = (*FlatStatCache)(nil)
|
2011-09-17 17:26:32 +00:00
|
|
|
|
2012-12-28 18:25:03 +00:00
|
|
|
var errCacheMiss = errors.New("not in cache")
|
2011-09-17 17:26:32 +00:00
|
|
|
|
2012-12-28 18:25:03 +00:00
|
|
|
// cacheKey returns the cleaned absolute path of joining pwd and filename.
|
2011-09-17 17:26:32 +00:00
|
|
|
func cacheKey(pwd, filename string) string {
|
2012-04-20 21:43:23 +00:00
|
|
|
if filepath.IsAbs(filename) {
|
|
|
|
return filepath.Clean(filename)
|
|
|
|
}
|
2012-04-20 21:27:52 +00:00
|
|
|
return filepath.Join(pwd, filename)
|
2011-09-17 17:26:32 +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 (c *FlatStatCache) CachedPutResult(pwd, filename string, fi os.FileInfo) (*client.PutResult, error) {
|
2012-04-23 00:18:19 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
2011-09-17 17:26:32 +00:00
|
|
|
|
2012-04-20 21:27:52 +00:00
|
|
|
fp := fileInfoToFingerprint(fi)
|
|
|
|
|
2011-09-17 22:14:37 +00:00
|
|
|
key := cacheKey(pwd, filename)
|
|
|
|
val, ok := c.m[key]
|
2011-09-17 17:26:32 +00:00
|
|
|
if !ok {
|
2011-09-25 00:20:47 +00:00
|
|
|
cachelog.Printf("cache MISS on %q: not in cache", key)
|
2012-12-28 18:25:03 +00:00
|
|
|
return nil, errCacheMiss
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
2012-04-23 00:18:19 +00:00
|
|
|
if val.Fingerprint != fp {
|
2012-04-20 21:27:52 +00:00
|
|
|
cachelog.Printf("cache MISS on %q: stats not equal:\n%#v\n%#v", key, val.Fingerprint, fp)
|
2012-12-28 18:25:03 +00:00
|
|
|
return nil, errCacheMiss
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
2012-04-20 21:27:52 +00:00
|
|
|
pr := val.Result
|
2011-09-17 17:26:32 +00:00
|
|
|
return &pr, 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 (c *FlatStatCache) AddCachedPutResult(pwd, filename string, fi os.FileInfo, pr *client.PutResult) {
|
2011-09-17 17:26:32 +00:00
|
|
|
c.mu.Lock()
|
2011-09-17 22:14:37 +00:00
|
|
|
defer c.mu.Unlock()
|
2011-09-17 17:26:32 +00:00
|
|
|
key := cacheKey(pwd, filename)
|
2012-04-20 21:27:52 +00:00
|
|
|
val := fileInfoPutRes{fileInfoToFingerprint(fi), *pr}
|
2011-09-17 17:26:32 +00:00
|
|
|
|
2011-09-25 00:20:47 +00:00
|
|
|
cachelog.Printf("Adding to stat cache %q: %v", key, val)
|
2011-09-17 17:26:32 +00:00
|
|
|
|
2011-09-17 22:14:37 +00:00
|
|
|
c.m[key] = val
|
2012-04-23 00:18:19 +00:00
|
|
|
if c.af == nil {
|
|
|
|
var err error
|
|
|
|
c.af, err = os.OpenFile(c.filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("opening stat cache for append: %v", err)
|
|
|
|
return
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-31 21:53:44 +00:00
|
|
|
// TODO: flocking. see leveldb-go.
|
2012-04-23 00:18:19 +00:00
|
|
|
c.af.Seek(0, os.SEEK_END)
|
|
|
|
c.af.Write([]byte(fmt.Sprintf("%s\t%s\t%s/%d\n", key, val.Fingerprint, val.Result.BlobRef.String(), val.Result.Size)))
|
2011-09-17 23:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FlatHaveCache struct {
|
2012-04-23 00:18:19 +00:00
|
|
|
mu sync.RWMutex
|
2011-09-17 23:59:04 +00:00
|
|
|
filename string
|
2013-01-02 04:23:44 +00:00
|
|
|
m map[string]int64 // blobref string -> size
|
|
|
|
af *os.File // appending file
|
2011-09-17 23:59:04 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 21:45:13 +00:00
|
|
|
func NewFlatHaveCache(gen string) *FlatHaveCache {
|
2013-01-02 04:23:44 +00:00
|
|
|
filename := filepath.Join(osutil.CacheDir(), "camput.havecache."+escapeGen(gen))
|
2011-09-17 23:59:04 +00:00
|
|
|
c := &FlatHaveCache{
|
|
|
|
filename: filename,
|
2013-01-02 04:23:44 +00:00
|
|
|
m: make(map[string]int64),
|
2011-09-17 23:59:04 +00:00
|
|
|
}
|
2012-04-23 00:18:19 +00:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("opening camput have-cache: %v", filename, err)
|
|
|
|
}
|
|
|
|
br := bufio.NewReader(f)
|
|
|
|
for {
|
|
|
|
ln, err := br.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
2011-09-17 23:59:04 +00:00
|
|
|
}
|
2012-04-23 00:18:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Warning: (ignoring) reading have-cache: %v", err)
|
|
|
|
break
|
|
|
|
}
|
2013-01-02 04:23:44 +00:00
|
|
|
f := strings.Fields(strings.TrimSpace(ln))
|
|
|
|
if len(f) == 2 {
|
|
|
|
br, sizea := f[0], f[1]
|
|
|
|
if size, err := strconv.ParseInt(sizea, 10, 64); err == nil && size >= 0 {
|
|
|
|
c.m[br] = size
|
|
|
|
}
|
|
|
|
}
|
2011-09-17 23:59:04 +00:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2013-01-02 04:23:44 +00:00
|
|
|
func (c *FlatHaveCache) StatBlobCache(br *blobref.BlobRef) (size int64, ok bool) {
|
2012-04-23 00:18:19 +00:00
|
|
|
c.mu.RLock()
|
|
|
|
defer c.mu.RUnlock()
|
2013-01-02 04:23:44 +00:00
|
|
|
size, ok = c.m[br.String()]
|
|
|
|
return
|
2011-09-17 23:59:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-02 04:23:44 +00:00
|
|
|
func (c *FlatHaveCache) NoteBlobExists(br *blobref.BlobRef, size int64) {
|
2011-09-17 23:59:04 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2013-01-02 04:23:44 +00:00
|
|
|
if size < 0 {
|
|
|
|
panic("negative size")
|
|
|
|
}
|
2011-09-17 23:59:04 +00:00
|
|
|
k := br.String()
|
2013-01-02 04:23:44 +00:00
|
|
|
if c.m[k] == size {
|
|
|
|
// dup
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.m[k] = size
|
2011-09-17 23:59:04 +00:00
|
|
|
|
2012-04-23 00:18:19 +00:00
|
|
|
if c.af == nil {
|
|
|
|
var err error
|
|
|
|
c.af, err = os.OpenFile(c.filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("opening have-cache for append: %v", err)
|
|
|
|
return
|
2011-09-17 23:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-31 21:53:44 +00:00
|
|
|
// TODO: flocking. see leveldb-go.
|
2012-04-23 00:18:19 +00:00
|
|
|
c.af.Seek(0, os.SEEK_END)
|
2013-01-02 04:23:44 +00:00
|
|
|
c.af.Write([]byte(fmt.Sprintf("%s %d\n", k, size)))
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|