From d2c2064b2599212a3971d770206a4d709de39790 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 30 Jun 2015 15:17:04 -0700 Subject: [PATCH] Add fakeReader to sessions to populate progress bar as it builds and skips previously uploaded files --- cmd-cp.go | 6 +++--- cmd-sync.go | 2 +- pb.go | 21 +++++++++++++++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cmd-cp.go b/cmd-cp.go index dc824c16..69909069 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -79,14 +79,14 @@ func doCopySession(cURLs cpURLs, bar *barSend, s *sessionV1) error { _, ok := s.Files[cURLs.SourceContent.Name] if ok { - bar.ErrorGet(int64(cURLs.SourceContent.Size)) + putFakeTarget(bar.NewProxyReader(&fakeReader{size: cURLs.SourceContent.Size})) return nil } reader, length, err := getSource(cURLs.SourceContent.Name) if err != nil { if !globalQuietFlag { - bar.ErrorGet(int64(length)) + bar.ErrorGet(length) } return iodine.New(err, map[string]string{"URL": cURLs.SourceContent.Name}) } @@ -109,7 +109,7 @@ func doCopySession(cURLs cpURLs, bar *barSend, s *sessionV1) error { err = putTarget(cURLs.TargetContent.Name, length, newReader) if err != nil { if !globalQuietFlag { - bar.ErrorPut(int64(length)) + bar.ErrorPut(length) } return iodine.New(err, map[string]string{"URL": cURLs.TargetContent.Name}) } diff --git a/cmd-sync.go b/cmd-sync.go index 80fe69e6..afe6e71d 100644 --- a/cmd-sync.go +++ b/cmd-sync.go @@ -84,7 +84,7 @@ func doSyncSession(sURLs syncURLs, bar *barSend, syncQueue chan bool, ssCh chan _, ok := s.Files[sURLs.SourceContent.Name] if ok { if !globalQuietFlag { - bar.ErrorGet(int64(sURLs.SourceContent.Size)) + putFakeTarget(bar.NewProxyReader(&fakeReader{size: sURLs.SourceContent.Size})) } <-syncQueue // Signal that this copy routine is done. return diff --git a/pb.go b/pb.go index 7c863263..b16a54b2 100644 --- a/pb.go +++ b/pb.go @@ -18,6 +18,7 @@ package main import ( "io" + "io/ioutil" "runtime" "strings" "sync/atomic" @@ -38,6 +39,18 @@ const ( pbBarCmdSetCaption ) +type fakeReader struct { + size int64 +} + +func (r *fakeReader) Read(p []byte) (n int, err error) { + return int(r.size), io.EOF +} + +func putFakeTarget(reader io.Reader) { + io.Copy(ioutil.Discard, reader) +} + type proxyReader struct { io.Reader bar *barSend @@ -49,6 +62,10 @@ func (r *proxyReader) Read(p []byte) (n int, err error) { return } +func (b *barSend) NewProxyReader(r io.Reader) *proxyReader { + return &proxyReader{r, b} +} + type barMsg struct { Cmd pbBarCmd Arg interface{} @@ -75,10 +92,6 @@ func (b barSend) ErrorGet(size int64) { b.cmdCh <- barMsg{Cmd: pbBarCmdGetError, Arg: size} } -func (b *barSend) NewProxyReader(r io.Reader) *proxyReader { - return &proxyReader{r, b} -} - func (b *barSend) SetCaption(c string) { b.cmdCh <- barMsg{Cmd: pbBarCmdSetCaption, Arg: c} }