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-11 19:49:35 -05:00
|
|
|
type fileSystemStorage struct {
|
2014-11-14 20:22:50 -05:00
|
|
|
RootDir string
|
|
|
|
}
|
|
|
|
|
2014-12-11 19:49:35 -05:00
|
|
|
func NewStorage(rootDir string) (storage.ObjectStorage, error) {
|
|
|
|
newStorage := fileSystemStorage{
|
|
|
|
RootDir: rootDir,
|
|
|
|
}
|
|
|
|
return &newStorage, nil
|
|
|
|
}
|
|
|
|
|
2014-12-12 05:50:47 -05:00
|
|
|
func (fsStorage *fileSystemStorage) List() ([]storage.ObjectDescription, error) {
|
|
|
|
fileInfos, err := ioutil.ReadDir(fsStorage.RootDir)
|
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{
|
2014-12-12 05:50:47 -05:00
|
|
|
Name: fi.Name(),
|
|
|
|
Md5sum: "",
|
|
|
|
Protectionlevel: "",
|
2014-12-10 21:54:04 -05:00
|
|
|
}
|
|
|
|
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-11 19:49:35 -05:00
|
|
|
func (storage *fileSystemStorage) Get(objectPath string) (io.Reader, error) {
|
2014-12-10 23:40:53 -05:00
|
|
|
return os.Open(path.Join(storage.RootDir, objectPath))
|
2014-11-14 20:22:50 -05:00
|
|
|
}
|
|
|
|
|
2014-12-11 19:49:35 -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
|
|
|
}
|