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.
|
|
|
|
*/
|
|
|
|
|
2010-07-11 04:18:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2010-12-03 15:34:24 +00:00
|
|
|
"camli/blobref"
|
2010-07-11 04:18:16 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2011-02-03 06:42:31 +00:00
|
|
|
"log"
|
2010-12-14 02:20:31 +00:00
|
|
|
"os"
|
2010-07-11 04:18:16 +00:00
|
|
|
)
|
|
|
|
|
2010-12-14 02:20:31 +00:00
|
|
|
type diskStorage struct {
|
|
|
|
Root string
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func (ds *diskStorage) Fetch(blob *blobref.BlobRef) (blobref.ReadSeekCloser, int64, os.Error) {
|
2010-12-14 02:20:31 +00:00
|
|
|
fileName := BlobFileName(blob)
|
|
|
|
stat, err := os.Stat(fileName)
|
|
|
|
if err == os.ENOENT {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
file, err := os.Open(fileName, os.O_RDONLY, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
return file, stat.Size, nil
|
|
|
|
}
|
|
|
|
|
2011-02-03 06:42:31 +00:00
|
|
|
func (ds *diskStorage) Remove(partition string, blobs []*blobref.BlobRef) os.Error {
|
|
|
|
for _, blob := range blobs {
|
|
|
|
fileName := PartitionBlobFileName(partition, blob)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2010-12-14 02:20:31 +00:00
|
|
|
func newDiskStorage(root string) *diskStorage {
|
|
|
|
return &diskStorage{Root: root}
|
|
|
|
}
|
|
|
|
|
2010-07-11 04:18:16 +00:00
|
|
|
var kGetPutPattern *regexp.Regexp = regexp.MustCompile(`^/camli/([a-z0-9]+)-([a-f0-9]+)$`)
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func BlobFileBaseName(b *blobref.BlobRef) string {
|
2010-12-03 15:34:24 +00:00
|
|
|
return fmt.Sprintf("%s-%s.dat", b.HashName(), b.Digest())
|
2010-07-11 04:18:16 +00:00
|
|
|
}
|
|
|
|
|
2011-02-02 06:48:12 +00:00
|
|
|
func blobPartitionDirName(partitionDirSlash string, b *blobref.BlobRef) string {
|
2010-12-03 15:34:24 +00:00
|
|
|
d := b.Digest()
|
2011-01-31 18:19:37 +00:00
|
|
|
if len(d) < 6 {
|
|
|
|
d = d + "______"
|
|
|
|
}
|
2011-02-02 06:48:12 +00:00
|
|
|
return fmt.Sprintf("%s/%s%s/%s/%s",
|
|
|
|
*flagStorageRoot, partitionDirSlash,
|
|
|
|
b.HashName(), d[0:3], d[3:6])
|
|
|
|
}
|
|
|
|
|
|
|
|
func BlobDirectoryName(b *blobref.BlobRef) string {
|
|
|
|
return blobPartitionDirName("", b)
|
2010-07-11 04:18:16 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func BlobFileName(b *blobref.BlobRef) string {
|
2010-12-03 15:34:24 +00:00
|
|
|
return fmt.Sprintf("%s/%s-%s.dat", BlobDirectoryName(b), b.HashName(), b.Digest())
|
2010-07-11 04:18:16 +00:00
|
|
|
}
|
|
|
|
|
2011-02-02 06:48:12 +00:00
|
|
|
func BlobPartitionDirectoryName(partition string, b *blobref.BlobRef) string {
|
|
|
|
return blobPartitionDirName("partition/" + partition + "/", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PartitionBlobFileName(partition string, b *blobref.BlobRef) string {
|
|
|
|
return fmt.Sprintf("%s/%s-%s.dat", BlobPartitionDirectoryName(partition, b), b.HashName(), b.Digest())
|
|
|
|
}
|
|
|
|
|
2010-12-17 17:59:03 +00:00
|
|
|
func BlobFromUrlPath(path string) *blobref.BlobRef {
|
2010-12-03 15:34:24 +00:00
|
|
|
return blobref.FromPattern(kGetPutPattern, path)
|
2010-07-11 04:18:16 +00:00
|
|
|
}
|