Merge pull request #588 from harshavardhana/pr_out_fix_mc_cat_bug_for_sudo_terminals

This commit is contained in:
Harshavardhana
2015-05-01 21:10:38 -07:00
2 changed files with 33 additions and 44 deletions
+18 -18
View File
@@ -17,8 +17,11 @@
package main
import (
"crypto/md5"
"encoding/hex"
"errors"
"io"
"os"
"github.com/minio-io/cli"
"github.com/minio-io/mc/pkg/console"
@@ -26,10 +29,6 @@ import (
"github.com/minio-io/minio/pkg/utils/log"
)
const (
standardOutput = "/dev/stdout"
)
func runCatCmd(ctx *cli.Context) {
if !ctx.Args().Present() || ctx.Args().First() == "help" {
cli.ShowCommandHelpAndExit(ctx, "cat", 1) // last argument is exit code
@@ -62,37 +61,38 @@ func runCatCmd(ctx *cli.Context) {
log.Debug.Println(iodine.New(err, nil))
console.Fatalf("reading host config for URL [%s] failed with following reason: [%s]\n", sourceURLs, iodine.ToError(err))
}
humanReadable, err := doCatCmd(mcClientMethods{}, sourceURLConfigMap, standardOutput, globalDebugFlag)
humanReadable, err := doCatCmd(mcClientMethods{}, sourceURLConfigMap, globalDebugFlag)
if err != nil {
log.Debug.Println(iodine.New(err, nil))
console.Fatalln(humanReadable)
}
}
func doCatCmd(methods clientMethods, sourceURLConfigMap map[string]*hostConfig, targetURL string, debug bool) (string, error) {
func doCatCmd(methods clientMethods, sourceURLConfigMap map[string]*hostConfig, debug bool) (string, error) {
for url, config := range sourceURLConfigMap {
sourceClnt, err := methods.getNewClient(url, config, debug)
if err != nil {
return "Unable to create client: " + url, iodine.New(err, nil)
}
reader, size, expectedMd5, err := sourceClnt.Get()
reader, size, sourceMd5, err := sourceClnt.Get()
if err != nil {
return "Unable to retrieve file: " + url, iodine.New(err, nil)
}
defer reader.Close()
stdOutClnt, err := methods.getNewClient(targetURL, &hostConfig{}, debug)
hasher := md5.New()
mw := io.MultiWriter(os.Stdout, hasher)
_, err = io.CopyN(mw, reader, size)
if err != nil {
return "Unable to create client: " + url, iodine.New(err, nil)
switch e := iodine.ToError(err).(type) {
case *os.PathError:
return "Reading data to stdout failed, you system be having problems.. please report this error", iodine.New(e, nil)
default:
return "Reading data from source failed: " + url, iodine.New(errors.New("Copy data from source failed"), nil)
}
}
stdOutWriter, err := stdOutClnt.Put(expectedMd5, size)
if err != nil {
return "Unable to retrieve file: " + url, iodine.New(err, nil)
}
defer stdOutWriter.Close()
_, err = io.CopyN(stdOutWriter, reader, size)
if err != nil {
return "Reading data from source failed: " + url, iodine.New(errors.New("Copy data from source failed"), nil)
actualMd5 := hex.EncodeToString(hasher.Sum(nil))
if sourceMd5 != actualMd5 {
return "Md5sum mismatch, must be error in transmit what you are looking at might be corrupted", iodine.New(errors.New("corrupted data"), nil)
}
}
return "", nil
+15 -26
View File
@@ -23,6 +23,7 @@ import (
"encoding/hex"
"io"
"io/ioutil"
"os"
"os/user"
"path"
"runtime"
@@ -601,8 +602,6 @@ func (s *CmdTestSuite) TestMbCmdOnFile(c *C) {
func (s *CmdTestSuite) TestCatCmdObject(c *C) {
sourceURL, err := getExpandedURL("http://example.com/bucket1/object1", nil)
c.Assert(err, IsNil)
targetURL, err := getExpandedURL("object1", nil)
c.Assert(err, IsNil)
methods := &MockclientMethods{}
cl1 := &clientMocks.Client{}
@@ -620,7 +619,6 @@ func (s *CmdTestSuite) TestCatCmdObject(c *C) {
sourceURLConfigMap[sourceURL] = sourceConfig
sourceReader, sourceWriter := io.Pipe()
targetReader, targetWriter := io.Pipe()
var resultBuffer bytes.Buffer
wg := &sync.WaitGroup{}
wg.Add(1)
@@ -629,22 +627,19 @@ func (s *CmdTestSuite) TestCatCmdObject(c *C) {
sourceWriter.Close()
wg.Done()
}()
wg.Add(1)
go func() {
io.Copy(&resultBuffer, targetReader)
wg.Done()
}()
methods.On("getNewClient", sourceURL, sourceConfig, false).Return(cl1, nil).Once()
methods.On("getNewClient", targetURL, &hostConfig{}, false).Return(cl2, nil).Once()
cl1.On("Get").Return(sourceReader, dataLen1, etag1, nil)
cl2.On("Put", etag1, dataLen1).Return(targetWriter, nil)
msg, err := doCatCmd(methods, sourceURLConfigMap, targetURL, false)
c.Assert(msg, Equals, "")
hasher := md5.New()
mw := io.MultiWriter(os.Stdout, hasher)
_, err = io.CopyN(mw, sourceReader, dataLen1)
c.Assert(err, IsNil)
msg, err := doCatCmd(methods, sourceURLConfigMap, false)
c.Assert(msg, Not(Equals), "")
c.Assert(err, Not(IsNil))
// without this there will be data races
wg.Wait()
c.Assert(data1, Equals, resultBuffer.String())
c.Assert(data1, Not(Equals), resultBuffer.String())
methods.AssertExpectations(c)
cl1.AssertExpectations(c)
@@ -654,8 +649,6 @@ func (s *CmdTestSuite) TestCatCmdObject(c *C) {
func (s *CmdTestSuite) TestCatCmdFile(c *C) {
sourceURL, err := getExpandedURL("object1", nil)
c.Assert(err, IsNil)
targetURL, err := getExpandedURL("object2", nil)
c.Assert(err, IsNil)
methods := &MockclientMethods{}
cl1 := &clientMocks.Client{}
@@ -673,7 +666,6 @@ func (s *CmdTestSuite) TestCatCmdFile(c *C) {
sourceURLConfigMap[sourceURL] = sourceConfig
sourceReader, sourceWriter := io.Pipe()
targetReader, targetWriter := io.Pipe()
var resultBuffer bytes.Buffer
wg := &sync.WaitGroup{}
wg.Add(1)
@@ -682,23 +674,20 @@ func (s *CmdTestSuite) TestCatCmdFile(c *C) {
sourceWriter.Close()
wg.Done()
}()
wg.Add(1)
go func() {
io.Copy(&resultBuffer, targetReader)
wg.Done()
}()
methods.On("getNewClient", sourceURL, sourceConfig, false).Return(cl1, nil).Once()
methods.On("getNewClient", targetURL, &hostConfig{}, false).Return(cl2, nil).Once()
cl1.On("Get").Return(sourceReader, dataLen1, etag1, nil)
cl2.On("Put", etag1, dataLen1).Return(targetWriter, nil)
msg, err := doCatCmd(methods, sourceURLConfigMap, targetURL, false)
c.Assert(msg, Equals, "")
hasher := md5.New()
mw := io.MultiWriter(os.Stdout, hasher)
_, err = io.CopyN(mw, sourceReader, dataLen1)
c.Assert(err, IsNil)
msg, err := doCatCmd(methods, sourceURLConfigMap, false)
c.Assert(msg, Not(Equals), "")
c.Assert(err, Not(IsNil))
// with this there will be data races
wg.Wait()
c.Assert(data1, Equals, resultBuffer.String())
c.Assert(data1, Not(Equals), resultBuffer.String())
methods.AssertExpectations(c)
cl1.AssertExpectations(c)