Make get/put more robust

- init folder if not present already in 'get()' call
  - put.go was referencing wrong args - fix it
  - if no OBJECTNAME is specified during 'get' - grab a
    list of files under "~/.minio"
  - Provide new API GetList() for ObjectStorage struct
This commit is contained in:
Harshavardhana 2014-12-09 03:32:31 -08:00
parent 3fe2c2af01
commit f6a5b61b00
7 changed files with 92 additions and 28 deletions

View File

@ -32,7 +32,7 @@ build-split: build-strbyteconv
build-strbyteconv:
@godep go test -race -coverprofile=cover.out github.com/minio-io/minio/pkgs/strbyteconv
cover: build-erasure build-signify build-split build-crc32c
cover: build-erasure build-signify build-split build-crc32c build-cpu build-sha1
@godep go test -race -coverprofile=cover.out github.com/minio-io/minio/pkgs/storage
@godep go test -race -coverprofile=cover.out github.com/minio-io/minio/pkgs/gateway

View File

@ -10,6 +10,18 @@ import (
"github.com/minio-io/minio/pkgs/storage/fsstorage"
)
func fsGetList(config inputConfig) (io.Reader, error) {
var objectStorage storage.ObjectStorage
rootDir := path.Join(config.rootDir, config.storageDriver)
objectStorage = fsstorage.FileSystemStorage{RootDir: rootDir}
objectlist, err := objectStorage.GetList()
if err != nil {
return nil, err
}
objectListBuffer := bytes.NewBuffer(objectlist)
return objectListBuffer, nil
}
func fsGet(config inputConfig, objectPath string) (io.Reader, error) {
var objectStorage storage.ObjectStorage
rootDir := path.Join(config.rootDir, config.storageDriver)

View File

@ -14,12 +14,20 @@ func get(c *cli.Context) {
log.Fatal(err)
}
var objectReader io.Reader
objectName := c.Args().Get(0)
switch config.storageDriver {
case "fs":
{
if objectReader, err = fsGet(config, c.Args().Get(0)); err != nil {
if len(objectName) == 0 {
if objectReader, err = fsGetList(config); err != nil {
log.Fatal(err)
}
} else {
if objectReader, err = fsGet(config, objectName); err != nil {
log.Fatal(err)
}
}
}
default:
{
@ -28,25 +36,3 @@ func get(c *cli.Context) {
}
io.Copy(os.Stdout, objectReader)
}
func put(c *cli.Context) {
config, err := parseInput(c)
if err != nil {
log.Fatal(err)
}
filePath := c.Args().Get(1)
inputFile, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
switch config.storageDriver {
case "fs":
{
fsPut(config, c.Args().Get(0), inputFile)
}
default:
{
log.Fatal("Unknown driver")
}
}
}

View File

@ -176,5 +176,23 @@ func getMinioDir() string {
log.Fatal(err)
}
homePath := user.HomeDir
return path.Join(homePath, ".minio")
minioPath := path.Join(homePath, ".minio")
err = _initMinioDir(minioPath)
if err != nil {
log.Fatal(err)
}
return minioPath
}
func _initMinioDir(dirPath string) error {
_, err := os.Lstat(dirPath)
if err != nil {
log.Printf("%s not found, creating a new-one for the first time",
dirPath)
err = os.Mkdir(dirPath, 0700)
if err != nil {
return err
}
}
return nil
}

View File

@ -1 +1,33 @@
package main
import (
"log"
"os"
"github.com/codegangsta/cli"
)
func put(c *cli.Context) {
config, err := parseInput(c)
if err != nil {
log.Fatal(err)
}
filePath := c.Args().Get(0)
if len(filePath) == 0 {
log.Fatal("Please specify a valid object name \n # erasure-demo put <OBJECTNAME>")
}
inputFile, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
switch config.storageDriver {
case "fs":
{
fsPut(config, c.Args().Get(0), inputFile)
}
default:
{
log.Fatal("Unknown driver")
}
}
}

View File

@ -11,12 +11,27 @@ type FileSystemStorage struct {
RootDir string
}
func (storage FileSystemStorage) GetList() ([]byte, error) {
fileInfos, err := ioutil.ReadDir(storage.RootDir)
if err != nil {
return nil, err
}
var list []byte
for _, fi := range fileInfos {
list = append(list, "{"+fi.Name()+"}\n"...)
}
return list, nil
}
func (storage FileSystemStorage) Get(objectPath string) ([]byte, error) {
return ioutil.ReadFile(path.Join(storage.RootDir, objectPath))
}
func (storage FileSystemStorage) Put(objectPath string, object []byte) error {
os.MkdirAll(filepath.Dir(path.Join(storage.RootDir, objectPath)), 0700)
err := os.MkdirAll(filepath.Dir(path.Join(storage.RootDir, objectPath)), 0700)
if err != nil {
return err
}
return ioutil.WriteFile(path.Join(storage.RootDir, objectPath), object, 0600)
}

View File

@ -1,6 +1,7 @@
package storage
type ObjectStorage interface {
GetList() ([]byte, error)
Get(path string) ([]byte, error)
Put(path string, object []byte) error
}