Skip to content

Commit

Permalink
set all file sizes to -1
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Nov 14, 2024
1 parent 2987f01 commit 9ce1f97
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 39 deletions.
17 changes: 15 additions & 2 deletions internal/agent/sftp/filelister.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ import (
"github.com/pkg/sftp"
)

// FileInfoWithUnknownSize wraps an os.FileInfo and overrides the Size method to return -1.
type FileInfoWithUnknownSize struct {
os.FileInfo
}

// Size overrides the original Size method to always return -1.
func (f *FileInfoWithUnknownSize) Size() int64 {
return -1
}

type FileLister struct {
files []os.FileInfo
}
Expand Down Expand Up @@ -50,7 +60,8 @@ func (h *SftpHandler) FileLister(dirPath string) (*FileLister, error) {
if h.skipFile(fullPath) {
continue
}
fileInfos = append(fileInfos, info)
// Wrap the original os.FileInfo to override its Size method.
fileInfos = append(fileInfos, &FileInfoWithUnknownSize{FileInfo: info})
}
}

Expand All @@ -74,9 +85,11 @@ func (h *SftpHandler) FileStat(filename string) (*FileLister, error) {
}
}

return &FileLister{files: []os.FileInfo{stat}}, nil
// Wrap the original os.FileInfo to override its Size method.
return &FileLister{files: []os.FileInfo{&FileInfoWithUnknownSize{FileInfo: stat}}}, nil
}

func (h *SftpHandler) setFilePath(r *sftp.Request) {
r.Filepath = filepath.Join(h.Snapshot.SnapshotPath, filepath.Clean(r.Filepath))
}

4 changes: 0 additions & 4 deletions internal/agent/sftp/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,5 @@ func (h *SftpHandler) skipFile(path string) bool {
return true
}

if isFileOpen(strings.Replace(pathWithoutSnap, "\\", ":\\", 1)) {
return true
}

return false
}
33 changes: 0 additions & 33 deletions internal/agent/sftp/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package sftp

import (
"os"
"syscall"
"unsafe"

"golang.org/x/sys/windows"
Expand Down Expand Up @@ -59,35 +58,3 @@ func invalidAttributes(path string) (bool, error) {

return false, nil
}

const ERROR_SHARING_VIOLATION syscall.Errno = 32

func isFileOpen(path string) bool {
p, err := syscall.UTF16PtrFromString(path)
if err != nil {
return false
}

// Use CreateFileW system call to open the file with read-write and exclusive access
// FILE_SHARE_NONE ensures that the file cannot be opened by any other process
h, err := syscall.CreateFile(
p,
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
0,
nil,
syscall.OPEN_EXISTING,
syscall.FILE_ATTRIBUTE_NORMAL,
0,
)

if err != nil {
// ERROR_SHARING_VIOLATION means the file is already open by another process
if errno, ok := err.(syscall.Errno); ok && errno == ERROR_SHARING_VIOLATION {
return true
}
return false
}

syscall.CloseHandle(h)
return false
}

0 comments on commit 9ce1f97

Please sign in to comment.