Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: remotefs/download support file download #194

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions remotefs/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package remotefs

import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"os"
)

// Download a file from the remote host.
func Download(fs FS, src, dst string) error {
remote, err := fs.Open(src)
if err != nil {
return fmt.Errorf("open remote file for download: %w", err)
}
defer remote.Close()

remoteStat, err := remote.Stat()
if err != nil {
return fmt.Errorf("stat remote file for download: %w", err)
}

remoteSum := sha256.New()
localSum := sha256.New()

local, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, remoteStat.Mode())
kaplan-michael marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("open local file for download: %w", err)
}
defer local.Close()

remoteReader := io.TeeReader(remote, remoteSum)
if _, err := io.Copy(io.MultiWriter(local, localSum), remoteReader); err != nil {
_ = local.Close()
return fmt.Errorf("copy file from remote host: %w", err)
}
if err := local.Close(); err != nil {
return fmt.Errorf("close local file after download: %w", err)
}

if !bytes.Equal(localSum.Sum(nil), remoteSum.Sum(nil)) {
return fmt.Errorf("downloading %s failed: %w", src, ErrChecksumMismatch)
}

return nil
}
43 changes: 43 additions & 0 deletions remotefs/downloaddirectory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package remotefs

import (
"fmt"
"io/fs"
"os"
"path/filepath"
)

// DownloadDirectory downloads all files and directories recursively from the remote system to local directory.
func DownloadDirectory(fsys FS, src, dst string) error {
walkErr := fs.WalkDir(fsys, src, func(path string, dir fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("walk remote directory: %w", err)
}

relPath, err := filepath.Rel(src, path)
if err != nil {
return fmt.Errorf("calculate relative path: %w", err)
}
targetPath := filepath.Join(dst, relPath)

if dir.IsDir() {
dirInfo, err := dir.Info()
if err != nil {
return fmt.Errorf("get dir info: %w", err)
}
if err := os.MkdirAll(targetPath, dirInfo.Mode()&os.ModePerm); err != nil {
return fmt.Errorf("create local directory: %w", err)
}
} else {
if err := Download(fsys, path, targetPath); err != nil {
return fmt.Errorf("download directory: %w", err)
}
}
return nil
})

if walkErr != nil {
return fmt.Errorf("walk remote directory tree: %w", walkErr)
}
return nil
}
43 changes: 43 additions & 0 deletions remotefs/uploaddirectory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package remotefs

import (
"fmt"
"io/fs"
"os"
"path/filepath"
)

// UploadDirectory uploads all files and directories recursively to the remote system.
func UploadDirectory(fsys FS, src, dst string) error {
walkErr := filepath.WalkDir(src, func(path string, dir fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("walk local directory: %w", err)
}

relPath, err := filepath.Rel(src, path)
if err != nil {
return fmt.Errorf("calculate relative path: %w", err)
}
targetPath := filepath.Join(dst, relPath)

if dir.IsDir() {
dirInfo, err := dir.Info()
if err != nil {
return fmt.Errorf("get dir info: %w", err)
}
if err := fsys.MkdirAll(targetPath, dirInfo.Mode()&os.ModePerm); err != nil {
return fmt.Errorf("create remote directory: %w", err)
}
} else {
if err := Upload(fsys, path, targetPath); err != nil {
return fmt.Errorf("upload directory: %w", err)
}
}
return nil
})

if walkErr != nil {
return fmt.Errorf("walk remote directory tree: %w", walkErr)
}
return nil
}