From 4709af19ab016f27f49966c47a49518d0f27ffae Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Thu, 4 Jun 2015 19:49:40 -0700 Subject: [PATCH] Handle prefixed recursive behavior and mimick filesystem behavior --- cp-url.go | 3 ++- pkg/client/s3/s3.go | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cp-url.go b/cp-url.go index 666142bb..c66ecbf2 100644 --- a/cp-url.go +++ b/cp-url.go @@ -211,7 +211,8 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs { return } - sourceURL = stripRecursiveURL(sourceURL) + // add `/` after trimming off `...` to emulate directories + sourceURL = strings.TrimSuffix(stripRecursiveURL(sourceURL), "/") + "/" sourceClient, err := source2Client(sourceURL) if err != nil { cpURLsCh <- &cpURLs{Error: iodine.New(err, nil)} diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index 88ac26a7..ac5d5119 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -154,13 +154,26 @@ func (c *s3Client) SetBucketACL(acl string) error { // Stat - send a 'HEAD' on a bucket or object to get its metadata func (c *s3Client) Stat() (*client.Content, error) { + objectMetadata := new(client.Content) bucket, object := c.url2BucketAndObject() if object != "" { metadata, err := c.api.StatObject(bucket, object) if err != nil { + if err.Error() == "404 Not Found" { + 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 + } + } + } return nil, iodine.New(err, nil) } - objectMetadata := new(client.Content) objectMetadata.Name = c.hostURL.String() // do not change this objectMetadata.Time = metadata.LastModified objectMetadata.Size = metadata.Size @@ -210,9 +223,9 @@ func (c *s3Client) List(recursive bool) <-chan client.ContentOnChannel { func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { defer close(contentCh) - bucket, object := c.url2BucketAndObject() + b, o := c.url2BucketAndObject() switch { - case bucket == "" && object == "": + case b == "" && o == "": for bucket := range c.api.ListBuckets() { if bucket.Err != nil { contentCh <- client.ContentOnChannel{ @@ -232,7 +245,7 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { } } default: - metadata, err := c.api.StatObject(bucket, object) + metadata, err := c.api.StatObject(b, o) switch err.(type) { case nil: content := new(client.Content) @@ -245,7 +258,7 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { Err: nil, } default: - for object := range c.api.ListObjects(bucket, object, false) { + for object := range c.api.ListObjects(b, o, false) { if object.Err != nil { contentCh <- client.ContentOnChannel{ Content: nil, @@ -254,7 +267,12 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { return } content := new(client.Content) - content.Name = object.Stat.Key + normalizedPrefix := strings.TrimSuffix(o, "/") + "/" + normalizedKey := object.Stat.Key + if normalizedPrefix != object.Stat.Key && strings.HasPrefix(object.Stat.Key, normalizedPrefix) { + normalizedKey = strings.TrimPrefix(object.Stat.Key, normalizedPrefix) + } + content.Name = normalizedKey switch { case strings.HasSuffix(object.Stat.Key, "/"): content.Time = time.Now() @@ -275,9 +293,9 @@ func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) { func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel) { defer close(contentCh) - bucket, object := c.url2BucketAndObject() + b, o := c.url2BucketAndObject() switch { - case bucket == "" && object == "": + case b == "" && o == "": for bucket := range c.api.ListBuckets() { if bucket.Err != nil { contentCh <- client.ContentOnChannel{ @@ -286,7 +304,7 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel } return } - for object := range c.api.ListObjects(bucket.Stat.Name, object, true) { + for object := range c.api.ListObjects(bucket.Stat.Name, o, true) { if object.Err != nil { contentCh <- client.ContentOnChannel{ Content: nil, @@ -306,7 +324,7 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel } } default: - for object := range c.api.ListObjects(bucket, object, true) { + for object := range c.api.ListObjects(b, o, true) { if object.Err != nil { contentCh <- client.ContentOnChannel{ Content: nil, @@ -315,7 +333,9 @@ func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel return } content := new(client.Content) - content.Name = strings.TrimSuffix(c.hostURL.String(), "/") + "/" + object.Stat.Key + normalizedKey := strings.TrimPrefix(object.Stat.Key, strings.TrimSuffix(o, "/")+"/") + hostPrefix := strings.TrimSuffix(c.hostURL.String(), "/") + "/" + content.Name = hostPrefix + normalizedKey content.Size = object.Stat.Size content.Time = object.Stat.LastModified content.Type = os.FileMode(0664)