2011-02-28 00:18:17 +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 localdisk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-06-09 09:36:26 +00:00
|
|
|
"log"
|
2011-02-28 00:18:17 +00:00
|
|
|
"os"
|
2012-02-22 12:50:58 +00:00
|
|
|
"path/filepath"
|
2011-02-28 00:18:17 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2011-05-09 16:11:18 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
2013-12-02 21:20:51 +00:00
|
|
|
"camlistore.org/pkg/context"
|
2011-02-28 00:18:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type readBlobRequest struct {
|
2013-12-02 21:20:51 +00:00
|
|
|
done <-chan struct{}
|
2013-08-04 02:54:30 +00:00
|
|
|
ch chan<- blob.SizedRef
|
2011-02-28 00:18:17 +00:00
|
|
|
after string
|
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
|
|
|
remain *int // limit countdown
|
2011-02-28 00:18:17 +00:00
|
|
|
dirRoot string
|
|
|
|
|
|
|
|
// Not used on initial request, only on recursion
|
|
|
|
blobPrefix, pathInto string
|
|
|
|
}
|
|
|
|
|
|
|
|
type enumerateError struct {
|
|
|
|
msg string
|
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
|
|
|
err error
|
2011-02-28 00:18:17 +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 (ee *enumerateError) Error() string {
|
2011-02-28 00:18:17 +00:00
|
|
|
return fmt.Sprintf("Enumerate error: %s: %v", ee.msg, ee.err)
|
|
|
|
}
|
|
|
|
|
2013-09-08 21:55:41 +00:00
|
|
|
func (ds *DiskStorage) readBlobs(opts readBlobRequest) error {
|
2012-04-21 15:29:32 +00:00
|
|
|
dirFullPath := filepath.Join(opts.dirRoot, opts.pathInto)
|
2011-04-07 17:58:29 +00:00
|
|
|
dir, err := os.Open(dirFullPath)
|
2011-02-28 00:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return &enumerateError{"localdisk: opening directory " + dirFullPath, err}
|
|
|
|
}
|
2013-08-20 23:29:24 +00:00
|
|
|
names, err := dir.Readdirnames(-1)
|
2013-11-30 04:53:46 +00:00
|
|
|
dir.Close()
|
2013-08-20 23:29:24 +00:00
|
|
|
if err == nil && len(names) == 0 {
|
2012-02-20 14:06:49 +00:00
|
|
|
// remove empty blob dir if we are in a queue but not the queue root itself
|
|
|
|
if strings.Contains(dirFullPath, "queue-") &&
|
|
|
|
!strings.Contains(filepath.Base(dirFullPath), "queue-") {
|
2013-09-08 21:55:41 +00:00
|
|
|
go ds.tryRemoveDir(dirFullPath)
|
2012-02-20 14:06:49 +00:00
|
|
|
}
|
2011-05-21 15:51:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
2011-02-28 00:18:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return &enumerateError{"localdisk: readdirnames of " + dirFullPath, err}
|
|
|
|
}
|
2011-07-08 21:34:23 +00:00
|
|
|
sort.Strings(names)
|
2013-11-30 04:53:46 +00:00
|
|
|
stat := make(map[string]chan interface{}) // gets sent error or os.FileInfo
|
|
|
|
for _, name := range names {
|
|
|
|
if skipDir(name) || isShardDir(name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ch := make(chan interface{}, 1) // 1 in case it's not read
|
|
|
|
name := name
|
|
|
|
stat[name] = ch
|
|
|
|
go func() {
|
|
|
|
fi, err := os.Stat(filepath.Join(dirFullPath, name))
|
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
} else {
|
|
|
|
ch <- fi
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2011-02-28 00:18:17 +00:00
|
|
|
for _, name := range names {
|
|
|
|
if *opts.remain == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2013-11-30 04:53:46 +00:00
|
|
|
if skipDir(name) {
|
2011-03-13 05:11:03 +00:00
|
|
|
continue
|
|
|
|
}
|
2013-11-30 04:53:46 +00:00
|
|
|
var (
|
|
|
|
fi os.FileInfo
|
|
|
|
err error
|
|
|
|
didStat bool
|
|
|
|
)
|
|
|
|
stat := func() {
|
|
|
|
if didStat {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
didStat = true
|
|
|
|
fiv := <-stat[name]
|
|
|
|
var ok bool
|
|
|
|
if err, ok = fiv.(error); ok {
|
|
|
|
err = &enumerateError{"localdisk: stat of file " + filepath.Join(dirFullPath, name), err}
|
|
|
|
} else {
|
|
|
|
fi = fiv.(os.FileInfo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isDir := func() bool {
|
|
|
|
stat()
|
|
|
|
return fi != nil && fi.IsDir()
|
2011-02-28 00:18:17 +00:00
|
|
|
}
|
|
|
|
|
2013-11-30 04:53:46 +00:00
|
|
|
if isShardDir(name) || isDir() {
|
2011-02-28 00:18:17 +00:00
|
|
|
var newBlobPrefix string
|
|
|
|
if opts.blobPrefix == "" {
|
|
|
|
newBlobPrefix = name + "-"
|
|
|
|
} else {
|
|
|
|
newBlobPrefix = opts.blobPrefix + name
|
|
|
|
}
|
|
|
|
if len(opts.after) > 0 {
|
|
|
|
compareLen := len(newBlobPrefix)
|
|
|
|
if len(opts.after) < compareLen {
|
|
|
|
compareLen = len(opts.after)
|
|
|
|
}
|
2013-08-20 23:29:24 +00:00
|
|
|
if newBlobPrefix[:compareLen] < opts.after[:compareLen] {
|
2011-02-28 00:18:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ropts := opts
|
|
|
|
ropts.blobPrefix = newBlobPrefix
|
|
|
|
ropts.pathInto = opts.pathInto + "/" + name
|
2014-05-02 17:53:12 +00:00
|
|
|
if err := ds.readBlobs(ropts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2011-02-28 00:18:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-11-30 04:53:46 +00:00
|
|
|
stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 !fi.IsDir() && strings.HasSuffix(name, ".dat") {
|
2013-11-30 04:53:46 +00:00
|
|
|
blobName := strings.TrimSuffix(name, ".dat")
|
2011-02-28 00:18:17 +00:00
|
|
|
if blobName <= opts.after {
|
|
|
|
continue
|
|
|
|
}
|
2013-08-04 02:54:30 +00:00
|
|
|
if blobRef, ok := blob.Parse(blobName); ok {
|
2013-12-02 21:20:51 +00:00
|
|
|
select {
|
2014-01-28 20:46:52 +00:00
|
|
|
case opts.ch <- blob.SizedRef{Ref: blobRef, Size: uint32(fi.Size())}:
|
2013-12-02 21:20:51 +00:00
|
|
|
case <-opts.done:
|
|
|
|
return context.ErrCanceled
|
|
|
|
}
|
2011-02-28 00:18:17 +00:00
|
|
|
(*opts.remain)--
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-12-02 21:20:51 +00:00
|
|
|
func (ds *DiskStorage) EnumerateBlobs(ctx *context.Context, dest chan<- blob.SizedRef, after string, limit int) error {
|
2011-05-11 13:55:14 +00:00
|
|
|
defer close(dest)
|
2013-06-09 09:36:26 +00:00
|
|
|
if limit == 0 {
|
|
|
|
log.Printf("Warning: localdisk.EnumerateBlobs called with a limit of 0")
|
|
|
|
}
|
2011-05-11 13:55:14 +00:00
|
|
|
|
2011-02-28 00:18:17 +00:00
|
|
|
limitMutable := limit
|
2013-09-08 21:55:41 +00:00
|
|
|
return ds.readBlobs(readBlobRequest{
|
2013-12-02 21:20:51 +00:00
|
|
|
done: ctx.Done(),
|
2013-08-21 20:57:28 +00:00
|
|
|
ch: dest,
|
Get rid of QueueCreator and all its associated complexity.
Previous TODO entry was:
-- Get rid of QueueCreator entirely. Plan:
-- sync handler still has a source and dest (one pair) but
instead of calling CreateQueue on the source, it instead
has an index.Storage (configured via a RequiredObject
so it can be a kvfile, leveldb, mysql, postgres etc)
-- make all the index.Storage types be instantiable
from a jsonconfig Object, perhaps with constructors keyed
on a "type" field.
-- make sync handler support blobserver.Receiver (or StatReceiver)
like indexes, so it can receive blobs. but all it needs to
do to acknowledge the ReceiveBlob is write and flush to its
index.Storage. the syncing is async by default. (otherwise callers
could just use "replica" if they wanted sync replication).
But maybe for ease of configuration switching, we could also
support a sync mode. when it needs to replicate a blob,
it uses the source.
-- future option: sync mirror to an alternate path on ReceiveBlob
that can delete. e.g. you're uploading to s3 and google,
but don't want to upload to both at once, so you use the localdisk
as a buffer to spread out your upstream bandwidth.
-- end result: no more hardlinks or queue creator.
Change-Id: I6244fc4f3a655f08470ae3160502659399f468ed
2013-11-22 22:33:31 +00:00
|
|
|
dirRoot: ds.root,
|
2013-08-21 20:57:28 +00:00
|
|
|
after: after,
|
|
|
|
remain: &limitMutable,
|
|
|
|
})
|
2011-02-28 00:18:17 +00:00
|
|
|
}
|
2013-11-30 04:53:46 +00:00
|
|
|
|
|
|
|
func skipDir(name string) bool {
|
|
|
|
// The partition directory is old. (removed from codebase, but
|
|
|
|
// likely still on disk for some people)
|
|
|
|
// the "cache" directory is just a hack: it's used
|
|
|
|
// by the serverconfig/genconfig code, as a default
|
|
|
|
// location for most users to put their thumbnail
|
|
|
|
// cache. For now we just also skip it here.
|
|
|
|
return name == "partition" || name == "cache"
|
|
|
|
}
|
|
|
|
|
|
|
|
func isShardDir(name string) bool {
|
|
|
|
return len(name) == 2 && isHex(name[0]) && isHex(name[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func isHex(b byte) bool {
|
|
|
|
return ('0' <= b && b <= '9') || ('a' <= b && b <= 'f')
|
|
|
|
}
|