2014-12-08 17:25:57 -05:00
|
|
|
package fsstorage
|
2014-11-14 20:22:50 -05:00
|
|
|
|
|
|
|
import (
|
2014-12-10 23:40:53 -05:00
|
|
|
"io"
|
2014-11-14 20:22:50 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2014-12-10 21:54:04 -05:00
|
|
|
|
|
|
|
"github.com/minio-io/minio/pkgs/storage"
|
2014-11-14 20:22:50 -05:00
|
|
|
)
|
|
|
|
|
2014-12-08 17:25:57 -05:00
|
|
|
type FileSystemStorage struct {
|
2014-11-14 20:22:50 -05:00
|
|
|
RootDir string
|
|
|
|
}
|
|
|
|
|
2014-12-10 21:54:04 -05:00
|
|
|
func (fsStorage FileSystemStorage) List(listPath string) ([]storage.ObjectDescription, error) {
|
|
|
|
fileInfos, err := ioutil.ReadDir(path.Join(fsStorage.RootDir, listPath))
|
2014-12-09 06:32:31 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-10 21:54:04 -05:00
|
|
|
var descriptions []storage.ObjectDescription
|
|
|
|
|
2014-12-09 06:32:31 -05:00
|
|
|
for _, fi := range fileInfos {
|
2014-12-10 21:54:04 -05:00
|
|
|
description := storage.ObjectDescription{
|
|
|
|
Path: fi.Name(),
|
|
|
|
IsDir: fi.IsDir(),
|
|
|
|
Hash: "", // TODO
|
|
|
|
}
|
|
|
|
descriptions = append(descriptions, description)
|
2014-12-09 06:32:31 -05:00
|
|
|
}
|
2014-12-10 21:54:04 -05:00
|
|
|
return descriptions, nil
|
2014-12-09 06:32:31 -05:00
|
|
|
}
|
|
|
|
|
2014-12-10 23:40:53 -05:00
|
|
|
func (storage FileSystemStorage) Get(objectPath string) (io.Reader, error) {
|
|
|
|
return os.Open(path.Join(storage.RootDir, objectPath))
|
2014-11-14 20:22:50 -05:00
|
|
|
}
|
|
|
|
|
2014-12-10 23:40:53 -05:00
|
|
|
func (storage FileSystemStorage) Put(objectPath string, object io.Reader) error {
|
2014-12-09 06:32:31 -05:00
|
|
|
err := os.MkdirAll(filepath.Dir(path.Join(storage.RootDir, objectPath)), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-10 23:40:53 -05:00
|
|
|
objectBytes, err := ioutil.ReadAll(object)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ioutil.WriteFile(path.Join(storage.RootDir, objectPath), objectBytes, 0600)
|
2014-11-14 20:22:50 -05:00
|
|
|
}
|