fs: Make fs behave like s3 - fixes #1246

This commit is contained in:
Harshavardhana
2015-11-12 01:26:47 -08:00
parent d8d7e89a42
commit 5018b01a51
4 changed files with 119 additions and 4 deletions
+36 -2
View File
@@ -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.
+14 -2
View File
@@ -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) {
+15
View File
@@ -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() {
+54
View File
@@ -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),