Merge pull request #911 from harshavardhana/pr_out_handle_cases_where_prefix_exists_or_not_for_both_cast_and_cp

Handle cases where prefix exists or not for both cast and cp
This commit is contained in:
Harshavardhana
2015-07-23 06:24:33 +00:00
5 changed files with 60 additions and 62 deletions
+4 -29
View File
@@ -17,7 +17,6 @@
package main
import (
"os"
"path/filepath"
"strings"
@@ -90,31 +89,9 @@ func checkCastSyntax(ctx *cli.Context) {
switch guessCastURLType(srcURL, tgtURLs) {
case castURLsTypeA: // Source is already a regular file.
// All targets should be a valid file or folder.
for _, tgtURL := range tgtURLs {
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?
if err != nil {
console.Fatalf("Unable to stat target ‘%s’. %s\n", tgtURL, iodine.New(err, nil))
}
if !tgtContent.Type.IsRegular() {
console.Fatalf("Target ‘%s’ is not a valid file.\n", tgtURL)
}
}
//
case castURLsTypeB: // Source is already a regular file.
// All targets should be a valid file or folder.
for _, tgtURL := range tgtURLs {
_, tgtContent, err := url2Stat(tgtURL)
// Target exist?
if err != nil {
console.Fatalf("Unable to stat target ‘%s’. %s\n", tgtURL, iodine.New(err, nil))
}
if !tgtContent.Type.IsRegular() {
if !tgtContent.Type.IsDir() {
console.Fatalf("Target ‘%s’ is not a valid file or directory.\n", tgtURL)
}
}
}
//
case castURLsTypeC:
srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL)
@@ -126,6 +103,7 @@ func checkCastSyntax(ctx *cli.Context) {
if srcContent.Type.IsRegular() { // Ellipses is supported only for directories.
console.Fatalf("Source ‘%s’ is not a directory. %s\n", stripRecursiveURL(srcURL), iodine.New(err, nil))
}
default:
console.Fatalln("Invalid arguments. Unable to determine how to cast. Please report this issue at https://github.com/minio/mc/issues")
}
@@ -200,13 +178,10 @@ func prepareSingleCastURLsTypeB(sourceURL string, targetURL string) castURLs {
}
_, targetContent, err := url2Stat(targetURL)
if os.IsNotExist(iodine.ToError(err)) {
if err != nil {
// Source and target are files. Already reduced to Type A.
return prepareSingleCastURLsTypeA(sourceURL, targetURL)
}
if err != nil {
return castURLs{Error: NewIodine(iodine.New(err, nil))}
}
if targetContent.Type.IsRegular() { // File to File
// Source and target are files. Already reduced to Type A.
+1 -1
View File
@@ -220,7 +220,7 @@ func doCastCmdSession(session *sessionV2) {
if cURLs.Error == nil {
session.Header.LastCopied = cURLs.SourceContent.Name
} else {
console.Errorf("Failed to cast ‘%s’, %s\n", cURLs.SourceContent.Name, cURLs.Error)
console.Errorf("Failed to cast ‘%s’, %s\n", cURLs.SourceContent.Name, NewIodine(cURLs.Error))
}
case <-trapCh: // Receive interrupt notification.
session.Save()
+2 -3
View File
@@ -107,6 +107,7 @@ func doCopy(cpURLs copyURLs, bar *barSend, cpQueue chan bool, wg *sync.WaitGroup
if !globalQuietFlag || !globalJSONFlag {
bar.ErrorPut(length)
}
console.Println("")
console.Errorln(NewIodine(err))
}
return nil
@@ -211,9 +212,7 @@ func doCopyCmdSession(session *sessionV2) {
// runCopyCmd is bound to sub-command
func runCopyCmd(ctx *cli.Context) {
if len(ctx.Args()) < 2 || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "cp", 1) // last argument is exit code
}
checkCopySyntax(ctx)
session := newSessionV2()
defer session.Close()
+5 -2
View File
@@ -35,14 +35,17 @@ func isTargetURLDir(targetURL string) bool {
if err != nil {
return false
}
if strings.HasSuffix(targetURLParse.String(), string(targetURLParse.Separator)) {
if targetURLParse.Path == string(targetURLParse.Separator) && targetURLParse.Scheme != "" {
return false
}
if strings.HasSuffix(targetURLParse.Path, string(targetURLParse.Separator)) {
return true
}
_, targetContent, err := url2Stat(targetURL)
if err != nil {
return false
}
if !targetContent.Type.IsDir() { // Target is a dir. Type B
if !targetContent.Type.IsDir() { // Target is a dir.
return false
}
return true
+48 -27
View File
@@ -20,7 +20,9 @@ import (
"path/filepath"
"strings"
"github.com/minio/cli"
"github.com/minio/mc/pkg/client"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/iodine"
)
@@ -54,6 +56,52 @@ const (
// B: copy(d..., f)
// C: copy(*, d...)
//
func checkCopySyntax(ctx *cli.Context) {
if len(ctx.Args()) < 2 || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "cp", 1) // last argument is exit code.
}
// extract URLs.
URLs, err := args2URLs(ctx.Args())
if err != nil {
console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), iodine.New(err, nil))
}
srcURLs := URLs[:len(URLs)-1]
tgtURL := URLs[len(URLs)-1]
/****** Generic rules *******/
// Recursive URLs are not allowed in target.
if isURLRecursive(tgtURL) {
console.Fatalf("Target ‘%s’ cannot be recursive. %s\n", tgtURL, iodine.New(err, nil))
}
switch guessCopyURLType(srcURLs, tgtURL) {
case copyURLsTypeA: // Source is already a regular file.
// no verification needed, pass through
case copyURLsTypeB: // Source is already a regular file.
// no verification needed, pass through
case copyURLsTypeC:
for _, srcURL := range srcURLs {
srcURL = stripRecursiveURL(srcURL)
_, srcContent, err := url2Stat(srcURL)
// Source exist?.
if err != nil {
console.Fatalf("Unable to stat source ‘%s’. %s\n", srcURL, iodine.New(err, nil))
}
if srcContent.Type.IsRegular() { // Ellipses is supported only for directories.
console.Fatalf("Source ‘%s’ is not a directory. %s\n", stripRecursiveURL(srcURL), iodine.New(err, nil))
}
}
case copyURLsTypeD:
// only verify if target is a valid directory and exists
if !isTargetURLDir(tgtURL) {
console.Fatalf("Target ‘%s’ should be a directory and exist, when we have a mixture of files and folders in source\n", tgtURL)
}
default:
console.Fatalln("Invalid arguments. Unable to determine how to copy. Please report this issue at https://github.com/minio/mc/issues")
}
}
// guessCopyURLType guesses the type of URL. This approach all allows prepareURL
// functions to accurately report failure causes.
@@ -123,19 +171,6 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) <-chan copyURLs {
return
}
_, targetContent, err := url2Stat(targetURL)
if err != nil {
copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(err, nil))}
return
}
if err == nil {
if !targetContent.Type.IsDir() {
// Target exists, but is not a directory.
copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(errTargetIsNotDir{URL: targetURL}, nil))}
return
}
} // Else name is available to create.
// All OK.. We can proceed. Type B: source is a file, target is a directory and exists.
sourceURLParse, err := client.Parse(sourceURL)
if err != nil {
@@ -244,20 +279,6 @@ func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan copyURLs
return
}
_, targetContent, err := url2Stat(targetURL)
// Target exist?
if err != nil {
// Target does not exist.
copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(errTargetNotFound{URL: targetURL}, nil))}
return
}
if !targetContent.Type.IsDir() {
// Target exists, but is not a directory.
copyURLsCh <- copyURLs{Error: NewIodine(iodine.New(errTargetIsNotDir{URL: targetURL}, nil))}
return
}
for _, sourceURL := range sourceURLs {
// Target is directory. Possibilities are only Type B and C
// Is it a recursive URL "..."?