From 88b4001dd77c65da7e23c3c1fe2e191d3e447844 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sat, 20 Jul 2019 08:55:54 -0700 Subject: [PATCH] bug fix: create directories for empty files --- src/croc/croc.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/croc/croc.go b/src/croc/croc.go index 2f7047e..18014ce 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -658,6 +658,7 @@ func (c *Client) updateState() (err error) { if err != nil { return } + c.Step2FileInfoTransfered = true } if !c.Options.IsSender && c.Step2FileInfoTransfered && !c.Step3RecipientRequestFile { @@ -673,6 +674,13 @@ func (c *Client) updateState() (err error) { fileHash, errHash := utils.HashFile(path.Join(fileInfo.FolderRemote, fileInfo.Name)) if fileInfo.Size == 0 { log.Debugf("touching file with folder / name") + if !utils.Exists(fileInfo.FolderRemote) { + err = os.MkdirAll(fileInfo.FolderRemote, os.ModePerm) + if err != nil { + log.Error(err) + return + } + } emptyFile, errCreate := os.Create(path.Join(fileInfo.FolderRemote, fileInfo.Name)) if errCreate != nil { log.Error(errCreate) @@ -680,6 +688,22 @@ func (c *Client) updateState() (err error) { return } emptyFile.Close() + // setup the progressbar + description := fmt.Sprintf("%-28s", c.FilesToTransfer[i].Name) + if len(c.FilesToTransfer) == 1 { + description = c.FilesToTransfer[i].Name + } + c.bar = progressbar.NewOptions64(1, + progressbar.OptionOnCompletion(func() { + fmt.Fprintf(os.Stderr, " ✔️\n") + }), + progressbar.OptionSetWidth(20), + progressbar.OptionSetDescription(description), + progressbar.OptionSetRenderBlankState(true), + progressbar.OptionSetBytes64(1), + progressbar.OptionSetWriter(os.Stderr), + ) + c.bar.Finish() continue } log.Debugf("%s %+x %+x %+v", fileInfo.Name, fileHash, fileInfo.Hash, errHash) @@ -766,8 +790,10 @@ func (c *Client) updateState() (err error) { log.Debug("converting to chunk range") c.CurrentFileChunks = utils.ChunkRangesToChunks(c.CurrentFileChunkRanges) - // setup the progressbar - c.setBar() + if !finished { + // setup the progressbar + c.setBar() + } log.Debugf("sending recipient ready with %d chunks", len(c.CurrentFileChunks)) err = message.Send(c.conn[0], c.Key, message.Message{ @@ -784,6 +810,27 @@ func (c *Client) updateState() (err error) { if !c.firstSend { fmt.Fprintf(os.Stderr, "\nSending (->%s)\n", c.ExternalIPConnected) c.firstSend = true + // if there are empty files, show them as already have been transfered now + for i := range c.FilesToTransfer { + if c.FilesToTransfer[i].Size == 0 { + // setup the progressbar and takedown the progress bar for empty files + description := fmt.Sprintf("%-28s", c.FilesToTransfer[i].Name) + if len(c.FilesToTransfer) == 1 { + description = c.FilesToTransfer[i].Name + } + c.bar = progressbar.NewOptions64(1, + progressbar.OptionOnCompletion(func() { + fmt.Fprintf(os.Stderr, " ✔️\n") + }), + progressbar.OptionSetWidth(20), + progressbar.OptionSetDescription(description), + progressbar.OptionSetRenderBlankState(true), + progressbar.OptionSetBytes64(1), + progressbar.OptionSetWriter(os.Stderr), + ) + c.bar.Finish() + } + } } c.Step4FileTransfer = true // setup the progressbar