From 32a188fa85f2fe49bad391a39476c4300e67c443 Mon Sep 17 00:00:00 2001 From: iulius98 Date: Mon, 21 Feb 2022 20:41:19 +0200 Subject: [PATCH] #431 Integrate GetFilesInfo function with the code --- src/cli/cli.go | 48 ++----------------------- src/croc/croc.go | 84 +++++++++++++------------------------------ src/croc/croc_test.go | 26 ++++++++------ 3 files changed, 42 insertions(+), 116 deletions(-) diff --git a/src/cli/cli.go b/src/cli/cli.go index 1923b0f..65fef58 100644 --- a/src/cli/cli.go +++ b/src/cli/cli.go @@ -267,7 +267,7 @@ func send(c *cli.Context) (err error) { crocOptions.SharedSecret = utils.GetRandomName() } - paths, haveFolder, err := getPaths(fnames) + minimalFileInfos, err := croc.GetFilesInfo(fnames) if err != nil { return } @@ -280,10 +280,7 @@ func send(c *cli.Context) (err error) { // save the config saveConfig(c, crocOptions) - err = cr.Send(croc.TransferOptions{ - PathToFiles: paths, - KeepPathInRemote: haveFolder, - }) + err = cr.Send(minimalFileInfos) return } @@ -324,47 +321,6 @@ func makeTempFileWithString(s string) (fnames []string, err error) { return } -func getPaths(fnames []string) (paths []string, haveFolder bool, err error) { - haveFolder = false - paths = []string{} - for _, fname := range fnames { - // Support wildcard - if strings.Contains(fname, "*") { - matches, errGlob := filepath.Glob(fname) - if errGlob != nil { - err = errGlob - return - } - paths = append(paths, matches...) - continue - } - - stat, errStat := os.Lstat(fname) - if errStat != nil { - err = errStat - return - } - if stat.IsDir() { - haveFolder = true - err = filepath.Walk(fname, - func(pathName string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if !info.IsDir() { - paths = append(paths, filepath.ToSlash(pathName)) - } - return nil - }) - if err != nil { - return - } - } else { - paths = append(paths, filepath.ToSlash(fname)) - } - } - return -} func saveConfig(c *cli.Context, crocOptions croc.Options) { if c.Bool("remember") { diff --git a/src/croc/croc.go b/src/croc/croc.go index 1afa5c7..2cdedb5 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -302,72 +302,38 @@ func GetFilesInfo(fnames []string) (filesInfo []FileInfo, err error) { return } -func (c *Client) sendCollectFiles(options TransferOptions) (err error) { - c.FilesToTransfer = make([]FileInfo, len(options.PathToFiles)) +func (c *Client) sendCollectFiles(filesInfo []FileInfo) (err error) { + c.FilesToTransfer = filesInfo totalFilesSize := int64(0) - for i, pathToFile := range options.PathToFiles { - var fstats os.FileInfo - var fullPath string - fullPath, err = filepath.Abs(pathToFile) - if err != nil { - return - } - fullPath = filepath.Clean(fullPath) - var folderName string - folderName, _ = filepath.Split(fullPath) - fstats, err = os.Lstat(fullPath) - if err != nil { - return + for i, fileInfo := range c.FilesToTransfer { + var fullPath string + fullPath = fileInfo.FolderSource + string(os.PathSeparator) + fileInfo.Name + fullPath = filepath.Clean(fullPath) + + if len(fileInfo.Name) > c.longestFilename { + c.longestFilename = len(fileInfo.Name) } - if len(fstats.Name()) > c.longestFilename { - c.longestFilename = len(fstats.Name()) - } - c.FilesToTransfer[i] = FileInfo{ - Name: fstats.Name(), - FolderRemote: ".", - FolderSource: folderName, - Size: fstats.Size(), - ModTime: fstats.ModTime(), - } - if fstats.Mode()&os.ModeSymlink != 0 { - log.Debugf("%s is symlink", fstats.Name()) - c.FilesToTransfer[i].Symlink, err = os.Readlink(pathToFile) + + if fileInfo.Mode&os.ModeSymlink != 0 { + log.Debugf("%s is symlink", fileInfo.Name) + c.FilesToTransfer[i].Symlink, err = os.Readlink(fullPath) if err != nil { log.Debugf("error getting symlink: %s", err.Error()) } log.Debugf("%+v", c.FilesToTransfer[i]) } + if c.Options.HashAlgorithm == "" { c.Options.HashAlgorithm = "xxhash" } + c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath, c.Options.HashAlgorithm) log.Debugf("hashed %s to %x using %s", fullPath, c.FilesToTransfer[i].Hash, c.Options.HashAlgorithm) - totalFilesSize += fstats.Size() + totalFilesSize += fileInfo.Size if err != nil { return } - if options.KeepPathInRemote { - var curFolder string - curFolder, err = os.Getwd() - if err != nil { - return - } - curFolder, err = filepath.Abs(curFolder) - if err != nil { - return - } - if !strings.HasPrefix(folderName, curFolder) { - err = fmt.Errorf("remote directory must be relative to current") - return - } - c.FilesToTransfer[i].FolderRemote = strings.TrimPrefix(folderName, curFolder) - c.FilesToTransfer[i].FolderRemote = filepath.ToSlash(c.FilesToTransfer[i].FolderRemote) - c.FilesToTransfer[i].FolderRemote = strings.TrimPrefix(c.FilesToTransfer[i].FolderRemote, "/") - if c.FilesToTransfer[i].FolderRemote == "" { - c.FilesToTransfer[i].FolderRemote = "." - } - } log.Debugf("file %d info: %+v", i, c.FilesToTransfer[i]) fmt.Fprintf(os.Stderr, "\r ") fmt.Fprintf(os.Stderr, "\rSending %d files (%s)", i, utils.ByteCountDecimal(totalFilesSize)) @@ -440,7 +406,7 @@ func (c *Client) broadcastOnLocalNetwork(useipv6 bool) { } } -func (c *Client) transferOverLocalRelay(options TransferOptions, errchan chan<- error) { +func (c *Client) transferOverLocalRelay(errchan chan<- error) { time.Sleep(500 * time.Millisecond) log.Debug("establishing connection") var banner string @@ -472,12 +438,12 @@ func (c *Client) transferOverLocalRelay(options TransferOptions, errchan chan<- c.Options.RelayPorts = []string{c.Options.RelayPorts[0]} } c.ExternalIP = ipaddr - errchan <- c.transfer(options) + errchan <- c.transfer() } // Send will send the specified file -func (c *Client) Send(options TransferOptions) (err error) { - err = c.sendCollectFiles(options) +func (c *Client) Send(filesInfo []FileInfo) (err error) { + err = c.sendCollectFiles(filesInfo) if err != nil { return } @@ -507,7 +473,7 @@ func (c *Client) Send(options TransferOptions) (err error) { go c.broadcastOnLocalNetwork(false) // broadcast on ipv6 go c.broadcastOnLocalNetwork(true) - go c.transferOverLocalRelay(options, errchan) + go c.transferOverLocalRelay(errchan) } if !c.Options.OnlyLocal { @@ -591,7 +557,7 @@ func (c *Client) Send(options TransferOptions) (err error) { } c.ExternalIP = ipaddr log.Debug("exchanged header message") - errchan <- c.transfer(options) + errchan <- c.transfer() }() } @@ -809,7 +775,7 @@ func (c *Client) Receive() (err error) { } log.Debug("exchanged header message") fmt.Fprintf(os.Stderr, "\rsecuring channel...") - err = c.transfer(TransferOptions{}) + err = c.transfer() if err == nil { if c.numberOfTransferredFiles == 0 { fmt.Fprintf(os.Stderr, "\rNo files transferred.") @@ -818,7 +784,7 @@ func (c *Client) Receive() (err error) { return } -func (c *Client) transfer(options TransferOptions) (err error) { +func (c *Client) transfer() (err error) { // connect to the server // quit with c.quit <- true @@ -1635,7 +1601,7 @@ func (c *Client) sendData(i int) { n, errRead := c.fread.ReadAt(data, readingPos) // log.Debugf("%d read %d bytes", i, n) readingPos += int64(n) - if c.limiter != nil { + if (c.limiter != nil) { r := c.limiter.ReserveN(time.Now(), n) log.Debugf("Limiting Upload for %d", r.Delay()) time.Sleep(r.Delay()) diff --git a/src/croc/croc_test.go b/src/croc/croc_test.go index 140ab63..5c61ef2 100644 --- a/src/croc/croc_test.go +++ b/src/croc/croc_test.go @@ -63,9 +63,11 @@ func TestCrocReadme(t *testing.T) { var wg sync.WaitGroup wg.Add(2) go func() { - err := sender.Send(TransferOptions{ - PathToFiles: []string{"../../README.md"}, - }) + filesInfo, errGet := GetFilesInfo([]string{"../../README.md"}) + if errGet != nil { + t.Errorf("failed to get minimal info: %v", errGet) + } + err := sender.Send(filesInfo) if err != nil { t.Errorf("send failed: %v", err) } @@ -129,10 +131,11 @@ func TestCrocLocal(t *testing.T) { os.Create("touched") wg.Add(2) go func() { - err := sender.Send(TransferOptions{ - PathToFiles: []string{"../../LICENSE", "touched"}, - KeepPathInRemote: false, - }) + filesInfo, errGet := GetFilesInfo([]string{"../../LICENSE", "touched"}) + if errGet != nil { + t.Errorf("failed to get minimal info: %v", errGet) + } + err := sender.Send(filesInfo) if err != nil { t.Errorf("send failed: %v", err) } @@ -181,10 +184,11 @@ func TestCrocError(t *testing.T) { Curve: "siec", Overwrite: true, }) - err = sender.Send(TransferOptions{ - PathToFiles: []string{tmpfile.Name()}, - KeepPathInRemote: true, - }) + filesInfo, errGet := GetFilesInfo([]string{tmpfile.Name()}) + if errGet != nil { + t.Errorf("failed to get minimal info: %v", errGet) + } + err = sender.Send(filesInfo) log.Debug(err) assert.NotNil(t, err)