diff --git a/cp-url.go b/cp-url.go index 2f33261e..eb3c870b 100644 --- a/cp-url.go +++ b/cp-url.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "os" "path/filepath" "strings" @@ -150,6 +151,20 @@ func checkCopySyntaxTypeC(srcURLs []string, tgtURL string) { srcURL := srcURLs[0] srcURL = stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(srcURL) + if err != nil { + // if file not found it could be that user has provided a valid + // prefix, populate parent content properly and set error to nil. + // if at all the prefix doesn't exist eventually 'List' will handle + // it properly. + if os.IsNotExist(err.ToGoError()) { + // set the err back to nil consciously. + err = nil + // fill in srcContent for subsequent verification. + srcContent = new(client.Content) + srcContent.URL = *client.NewURL(srcURL) + srcContent.Type = os.ModeDir + } + } fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+srcURL+"’.") if srcContent.Type.IsRegular() { // Ellipses is supported only for folders. @@ -280,12 +295,31 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { // add `/` after trimming off `...` to emulate folders sourceURL = stripRecursiveURL(sourceURL) - sourceClient, sourceContent, err := url2Stat(sourceURL) + sourceClient, err := url2Client(sourceURL) if err != nil { - // Source does not exist or insufficient privileges. + // Source initialization failed. copyURLsCh <- copyURLs{Error: err.Trace(sourceURL)} return } + _, sourceContent, err := url2Stat(sourceURL) + if err != nil { + // if file not found it could be that user has provided a valid + // prefix, populate parent content properly and set error to nil. + // if at all the prefix doesn't exist eventually 'List' will handle + // it properly. + if os.IsNotExist(err.ToGoError()) { + // set the err back to nil consciously. + err = nil + // fill sourceContent with source client data. + sourceContent = new(client.Content) + sourceContent.URL = sourceClient.GetURL() + sourceContent.Type = os.ModeDir + } else { + // Source does not exist or insufficient privileges. + copyURLsCh <- copyURLs{Error: err.Trace(sourceURL)} + return + } + } if !sourceContent.Type.IsDir() { // Source is not a dir. diff --git a/ls.go b/ls.go index 2f7f0cf1..374d5af9 100644 --- a/ls.go +++ b/ls.go @@ -142,9 +142,21 @@ func trimContent(parentContent, childContent *client.Content, recursive bool) *c func doList(clnt client.Client, isRecursive, isIncomplete bool) *probe.Error { _, parentContent, err := url2Stat(clnt.GetURL().String()) if err != nil { - return err.Trace(clnt.GetURL().String()) + // if file not found it could be that user has provided a valid + // prefix, populate parent content properly and set error to nil. + // if at all the prefix doesn't exist eventually 'List' will handle + // it properly. + if os.IsNotExist(err.ToGoError()) { + // set the err back to nil consciously. + err = nil + // fill parentContent with input client + parentContent = new(client.Content) + parentContent.URL = clnt.GetURL() + parentContent.Type = os.ModeDir + } else { + return err.Trace(clnt.GetURL().String()) + } } - for contentCh := range clnt.List(isRecursive, isIncomplete) { if contentCh.Err != nil { switch contentCh.Err.ToGoError().(type) { diff --git a/mirror-url.go b/mirror-url.go index 35db4ca6..1a832323 100644 --- a/mirror-url.go +++ b/mirror-url.go @@ -18,6 +18,7 @@ package main import ( "fmt" + "os" "strings" "github.com/minio/cli" @@ -63,6 +64,20 @@ func checkMirrorSyntax(ctx *cli.Context) { // Recursive source URL. newSrcURL := stripRecursiveURL(srcURL) _, srcContent, err := url2Stat(newSrcURL) + if err != nil { + // if file not found it could be that user has provided a valid + // prefix, populate parent content properly and set error to nil. + // if at all the prefix doesn't exist eventually 'List' will handle + // it properly. + if os.IsNotExist(err.ToGoError()) { + // set the err back to nil consciously. + err = nil + // fill srcContent for subsequent verification. + srcContent = new(client.Content) + srcContent.URL = *client.NewURL(newSrcURL) + srcContent.Type = os.ModeDir + } + } fatalIf(err.Trace(srcURL), "Unable to stat source ‘"+newSrcURL+"’.") if !srcContent.Type.IsDir() { diff --git a/pkg/client/fs/fs.go b/pkg/client/fs/fs.go index 4e25fb28..92c3ab6b 100644 --- a/pkg/client/fs/fs.go +++ b/pkg/client/fs/fs.go @@ -230,6 +230,60 @@ func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) { fst, err := f.fsStat() if err != nil { + if os.IsNotExist(err.ToGoError()) { + dir, err := os.Open(filepath.Dir(fpath)) + if err != nil { + contentCh <- client.ContentOnChannel{ + Content: nil, + Err: probe.NewError(err), + } + } + files, err := dir.Readdir(-1) + if err != nil { + contentCh <- client.ContentOnChannel{ + Content: nil, + Err: probe.NewError(err), + } + return + } + for _, fi := range files { + file := filepath.Join(dir.Name(), fi.Name()) + if fi.Mode()&os.ModeSymlink == os.ModeSymlink { + st, err := os.Stat(file) + if err != nil { + contentCh <- client.ContentOnChannel{ + Content: nil, + Err: probe.NewError(err), + } + } + if strings.HasPrefix(file, fpath) { + contentCh <- client.ContentOnChannel{ + Content: &client.Content{ + URL: *client.NewURL(file), + Time: st.ModTime(), + Size: st.Size(), + Type: st.Mode(), + }, + Err: nil, + } + continue + } + } + if strings.HasPrefix(file, fpath) { + contentCh <- client.ContentOnChannel{ + Content: &client.Content{ + URL: *client.NewURL(file), + Time: fi.ModTime(), + Size: fi.Size(), + Type: fi.Mode(), + }, + Err: nil, + } + } + } + return + } + // if os.IsNotExit() fails we return genuine error back to the caller. contentCh <- client.ContentOnChannel{ Content: nil, Err: err.Trace(fpath),