Merge pull request #114 from harshavardhana/pr_out_refactor_mc_cp_with_various_modes_of_operation

This commit is contained in:
Harshavardhana
2015-03-12 19:22:14 -07:00
5 changed files with 260 additions and 134 deletions
+191 -99
View File
@@ -23,11 +23,190 @@ import (
"github.com/cheggaaa/pb"
"github.com/codegangsta/cli"
"github.com/minio-io/mc/pkg/s3"
)
// TODO
// - <S3Path> <S3Path>
// - <S3Path> <S3Bucket>
// Different modes of cp operation
const (
first = iota // <Path> <S3Path> or <Path> <S3Bucket>
second // <S3Path> <Path> or <S3Path> .
third // <S3Path> <S3Path> or <S3Path> <S3Bucket>
fourth // <S3Bucket> <S3Bucket> - TODO
invalid
)
func getMode(options *cmdOptions) int {
switch true {
// <Path> <S3Path> or <Path> <S3Bucket>
case options.source.bucket == "" && options.destination.bucket != "":
return first
// <S3Path> <Path> or <S3Path> .
case options.source.bucket != "" && options.destination.bucket == "":
return second
// <S3Path> <S3Path> or <S3Path> <S3Bucket>
case options.source.bucket != "" && options.destination.bucket != "" && options.source.key != "":
return third
}
return invalid
}
// First mode <Path> <S3Path> or <Path> <S3Bucket>
func firstMode(s3c *s3.Client, options *cmdOptions) error {
if options.source.key == "" {
return fmt.Errorf("invalid")
}
st, err := os.Stat(options.source.key)
if os.IsNotExist(err) {
return err
}
if st.IsDir() {
fmt.Errorf("Is a directory")
}
size := st.Size()
source, err := os.Open(options.source.key)
defer source.Close()
if err != nil {
return err
}
// s3://<bucket> is specified without key
if options.destination.key == "" {
options.destination.key = options.source.key
}
err = s3c.Put(options.destination.bucket, options.destination.key, size, source)
if err != nil {
return err
}
msg := fmt.Sprintf("%s uploaded -- to bucket:(s3://%s/%s)", options.source.key,
options.destination.bucket, options.destination.key)
info(msg)
return nil
}
// Second mode <S3Path> <Path> or <S3Path> .
func secondMode(s3c *s3.Client, options *cmdOptions) error {
var objectReader io.ReadCloser
var objectSize, downloadedSize int64
var destination *os.File
var err error
var st os.FileInfo
// Send HEAD request to validate if file exists.
objectSize, _, err = s3c.Stat(options.source.bucket, options.source.key)
if err != nil {
return err
}
if options.destination.key == "." {
options.destination.key = options.source.key
}
var bar *pb.ProgressBar
if !options.quiet {
// get progress bar
bar = startBar(objectSize)
}
// Check if the object already exists
st, err = os.Stat(options.destination.key)
switch os.IsNotExist(err) {
case true:
// Create if it doesn't exist
destination, err = os.Create(options.destination.key)
defer destination.Close()
if err != nil {
return err
}
objectReader, _, err = s3c.Get(options.source.bucket, options.source.key)
if err != nil {
return err
}
case false:
downloadedSize = st.Size()
// Verify if file is already downloaded
if downloadedSize == objectSize {
return fmt.Errorf("%s object has been already downloaded", options.destination.key)
}
destination, err = os.OpenFile(options.destination.key, os.O_RDWR, 0600)
defer destination.Close()
if err != nil {
return err
}
_, err := destination.Seek(downloadedSize, os.SEEK_SET)
if err != nil {
return err
}
remainingSize := objectSize - downloadedSize
objectReader, objectSize, err = s3c.GetPartial(options.source.bucket,
options.source.key, downloadedSize, remainingSize)
if err != nil {
return err
}
if !options.quiet {
bar.Set(int(downloadedSize))
}
}
writer := io.Writer(destination)
if !options.quiet {
// Start the bar now
bar.Start()
// create multi writer to feed data
writer = io.MultiWriter(destination, bar)
}
_, err = io.CopyN(writer, objectReader, objectSize)
if err != nil {
return err
}
bar.Finish()
info("Success!")
return nil
}
// <S3Path> <S3Path> or <S3Path> <S3Bucket>
func thirdMode(s3c *s3.Client, options *cmdOptions) error {
var objectReader io.ReadCloser
var objectSize int64
var err error
// Send HEAD request to validate if file exists.
objectSize, _, err = s3c.Stat(options.source.bucket, options.source.key)
if err != nil {
return err
}
if options.destination.key == "" {
options.destination.key = options.source.key
}
// Check if the object already exists
_, _, err = s3c.Stat(options.destination.bucket, options.destination.key)
switch os.IsNotExist(err) {
case true:
objectReader, _, err = s3c.Get(options.source.bucket, options.source.key)
if err != nil {
return err
}
err = s3c.Put(options.destination.bucket, options.destination.key, objectSize, objectReader)
if err != nil {
return err
}
case false:
return fmt.Errorf("Ranges not supported")
}
msg := fmt.Sprintf("s3://%s/%s uploaded -- to bucket:(s3://%s/%s)", options.source.bucket, options.source.key,
options.destination.bucket, options.destination.key)
info(msg)
return nil
}
func doFsCopy(c *cli.Context) {
s3c, err := getNewClient(c)
@@ -45,110 +224,23 @@ func doFsCopy(c *cli.Context) {
fatal(err.Error())
}
switch true {
case cmdoptions.isput == true:
stat, err := os.Stat(cmdoptions.body)
if os.IsNotExist(err) {
fatal(err.Error())
}
if stat.IsDir() {
fatal("Is a directory")
}
size := stat.Size()
bodyFile, err := os.Open(cmdoptions.body)
defer bodyFile.Close()
switch getMode(cmdoptions) {
case first:
err := firstMode(s3c, cmdoptions)
if err != nil {
fatal(err.Error())
}
// s3://<bucket> is specified without key
if cmdoptions.key == "" {
cmdoptions.key = cmdoptions.body
}
err = s3c.Put(cmdoptions.bucket, cmdoptions.key, size, bodyFile)
case second:
err := secondMode(s3c, cmdoptions)
if err != nil {
fatal(err.Error())
}
msg := fmt.Sprintf("%s uploaded -- to bucket:(%s)", cmdoptions.key, cmdoptions.bucket)
info(msg)
case cmdoptions.isget == true:
var objectReader io.ReadCloser
var objectSize, downloadedSize int64
var bodyFile *os.File
var err error
var st os.FileInfo
// Send HEAD request to validate if file exists.
objectSize, _, err = s3c.Stat(cmdoptions.key, cmdoptions.bucket)
case third:
err := thirdMode(s3c, cmdoptions)
if err != nil {
fatal(err.Error())
}
var bar *pb.ProgressBar
if !c.GlobalBool("quiet") {
// get progress bar
bar = startBar(objectSize)
}
// Check if the object already exists
st, err = os.Stat(cmdoptions.body)
switch os.IsNotExist(err) {
case true:
// Create if it doesn't exist
bodyFile, err = os.Create(cmdoptions.body)
defer bodyFile.Close()
if err != nil {
fatal(err.Error())
}
objectReader, _, err = s3c.Get(cmdoptions.bucket, cmdoptions.key)
if err != nil {
fatal(err.Error())
}
case false:
downloadedSize = st.Size()
// Verify if file is already downloaded
if downloadedSize == objectSize {
msg := fmt.Sprintf("%s object has been already downloaded", cmdoptions.body)
fatal(msg)
}
bodyFile, err = os.OpenFile(cmdoptions.body, os.O_RDWR, 0600)
defer bodyFile.Close()
if err != nil {
fatal(err.Error())
}
_, err := bodyFile.Seek(downloadedSize, os.SEEK_SET)
if err != nil {
fatal(err.Error())
}
remainingSize := objectSize - downloadedSize
objectReader, objectSize, err = s3c.GetPartial(cmdoptions.bucket, cmdoptions.key, downloadedSize, remainingSize)
if err != nil {
fatal(err.Error())
}
if !c.GlobalBool("quiet") {
bar.Set(int(downloadedSize))
}
}
writer := io.Writer(bodyFile)
if !c.GlobalBool("quiet") {
// Start the bar now
bar.Start()
// create multi writer to feed data
writer = io.MultiWriter(bodyFile, bar)
}
_, err = io.CopyN(writer, objectReader, objectSize)
if err != nil {
fatal(err.Error())
}
bar.Finish()
info("Success!")
default:
fatal("Invalid request")
}
}
+7 -7
View File
@@ -59,29 +59,29 @@ func doFsList(c *cli.Context) {
fatal(err.Error())
}
switch true {
case cmdoptions.bucket == "": // List all buckets
case cmdoptions.source.bucket == "": // List all buckets
buckets, err := s3c.ListBuckets()
if err != nil {
fatal(err.Error())
}
printBuckets(buckets)
case cmdoptions.key == "": // List objects in a bucket
items, _, err = s3c.ListObjects(cmdoptions.bucket, "", "", "", s3.MaxKeys)
case cmdoptions.source.key == "": // List objects in a bucket
items, _, err = s3c.ListObjects(cmdoptions.source.bucket, "", "", "", s3.MaxKeys)
if err != nil {
fatal(err.Error())
}
printObjects(items)
case cmdoptions.key != "": // List objects matching the key prefix
case cmdoptions.source.key != "": // List objects matching the key prefix
var date time.Time
var size int64
size, date, err = s3c.Stat(cmdoptions.key, cmdoptions.bucket)
size, date, err = s3c.Stat(cmdoptions.source.bucket, cmdoptions.source.key)
switch err {
case nil: // List a single object. Exact key
printObject(date, size, cmdoptions.key)
printObject(date, size, cmdoptions.source.key)
case os.ErrNotExist:
// List all objects matching the key prefix
items, _, err = s3c.ListObjects(cmdoptions.bucket, "", cmdoptions.key, "", s3.MaxKeys)
items, _, err = s3c.ListObjects(cmdoptions.source.bucket, "", cmdoptions.source.key, "", s3.MaxKeys)
if err != nil {
fatal(err.Error())
}
+7 -4
View File
@@ -81,12 +81,15 @@ var configure = cli.Command{
},
}
type cmdOptions struct {
type object struct {
bucket string
body string
key string
isget bool
isput bool
}
type cmdOptions struct {
quiet bool
source object
destination object
}
var options = []cli.Command{
+54 -23
View File
@@ -96,6 +96,8 @@ func parseGlobalOptions(c *cli.Context) {
// Parse subcommand options
func parseOptions(c *cli.Context) (cmdoptions *cmdOptions, err error) {
cmdoptions = new(cmdOptions)
cmdoptions.quiet = c.GlobalBool("quiet")
switch len(c.Args()) {
case 1:
if strings.HasPrefix(c.Args().Get(0), "s3://") {
@@ -106,43 +108,72 @@ func parseOptions(c *cli.Context) (cmdoptions *cmdOptions, err error) {
if uri.Scheme != "s3" {
return nil, errInvalidScheme
}
cmdoptions.bucket = uri.Host
cmdoptions.key = strings.TrimPrefix(uri.Path, "/")
cmdoptions.source.bucket = uri.Host
cmdoptions.source.key = strings.TrimPrefix(uri.Path, "/")
} else {
return nil, errInvalidScheme
}
case 2:
if strings.HasPrefix(c.Args().Get(0), "s3://") {
switch true {
case c.Args().Get(0) != "":
uri, err := url.Parse(c.Args().Get(0))
if err != nil {
return nil, err
}
cmdoptions.bucket = uri.Host
if uri.Path == "" {
return nil, errFskey
switch true {
case uri.Scheme == "s3":
if uri.Host == "" {
if uri.Path == "" {
return nil, errInvalidScheme
}
return nil, errInvalidScheme
}
cmdoptions.source.bucket = uri.Host
cmdoptions.source.key = strings.TrimPrefix(uri.Path, "/")
case uri.Scheme == "":
if uri.Host != "" {
return nil, errInvalidScheme
}
if uri.Path != c.Args().Get(0) {
return nil, errInvalidScheme
}
if uri.Path == "." {
return nil, errFskey
}
cmdoptions.source.bucket = uri.Host
cmdoptions.source.key = strings.TrimPrefix(uri.Path, "/")
case uri.Scheme != "s3":
return nil, errInvalidScheme
}
cmdoptions.key = strings.TrimPrefix(uri.Path, "/")
if c.Args().Get(1) == "." {
cmdoptions.body = path.Base(cmdoptions.key)
} else {
cmdoptions.body = c.Args().Get(1)
}
cmdoptions.isget = true
cmdoptions.isput = false
} else if strings.HasPrefix(c.Args().Get(1), "s3://") {
fallthrough
case c.Args().Get(1) != "":
uri, err := url.Parse(c.Args().Get(1))
if err != nil {
return nil, err
}
cmdoptions.bucket = uri.Host
if uri.Path == "" {
cmdoptions.key = c.Args().Get(0)
} else {
cmdoptions.key = strings.TrimPrefix(uri.Path, "/")
switch true {
case uri.Scheme == "s3":
if uri.Host == "" {
if uri.Path == "" {
return nil, errInvalidScheme
}
return nil, errInvalidScheme
}
cmdoptions.destination.bucket = uri.Host
cmdoptions.destination.key = strings.TrimPrefix(uri.Path, "/")
case uri.Scheme == "":
if uri.Host != "" {
return nil, errInvalidScheme
}
if uri.Path == "." {
cmdoptions.destination.key = cmdoptions.source.key
} else {
cmdoptions.destination.key = strings.TrimPrefix(uri.Path, "/")
}
cmdoptions.destination.bucket = uri.Host
case uri.Scheme != "s3":
return nil, errInvalidScheme
}
cmdoptions.body = c.Args().Get(0)
cmdoptions.isget = false
cmdoptions.isput = true
}
default:
return nil, errInvalidScheme
+1 -1
View File
@@ -92,7 +92,7 @@ func (c *Client) Put(bucket, key string, size int64, contents io.Reader) error {
}
// Stat - returns 0, "", os.ErrNotExist if not on S3
func (c *Client) Stat(key, bucket string) (size int64, date time.Time, reterr error) {
func (c *Client) Stat(bucket, key string) (size int64, date time.Time, reterr error) {
req := newReq(c.keyURL(bucket, key))
req.Method = "HEAD"
c.Auth.signRequest(req)