diff --git a/cmd-sync.go b/cmd-sync.go index b93e0996..b443f313 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -109,7 +109,7 @@ func doSync(sURLs syncURLs, bar *barSend, syncQueue chan bool, wg *sync.WaitGrou if !globalQuietFlag || !globalJSONFlag { bar.ErrorPut(int64(length)) } - console.Errorf("Unable to write to one or many destinations. %s\n", err) + console.Errorf("Unable to write to one or more destinations. %s\n", err) } } return nil diff --git a/common-methods.go b/common-methods.go index f1054ae0..f6fa1457 100644 --- a/common-methods.go +++ b/common-methods.go @@ -56,40 +56,40 @@ func putTargets(targetURLs []string, length int64, reader io.Reader) <-chan erro go func() { defer close(errorCh) - tgtReaders := make([]io.ReadCloser, len(targetURLs)) - tgtWriters := make([]io.WriteCloser, len(targetURLs)) - tgtClients := make([]client.Client, len(targetURLs)) + var tgtReaders []io.ReadCloser + var tgtWriters []io.WriteCloser + var tgtClients []client.Client - for i, targetURL := range targetURLs { - tgtReaders[i], tgtWriters[i] = io.Pipe() + for _, targetURL := range targetURLs { tgtClient, err := target2Client(targetURL) if err != nil { errorCh <- NewIodine(iodine.New(err, nil)) continue } - tgtClients[i] = tgtClient + tgtClients = append(tgtClients, tgtClient) + tgtReader, tgtWriter := io.Pipe() + tgtReaders = append(tgtReaders, tgtReader) + tgtWriters = append(tgtWriters, tgtWriter) } - var wg sync.WaitGroup - writers := make([]io.Writer, len(targetURLs)) - for i, writer := range tgtWriters { - writers[i] = io.Writer(writer) - } - multiTgtWriter := io.MultiWriter(writers...) go func() { + var writers []io.Writer for _, tgtWriter := range tgtWriters { defer tgtWriter.Close() + writers = append(writers, io.Writer(tgtWriter)) } - + multiTgtWriter := io.MultiWriter(writers...) io.CopyN(multiTgtWriter, reader, length) }() + var wg sync.WaitGroup for i := range tgtClients { wg.Add(1) - go func(targetClient client.Client, reader io.Reader, errorCh chan error) { + go func(targetClient client.Client, reader io.ReadCloser, errorCh chan error) { defer wg.Done() err := targetClient.PutObject(length, reader) if err != nil { + reader.Close() errorCh <- NewIodine(iodine.New(err, map[string]string{"failedURL": targetClient.URL().String()})) } }(tgtClients[i], tgtReaders[i], errorCh) diff --git a/cp-url.go b/cp-url.go index 9c38b242..53c6ac89 100644 --- a/cp-url.go +++ b/cp-url.go @@ -117,7 +117,7 @@ func guessCopyURLType(sourceURLs []string, targetURL string) copyURLsType { // SINGLE SOURCE - Type A: copy(f, f) -> copy(f, f) // prepareCopyURLsTypeA - prepares target and source URLs for copying. func prepareCopyURLsTypeA(sourceURL string, targetURL string) <-chan copyURLs { - copyURLsCh := make(chan copyURLs, 10000) + copyURLsCh := make(chan copyURLs) go func(sourceURL, targetURL string, copyURLsCh chan copyURLs) { defer close(copyURLsCh) _, sourceContent, err := url2Stat(sourceURL) @@ -154,7 +154,7 @@ func prepareCopyURLsTypeA(sourceURL string, targetURL string) <-chan copyURLs { // SINGLE SOURCE - Type B: copy(f, d) -> copy(f, d/f) -> A // prepareCopyURLsTypeB - prepares target and source URLs for copying. func prepareCopyURLsTypeB(sourceURL string, targetURL string) <-chan copyURLs { - copyURLsCh := make(chan copyURLs, 10000) + copyURLsCh := make(chan copyURLs) go func(sourceURL, targetURL string, copyURLsCh chan copyURLs) { defer close(copyURLsCh) _, sourceContent, err := url2Stat(sourceURL) @@ -207,7 +207,7 @@ func prepareCopyURLsTypeB(sourceURL string, targetURL string) <-chan copyURLs { // SINGLE SOURCE - Type C: copy(d1..., d2) -> []copy(d1/f, d1/d2/f) -> []A // prepareCopyRecursiveURLTypeC - prepares target and source URLs for copying. func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { - copyURLsCh := make(chan copyURLs, 10000) + copyURLsCh := make(chan copyURLs) go func(sourceURL, targetURL string, copyURLsCh chan copyURLs) { defer close(copyURLsCh) if !isURLRecursive(sourceURL) { @@ -295,7 +295,7 @@ func prepareCopyURLsTypeC(sourceURL, targetURL string) <-chan copyURLs { // MULTI-SOURCE - Type D: copy([]f, d) -> []B // prepareCopyURLsTypeD - prepares target and source URLs for copying. func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan copyURLs { - copyURLsCh := make(chan copyURLs, 10000) + copyURLsCh := make(chan copyURLs) go func(sourceURLs []string, targetURL string, copyURLsCh chan copyURLs) { defer close(copyURLsCh) @@ -339,7 +339,7 @@ func prepareCopyURLsTypeD(sourceURLs []string, targetURL string) <-chan copyURLs // prepareCopyURLs - prepares target and source URLs for copying. func prepareCopyURLs(sourceURLs []string, targetURL string) <-chan copyURLs { - copyURLsCh := make(chan copyURLs, 10000) + copyURLsCh := make(chan copyURLs) go func(sourceURLs []string, targetURL string, copyURLsCh chan copyURLs) { defer close(copyURLsCh) switch guessCopyURLType(sourceURLs, targetURL) { diff --git a/sync-url.go b/sync-url.go index 16ce428d..7da50ffe 100644 --- a/sync-url.go +++ b/sync-url.go @@ -24,28 +24,6 @@ import ( "github.com/minio/minio/pkg/iodine" ) -// -// NOTE: All the parse rules should reduced to A: Copy(Source, Target). -// -// * SINGLE SOURCE - VALID -// ======================= -// A: sync(f, f) -> copy(f, f) -// B: sync(f, d) -> copy(f, d/f) -> A -// C: sync(f, []d) -> []copy(f, d/f) -> []A -// sync(d1..., d2) -> []copy(d1/f, d2/d1/f) -> []A -// sync(d1..., []d2) -> [][]copy(d1/f, d2/d1/f) -> [][]A -// -// * SINGLE SOURCE - INVALID -// ========================= -// sync(d, *) -// sync(d..., f) -// sync(*, d...) -// -// * MULTI-TARGET RECURSIVE - INVALID -// ================================== -// sync(*, f1) -// sync(*, []f1) - type syncURLs struct { SourceContent *client.Content TargetContents []*client.Content @@ -68,79 +46,150 @@ func (s syncURLs) IsEmpty() bool { return empty } -type syncURLsType copyURLsType +type syncURLsType uint8 + +const ( + syncURLsTypeInvalid syncURLsType = iota + syncURLsTypeA + syncURLsTypeB + syncURLsTypeC + syncURLsTypeD +) + +// NOTE: All the parse rules should reduced to A: Sync(Source, []Target). +// +// * SYNC ARGS - VALID CASES +// ========================= +// A: sync(f, []f) -> sync(f, []f) +// B: sync(f, [](d | f)) -> sync(f, [](d/f | f)) -> A: +// C: sync(d1..., [](d2 | f)) -> []sync(d1/f, [](d1/d2/f | d1/f)) -> []A: +// +// * SYNC ARGS - INVALID CASES +// =========================== +// sync(d, *) +// sync(d..., f) +// sync(*, d...) // guessSyncURLType guesses the type of URL. This approach all allows prepareURL // functions to accurately report failure causes. func guessSyncURLType(sourceURL string, targetURLs []string) syncURLsType { - if targetURLs == nil { // Target is empty - return syncURLsType(copyURLsTypeInvalid) + if targetURLs == nil || len(targetURLs) == 0 { // Target is empty + return syncURLsTypeInvalid } - if sourceURL == "" { // Source list is empty - return syncURLsType(copyURLsTypeInvalid) + if sourceURL == "" { // Source is empty + return syncURLsTypeInvalid } + for _, targetURL := range targetURLs { + if targetURL == "" { // One of the target is empty + return syncURLsTypeInvalid + } + } + if isURLRecursive(sourceURL) { // Type C - return syncURLsType(copyURLsTypeC) + return syncURLsTypeC } // else Type A or Type B for _, targetURL := range targetURLs { if isTargetURLDir(targetURL) { // Type B - return syncURLsType(copyURLsTypeB) + return syncURLsTypeB } } // else Type A - return syncURLsType(copyURLsTypeA) + return syncURLsTypeA } -// prepareSyncURLsTypeA - A: sync(f, f) -> copy(f, f) -func prepareSyncURLsTypeA(sourceURL string, targetURLs []string) <-chan syncURLs { - syncURLsCh := make(chan syncURLs, 10000) - go func() { - defer close(syncURLsCh) - var sURLs syncURLs - for _, targetURL := range targetURLs { - var cURLs copyURLs - for cURLs = range prepareCopyURLsTypeA(sourceURL, targetURL) { - if cURLs.Error != nil { - syncURLsCh <- syncURLs{Error: NewIodine(iodine.New(cURLs.Error, nil))} - continue - } - } - sURLs.SourceContent = cURLs.SourceContent - sURLs.TargetContents = append(sURLs.TargetContents, cURLs.TargetContent) +// prepareSingleSyncURLTypeA - prepares a single source and single target argument for syncing. +func prepareSingleSyncURLsTypeA(sourceURL string, targetURL string) syncURLs { + _, sourceContent, err := url2Stat(sourceURL) + if err != nil { // Source does not exist or insufficient privileges. + return syncURLs{Error: NewIodine(iodine.New(err, nil))} + } + if !sourceContent.Type.IsRegular() { // Source is not a regular file + return syncURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))} + } + targetClient, err := target2Client(targetURL) + if err != nil { + return syncURLs{Error: NewIodine(iodine.New(err, nil))} + } + // Target exists? + targetContent, err := targetClient.Stat() + if err == nil { // Target exists. + if !targetContent.Type.IsRegular() { // Target is not a regular file + return syncURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))} } - if !sURLs.IsEmpty() { - syncURLsCh <- sURLs - } - }() - return syncURLsCh + } + // All OK.. We can proceed. Type A + sourceContent.Name = sourceURL + return syncURLs{SourceContent: sourceContent, TargetContents: []*client.Content{{Name: targetURL}}} } -// prepareSyncURLsTypeB - B: sync(f, d) -> copy(f, d/f) -> A -func prepareSyncURLsTypeB(sourceURL string, targetURLs []string) <-chan syncURLs { - syncURLsCh := make(chan syncURLs, 10000) - go func() { - defer close(syncURLsCh) - var sURLs syncURLs - for _, targetURL := range targetURLs { - var cURLs copyURLs - for cURLs = range prepareCopyURLsTypeB(sourceURL, targetURL) { - if cURLs.Error != nil { - syncURLsCh <- syncURLs{Error: NewIodine(iodine.New(cURLs.Error, nil))} - continue - } - } - sURLs.SourceContent = cURLs.SourceContent - sURLs.TargetContents = append(sURLs.TargetContents, cURLs.TargetContent) +// prepareSyncURLsTypeA - A: sync(f, f) -> sync(f, f) +func prepareSyncURLsTypeA(sourceURL string, targetURLs []string) syncURLs { + var sURLs syncURLs + for _, targetURL := range targetURLs { // Prepare each target separately + URLs := prepareSingleSyncURLsTypeA(sourceURL, targetURL) + if URLs.Error != nil { + return syncURLs{Error: NewIodine(iodine.New(URLs.Error, nil))} } - if !sURLs.IsEmpty() { - syncURLsCh <- sURLs - } - }() - return syncURLsCh + sURLs.SourceContent = URLs.SourceContent + sURLs.TargetContents = append(sURLs.TargetContents, URLs.TargetContents...) + } + return sURLs } -// prepareSyncURLsTypeC - C: sync(f, []d) -> []copy(f, d/f) -> []A +// prepareSingleSyncURLsTypeB - prepares a single target and single source URLs for syncing. +func prepareSingleSyncURLsTypeB(sourceURL string, targetURL string) syncURLs { + _, sourceContent, err := url2Stat(sourceURL) + if err != nil { + // Source does not exist or insufficient privileges. + return syncURLs{Error: NewIodine(iodine.New(err, nil))} + } + + if !sourceContent.Type.IsRegular() { + // Source is not a regular file. + return syncURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))} + } + + _, targetContent, err := url2Stat(targetURL) + if err != nil { + return syncURLs{Error: NewIodine(iodine.New(err, nil))} + } + + if targetContent.Type.IsRegular() { // File to File + // Source and target are files. Already reduced to Type A. + return prepareSingleSyncURLsTypeA(sourceURL, targetURL) + } + + // Source is a file, target is a directory and exists. + sourceURLParse, err := client.Parse(sourceURL) + if err != nil { + return syncURLs{Error: NewIodine(iodine.New(errInvalidSource{URL: sourceURL}, nil))} + } + + targetURLParse, err := client.Parse(targetURL) + if err != nil { + return syncURLs{Error: NewIodine(iodine.New(errInvalidTarget{URL: targetURL}, nil))} + } + // Reduce Type B to Type A. + targetURLParse.Path = filepath.Join(targetURLParse.Path, filepath.Base(sourceURLParse.Path)) + return prepareSingleSyncURLsTypeA(sourceURL, targetURLParse.String()) +} + +// prepareSyncURLsTypeB - B: sync(f, d) -> sync(f, d/f) -> A +func prepareSyncURLsTypeB(sourceURL string, targetURLs []string) syncURLs { + var sURLs syncURLs + for _, targetURL := range targetURLs { + URLs := prepareSingleSyncURLsTypeB(sourceURL, targetURL) + if URLs.Error != nil { + return syncURLs{Error: NewIodine(iodine.New(URLs.Error, nil))} + } + sURLs.SourceContent = URLs.SourceContent + sURLs.TargetContents = append(sURLs.TargetContents, URLs.TargetContents[0]) + } + return sURLs +} + +// prepareSyncURLsTypeC - C: sync(f, []d) -> []sync(f, d/f) -> []A func prepareSyncURLsTypeC(sourceURL string, targetURLs []string) <-chan syncURLs { - syncURLsCh := make(chan syncURLs, 10000) + syncURLsCh := make(chan syncURLs) go func() { defer close(syncURLsCh) if !isURLRecursive(sourceURL) { @@ -164,6 +213,7 @@ func prepareSyncURLsTypeC(sourceURL string, targetURLs []string) <-chan syncURLs return } + // Type C requires all targets to be a dir and it should exist. for _, targetURL := range targetURLs { _, targetContent, err := url2Stat(targetURL) // Target exist? @@ -179,6 +229,7 @@ func prepareSyncURLsTypeC(sourceURL string, targetURLs []string) <-chan syncURLs return } } + for sourceContent := range sourceClient.List(true) { if sourceContent.Err != nil { // Listing failed. @@ -186,7 +237,7 @@ func prepareSyncURLsTypeC(sourceURL string, targetURLs []string) <-chan syncURLs continue } if !sourceContent.Content.Type.IsRegular() { - // Source is not a regular file. Skip it for copy. + // Source is not a regular file. Skip it for sync. continue } // All OK.. We can proceed. Type B: source is a file, target is a directory and exists. @@ -217,29 +268,23 @@ func prepareSyncURLsTypeC(sourceURL string, targetURLs []string) <-chan syncURLs newTargetURLParse.Path = filepath.Join(newTargetURLParse.Path, sourceContentName) newTargetURLs = append(newTargetURLs, newTargetURLParse.String()) } - for sURLs := range prepareSyncURLsTypeA(sourceContentParse.String(), newTargetURLs) { - syncURLsCh <- sURLs - } + syncURLsCh <- prepareSyncURLsTypeA(sourceContentParse.String(), newTargetURLs) } }() return syncURLsCh } -// prepareCopyURLs - prepares target and source URLs for syncing. +// prepareSyncURLs - prepares target and source URLs for syncing. func prepareSyncURLs(sourceURL string, targetURLs []string) <-chan syncURLs { - syncURLsCh := make(chan syncURLs, 10000) + syncURLsCh := make(chan syncURLs) go func() { defer close(syncURLsCh) switch guessSyncURLType(sourceURL, targetURLs) { - case syncURLsType(copyURLsTypeA): - for sURLs := range prepareSyncURLsTypeA(sourceURL, targetURLs) { - syncURLsCh <- sURLs - } - case syncURLsType(copyURLsTypeB): - for sURLs := range prepareSyncURLsTypeB(sourceURL, targetURLs) { - syncURLsCh <- sURLs - } - case syncURLsType(copyURLsTypeC): + case syncURLsType(syncURLsTypeA): + syncURLsCh <- prepareSyncURLsTypeA(sourceURL, targetURLs) + case syncURLsType(syncURLsTypeB): + syncURLsCh <- prepareSyncURLsTypeB(sourceURL, targetURLs) + case syncURLsType(syncURLsTypeC): for sURLs := range prepareSyncURLsTypeC(sourceURL, targetURLs) { syncURLsCh <- sURLs }