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.
|
|
|
|
*/
|
|
|
|
|
2011-02-04 22:31:23 +00:00
|
|
|
package localdisk
|
2010-07-11 04:18:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2011-04-09 06:20:24 +00:00
|
|
|
"io"
|
2010-12-14 02:20:31 +00:00
|
|
|
"os"
|
2011-05-09 18:08:14 +00:00
|
|
|
"regexp"
|
2011-04-03 15:07:40 +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
|
|
|
"camlistore.org/pkg/blobref"
|
|
|
|
"camlistore.org/pkg/blobserver"
|
|
|
|
"camlistore.org/pkg/jsonconfig"
|
2010-07-11 04:18:16 +00:00
|
|
|
)
|
|
|
|
|
2011-05-09 16:11:18 +00:00
|
|
|
type DiskStorage struct {
|
2011-03-28 05:06:29 +00:00
|
|
|
*blobserver.SimpleBlobHubPartitionMap
|
2011-02-04 22:31:23 +00:00
|
|
|
root string
|
|
|
|
|
2011-05-09 18:08:14 +00:00
|
|
|
// the sub-partition (queue) to write to / read from, or "" for none.
|
|
|
|
partition string
|
2011-05-09 16:11:18 +00:00
|
|
|
|
2011-05-09 18:08:14 +00:00
|
|
|
// queue partitions to mirror new blobs into (when partition
|
|
|
|
// above is the empty string)
|
|
|
|
mirrorPartitions []*DiskStorage
|
2011-04-09 06:20:24 +00:00
|
|
|
}
|
|
|
|
|
2012-11-07 19:55:37 +00:00
|
|
|
// New returns a new local disk storage implementation, rooted at the provided
|
|
|
|
// directory, which must already exist.
|
|
|
|
func New(root string) (*DiskStorage, error) {
|
2011-02-04 22:31:23 +00:00
|
|
|
// Local disk.
|
2012-11-07 19:55:37 +00:00
|
|
|
fi, err := os.Stat(root)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("Storage root %q doesn't exist", root)
|
2011-02-04 22:31:23 +00:00
|
|
|
}
|
2012-11-07 19:55:37 +00:00
|
|
|
if err != nil {
|
2013-01-12 00:48:44 +00:00
|
|
|
return nil, fmt.Errorf("Failed to stat directory %q: %v", root, err)
|
2012-11-07 19:55:37 +00:00
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil, fmt.Errorf("Storage root %q exists but is not a directory.", root)
|
|
|
|
}
|
|
|
|
ds := &DiskStorage{
|
2011-05-09 16:11:18 +00:00
|
|
|
SimpleBlobHubPartitionMap: &blobserver.SimpleBlobHubPartitionMap{},
|
|
|
|
root: root,
|
|
|
|
}
|
2012-11-07 19:55:37 +00:00
|
|
|
if _, _, err := ds.StorageGeneration(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error initialization generation for %q: %v", root, err)
|
|
|
|
}
|
|
|
|
return ds, nil
|
2010-12-14 02:20:31 +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 newFromConfig(_ blobserver.Loader, config jsonconfig.Obj) (storage blobserver.Storage, err error) {
|
2013-01-12 00:48:44 +00:00
|
|
|
path := config.RequiredString("path")
|
2011-04-03 15:07:40 +00:00
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-01-12 00:48:44 +00:00
|
|
|
return New(path)
|
2011-04-02 01:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
blobserver.RegisterStorageConstructor("filesystem", blobserver.StorageConstructor(newFromConfig))
|
|
|
|
}
|
|
|
|
|
2011-05-09 18:08:14 +00:00
|
|
|
var validQueueName = regexp.MustCompile(`^[a-zA-Z0-9\-\_]+$`)
|
|
|
|
|
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 (ds *DiskStorage) CreateQueue(name string) (blobserver.Storage, error) {
|
2011-05-09 18:08:14 +00:00
|
|
|
if !validQueueName.MatchString(name) {
|
|
|
|
return nil, fmt.Errorf("invalid queue name %q", name)
|
|
|
|
}
|
|
|
|
if ds.partition != "" {
|
|
|
|
return nil, fmt.Errorf("can't create queue %q on existing queue %q",
|
|
|
|
name, ds.partition)
|
|
|
|
}
|
|
|
|
q := &DiskStorage{
|
|
|
|
SimpleBlobHubPartitionMap: &blobserver.SimpleBlobHubPartitionMap{},
|
|
|
|
root: ds.root,
|
2011-05-11 13:29:58 +00:00
|
|
|
partition: "queue-" + name,
|
2011-05-09 18:08:14 +00:00
|
|
|
}
|
2011-05-11 13:29:58 +00:00
|
|
|
baseDir := ds.PartitionRoot(q.partition)
|
|
|
|
if err := os.MkdirAll(baseDir, 0700); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create queue base dir: %v", err)
|
|
|
|
}
|
|
|
|
|
2011-05-09 18:08:14 +00:00
|
|
|
ds.mirrorPartitions = append(ds.mirrorPartitions, q)
|
|
|
|
return q, 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 (ds *DiskStorage) FetchStreaming(blob *blobref.BlobRef) (io.ReadCloser, int64, error) {
|
2011-04-09 06:20:24 +00:00
|
|
|
return ds.Fetch(blob)
|
|
|
|
}
|
|
|
|
|
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 (ds *DiskStorage) Fetch(blob *blobref.BlobRef) (blobref.ReadSeekCloser, int64, error) {
|
2011-05-09 18:08:14 +00:00
|
|
|
fileName := ds.blobPath("", blob)
|
2010-12-14 02:20:31 +00:00
|
|
|
stat, err := os.Stat(fileName)
|
2012-11-07 19:02:34 +00:00
|
|
|
if os.IsNotExist(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
|
|
|
return nil, 0, os.ErrNotExist
|
2010-12-14 02:20:31 +00:00
|
|
|
}
|
2011-04-07 17:58:29 +00:00
|
|
|
file, err := os.Open(fileName)
|
2010-12-14 02:20:31 +00:00
|
|
|
if err != nil {
|
2012-11-07 19:02:34 +00:00
|
|
|
if os.IsNotExist(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
|
|
|
err = os.ErrNotExist
|
2011-03-05 08:03:53 +00:00
|
|
|
}
|
2010-12-14 02:20:31 +00:00
|
|
|
return nil, 0, 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
|
|
|
return file, stat.Size(), nil
|
2010-12-14 02:20:31 +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 (ds *DiskStorage) RemoveBlobs(blobs []*blobref.BlobRef) error {
|
2011-02-03 06:42:31 +00:00
|
|
|
for _, blob := range blobs {
|
2011-05-09 16:11:18 +00:00
|
|
|
fileName := ds.blobPath(ds.partition, blob)
|
2011-02-03 06:42:31 +00:00
|
|
|
err := os.Remove(fileName)
|
2011-02-03 16:28:47 +00:00
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
continue
|
2012-11-07 19:02:34 +00:00
|
|
|
case os.IsNotExist(err):
|
2012-11-07 21:40:17 +00:00
|
|
|
// deleting already-deleted file; harmless.
|
2011-02-03 06:42:31 +00:00
|
|
|
continue
|
2011-02-03 16:28:47 +00:00
|
|
|
default:
|
|
|
|
return err
|
2011-02-03 06:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|