From 6c7dbcaf4ed4783db1a0cda78e909534f31bfbfb Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 7 Jun 2015 02:51:31 -0700 Subject: [PATCH 1/4] More prefix url cleanup --- cp-url.go | 18 +++++++----------- pkg/client/s3/s3.go | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/cp-url.go b/cp-url.go index f1034337..1c9e43f7 100644 --- a/cp-url.go +++ b/cp-url.go @@ -213,13 +213,6 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs { // add `/` after trimming off `...` to emulate directories sourceURL = stripRecursiveURL(sourceURL) - // is set when ... includes a directory, i.e. /path/to/dir... instead of path/to/dir/... - recursivePrefix := "" - if !strings.HasSuffix(sourceURL, string(filepath.Separator)) { - recursivePrefix = filepath.Base(sourceURL) - } - sourceURL = strings.TrimSuffix(sourceURL, string(filepath.Separator)) + string(filepath.Separator) - sourceClient, err := source2Client(sourceURL) if err != nil { cpURLsCh <- &cpURLs{Error: iodine.New(err, nil)} @@ -285,16 +278,19 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs { continue } - sourceContentParse, err := client.Parse(sourceURLParse.String() + sourceContent.Content.Name) + sourceURLDelimited := sourceURLParse.String()[:strings.LastIndex(sourceURLParse.String(), + string(sourceURLParse.Separator))+1] + sourceContentName := sourceContent.Content.Name + sourceContentURL := sourceURLDelimited + sourceContentName + sourceContentParse, err := client.Parse(sourceContentURL) if err != nil { - cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceContent.Content.Name}, nil)} + cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceContentName}, nil)} continue } // Construct target path from recursive path of source without its prefix dir. newTargetURLParse := *targetURLParse - newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, recursivePrefix, - strings.TrimPrefix(sourceContentParse.Path, filepath.Dir(sourceURLParse.Path))) + newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, sourceContentName) cpURLsCh <- prepareCopyURLsTypeA(sourceContentParse.String(), newTargetURLParse.String()) } }(sourceURL, targetURL, cpURLsCh) diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index 34158782..45169b61 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -162,17 +162,16 @@ func (c *s3Client) Stat() (*client.Content, error) { if object != "" { metadata, err := c.api.StatObject(bucket, object) if err != nil { - if err.Error() == "404 Not Found" { + errResponse := s3.ToErrorResponse(err) + if errResponse.Code == "NoSuchKey" { for content := range c.List(false) { if content.Err != nil { return nil, iodine.New(err, nil) } - if !strings.HasPrefix(content.Content.Name, object) { - content.Content.Type = os.ModeDir - content.Content.Name = object - content.Content.Size = 0 - return content.Content, nil - } + content.Content.Type = os.ModeDir + content.Content.Name = object + content.Content.Size = 0 + return content.Content, nil } } return nil, iodine.New(err, nil) @@ -336,7 +335,10 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel return } content := new(client.Content) - normalizedKey := strings.TrimPrefix(object.Stat.Key, strings.TrimSuffix(o, delimiter)+delimiter) + normalizedKey := object.Stat.Key + if strings.HasSuffix(o, delimiter) { + normalizedKey = strings.TrimPrefix(object.Stat.Key, o) + } content.Name = normalizedKey content.Size = object.Stat.Size content.Time = object.Stat.LastModified From 58d832bcd08646e5dadc9c9d85bb37f16509763b Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 8 Jun 2015 01:20:18 -0700 Subject: [PATCH 2/4] Update all dependencies --- Godeps/Godeps.json | 4 +- .../github.com/dustin/go-humanize/comma.go | 4 +- .../src/github.com/fatih/structs/README.md | 5 -- .../src/github.com/fatih/structs/structs.go | 25 ------ .../github.com/fatih/structs/structs_test.go | 33 -------- .../src/github.com/minio/minio-go/api_test.go | 6 +- .../github.com/minio/minio-go/lowlevelapi.go | 79 +++++++++++++++++-- .../minio/minio-go/lowlevelmultipartapi.go | 19 ++++- .../shiena/ansicolor/ansicolor_windows.go | 2 +- Makefile | 3 + 10 files changed, 102 insertions(+), 78 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index a6506ce0..7125f23c 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -15,7 +15,7 @@ }, { "ImportPath": "github.com/fatih/structs", - "Rev": "c06d8730d42f16f3e8dddc236c7d69c0952a8a2c" + "Rev": "c00d27128bb88e9c1adab1a53cda9c72c6d1ff9b" }, { "ImportPath": "github.com/minio/check", @@ -28,7 +28,7 @@ }, { "ImportPath": "github.com/minio/minio-go", - "Rev": "745daad6222582eb6ecbb864f0cbcdc4427dd52f" + "Rev": "dee5d16f0c4515ddb2785a1b8317ec11796d8fc3" }, { "ImportPath": "github.com/minio/minio/pkg/iodine", diff --git a/Godeps/_workspace/src/github.com/dustin/go-humanize/comma.go b/Godeps/_workspace/src/github.com/dustin/go-humanize/comma.go index 80dbfa78..9d0d2ed1 100644 --- a/Godeps/_workspace/src/github.com/dustin/go-humanize/comma.go +++ b/Godeps/_workspace/src/github.com/dustin/go-humanize/comma.go @@ -33,7 +33,7 @@ func Comma(v int64) string { j-- } parts[j] = strconv.Itoa(int(v)) - return sign + strings.Join(parts[j:], ",") + return sign + strings.Join(parts[j:len(parts)], ",") } // Commaf produces a string form of the given number in base 10 with @@ -97,5 +97,5 @@ func BigComma(b *big.Int) string { j-- } parts[j] = strconv.Itoa(int(b.Int64())) - return sign + strings.Join(parts[j:], ",") + return sign + strings.Join(parts[j:len(parts)], ",") } diff --git a/Godeps/_workspace/src/github.com/fatih/structs/README.md b/Godeps/_workspace/src/github.com/fatih/structs/README.md index 1dc34048..87fccf24 100644 --- a/Godeps/_workspace/src/github.com/fatih/structs/README.md +++ b/Godeps/_workspace/src/github.com/fatih/structs/README.md @@ -43,10 +43,6 @@ m := structs.Map(server) // => ["gopher", 123456, true] v := structs.Values(server) -// Convert the names of a struct to a []string -// (see "Names methods" for more info about fields) -n := structs.Names(server) - // Convert the values of a struct to a []*Field // (see "Field methods" for more info about fields) f := structs.Fields(server) @@ -77,7 +73,6 @@ s := structs.New(server) m := s.Map() // Get a map[string]interface{} v := s.Values() // Get a []interface{} f := s.Fields() // Get a []*Field -n := s.Names() // Get a []string f := s.Field(name) // Get a *Field based on the given field name f, ok := s.FieldsOk(name) // Get a *Field based on the given field name n := s.Name() // Get the struct name diff --git a/Godeps/_workspace/src/github.com/fatih/structs/structs.go b/Godeps/_workspace/src/github.com/fatih/structs/structs.go index 1c1f68bb..47bf3545 100644 --- a/Godeps/_workspace/src/github.com/fatih/structs/structs.go +++ b/Godeps/_workspace/src/github.com/fatih/structs/structs.go @@ -171,25 +171,6 @@ func (s *Struct) Fields() []*Field { return getFields(s.value, s.TagName) } -// Names returns a slice of field names. A struct tag with the content of "-" -// ignores the checking of that particular field. Example: -// -// // Field is ignored by this package. -// Field bool `structs:"-"` -// -// It panics if s's kind is not struct. -func (s *Struct) Names() []string { - fields := getFields(s.value, s.TagName) - - names := make([]string, len(fields)) - - for i, field := range fields { - names[i] = field.Name() - } - - return names -} - func getFields(v reflect.Value, tagName string) []*Field { if v.Kind() == reflect.Ptr { v = v.Elem() @@ -406,12 +387,6 @@ func Fields(s interface{}) []*Field { return New(s).Fields() } -// Names returns a slice of field names. For more info refer to Struct types -// Names() method. It panics if s's kind is not struct. -func Names(s interface{}) []string { - return New(s).Names() -} - // IsZero returns true if all fields is equal to a zero value. For more info // refer to Struct types IsZero() method. It panics if s's kind is not struct. func IsZero(s interface{}) bool { diff --git a/Godeps/_workspace/src/github.com/fatih/structs/structs_test.go b/Godeps/_workspace/src/github.com/fatih/structs/structs_test.go index 83261760..02788b10 100644 --- a/Godeps/_workspace/src/github.com/fatih/structs/structs_test.go +++ b/Godeps/_workspace/src/github.com/fatih/structs/structs_test.go @@ -442,39 +442,6 @@ func TestValues_Anonymous(t *testing.T) { } } -func TestNames(t *testing.T) { - var T = struct { - A string - B int - C bool - }{ - A: "a-value", - B: 2, - C: true, - } - - s := Names(T) - - if len(s) != 3 { - t.Errorf("Names should return a slice of len 3, got: %d", len(s)) - } - - inSlice := func(val string) bool { - for _, v := range s { - if reflect.DeepEqual(v, val) { - return true - } - } - return false - } - - for _, val := range []string{"A", "B", "C"} { - if !inSlice(val) { - t.Errorf("Names should have the value %v", val) - } - } -} - func TestFields(t *testing.T) { var T = struct { A string diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go b/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go index 92117050..83187253 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go @@ -89,7 +89,7 @@ func TestBucketOperations(t *testing.T) { if err == nil { t.Errorf("Error") } - if err.Error() != "403 Forbidden" { + if err.Error() != "Access Denied" { t.Errorf("Error") } @@ -133,7 +133,7 @@ func TestBucketOperations(t *testing.T) { if err == nil { t.Fatalf("Error") } - if err.Error() != "404 Not Found" { + if err.Error() != "The specified bucket does not exist." { t.Errorf("Error") } } @@ -191,7 +191,7 @@ func TestObjectOperations(t *testing.T) { if err == nil { t.Fatalf("Error") } - if err.Error() != "404 Not Found" { + if err.Error() != "The specified key does not exist." { t.Errorf("Error") } } diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelapi.go b/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelapi.go index 6bcb9f31..97533124 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelapi.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelapi.go @@ -335,7 +335,24 @@ func (a lowLevelAPI) headBucket(bucket string) error { if resp != nil { if resp.StatusCode != http.StatusOK { // Head has no response body, handle it - return fmt.Errorf("%s", resp.Status) + var errorResponse ErrorResponse + switch { + case resp.StatusCode == http.StatusNotFound: + errorResponse = ErrorResponse{ + Code: "NoSuchBucket", + Message: "The specified bucket does not exist.", + Resource: "/" + bucket, + RequestID: resp.Header.Get("x-amz-request-id"), + } + case resp.StatusCode == http.StatusForbidden: + errorResponse = ErrorResponse{ + Code: "AccessDenied", + Message: "Access Denied", + Resource: "/" + bucket, + RequestID: resp.Header.Get("x-amz-request-id"), + } + } + return errorResponse } } return nil @@ -367,8 +384,24 @@ func (a lowLevelAPI) deleteBucket(bucket string) error { defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - // Head has no response body, handle it - return fmt.Errorf("%s", resp.Status) + var errorResponse ErrorResponse + switch { + case resp.StatusCode == http.StatusNotFound: + errorResponse = ErrorResponse{ + Code: "NoSuchBucket", + Message: "The specified bucket does not exist.", + Resource: "/" + bucket, + RequestID: resp.Header.Get("x-amz-request-id"), + } + case resp.StatusCode == http.StatusForbidden: + errorResponse = ErrorResponse{ + Code: "AccessDenied", + Message: "Access Denied", + Resource: "/" + bucket, + RequestID: resp.Header.Get("x-amz-request-id"), + } + } + return errorResponse } } return nil @@ -512,7 +545,24 @@ func (a lowLevelAPI) deleteObject(bucket, object string) error { defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return fmt.Errorf("%s", resp.Status) + var errorResponse ErrorResponse + switch { + case resp.StatusCode == http.StatusNotFound: + errorResponse = ErrorResponse{ + Code: "NoSuchKey", + Message: "The specified key does not exist.", + Resource: "/" + bucket + "/" + object, + RequestID: resp.Header.Get("x-amz-request-id"), + } + case resp.StatusCode == http.StatusForbidden: + errorResponse = ErrorResponse{ + Code: "AccessDenied", + Message: "Access Denied", + Resource: "/" + bucket + "/" + object, + RequestID: resp.Header.Get("x-amz-request-id"), + } + } + return errorResponse } } return nil @@ -545,7 +595,24 @@ func (a lowLevelAPI) headObject(bucket, object string) (ObjectStat, error) { defer resp.Body.Close() if resp != nil { if resp.StatusCode != http.StatusOK { - return ObjectStat{}, fmt.Errorf("%s", resp.Status) + var errorResponse ErrorResponse + switch { + case resp.StatusCode == http.StatusNotFound: + errorResponse = ErrorResponse{ + Code: "NoSuchKey", + Message: "The specified key does not exist.", + Resource: "/" + bucket + "/" + object, + RequestID: resp.Header.Get("x-amz-request-id"), + } + case resp.StatusCode == http.StatusForbidden: + errorResponse = ErrorResponse{ + Code: "AccessDenied", + Message: "Access Denied", + Resource: "/" + bucket + "/" + object, + RequestID: resp.Header.Get("x-amz-request-id"), + } + } + return ObjectStat{}, errorResponse } } md5sum := strings.Trim(resp.Header.Get("ETag"), "\"") // trim off the odd double quotes @@ -594,7 +661,7 @@ func (a lowLevelAPI) listBuckets() (listAllMyBucketsResult, error) { if resp != nil { // for un-authenticated requests, amazon sends a redirect handle it if resp.StatusCode == http.StatusTemporaryRedirect { - return listAllMyBucketsResult{}, fmt.Errorf("%s", resp.Status) + return listAllMyBucketsResult{}, errors.New(resp.Status) } if resp.StatusCode != http.StatusOK { return listAllMyBucketsResult{}, responseToError(resp.Body) diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelmultipartapi.go b/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelmultipartapi.go index 7f043ed9..5b8b5fc1 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelmultipartapi.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelmultipartapi.go @@ -230,7 +230,24 @@ func (a lowLevelAPI) abortMultipartUpload(bucket, object, uploadID string) error if resp != nil { if resp.StatusCode != http.StatusNoContent { // Abort has no response body, handle it - return fmt.Errorf("%s", resp.Status) + var errorResponse ErrorResponse + switch { + case resp.StatusCode == http.StatusNotFound: + errorResponse = ErrorResponse{ + Code: "NoSuchUpload", + Message: "The specified multipart upload does not exist.", + Resource: "/" + bucket + "/" + object, + RequestID: resp.Header.Get("x-amz-request-id"), + } + case resp.StatusCode == http.StatusForbidden: + errorResponse = ErrorResponse{ + Code: "AccessDenied", + Message: "Access Denied", + Resource: "/" + bucket + "/" + object, + RequestID: resp.Header.Get("x-amz-request-id"), + } + } + return errorResponse } } return resp.Body.Close() diff --git a/Godeps/_workspace/src/github.com/shiena/ansicolor/ansicolor_windows.go b/Godeps/_workspace/src/github.com/shiena/ansicolor/ansicolor_windows.go index f4938667..315f44c5 100644 --- a/Godeps/_workspace/src/github.com/shiena/ansicolor/ansicolor_windows.go +++ b/Godeps/_workspace/src/github.com/shiena/ansicolor/ansicolor_windows.go @@ -308,7 +308,7 @@ func (cw *ansiColorWriter) Write(p []byte) (int, error) { } if cw.state == outsideCsiCode { - nw, err = cw.w.Write(p[first:]) + nw, err = cw.w.Write(p[first:len(p)]) } return r + nw + nc, err diff --git a/Makefile b/Makefile index f84b6c5f..c3b01022 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,9 @@ release: getdeps verifiers @go run make.go install @mkdir -p $(HOME)/.mc +godepupdate: + @for i in $(grep ImportPath Godeps/Godeps.json | grep -v minio/mc | cut -f2 -d: | sed -e 's/,//' -e 's/^[ \t]*//' -e 's/[ \t]*$//' -e 's/\"//g'); do godep update $i; done + save: @godep save ./... From 71b34e43474ed0ae194f51df1983078d28d50d03 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 8 Jun 2015 02:17:22 -0700 Subject: [PATCH 3/4] Add some comments in ls.go --- ls.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ls.go b/ls.go index e1eeebc8..4673bc8b 100644 --- a/ls.go +++ b/ls.go @@ -38,13 +38,18 @@ const ( func parseContent(c *client.Content) Content { content := Content{} content.Time = c.Time.Local().Format(printDate) + + // guess file type content.Filetype = func() string { if c.Type.IsDir() { return "inode/directory" } return "application/octet-stream" }() + content.Size = humanize.IBytes(uint64(c.Size)) + + // Convert OS Type to match console file printing style content.Name = func() string { switch { case runtime.GOOS == "windows": From e53f009a9dd7749e805d00dd61952ae651a42438 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 8 Jun 2015 02:33:52 -0700 Subject: [PATCH 4/4] More prefix url fixes, all combinations work as intended --- cp-url.go | 7 ++++--- pkg/client/s3/s3.go | 24 ++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/cp-url.go b/cp-url.go index 1c9e43f7..66722d8f 100644 --- a/cp-url.go +++ b/cp-url.go @@ -124,7 +124,7 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) *cpURLs { return &cpURLs{Error: iodine.New(err, nil)} } - // Source exist? + // Source exists? sourceContent, err := sourceClient.Stat() if err != nil { // Source does not exist or insufficient privileges. @@ -134,12 +134,12 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) *cpURLs { // Source is not a regular file return &cpURLs{Error: iodine.New(errInvalidSource{URL: sourceURL}, nil)} } + targetClient, err := target2Client(targetURL) if err != nil { return &cpURLs{Error: iodine.New(err, nil)} } - - // Target exist? + // Target exists? targetContent, err := targetClient.Stat() if err == nil { // Target exists. if !targetContent.Type.IsRegular() { // Target is not a regular file @@ -148,6 +148,7 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) *cpURLs { } // All OK.. We can proceed. Type A + sourceContent.Name = sourceURL return &cpURLs{SourceContent: sourceContent, TargetContent: &client.Content{Name: targetURL}} } diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index 45169b61..5ce8c21b 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -159,6 +159,16 @@ func (c *s3Client) SetBucketACL(acl string) error { func (c *s3Client) Stat() (*client.Content, error) { objectMetadata := new(client.Content) bucket, object := c.url2BucketAndObject() + switch { + // valid case for s3:... + case bucket == "" && object == "": + for bucket := range c.api.ListBuckets() { + if bucket.Err != nil { + return nil, iodine.New(bucket.Err, nil) + } + return &client.Content{Type: os.ModeDir}, nil + } + } if object != "" { metadata, err := c.api.StatObject(bucket, object) if err != nil { @@ -336,8 +346,18 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel } content := new(client.Content) normalizedKey := object.Stat.Key - if strings.HasSuffix(o, delimiter) { - normalizedKey = strings.TrimPrefix(object.Stat.Key, o) + switch { + case o == "": + // if no prefix provided and also URL is not delimited then we add bucket back into object name + if strings.LastIndex(c.hostURL.Path, string(c.hostURL.Separator)) == 0 { + if c.hostURL.String()[:strings.LastIndex(c.hostURL.String(), string(c.hostURL.Separator))+1] != b { + normalizedKey = filepath.Join(b, object.Stat.Key) + } + } + default: + if strings.HasSuffix(o, delimiter) { + normalizedKey = strings.TrimPrefix(object.Stat.Key, o) + } } content.Name = normalizedKey content.Size = object.Stat.Size