2015-03-22 18:14:06 -04:00
|
|
|
package donut
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"encoding/json"
|
2015-03-26 13:21:24 -04:00
|
|
|
"github.com/minio-io/iodine"
|
2015-03-24 22:58:03 -04:00
|
|
|
"io/ioutil"
|
2015-03-22 18:14:06 -04:00
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
type localDirectoryNode struct {
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
2015-03-23 23:06:15 -04:00
|
|
|
func (d localDirectoryNode) CreateBucket(bucket string) error {
|
|
|
|
objectPath := path.Join(d.root, bucket)
|
2015-03-26 18:55:06 -04:00
|
|
|
return iodine.New(os.MkdirAll(objectPath, 0700), map[string]string{"bucket": bucket})
|
2015-03-23 23:06:15 -04:00
|
|
|
}
|
|
|
|
|
2015-03-22 18:14:06 -04:00
|
|
|
func (d localDirectoryNode) GetBuckets() ([]string, error) {
|
2015-03-24 22:58:03 -04:00
|
|
|
files, err := ioutil.ReadDir(d.root)
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, nil)
|
2015-03-24 22:58:03 -04:00
|
|
|
}
|
|
|
|
var results []string
|
|
|
|
for _, file := range files {
|
|
|
|
if file.IsDir() {
|
|
|
|
results = append(results, file.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, nil
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) GetWriter(bucket, object string) (Writer, error) {
|
2015-03-26 13:21:24 -04:00
|
|
|
errParams := map[string]string{"bucket": bucket, "object": object}
|
2015-03-22 18:14:06 -04:00
|
|
|
objectPath := path.Join(d.root, bucket, object)
|
|
|
|
err := os.MkdirAll(objectPath, 0700)
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
2015-03-26 13:21:24 -04:00
|
|
|
writer, err := newDonutObjectWriter(objectPath)
|
2015-03-26 18:55:06 -04:00
|
|
|
return writer, iodine.New(err, errParams)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) GetReader(bucket, object string) (io.ReadCloser, error) {
|
2015-03-26 13:21:24 -04:00
|
|
|
reader, err := os.Open(path.Join(d.root, bucket, object, "data"))
|
2015-03-26 18:55:06 -04:00
|
|
|
return reader, iodine.New(err, map[string]string{"bucket": bucket, "object": object})
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) GetMetadata(bucket, object string) (map[string]string, error) {
|
2015-03-26 13:21:24 -04:00
|
|
|
m, err := d.getMetadata(bucket, object, "metadata.json")
|
2015-03-26 18:55:06 -04:00
|
|
|
return m, iodine.New(err, map[string]string{"bucket": bucket, "object": object})
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
func (d localDirectoryNode) GetDonutMetadata(bucket, object string) (map[string]string, error) {
|
2015-03-26 13:21:24 -04:00
|
|
|
m, err := d.getMetadata(bucket, object, "donutMetadata.json")
|
2015-03-26 18:55:06 -04:00
|
|
|
return m, iodine.New(err, map[string]string{"bucket": bucket, "object": object})
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) getMetadata(bucket, object, fileName string) (map[string]string, error) {
|
2015-03-26 13:21:24 -04:00
|
|
|
errParams := map[string]string{"bucket": bucket, "object": object, "file": fileName}
|
2015-03-22 18:14:06 -04:00
|
|
|
file, err := os.Open(path.Join(d.root, bucket, object, fileName))
|
|
|
|
defer file.Close()
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
metadata := make(map[string]string)
|
|
|
|
decoder := json.NewDecoder(file)
|
|
|
|
if err := decoder.Decode(&metadata); err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d localDirectoryNode) ListObjects(bucketName string) ([]string, error) {
|
2015-03-26 13:21:24 -04:00
|
|
|
errParams := map[string]string{"bucket": bucketName}
|
2015-03-22 18:14:06 -04:00
|
|
|
prefix := path.Join(d.root, bucketName)
|
|
|
|
var objects []string
|
|
|
|
if err := filepath.Walk(prefix, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return iodine.New(err, errParams)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
if !info.IsDir() && strings.HasSuffix(path, "data") {
|
|
|
|
object := strings.TrimPrefix(path, prefix+"/")
|
|
|
|
object = strings.TrimSuffix(object, "/data")
|
|
|
|
objects = append(objects, object)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2015-03-26 18:55:06 -04:00
|
|
|
return nil, iodine.New(err, errParams)
|
2015-03-22 18:14:06 -04:00
|
|
|
}
|
|
|
|
sort.Strings(objects)
|
|
|
|
return objects, nil
|
|
|
|
}
|