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-02-03 06:42:31 +00:00
|
|
|
"log"
|
2011-04-09 06:20:24 +00:00
|
|
|
"io"
|
2010-12-14 02:20:31 +00:00
|
|
|
"os"
|
2011-04-03 15:07:40 +00:00
|
|
|
|
|
|
|
"camli/blobref"
|
|
|
|
"camli/blobserver"
|
2010-07-11 04:18:16 +00:00
|
|
|
)
|
|
|
|
|
2010-12-14 02:20:31 +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-04-09 06:20:24 +00:00
|
|
|
// TODO: lazy hack because I didn't want to rename diskStorage everywhere
|
|
|
|
// during an experiment. should just rename it now.
|
|
|
|
type DiskStorage struct {
|
|
|
|
*diskStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(root string) (storage *DiskStorage, err os.Error) {
|
2011-02-04 22:31:23 +00:00
|
|
|
// Local disk.
|
|
|
|
fi, staterr := os.Stat(root)
|
|
|
|
if staterr != nil || !fi.IsDirectory() {
|
|
|
|
err = os.NewError(fmt.Sprintf("Storage root %q doesn't exist or is not a directory.", root))
|
|
|
|
return
|
|
|
|
}
|
2011-04-09 06:20:24 +00:00
|
|
|
storage = &DiskStorage{&diskStorage{
|
2011-03-28 05:06:29 +00:00
|
|
|
&blobserver.SimpleBlobHubPartitionMap{},
|
|
|
|
root,
|
2011-04-09 06:20:24 +00:00
|
|
|
}}
|
2011-02-04 22:31:23 +00:00
|
|
|
return
|
2010-12-14 02:20:31 +00:00
|
|
|
}
|
|
|
|
|
2011-04-03 15:07:40 +00:00
|
|
|
func newFromConfig(config blobserver.JSONConfig) (storage blobserver.Storage, err os.Error) {
|
|
|
|
sto := &diskStorage{
|
|
|
|
SimpleBlobHubPartitionMap: &blobserver.SimpleBlobHubPartitionMap{},
|
|
|
|
root: config.RequiredString("path"),
|
|
|
|
}
|
|
|
|
if err := config.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fi, err := os.Stat(sto.root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to stat directory %q: %v", sto.root, err)
|
|
|
|
}
|
|
|
|
if !fi.IsDirectory() {
|
|
|
|
return nil, fmt.Errorf("Path %q isn't a directory", sto.root)
|
|
|
|
}
|
|
|
|
return sto, nil
|
2011-04-02 01:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
blobserver.RegisterStorageConstructor("filesystem", blobserver.StorageConstructor(newFromConfig))
|
|
|
|
}
|
|
|
|
|
2011-04-09 06:20:24 +00:00
|
|
|
func (ds *diskStorage) FetchStreaming(blob *blobref.BlobRef) (io.ReadCloser, int64, os.Error) {
|
|
|
|
return ds.Fetch(blob)
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func (ds *diskStorage) Fetch(blob *blobref.BlobRef) (blobref.ReadSeekCloser, int64, os.Error) {
|
2011-03-07 04:11:36 +00:00
|
|
|
fileName := ds.blobPath(nil, blob)
|
2010-12-14 02:20:31 +00:00
|
|
|
stat, err := os.Stat(fileName)
|
2011-02-04 22:31:23 +00:00
|
|
|
if errorIsNoEnt(err) {
|
2011-03-13 02:28:05 +00:00
|
|
|
return nil, 0, os.ENOENT
|
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 {
|
2011-03-05 08:03:53 +00:00
|
|
|
if errorIsNoEnt(err) {
|
|
|
|
err = os.ENOENT
|
|
|
|
}
|
2010-12-14 02:20:31 +00:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return file, stat.Size, nil
|
|
|
|
}
|
|
|
|
|
2011-02-04 01:08:04 +00:00
|
|
|
func (ds *diskStorage) Remove(partition blobserver.Partition, blobs []*blobref.BlobRef) os.Error {
|
2011-02-03 06:42:31 +00:00
|
|
|
for _, blob := range blobs {
|
2011-03-07 04:11:36 +00:00
|
|
|
fileName := ds.blobPath(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
|
|
|
|
case errorIsNoEnt(err):
|
2011-02-03 06:42:31 +00:00
|
|
|
log.Printf("Deleting already-deleted file; harmless.")
|
|
|
|
continue
|
2011-02-03 16:28:47 +00:00
|
|
|
default:
|
|
|
|
return err
|
2011-02-03 06:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|