0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

#431 added file informations retrival functions to replace getPaths

This commit is contained in:
RCL98 2022-02-21 20:16:27 +02:00
parent 1a635de69c
commit ce8fb796b3

View file

@ -16,6 +16,7 @@ import (
"strings"
"sync"
"time"
"golang.org/x/time/rate"
"github.com/denisbrodbeck/machineid"
@ -141,6 +142,7 @@ type FileInfo struct {
IsCompressed bool `json:"c,omitempty"`
IsEncrypted bool `json:"e,omitempty"`
Symlink string `json:"sy,omitempty"`
Mode os.FileMode `json:"md,omitempty"`
}
// RemoteFileRequest requests specific bytes
@ -201,7 +203,7 @@ func New(ops Options) (c *Client, err error) {
}
// Somehow 4* is neccessary
rt = rate.Every(time.Second / (4 * time.Duration(uploadLimit)))
if (int(uploadLimit) > minBurstSize) {
if int(uploadLimit) > minBurstSize {
minBurstSize = int(uploadLimit)
}
c.limiter = rate.NewLimiter(rt, minBurstSize)
@ -226,6 +228,80 @@ type TransferOptions struct {
KeepPathInRemote bool
}
// This function retrives the important file informations
// for every file that will be transfered
func GetFilesInfo(fnames []string) (filesInfo []FileInfo, err error) {
// fnames: the relativ/absolute paths of files/folders that will be transfered
var 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
} else {
paths = append(paths, fname)
}
}
for _, path := range paths {
stat, errStat := os.Lstat(path)
if errStat != nil {
err = errStat
return
}
absPath, errAbs := filepath.Abs(path)
absPath = filepath.ToSlash(absPath)
if errAbs != nil {
err = errAbs
return
}
if stat.IsDir() {
err = filepath.Walk(absPath,
func(pathName string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
filesInfo = append(filesInfo, FileInfo{
Name: info.Name(),
FolderRemote: strings.TrimPrefix(filepath.Dir(filepath.ToSlash(pathName)),
filepath.Dir(absPath)+"/"),
FolderSource: filepath.Dir(pathName),
Size: info.Size(),
ModTime: info.ModTime(),
Mode: info.Mode(),
})
}
return nil
})
if err != nil {
return
}
} else {
filesInfo = append(filesInfo, FileInfo{
Name: stat.Name(),
FolderRemote: "./",
FolderSource: filepath.Dir(absPath),
Size: stat.Size(),
ModTime: stat.ModTime(),
Mode: stat.Mode(),
})
}
}
return
}
func (c *Client) sendCollectFiles(options TransferOptions) (err error) {
c.FilesToTransfer = make([]FileInfo, len(options.PathToFiles))
totalFilesSize := int64(0)
@ -1559,7 +1635,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())