Handle permission errors for progress bar

This commit is contained in:
Harshavardhana
2015-05-28 11:07:20 -07:00
parent 87ce028ccc
commit 9da87f5113
4 changed files with 37 additions and 19 deletions
+9 -5
View File
@@ -33,8 +33,13 @@ import (
func doCopy(sourceURL string, sourceConfig *hostConfig, targetURL string, targetConfig *hostConfig, bar *barSend) error {
reader, length, err := getSource(sourceURL, sourceConfig)
if err != nil {
if !globalQuietFlag {
bar.ErrorGet(int64(length))
}
return iodine.New(err, nil)
}
defer reader.Close()
var newReader io.Reader
switch globalQuietFlag {
case true:
@@ -44,12 +49,14 @@ func doCopy(sourceURL string, sourceConfig *hostConfig, targetURL string, target
// set up progress
newReader = bar.NewProxyReader(reader)
}
err = putTarget(targetURL, targetConfig, length, newReader)
if err != nil {
reader.Close()
if !globalQuietFlag {
bar.ErrorPut(int64(length))
}
return iodine.New(err, nil)
}
reader.Close()
return nil
}
@@ -174,9 +181,6 @@ func runCopyCmd(ctx *cli.Context) {
Message: "Failed with",
Error: iodine.New(err, nil),
})
if !globalQuietFlag {
bar.Error()
}
}
}
if !globalQuietFlag {
+19 -10
View File
@@ -31,7 +31,8 @@ const (
cpBarCmdExtend cpBarCmd = iota
cpBarCmdProgress
cpBarCmdFinish
cpBarCmdError
cpBarCmdPutError
cpBarCmdGetError
)
type copyReader struct {
@@ -63,8 +64,12 @@ func (b barSend) progress(progress int64) {
b.cmdCh <- barMsg{Cmd: cpBarCmdProgress, Arg: progress}
}
func (b barSend) Error() {
b.cmdCh <- barMsg{Cmd: cpBarCmdError}
func (b barSend) ErrorPut(size int64) {
b.cmdCh <- barMsg{Cmd: cpBarCmdPutError, Arg: size}
}
func (b barSend) ErrorGet(size int64) {
b.cmdCh <- barMsg{Cmd: cpBarCmdGetError, Arg: size}
}
func (b *barSend) NewProxyReader(r io.Reader) *copyReader {
@@ -83,8 +88,7 @@ func newCpBar() barSend {
finishCh := make(chan bool)
go func(cmdCh <-chan barMsg, finishCh chan<- bool) {
started := false
var totalBytesRead int64
var currentSizeRead int64
var totalBytesRead int64 // total amounts of bytes copied
bar := pb.New64(0)
bar.SetUnits(pb.U_BYTES)
bar.SetRefreshRate(time.Millisecond * 10)
@@ -106,12 +110,17 @@ func newCpBar() barSend {
}
case cpBarCmdProgress:
if msg.Arg.(int64) > 0 {
currentSizeRead = msg.Arg.(int64)
totalBytesRead += currentSizeRead
bar.Add64(currentSizeRead)
totalBytesRead += msg.Arg.(int64)
bar.Add64(msg.Arg.(int64))
}
case cpBarCmdPutError:
if totalBytesRead > msg.Arg.(int64) {
bar.Set64(totalBytesRead - msg.Arg.(int64))
}
case cpBarCmdGetError:
if msg.Arg.(int64) > 0 {
bar.Add64(msg.Arg.(int64))
}
case cpBarCmdError:
bar.Set64(totalBytesRead - currentSizeRead)
case cpBarCmdFinish:
if started {
bar.Finish()
+8 -3
View File
@@ -76,12 +76,14 @@ func (f *fsClient) fsStat() (os.FileInfo, error) {
func (f *fsClient) get(content *client.Content) (io.ReadCloser, uint64, error) {
body, err := os.Open(f.path)
if err != nil {
return nil, 0, iodine.New(err, nil)
return nil, uint64(content.Size), iodine.New(err, nil)
}
return body, uint64(content.Size), nil
}
// GetObject download an full or part object from bucket
// getobject returns a reader, length and nil for no errors
// with errors getobject will return nil reader, length and typed errors
func (f *fsClient) GetObject(offset, length uint64) (io.ReadCloser, uint64, error) {
content, err := f.getFSMetadata()
if err != nil {
@@ -106,17 +108,20 @@ func (f *fsClient) GetObject(offset, length uint64) (io.ReadCloser, uint64, erro
if os.IsNotExist(err) {
return nil, 0, iodine.New(NotFound{path: f.path}, nil)
}
if err != nil {
return nil, 0, iodine.New(err, nil)
}
if offset == 0 && length == 0 {
return f.get(content)
}
body, err := os.Open(f.path)
if err != nil {
return nil, 0, iodine.New(err, nil)
return nil, length, iodine.New(err, nil)
}
_, err = io.CopyN(ioutil.Discard, body, int64(offset))
if err != nil {
return nil, 0, iodine.New(err, nil)
return nil, length, iodine.New(err, nil)
}
return body, length, nil
}
+1 -1
View File
@@ -102,7 +102,7 @@ func (c *s3Client) GetObject(offset, length uint64) (io.ReadCloser, uint64, erro
bucket, object := c.url2BucketAndObject()
reader, metadata, err := c.api.GetObject(bucket, object, offset, length)
if err != nil {
return nil, 0, iodine.New(err, nil)
return nil, length, iodine.New(err, nil)
}
return reader, uint64(metadata.Size), nil
}