Skip to content

Commit

Permalink
try to trigger file list ahead of time
Browse files Browse the repository at this point in the history
  • Loading branch information
sonroyaalmerol committed Nov 15, 2024
1 parent eb0e902 commit 92eaf88
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
38 changes: 35 additions & 3 deletions internal/agent/sftp/filelister.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,45 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"github.com/pkg/sftp"
)

var cacheMutex sync.RWMutex
var sizeCache map[string]map[string]int64
var cacheOnce sync.Once

// CustomFileInfo wraps an os.FileInfo and overrides the Size method to return -1.
type CustomFileInfo struct {
os.FileInfo
filePath string
filePath string
snapshotId string
}

func initializeSizeCache() {
cacheMutex.Lock()
defer cacheMutex.Unlock()
if sizeCache == nil {
sizeCache = make(map[string]map[string]int64)
}
}

func (f *CustomFileInfo) Size() int64 {
cacheOnce.Do(initializeSizeCache)

cacheMutex.RLock()
if _, ok := sizeCache[f.filePath]; ok {
if cachedSize, ok := sizeCache[f.filePath][f.snapshotId]; ok {
return cachedSize
}
}
cacheMutex.RUnlock()

cacheMutex.Lock()
sizeCache[f.filePath] = make(map[string]int64)
cacheMutex.Unlock()

file, err := os.Open(f.filePath)
if err != nil {
return 0
Expand All @@ -31,6 +59,10 @@ func (f *CustomFileInfo) Size() int64 {
return 0
}

cacheMutex.Lock()
sizeCache[f.filePath][f.snapshotId] = byteCount
cacheMutex.Unlock()

return byteCount
}

Expand Down Expand Up @@ -72,7 +104,7 @@ func (h *SftpHandler) FileLister(dirPath string) (*FileLister, error) {
continue
}
// Wrap the original os.FileInfo to override its Size method.
fileInfos = append(fileInfos, &CustomFileInfo{FileInfo: info, filePath: fullPath})
fileInfos = append(fileInfos, &CustomFileInfo{FileInfo: info, filePath: fullPath, snapshotId: h.Snapshot.Id})
}
}

Expand All @@ -97,7 +129,7 @@ func (h *SftpHandler) FileStat(filename string) (*FileLister, error) {
}

// Wrap the original os.FileInfo to override its Size method.
return &FileLister{files: []os.FileInfo{&CustomFileInfo{FileInfo: stat, filePath: filename}}}, nil
return &FileLister{files: []os.FileInfo{&CustomFileInfo{FileInfo: stat, filePath: filename, snapshotId: h.Snapshot.Id}}}, nil
}

func (h *SftpHandler) setFilePath(r *sftp.Request) {
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/sftp/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func compileExcludedPaths() []*regexp.Regexp {
`AppData\Local\Microsoft\Windows\WebCache`,
`AppData\Local\Microsoft\Windows Store`,
`AppData\Local\Packages`,
`AppData\Local\Veritas\DLO`,
`AppData\Local\Veritas`,
`Application Data\Apple Computer\Mobile Sync`,
`Application Data\Application Data**`,
`Dropbox\Dropbox.exe.log`,
Expand Down
5 changes: 5 additions & 0 deletions internal/backend/mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func Mount(target *store.Target) (*AgentMount, error) {
return nil, fmt.Errorf("Mount: error starting rclone for sftp -> %w", err)
}

ls := exec.Command("bash", "-c", fmt.Sprintf("find %s -type f | xargs -P 8 -n 1 true", agentMount.Path))
ls.Env = os.Environ()

_ = ls.Start()

return agentMount, nil
}

Expand Down

0 comments on commit 92eaf88

Please sign in to comment.