|
|
@ -32,32 +32,30 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
type storage struct { |
|
|
|
type storage struct { |
|
|
|
root string |
|
|
|
root string |
|
|
|
writeLock sync.Mutex |
|
|
|
lock *sync.Mutex |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type SerializedMetadata struct { |
|
|
|
type SerializedMetadata struct { |
|
|
|
ContentType string |
|
|
|
ContentType string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type MkdirFailedError struct{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (self MkdirFailedError) Error() string { |
|
|
|
|
|
|
|
return "Mkdir Failed" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func Start(root string) (chan<- string, <-chan error, *storage) { |
|
|
|
func Start(root string) (chan<- string, <-chan error, *storage) { |
|
|
|
ctrlChannel := make(chan string) |
|
|
|
ctrlChannel := make(chan string) |
|
|
|
errorChannel := make(chan error) |
|
|
|
errorChannel := make(chan error) |
|
|
|
go start(ctrlChannel, errorChannel) |
|
|
|
s := storage{} |
|
|
|
return ctrlChannel, errorChannel, &storage{root: root} |
|
|
|
s.root = root |
|
|
|
|
|
|
|
s.lock = new(sync.Mutex) |
|
|
|
|
|
|
|
go start(ctrlChannel, errorChannel, &s) |
|
|
|
|
|
|
|
return ctrlChannel, errorChannel, &s |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func start(ctrlChannel <-chan string, errorChannel chan<- error) { |
|
|
|
func start(ctrlChannel <-chan string, errorChannel chan<- error, s *storage) { |
|
|
|
|
|
|
|
err := os.MkdirAll(s.root, 0700) |
|
|
|
|
|
|
|
errorChannel <- err |
|
|
|
close(errorChannel) |
|
|
|
close(errorChannel) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Bucket Operations
|
|
|
|
// Bucket Operations
|
|
|
|
|
|
|
|
|
|
|
|
func (storage *storage) ListBuckets() ([]mstorage.BucketMetadata, error) { |
|
|
|
func (storage *storage) ListBuckets() ([]mstorage.BucketMetadata, error) { |
|
|
|
files, err := ioutil.ReadDir(storage.root) |
|
|
|
files, err := ioutil.ReadDir(storage.root) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -83,8 +81,8 @@ func (storage *storage) ListBuckets() ([]mstorage.BucketMetadata, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (storage *storage) StoreBucket(bucket string) error { |
|
|
|
func (storage *storage) StoreBucket(bucket string) error { |
|
|
|
storage.writeLock.Lock() |
|
|
|
storage.lock.Lock() |
|
|
|
defer storage.writeLock.Unlock() |
|
|
|
defer storage.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
// verify bucket path legal
|
|
|
|
// verify bucket path legal
|
|
|
|
if mstorage.IsValidBucket(bucket) == false { |
|
|
|
if mstorage.IsValidBucket(bucket) == false { |
|
|
@ -110,8 +108,8 @@ func (storage *storage) StoreBucket(bucket string) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (storage *storage) GetBucketPolicy(bucket string) (interface{}, error) { |
|
|
|
func (storage *storage) GetBucketPolicy(bucket string) (interface{}, error) { |
|
|
|
storage.writeLock.Lock() |
|
|
|
storage.lock.Lock() |
|
|
|
defer storage.writeLock.Unlock() |
|
|
|
defer storage.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
var p policy.BucketPolicy |
|
|
|
var p policy.BucketPolicy |
|
|
|
// verify bucket path legal
|
|
|
|
// verify bucket path legal
|
|
|
@ -153,8 +151,8 @@ func (storage *storage) GetBucketPolicy(bucket string) (interface{}, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (storage *storage) StoreBucketPolicy(bucket string, policy interface{}) error { |
|
|
|
func (storage *storage) StoreBucketPolicy(bucket string, policy interface{}) error { |
|
|
|
storage.writeLock.Lock() |
|
|
|
storage.lock.Lock() |
|
|
|
defer storage.writeLock.Unlock() |
|
|
|
defer storage.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
// verify bucket path legal
|
|
|
|
// verify bucket path legal
|
|
|
|
if mstorage.IsValidBucket(bucket) == false { |
|
|
|
if mstorage.IsValidBucket(bucket) == false { |
|
|
@ -332,8 +330,8 @@ func (storage *storage) ListObjects(bucket, prefix string, count int) ([]mstorag |
|
|
|
|
|
|
|
|
|
|
|
func (storage *storage) StoreObject(bucket, key, contentType string, data io.Reader) error { |
|
|
|
func (storage *storage) StoreObject(bucket, key, contentType string, data io.Reader) error { |
|
|
|
// TODO Commits should stage then move instead of writing directly
|
|
|
|
// TODO Commits should stage then move instead of writing directly
|
|
|
|
storage.writeLock.Lock() |
|
|
|
storage.lock.Lock() |
|
|
|
defer storage.writeLock.Unlock() |
|
|
|
defer storage.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
// check bucket name valid
|
|
|
|
// check bucket name valid
|
|
|
|
if mstorage.IsValidBucket(bucket) == false { |
|
|
|
if mstorage.IsValidBucket(bucket) == false { |
|
|
|