Merge pull request #653 from harshavardhana/pr_out_simplify_listing_recursively_into_one_function_diff_now_supports_filename_directory_style
Simplify listing recursively into one function, diff now supports <filename> <directory> style
This commit is contained in:
@@ -31,7 +31,7 @@ lint:
|
||||
|
||||
cyclo:
|
||||
@echo "Running $@:"
|
||||
@test -z "$$(gocyclo -over 15 . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)"
|
||||
@test -z "$$(gocyclo -over 16 . | grep -v Godeps/_workspace/src/ | tee /dev/stderr)"
|
||||
|
||||
deadcode:
|
||||
@echo "Running $@:"
|
||||
|
||||
+64
-135
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Minio Client (C) 2014, 2015 Minio, Inc.
|
||||
* Minio Client (C) 2015 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,10 +17,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/mc/pkg/client"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/minio/pkg/iodine"
|
||||
)
|
||||
@@ -72,142 +69,74 @@ func runDiffCmd(ctx *cli.Context) {
|
||||
console.Fatalf("Unable to parse argument [%s]. Reason: [%s].\n", secondURL, iodine.ToError(err))
|
||||
}
|
||||
}
|
||||
|
||||
doDiffCmd(firstURL, secondURL)
|
||||
// TODO recursive is not working yet
|
||||
newFirstURL := stripRecursiveURL(firstURL)
|
||||
for diff := range doDiffCmd(newFirstURL, secondURL, isURLRecursive(firstURL)) {
|
||||
if diff.err != nil {
|
||||
console.Fatalf(diff.message)
|
||||
}
|
||||
console.Infof(diff.message)
|
||||
}
|
||||
}
|
||||
|
||||
// urlJoinPath - Join a path to existing URL
|
||||
func urlJoinPath(urlStr string, path string) (newURLStr string, err error) {
|
||||
u, err := client.Parse(urlStr)
|
||||
func doDiffInRoutine(firstURL, secondURL string, recursive bool, ch chan diff) {
|
||||
defer close(ch)
|
||||
_, firstContent, err := url2Stat(firstURL)
|
||||
if err != nil {
|
||||
return "", iodine.New(err, nil)
|
||||
ch <- diff{
|
||||
message: "Failed to stat " + firstURL + ". Reason: [" + iodine.ToError(err).Error() + "]\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
_, secondContent, err := url2Stat(secondURL)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Failed to stat " + secondURL + ". Reason: [" + iodine.ToError(err).Error() + "]\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
if firstContent.Type.IsRegular() {
|
||||
switch {
|
||||
case secondContent.Type.IsDir():
|
||||
newSecondURL, err := urlJoinPath(secondURL, firstURL)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Unable to construct new URL from ‘" + secondURL + "’ using ‘" +
|
||||
firstURL + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
doDiffObjects(firstURL, newSecondURL, ch)
|
||||
case !secondContent.Type.IsRegular():
|
||||
ch <- diff{
|
||||
message: firstURL + " and " + secondURL + " differs in type.\n",
|
||||
err: nil,
|
||||
}
|
||||
return
|
||||
case secondContent.Type.IsRegular():
|
||||
doDiffObjects(firstURL, secondURL, ch)
|
||||
}
|
||||
}
|
||||
if firstContent.Type.IsDir() {
|
||||
switch {
|
||||
case !secondContent.Type.IsDir():
|
||||
ch <- diff{
|
||||
message: firstURL + " and " + secondURL + " differs in type.\n",
|
||||
err: nil,
|
||||
}
|
||||
return
|
||||
default:
|
||||
doDiffDirs(firstURL, secondURL, recursive, ch)
|
||||
}
|
||||
}
|
||||
|
||||
u.Path = filepath.Join(u.Path, path)
|
||||
newURLStr = u.String()
|
||||
return newURLStr, nil
|
||||
}
|
||||
|
||||
// doDiffCmd - Execute the diff command
|
||||
func doDiffCmd(firstURL string, secondURL string) {
|
||||
_, firstContent, err := URL2Stat(firstURL)
|
||||
if err != nil {
|
||||
console.Fatalf("Failed to stat ‘%s’. Reason: [%s].\n", firstURL, iodine.ToError(err))
|
||||
}
|
||||
|
||||
_, secondContent, err := URL2Stat(secondURL)
|
||||
if err != nil {
|
||||
console.Fatalf("Failed to stat ‘%s’. Reason: [%s].\n", secondURL, iodine.ToError(err))
|
||||
}
|
||||
|
||||
if firstContent.Type.IsRegular() {
|
||||
if !secondContent.Type.IsRegular() {
|
||||
console.Infof("‘%s’ and ‘%s’ differs in type.\n", firstURL, secondURL)
|
||||
return
|
||||
}
|
||||
doDiffCmdObjects(firstURL, secondURL)
|
||||
return
|
||||
}
|
||||
|
||||
if firstContent.Type.IsDir() {
|
||||
if !secondContent.Type.IsDir() {
|
||||
console.Infof("‘%s’ and ‘%s’ differs in type.\n", firstURL, secondURL)
|
||||
return
|
||||
}
|
||||
doDiffCmdDirs(firstURL, secondURL)
|
||||
return
|
||||
}
|
||||
|
||||
console.Fatalf("‘%s’ is of unknown type.\n", firstURL)
|
||||
}
|
||||
|
||||
// doDiffCmdObjects - Diff two object URLs
|
||||
func doDiffCmdObjects(firstURL string, secondURL string) {
|
||||
if firstURL == secondURL { // kind of lame :p
|
||||
return
|
||||
}
|
||||
_, firstContent, err := URL2Stat(firstURL)
|
||||
if err != nil {
|
||||
console.Fatalf("Failed to stat ‘%s’. Reason: [%s].\n", firstURL, iodine.ToError(err))
|
||||
}
|
||||
|
||||
_, secondContent, err := URL2Stat(secondURL)
|
||||
if err != nil {
|
||||
console.Fatalf("Failed to stat ‘%s’. Reason: [%s].\n", secondURL, iodine.ToError(err))
|
||||
}
|
||||
|
||||
if firstContent.Type.IsRegular() {
|
||||
if !secondContent.Type.IsRegular() {
|
||||
console.Infof("‘%s’ and ‘%s’ differs in type.\n", firstURL, secondURL)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
console.Fatalf("‘%s’ is not an object. Please report this bug with ‘--debug’ option\n.", firstURL)
|
||||
}
|
||||
|
||||
if firstContent.Size != secondContent.Size {
|
||||
console.Infof("‘%s’ and ‘%s’ differs in size.\n", firstURL, secondURL)
|
||||
}
|
||||
}
|
||||
|
||||
// doDiffCmdDirs - Diff two Dir URLs
|
||||
func doDiffCmdDirs(firstURL string, secondURL string) {
|
||||
firstClnt, firstContent, err := URL2Stat(firstURL)
|
||||
if err != nil {
|
||||
console.Fatalf("Failed to stat ‘%s’. Reason: [%s].\n", firstURL, iodine.ToError(err))
|
||||
}
|
||||
|
||||
_, secondContent, err := URL2Stat(secondURL)
|
||||
if err != nil {
|
||||
console.Fatalf("Failed to stat ‘%s’. Reason: [%s].\n", secondURL, iodine.ToError(err))
|
||||
}
|
||||
|
||||
if firstContent.Type.IsDir() {
|
||||
if !secondContent.Type.IsDir() {
|
||||
console.Infof("‘%s’ and ‘%s’ differs in type.\n", firstURL, secondURL)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
console.Fatalf("‘%s’ is not a directory. Please report this bug with ‘--debug’ option\n.", firstURL)
|
||||
}
|
||||
|
||||
for contentCh := range firstClnt.List() {
|
||||
if contentCh.Err != nil {
|
||||
console.Fatalf("Failed to list ‘%s’. Reason: [%s].\n", firstURL, iodine.ToError(contentCh.Err))
|
||||
}
|
||||
|
||||
newFirstURL, err := urlJoinPath(firstURL, contentCh.Content.Name)
|
||||
if err != nil {
|
||||
console.Fatalf("Unable to construct new URL from ‘%s’ using ‘%s’. Reason: [%s].\n", firstURL, contentCh.Content.Name, iodine.ToError(err))
|
||||
}
|
||||
|
||||
newSecondURL, err := urlJoinPath(secondURL, contentCh.Content.Name)
|
||||
if err != nil {
|
||||
console.Fatalf("Unable to construct new URL from ‘%s’ using ‘%s’. Reason: [%s].\n", secondURL, contentCh.Content.Name, iodine.ToError(err))
|
||||
}
|
||||
|
||||
_, newFirstContent, err := URL2Stat(newFirstURL)
|
||||
if err != nil {
|
||||
console.Fatalf("Failed to stat ‘%s’. Reason: [%s].\n", newFirstURL, iodine.ToError(err))
|
||||
}
|
||||
|
||||
_, newSecondContent, err := URL2Stat(newSecondURL)
|
||||
if err != nil {
|
||||
console.Infof("‘%s’ only in ‘%s’.\n", filepath.Base(newFirstContent.Name), firstURL)
|
||||
continue
|
||||
}
|
||||
|
||||
if newFirstContent.Type.IsDir() {
|
||||
if !newSecondContent.Type.IsDir() {
|
||||
console.Infof("‘%s’ and ‘%s’ differs in type.\n", newFirstURL, newSecondURL)
|
||||
continue
|
||||
}
|
||||
} else if newFirstContent.Type.IsRegular() {
|
||||
if !newSecondContent.Type.IsRegular() {
|
||||
console.Infof("‘%s’ and ‘%s’ differs in type.\n", newFirstURL, newSecondURL)
|
||||
continue
|
||||
}
|
||||
doDiffCmdObjects(newFirstURL, newSecondURL)
|
||||
}
|
||||
} // End of for-loop
|
||||
func doDiffCmd(firstURL, secondURL string, recursive bool) <-chan diff {
|
||||
ch := make(chan diff)
|
||||
go doDiffInRoutine(firstURL, secondURL, recursive, ch)
|
||||
return ch
|
||||
}
|
||||
|
||||
@@ -57,47 +57,24 @@ func runListCmd(ctx *cli.Context) {
|
||||
targetURLConfigMap[targetURL] = targetConfig
|
||||
}
|
||||
for targetURL, targetConfig := range targetURLConfigMap {
|
||||
if isURLRecursive(targetURL) {
|
||||
// if recursive strip off the "..."
|
||||
targetURL = stripRecursiveURL(targetURL)
|
||||
err = doListRecursiveCmd(targetURL, targetConfig, globalDebugFlag)
|
||||
err = iodine.New(err, nil)
|
||||
if err != nil {
|
||||
console.Debugln(err)
|
||||
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
|
||||
}
|
||||
} else {
|
||||
err = doListCmd(targetURL, targetConfig, globalDebugFlag)
|
||||
if err != nil {
|
||||
if err != nil {
|
||||
console.Debugln(err)
|
||||
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
|
||||
}
|
||||
}
|
||||
// if recursive strip off the "..."
|
||||
newTargetURL := stripRecursiveURL(targetURL)
|
||||
err = doListCmd(newTargetURL, targetConfig, isURLRecursive(targetURL), globalDebugFlag)
|
||||
err = iodine.New(err, nil)
|
||||
if err != nil {
|
||||
console.Debugln(err)
|
||||
console.Fatalf("Failed to list [%s]. Reason: [%s].\n", targetURL, iodine.ToError(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// doListCmd -
|
||||
func doListCmd(targetURL string, targetConfig *hostConfig, debug bool) error {
|
||||
func doListCmd(targetURL string, targetConfig *hostConfig, recursive, debug bool) error {
|
||||
clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag)
|
||||
if err != nil {
|
||||
return iodine.New(err, map[string]string{"Target": targetURL})
|
||||
}
|
||||
err = doList(clnt, targetURL)
|
||||
if err != nil {
|
||||
return iodine.New(err, nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// doListRecursiveCmd -
|
||||
func doListRecursiveCmd(targetURL string, targetConfig *hostConfig, debug bool) error {
|
||||
clnt, err := getNewClient(targetURL, targetConfig, globalDebugFlag)
|
||||
if err != nil {
|
||||
return iodine.New(err, map[string]string{"Target": targetURL})
|
||||
}
|
||||
err = doListRecursive(clnt, targetURL)
|
||||
err = doList(clnt, targetURL, recursive)
|
||||
if err != nil {
|
||||
return iodine.New(err, nil)
|
||||
}
|
||||
|
||||
+2
-2
@@ -75,8 +75,8 @@ func getNewClient(urlStr string, auth *hostConfig, debug bool) (clnt client.Clie
|
||||
return nil, iodine.New(errInvalidURL{url: urlStr}, nil)
|
||||
}
|
||||
|
||||
// URL2Stat - Returns client, config and its stat Content from the URL
|
||||
func URL2Stat(urlStr string) (client client.Client, content *client.Content, err error) {
|
||||
// url2Stat - Returns client, config and its stat Content from the URL
|
||||
func url2Stat(urlStr string) (client client.Client, content *client.Content, err error) {
|
||||
config, err := getHostConfig(urlStr)
|
||||
if err != nil {
|
||||
return nil, nil, iodine.New(err, map[string]string{"URL": urlStr})
|
||||
|
||||
@@ -306,7 +306,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan *cpURLs {
|
||||
return
|
||||
}
|
||||
|
||||
for sourceContent := range sourceClient.ListRecursive() {
|
||||
for sourceContent := range sourceClient.List(true) {
|
||||
if sourceContent.Err != nil {
|
||||
// Listing failed.
|
||||
cpURLsCh <- &cpURLs{Error: iodine.New(sourceContent.Err, nil)}
|
||||
@@ -421,7 +421,7 @@ func prepareCopyURLs(sourceURLs []string, targetURL string) <-chan *cpURLs {
|
||||
cpURLsCh <- cpURLs
|
||||
}
|
||||
default:
|
||||
cpURLsCh <- &cpURLs{Error: iodine.New(errors.New("Invalid arguments."), nil)}
|
||||
cpURLsCh <- &cpURLs{Error: iodine.New(errInvalidArgument{}, nil)}
|
||||
}
|
||||
}(sourceURLs, targetURL, cpURLsCh)
|
||||
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Minio Client (C) 2015 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/minio/mc/pkg/client"
|
||||
"github.com/minio/minio/pkg/iodine"
|
||||
)
|
||||
|
||||
type diff struct {
|
||||
message string
|
||||
err error
|
||||
}
|
||||
|
||||
// urlJoinPath Join a path to existing URL
|
||||
func urlJoinPath(url1, url2 string) (newURLStr string, err error) {
|
||||
u1, err := client.Parse(url1)
|
||||
if err != nil {
|
||||
return "", iodine.New(err, nil)
|
||||
}
|
||||
u2, err := client.Parse(url2)
|
||||
if err != nil {
|
||||
return "", iodine.New(err, nil)
|
||||
}
|
||||
u1.Path = filepath.Join(u1.Path, u2.Path)
|
||||
newURLStr = u1.String()
|
||||
return newURLStr, nil
|
||||
}
|
||||
|
||||
// doDiffObjects - Diff two object URLs
|
||||
func doDiffObjects(firstURL, secondURL string, ch chan diff) {
|
||||
_, firstContent, err := url2Stat(firstURL)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Failed to stat ‘" + firstURL + "’ " + "Reason: [" + iodine.ToError(err).Error() + "].\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_, secondContent, err := url2Stat(secondURL)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Failed to stat ‘" + secondURL + "’ " + "Reason: [" + iodine.ToError(err).Error() + "].\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if firstContent.Name == secondContent.Name {
|
||||
return
|
||||
}
|
||||
|
||||
switch {
|
||||
case firstContent.Type.IsRegular():
|
||||
if !secondContent.Type.IsRegular() {
|
||||
ch <- diff{
|
||||
message: firstURL + " and " + secondURL + " differs in type.\n",
|
||||
err: nil,
|
||||
}
|
||||
}
|
||||
default:
|
||||
ch <- diff{
|
||||
message: "‘" + firstURL + "’ is not an object. Please report this bug with ‘--debug’ option\n.",
|
||||
err: iodine.New(errNotAnObject{url: firstURL}, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if firstContent.Size != secondContent.Size {
|
||||
ch <- diff{
|
||||
message: firstURL + " and " + secondURL + " differs in size.\n",
|
||||
err: nil,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// doDiffDirs - Diff two Dir URLs
|
||||
func doDiffDirs(firstURL, secondURL string, recursive bool, ch chan diff) {
|
||||
firstClnt, firstContent, err := url2Stat(firstURL)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Failed to stat ‘" + firstURL + "’ ." + "Reason: [" + iodine.ToError(err).Error() + "].\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
_, secondContent, err := url2Stat(secondURL)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Failed to stat ‘" + secondURL + "’ ." + "Reason: [" + iodine.ToError(err).Error() + "].\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case firstContent.Type.IsDir():
|
||||
if !secondContent.Type.IsDir() {
|
||||
ch <- diff{
|
||||
message: firstURL + " and " + secondURL + " differs in type.\n",
|
||||
err: nil,
|
||||
}
|
||||
}
|
||||
default:
|
||||
ch <- diff{
|
||||
message: "‘" + firstURL + "’ is not an object. Please report this bug with ‘--debug’ option\n.",
|
||||
err: iodine.New(errNotAnObject{url: firstURL}, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
for contentCh := range firstClnt.List(recursive) {
|
||||
if contentCh.Err != nil {
|
||||
ch <- diff{
|
||||
message: "Failed to list ‘" + firstURL + "’. Reason: [" + iodine.ToError(contentCh.Err).Error() + "].\n",
|
||||
err: iodine.New(contentCh.Err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
newFirstURL, err := urlJoinPath(firstURL, contentCh.Content.Name)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Unable to construct new URL from ‘" + firstURL + "’ using ‘" +
|
||||
contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
newSecondURL, err := urlJoinPath(secondURL, contentCh.Content.Name)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Unable to construct new URL from ‘" + secondURL + "’ using ‘" +
|
||||
contentCh.Content.Name + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
_, newFirstContent, err := url2Stat(newFirstURL)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Failed to stat ‘" + newFirstURL + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
_, newSecondContent, err := url2Stat(newSecondURL)
|
||||
if err != nil {
|
||||
ch <- diff{
|
||||
message: "Failed to stat ‘" + newSecondURL + "’. Reason: [" + iodine.ToError(err).Error() + "].\n",
|
||||
err: iodine.New(err, nil),
|
||||
}
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case newFirstContent.Type.IsDir():
|
||||
if !newSecondContent.Type.IsDir() {
|
||||
ch <- diff{
|
||||
message: newFirstURL + " and " + newSecondURL + " differs in type.\n",
|
||||
err: nil,
|
||||
}
|
||||
}
|
||||
continue
|
||||
case newFirstContent.Type.IsRegular():
|
||||
if !newSecondContent.Type.IsRegular() {
|
||||
ch <- diff{
|
||||
message: newFirstURL + " and " + newSecondURL + " differs in type.\n",
|
||||
err: nil,
|
||||
}
|
||||
continue
|
||||
}
|
||||
doDiffObjects(newFirstURL, newSecondURL, ch)
|
||||
}
|
||||
} // End of for-loop
|
||||
}
|
||||
@@ -16,6 +16,14 @@
|
||||
|
||||
package main
|
||||
|
||||
type errNotAnObject struct {
|
||||
url string
|
||||
}
|
||||
|
||||
func (e errNotAnObject) Error() string {
|
||||
return "Not an object " + e.url
|
||||
}
|
||||
|
||||
type errInvalidACL struct {
|
||||
acl string
|
||||
}
|
||||
@@ -27,7 +35,7 @@ func (e errInvalidACL) Error() string {
|
||||
type errInvalidArgument struct{}
|
||||
|
||||
func (e errInvalidArgument) Error() string {
|
||||
return "Invalid argument"
|
||||
return "Invalid argument."
|
||||
}
|
||||
|
||||
type errUnsupportedScheme struct {
|
||||
|
||||
@@ -55,34 +55,21 @@ func printContent(date time.Time, v int64, name string, fileType os.FileMode) {
|
||||
}
|
||||
|
||||
// doList - list all entities inside a folder
|
||||
func doList(clnt client.Client, targetURL string) error {
|
||||
func doList(clnt client.Client, targetURL string, recursive bool) error {
|
||||
var err error
|
||||
for contentCh := range clnt.List() {
|
||||
for contentCh := range clnt.List(recursive) {
|
||||
if contentCh.Err != nil {
|
||||
err = contentCh.Err
|
||||
break
|
||||
}
|
||||
printContent(contentCh.Content.Time, contentCh.Content.Size, contentCh.Content.Name, contentCh.Content.Type)
|
||||
}
|
||||
if err != nil {
|
||||
return iodine.New(err, map[string]string{"Target": targetURL})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// doListRecursive - list all entities inside folders and sub-folders recursively
|
||||
func doListRecursive(clnt client.Client, targetURL string) error {
|
||||
var err error
|
||||
for contentCh := range clnt.ListRecursive() {
|
||||
if contentCh.Err != nil {
|
||||
err = contentCh.Err
|
||||
break
|
||||
contentName := contentCh.Content.Name
|
||||
if recursive {
|
||||
// this special handling is necessary since we are sending back absolute paths with in ListRecursive()
|
||||
// a user would not wish to see a URL just for recursive and not for regular List()
|
||||
//
|
||||
// To be consistent we have to filter them out
|
||||
contentName = strings.TrimPrefix(contentName, strings.TrimSuffix(targetURL, "/")+"/")
|
||||
}
|
||||
// this special handling is necessary since we are sending back absolute paths with in ListRecursive()
|
||||
// a user would not wish to see a URL just for recursive and not for regular List()
|
||||
//
|
||||
// To be consistent we have to filter them out
|
||||
contentName := strings.TrimPrefix(contentCh.Content.Name, strings.TrimSuffix(targetURL, "/")+"/")
|
||||
printContent(contentCh.Content.Time, contentCh.Content.Size, contentName, contentCh.Content.Type)
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
@@ -26,8 +26,7 @@ import (
|
||||
type Client interface {
|
||||
// Common operations
|
||||
Stat() (content *Content, err error)
|
||||
List() <-chan ContentOnChannel
|
||||
ListRecursive() <-chan ContentOnChannel
|
||||
List(recursive bool) <-chan ContentOnChannel
|
||||
|
||||
// Bucket operations
|
||||
MakeBucket() error
|
||||
|
||||
+9
-11
@@ -122,13 +122,18 @@ func (f *fsClient) GetObject(offset, length uint64) (io.ReadCloser, uint64, erro
|
||||
}
|
||||
|
||||
// List - list files and folders
|
||||
func (f *fsClient) List() <-chan client.ContentOnChannel {
|
||||
func (f *fsClient) List(recursive bool) <-chan client.ContentOnChannel {
|
||||
contentCh := make(chan client.ContentOnChannel)
|
||||
go f.list(contentCh)
|
||||
switch recursive {
|
||||
case true:
|
||||
go f.listRecursiveInRoutine(contentCh)
|
||||
default:
|
||||
go f.listInRoutine(contentCh)
|
||||
}
|
||||
return contentCh
|
||||
}
|
||||
|
||||
func (f *fsClient) list(contentCh chan client.ContentOnChannel) {
|
||||
func (f *fsClient) listInRoutine(contentCh chan client.ContentOnChannel) {
|
||||
defer close(contentCh)
|
||||
|
||||
fpath := f.path
|
||||
@@ -215,14 +220,7 @@ func (f *fsClient) list(contentCh chan client.ContentOnChannel) {
|
||||
}
|
||||
}
|
||||
|
||||
// ListRecursive - list files and folders recursively
|
||||
func (f *fsClient) ListRecursive() <-chan client.ContentOnChannel {
|
||||
contentCh := make(chan client.ContentOnChannel)
|
||||
go f.listRecursive(contentCh)
|
||||
return contentCh
|
||||
}
|
||||
|
||||
func (f *fsClient) listRecursive(contentCh chan client.ContentOnChannel) {
|
||||
func (f *fsClient) listRecursiveInRoutine(contentCh chan client.ContentOnChannel) {
|
||||
defer close(contentCh)
|
||||
visitFS := func(fp string, fi os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
|
||||
@@ -60,7 +60,7 @@ func (s *MySuite) TestList(c *C) {
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
var contents []*client.Content
|
||||
for contentCh := range fsc.ListRecursive() {
|
||||
for contentCh := range fsc.List(true) {
|
||||
contents = append(contents, contentCh.Content)
|
||||
}
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
+11
-13
@@ -152,7 +152,7 @@ func (c *s3Client) Stat() (*client.Content, error) {
|
||||
return nil, iodine.New(err, nil)
|
||||
}
|
||||
objectMetadata := new(client.Content)
|
||||
objectMetadata.Name = c.hostURL.String()
|
||||
objectMetadata.Name = metadata.Key
|
||||
objectMetadata.Time = metadata.LastModified
|
||||
objectMetadata.Size = metadata.Size
|
||||
objectMetadata.Type = os.FileMode(0664)
|
||||
@@ -187,21 +187,19 @@ func (c *s3Client) url2BucketAndObject() (bucketName, objectName string) {
|
||||
|
||||
/// Bucket API operations
|
||||
|
||||
// List - list at delimited path not recursive
|
||||
func (c *s3Client) List() <-chan client.ContentOnChannel {
|
||||
// List - list at delimited path, if not recursive
|
||||
func (c *s3Client) List(recursive bool) <-chan client.ContentOnChannel {
|
||||
contentCh := make(chan client.ContentOnChannel)
|
||||
go c.listInGoRoutine(contentCh)
|
||||
switch recursive {
|
||||
case true:
|
||||
go c.listRecursiveInRoutine(contentCh)
|
||||
default:
|
||||
go c.listInRoutine(contentCh)
|
||||
}
|
||||
return contentCh
|
||||
}
|
||||
|
||||
// ListRecursive - list buckets and objects recursively
|
||||
func (c *s3Client) ListRecursive() <-chan client.ContentOnChannel {
|
||||
contentCh := make(chan client.ContentOnChannel)
|
||||
go c.listRecursiveInGoRoutine(contentCh)
|
||||
return contentCh
|
||||
}
|
||||
|
||||
func (c *s3Client) listInGoRoutine(contentCh chan client.ContentOnChannel) {
|
||||
func (c *s3Client) listInRoutine(contentCh chan client.ContentOnChannel) {
|
||||
defer close(contentCh)
|
||||
bucket, object := c.url2BucketAndObject()
|
||||
switch {
|
||||
@@ -266,7 +264,7 @@ func (c *s3Client) listInGoRoutine(contentCh chan client.ContentOnChannel) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *s3Client) listRecursiveInGoRoutine(contentCh chan client.ContentOnChannel) {
|
||||
func (c *s3Client) listRecursiveInRoutine(contentCh chan client.ContentOnChannel) {
|
||||
defer close(contentCh)
|
||||
bucket, object := c.url2BucketAndObject()
|
||||
for object := range c.api.ListObjects(bucket, object, true) {
|
||||
|
||||
+2
-6
@@ -16,11 +16,7 @@
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/minio/minio/pkg/iodine"
|
||||
)
|
||||
import "github.com/minio/minio/pkg/iodine"
|
||||
|
||||
//
|
||||
// NOTE: All the parse rules should reduced to A: Copy(Source, Target).
|
||||
@@ -65,7 +61,7 @@ func prepareSyncURLs(sourceURL string, targetURLs []string) <-chan *cpURLs {
|
||||
syncURLsCh <- syncURLs
|
||||
}
|
||||
default:
|
||||
syncURLsCh <- &cpURLs{Error: iodine.New(errors.New("Invalid arguments."), nil)}
|
||||
syncURLsCh <- &cpURLs{Error: iodine.New(errInvalidArgument{}, nil)}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user