diff --git a/cmd-cp.go b/cmd-cp.go index ad1da088..374b95d6 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -78,8 +78,6 @@ func getClientReader(sourceURL string) (io.ReadCloser, int64, string, error) { // doCopyCmd copies objects into and from a bucket or between buckets func multiCopy(targetURLs []string, sourceURL string) (err error) { - fmt.Println(targetURLs) - errParams := map[string]string{ "targetURLs": fmt.Sprintln(targetURLs), "sourceURL": sourceURL, diff --git a/cmd-mb.go b/cmd-mb.go index ba734f63..09cfa595 100644 --- a/cmd-mb.go +++ b/cmd-mb.go @@ -17,6 +17,8 @@ package main import ( + "strings" + "github.com/minio-io/cli" "github.com/minio-io/mc/pkg/client" "github.com/minio-io/mc/pkg/console" @@ -29,7 +31,6 @@ func doMakeBucketCmd(ctx *cli.Context) { if len(ctx.Args()) < 1 { cli.ShowCommandHelpAndExit(ctx, "mb", 1) // last argument is exit code } - config, err := getMcConfig() if err != nil { log.Debug.Println(iodine.New(err, nil)) @@ -53,9 +54,11 @@ func doMakeBucketCmd(ctx *cli.Context) { console.Fatalln(err) } - if !client.IsValidBucketName(bucket) { - log.Debug.Println(iodine.New(err, nil)) - console.Fatalln(errInvalidbucket) + if !strings.HasPrefix(urlStr, "file:") { + if !client.IsValidBucketName(bucket) { + log.Debug.Println(iodine.New(err, nil)) + console.Fatalln(errInvalidbucket) + } } err = clnt.PutBucket(bucket) diff --git a/pkg/client/client.go b/pkg/client/client.go index 5e485a99..e4b2e917 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -19,7 +19,6 @@ package client import ( "io" "regexp" - "strconv" "time" ) @@ -73,105 +72,14 @@ func IsValidBucketName(bucket string) bool { return match } -/// Collection of standard errors +// BySize implements sort.Interface for []Item based on the Size field. +type BySize []*Item -// APINotImplemented - api not implemented -type APINotImplemented struct { - API string -} +// Len - +func (a BySize) Len() int { return len(a) } -func (e APINotImplemented) Error() string { - return "API not implemented: " + e.API -} +// Swap - +func (a BySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -// InvalidArgument - bad arguments provided -type InvalidArgument struct{} - -func (e InvalidArgument) Error() string { - return "invalid arguments" -} - -// InvalidMaxKeys - invalid maxkeys provided -type InvalidMaxKeys struct { - MaxKeys int -} - -func (e InvalidMaxKeys) Error() string { - return "invalid maxkeys: " + strconv.Itoa(e.MaxKeys) -} - -// InvalidAuthorizationKey - invalid authorization key -type InvalidAuthorizationKey struct{} - -func (e InvalidAuthorizationKey) Error() string { - return "invalid authorization key" -} - -// AuthorizationKeyEmpty - empty auth key provided -type AuthorizationKeyEmpty struct{} - -func (e AuthorizationKeyEmpty) Error() string { - return "authorization key empty" -} - -// InvalidRange - invalid range requested -type InvalidRange struct { - Offset int64 -} - -func (e InvalidRange) Error() string { - return "invalid range offset: " + strconv.FormatInt(e.Offset, 10) -} - -// GenericBucketError - generic bucket operations error -type GenericBucketError struct { - Bucket string -} - -// BucketNotFound - bucket requested does not exist -type BucketNotFound GenericBucketError - -func (e BucketNotFound) Error() string { - return "bucket " + e.Bucket + " not found" -} - -// BucketExists - bucket exists -type BucketExists GenericBucketError - -func (e BucketExists) Error() string { - return "bucket " + e.Bucket + " exists" -} - -// InvalidBucketName - bucket name invalid -type InvalidBucketName GenericBucketError - -func (e InvalidBucketName) Error() string { - return "Invalid bucketname " + e.Bucket -} - -// GenericObjectError - generic object operations error -type GenericObjectError struct { - Bucket string - Object string -} - -// ObjectNotFound - object requested does not exist -type ObjectNotFound GenericObjectError - -func (e ObjectNotFound) Error() string { - return "object " + e.Object + " not found in bucket " + e.Bucket -} - -// InvalidObjectName - object requested is invalid -type InvalidObjectName GenericObjectError - -func (e InvalidObjectName) Error() string { - return "object " + e.Object + "at" + e.Bucket + "is invalid" -} - -// ObjectExists - object exists -type ObjectExists GenericObjectError - -func (e ObjectExists) Error() string { - return "object " + e.Object + " exists" -} +// Less - +func (a BySize) Less(i, j int) bool { return a[i].Size < a[j].Size } diff --git a/pkg/client/errors.go b/pkg/client/errors.go new file mode 100644 index 00000000..52213d75 --- /dev/null +++ b/pkg/client/errors.go @@ -0,0 +1,106 @@ +package client + +import "strconv" + +/// Collection of standard errors + +// APINotImplemented - api not implemented +type APINotImplemented struct { + API string +} + +func (e APINotImplemented) Error() string { + return "API not implemented: " + e.API +} + +// InvalidArgument - bad arguments provided +type InvalidArgument struct{} + +func (e InvalidArgument) Error() string { + return "invalid argument" +} + +// InvalidMaxKeys - invalid maxkeys provided +type InvalidMaxKeys struct { + MaxKeys int +} + +func (e InvalidMaxKeys) Error() string { + return "invalid maxkeys: " + strconv.Itoa(e.MaxKeys) +} + +// InvalidAuthorizationKey - invalid authorization key +type InvalidAuthorizationKey struct{} + +func (e InvalidAuthorizationKey) Error() string { + return "invalid authorization key" +} + +// AuthorizationKeyEmpty - empty auth key provided +type AuthorizationKeyEmpty struct{} + +func (e AuthorizationKeyEmpty) Error() string { + return "authorization key empty" +} + +// InvalidRange - invalid range requested +type InvalidRange struct { + Offset int64 +} + +func (e InvalidRange) Error() string { + return "invalid range offset: " + strconv.FormatInt(e.Offset, 10) +} + +// GenericBucketError - generic bucket operations error +type GenericBucketError struct { + Bucket string +} + +// BucketNotFound - bucket requested does not exist +type BucketNotFound GenericBucketError + +func (e BucketNotFound) Error() string { + return "bucket " + e.Bucket + " not found" +} + +// BucketExists - bucket exists +type BucketExists GenericBucketError + +func (e BucketExists) Error() string { + return "bucket " + e.Bucket + " exists" +} + +// InvalidBucketName - bucket name invalid +type InvalidBucketName GenericBucketError + +func (e InvalidBucketName) Error() string { + return "Invalid bucketname " + e.Bucket +} + +// GenericObjectError - generic object operations error +type GenericObjectError struct { + Bucket string + Object string +} + +// ObjectNotFound - object requested does not exist +type ObjectNotFound GenericObjectError + +func (e ObjectNotFound) Error() string { + return "object " + e.Object + " not found in bucket " + e.Bucket +} + +// InvalidObjectName - object requested is invalid +type InvalidObjectName GenericObjectError + +func (e InvalidObjectName) Error() string { + return "object " + e.Object + "at" + e.Bucket + "is invalid" +} + +// ObjectExists - object exists +type ObjectExists GenericObjectError + +func (e ObjectExists) Error() string { + return "object " + e.Object + " exists" +} diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 5063015e..bbee5d12 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -23,6 +23,7 @@ import ( "os" "path" "path/filepath" + "sort" "strings" "time" @@ -151,29 +152,21 @@ func (f *fsClient) ListObjects(bucket, prefix string) (items []*client.Item, err if fi.IsDir() { return nil // not a fs skip } - if prefix != "" { - if strings.Contains(fp, prefix) { - item := &client.Item{ - Key: fp, - LastModified: fi.ModTime(), - Size: fi.Size(), - } - items = append(items, item) - } - } else { - item := &client.Item{ - Key: fp, - LastModified: fi.ModTime(), - Size: fi.Size(), - } - items = append(items, item) + // If bucket path is not absolute, trim it + // otherwise pass it down as is + item := &client.Item{ + Key: strings.TrimPrefix(fp, bucket), + LastModified: fi.ModTime(), + Size: fi.Size(), } + items = append(items, item) return nil } - err = filepath.Walk(bucket, visitFS) + err = filepath.Walk(path.Join(bucket, prefix), visitFS) if err != nil { return nil, iodine.New(err, nil) } + sort.Sort(client.BySize(items)) return items, nil } @@ -182,10 +175,7 @@ func (f *fsClient) PutBucket(bucket string) error { if bucket == "" || strings.TrimSpace(bucket) == "" { return iodine.New(client.InvalidArgument{}, nil) } - if !client.IsValidBucketName(bucket) || strings.Contains(bucket, ".") { - return iodine.New(client.InvalidBucketName{Bucket: bucket}, nil) - } - err := os.Mkdir(bucket, os.ModeDir) + err := os.Mkdir(bucket, 0700) if os.IsExist(err) { return iodine.New(client.BucketExists{Bucket: bucket}, nil) } diff --git a/pkg/client/s3/bucket.go b/pkg/client/s3/bucket.go index 35155e14..e95b31fe 100644 --- a/pkg/client/s3/bucket.go +++ b/pkg/client/s3/bucket.go @@ -48,13 +48,6 @@ import ( "github.com/minio-io/minio/pkg/iodine" ) -// bySize implements sort.Interface for []Item based on the Size field. -type bySize []*client.Item - -func (a bySize) Len() int { return len(a) } -func (a bySize) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a bySize) Less(i, j int) bool { return a[i].Size < a[j].Size } - /// Bucket API operations // ListBuckets - Get list of buckets diff --git a/pkg/client/s3/bucket_internal.go b/pkg/client/s3/bucket_internal.go index 72576a57..044185c9 100644 --- a/pkg/client/s3/bucket_internal.go +++ b/pkg/client/s3/bucket_internal.go @@ -125,6 +125,6 @@ func (c *s3Client) queryObjects(bucket string, startAt, prefix, delimiter string return nil, nil, iodine.New(errors.New("No items replied"), nil) } } - sort.Sort(bySize(items)) + sort.Sort(client.BySize(items)) return items, prefixes, nil } diff --git a/url-parser.go b/url-parser.go index 7184e5c5..70f65692 100644 --- a/url-parser.go +++ b/url-parser.go @@ -17,16 +17,26 @@ package main import ( + "path" + "path/filepath" "strings" "net/url" "github.com/minio-io/cli" + "github.com/minio-io/mc/pkg/client" "github.com/minio-io/mc/pkg/console" "github.com/minio-io/minio/pkg/iodine" "github.com/minio-io/minio/pkg/utils/log" ) +// TODO - y4m4 - this file needs to be cleaned up before we make a release +// +// The actual work on this file is about how do we handle generic +// conversions for both file:/// and http:// in a simple way. +// +// I propose we write this as a proper struct and its methods. + // URLType defines supported storage protocols type urlType int @@ -96,8 +106,9 @@ func fixFileURL(urlStr string) (fixedURL string, err error) { } // do not use u.Scheme since that would construct a path in the form // file:// which is an invalid file but url Parse doesn't report error - // so we construct manually instead - fixedURL = "file:///" + u.Path + // so we construct manually instead as an absolute path + path, _ := filepath.Abs(u.Path) + fixedURL = "file://" + path return fixedURL, nil } @@ -109,6 +120,12 @@ func url2Object(urlStr string) (bucketName, objectName string, err error) { // No bucket name passed. It is a valid case return "", "", nil } + // url is of scheme file, behave differently by returning + // directory and file instead using filepath.Split function + if u.Scheme == "file" { + bucketName, objectName = filepath.Split(u.Path) + return bucketName, objectName, nil + } splits := strings.SplitN(u.Path, "/", 3) switch len(splits) { case 0, 1: @@ -126,8 +143,26 @@ func url2Object(urlStr string) (bucketName, objectName string, err error) { // url2Bucket converts URL to bucket name func url2Bucket(urlStr string) (bucketName string, err error) { - bucketName, _, err = url2Object(urlStr) - return bucketName, iodine.New(err, nil) + u, err := url.Parse(urlStr) + if err != nil { + return "", iodine.New(err, nil) + } + if u.Scheme == "file" { + bucketName, objectName, err := url2Object(urlStr) + if err != nil { + return "", iodine.New(err, nil) + } + return path.Join(bucketName, objectName), nil + } + bucketName, objectName, err := url2Object(urlStr) + if objectName != "" { + // objectName also provided invalid argument + return "", iodine.New(client.InvalidArgument{}, nil) + } + if err != nil { + return "", iodine.New(err, nil) + } + return bucketName, nil } // parseURL extracts URL string from a single cmd-line argument