pkg/fs: DRY SetBucketMetadata

It had a lot of code that was the same as GetBucketMetadata, so instead
call GBM from SBM so as to reduce doing the same thing in two different
spots. Theoretically this will induce a small overhead as now at least
two calls of denormalizeBucket are made, although this shouldn't be
noticeable.
master
Brendan Ashworth 9 years ago
parent 294ea814bf
commit 0a0451a0fb
  1. 34
      pkg/fs/fs-bucket.go

@ -230,45 +230,27 @@ func (fs Filesystem) GetBucketMetadata(bucket string) (BucketMetadata, *probe.Er
// SetBucketMetadata - set bucket metadata. // SetBucketMetadata - set bucket metadata.
func (fs Filesystem) SetBucketMetadata(bucket string, metadata map[string]string) *probe.Error { func (fs Filesystem) SetBucketMetadata(bucket string, metadata map[string]string) *probe.Error {
// Input validation. bucketMetadata, err := fs.GetBucketMetadata(bucket)
if !IsValidBucketName(bucket) { if err != nil {
return probe.NewError(BucketNameInvalid{Bucket: bucket}) return err
} }
// Save the acl. // Save the acl.
acl := metadata["acl"] acl := metadata["acl"]
if !IsValidBucketACL(acl) { if !IsValidBucketACL(acl) {
return probe.NewError(InvalidACL{ACL: acl}) return probe.NewError(InvalidACL{ACL: acl})
} } else if acl == "" {
if acl == "" {
acl = "private" acl = "private"
} }
bucket = fs.denormalizeBucket(bucket)
bucketDir := filepath.Join(fs.path, bucket)
fi, e := os.Stat(bucketDir)
if e != nil {
// Check if bucket exists.
if os.IsNotExist(e) {
return probe.NewError(BucketNotFound{Bucket: bucket})
}
return probe.NewError(e)
}
// Critical region handle read lock.
fs.rwLock.RLock()
bucketMetadata, ok := fs.buckets.Metadata[bucket]
fs.rwLock.RUnlock()
if !ok {
bucketMetadata = &BucketMetadata{}
bucketMetadata.Name = fi.Name()
bucketMetadata.Created = fi.ModTime()
}
bucketMetadata.ACL = BucketACL(acl) bucketMetadata.ACL = BucketACL(acl)
bucket = fs.denormalizeBucket(bucket)
// Critical region handle write lock. // Critical region handle write lock.
fs.rwLock.Lock() fs.rwLock.Lock()
defer fs.rwLock.Unlock() defer fs.rwLock.Unlock()
fs.buckets.Metadata[bucket] = bucketMetadata fs.buckets.Metadata[bucket] = &bucketMetadata
if err := saveBucketsMetadata(*fs.buckets); err != nil { if err := saveBucketsMetadata(*fs.buckets); err != nil {
return err.Trace(bucket) return err.Trace(bucket)
} }

Loading…
Cancel
Save