#431 Integrate GetFilesInfo function with the code

This commit is contained in:
iulius98 2022-02-21 20:41:19 +02:00
parent ce8fb796b3
commit 32a188fa85
3 changed files with 42 additions and 116 deletions

View File

@ -267,7 +267,7 @@ func send(c *cli.Context) (err error) {
crocOptions.SharedSecret = utils.GetRandomName() crocOptions.SharedSecret = utils.GetRandomName()
} }
paths, haveFolder, err := getPaths(fnames) minimalFileInfos, err := croc.GetFilesInfo(fnames)
if err != nil { if err != nil {
return return
} }
@ -280,10 +280,7 @@ func send(c *cli.Context) (err error) {
// save the config // save the config
saveConfig(c, crocOptions) saveConfig(c, crocOptions)
err = cr.Send(croc.TransferOptions{ err = cr.Send(minimalFileInfos)
PathToFiles: paths,
KeepPathInRemote: haveFolder,
})
return return
} }
@ -324,47 +321,6 @@ func makeTempFileWithString(s string) (fnames []string, err error) {
return 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) { func saveConfig(c *cli.Context, crocOptions croc.Options) {
if c.Bool("remember") { if c.Bool("remember") {

View File

@ -302,72 +302,38 @@ func GetFilesInfo(fnames []string) (filesInfo []FileInfo, err error) {
return return
} }
func (c *Client) sendCollectFiles(options TransferOptions) (err error) { func (c *Client) sendCollectFiles(filesInfo []FileInfo) (err error) {
c.FilesToTransfer = make([]FileInfo, len(options.PathToFiles)) c.FilesToTransfer = filesInfo
totalFilesSize := int64(0) 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) for i, fileInfo := range c.FilesToTransfer {
if err != nil { var fullPath string
return 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()) if fileInfo.Mode&os.ModeSymlink != 0 {
} log.Debugf("%s is symlink", fileInfo.Name)
c.FilesToTransfer[i] = FileInfo{ c.FilesToTransfer[i].Symlink, err = os.Readlink(fullPath)
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 err != nil { if err != nil {
log.Debugf("error getting symlink: %s", err.Error()) log.Debugf("error getting symlink: %s", err.Error())
} }
log.Debugf("%+v", c.FilesToTransfer[i]) log.Debugf("%+v", c.FilesToTransfer[i])
} }
if c.Options.HashAlgorithm == "" { if c.Options.HashAlgorithm == "" {
c.Options.HashAlgorithm = "xxhash" c.Options.HashAlgorithm = "xxhash"
} }
c.FilesToTransfer[i].Hash, err = utils.HashFile(fullPath, c.Options.HashAlgorithm) 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) 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 { if err != nil {
return 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]) log.Debugf("file %d info: %+v", i, c.FilesToTransfer[i])
fmt.Fprintf(os.Stderr, "\r ") fmt.Fprintf(os.Stderr, "\r ")
fmt.Fprintf(os.Stderr, "\rSending %d files (%s)", i, utils.ByteCountDecimal(totalFilesSize)) 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) time.Sleep(500 * time.Millisecond)
log.Debug("establishing connection") log.Debug("establishing connection")
var banner string var banner string
@ -472,12 +438,12 @@ func (c *Client) transferOverLocalRelay(options TransferOptions, errchan chan<-
c.Options.RelayPorts = []string{c.Options.RelayPorts[0]} c.Options.RelayPorts = []string{c.Options.RelayPorts[0]}
} }
c.ExternalIP = ipaddr c.ExternalIP = ipaddr
errchan <- c.transfer(options) errchan <- c.transfer()
} }
// Send will send the specified file // Send will send the specified file
func (c *Client) Send(options TransferOptions) (err error) { func (c *Client) Send(filesInfo []FileInfo) (err error) {
err = c.sendCollectFiles(options) err = c.sendCollectFiles(filesInfo)
if err != nil { if err != nil {
return return
} }
@ -507,7 +473,7 @@ func (c *Client) Send(options TransferOptions) (err error) {
go c.broadcastOnLocalNetwork(false) go c.broadcastOnLocalNetwork(false)
// broadcast on ipv6 // broadcast on ipv6
go c.broadcastOnLocalNetwork(true) go c.broadcastOnLocalNetwork(true)
go c.transferOverLocalRelay(options, errchan) go c.transferOverLocalRelay(errchan)
} }
if !c.Options.OnlyLocal { if !c.Options.OnlyLocal {
@ -591,7 +557,7 @@ func (c *Client) Send(options TransferOptions) (err error) {
} }
c.ExternalIP = ipaddr c.ExternalIP = ipaddr
log.Debug("exchanged header message") 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") log.Debug("exchanged header message")
fmt.Fprintf(os.Stderr, "\rsecuring channel...") fmt.Fprintf(os.Stderr, "\rsecuring channel...")
err = c.transfer(TransferOptions{}) err = c.transfer()
if err == nil { if err == nil {
if c.numberOfTransferredFiles == 0 { if c.numberOfTransferredFiles == 0 {
fmt.Fprintf(os.Stderr, "\rNo files transferred.") fmt.Fprintf(os.Stderr, "\rNo files transferred.")
@ -818,7 +784,7 @@ func (c *Client) Receive() (err error) {
return return
} }
func (c *Client) transfer(options TransferOptions) (err error) { func (c *Client) transfer() (err error) {
// connect to the server // connect to the server
// quit with c.quit <- true // quit with c.quit <- true
@ -1635,7 +1601,7 @@ func (c *Client) sendData(i int) {
n, errRead := c.fread.ReadAt(data, readingPos) n, errRead := c.fread.ReadAt(data, readingPos)
// log.Debugf("%d read %d bytes", i, n) // log.Debugf("%d read %d bytes", i, n)
readingPos += int64(n) readingPos += int64(n)
if c.limiter != nil { if (c.limiter != nil) {
r := c.limiter.ReserveN(time.Now(), n) r := c.limiter.ReserveN(time.Now(), n)
log.Debugf("Limiting Upload for %d", r.Delay()) log.Debugf("Limiting Upload for %d", r.Delay())
time.Sleep(r.Delay()) time.Sleep(r.Delay())

View File

@ -63,9 +63,11 @@ func TestCrocReadme(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Add(2)
go func() { go func() {
err := sender.Send(TransferOptions{ filesInfo, errGet := GetFilesInfo([]string{"../../README.md"})
PathToFiles: []string{"../../README.md"}, if errGet != nil {
}) t.Errorf("failed to get minimal info: %v", errGet)
}
err := sender.Send(filesInfo)
if err != nil { if err != nil {
t.Errorf("send failed: %v", err) t.Errorf("send failed: %v", err)
} }
@ -129,10 +131,11 @@ func TestCrocLocal(t *testing.T) {
os.Create("touched") os.Create("touched")
wg.Add(2) wg.Add(2)
go func() { go func() {
err := sender.Send(TransferOptions{ filesInfo, errGet := GetFilesInfo([]string{"../../LICENSE", "touched"})
PathToFiles: []string{"../../LICENSE", "touched"}, if errGet != nil {
KeepPathInRemote: false, t.Errorf("failed to get minimal info: %v", errGet)
}) }
err := sender.Send(filesInfo)
if err != nil { if err != nil {
t.Errorf("send failed: %v", err) t.Errorf("send failed: %v", err)
} }
@ -181,10 +184,11 @@ func TestCrocError(t *testing.T) {
Curve: "siec", Curve: "siec",
Overwrite: true, Overwrite: true,
}) })
err = sender.Send(TransferOptions{ filesInfo, errGet := GetFilesInfo([]string{tmpfile.Name()})
PathToFiles: []string{tmpfile.Name()}, if errGet != nil {
KeepPathInRemote: true, t.Errorf("failed to get minimal info: %v", errGet)
}) }
err = sender.Send(filesInfo)
log.Debug(err) log.Debug(err)
assert.NotNil(t, err) assert.NotNil(t, err)