Skip to content

Commit

Permalink
Copy single file transfer to bundle
Browse files Browse the repository at this point in the history
Fixes #746.

For a single file transfer copy the blob data from the local temp file
to the bundle in the transfer directory.  Renaming (moving) the file
does not work when the souce and destination are on different logical
devices.

Ref: https://groups.google.com/g/golang-dev/c/5w7Jmg_iCJQ?pli=1
  • Loading branch information
djjuhasz committed Jan 31, 2024
1 parent 830ba23 commit 55c7b97
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions internal/workflow/activities/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func (a *BundleActivity) Execute(ctx context.Context, params *BundleActivityPara
unar := a.Unarchiver(params.Key, params.TempFile)
if unar == nil {
res.FullPath, err = a.SingleFile(ctx, params.TransferDir, params.Key, params.TempFile)
if err != nil {
err = fmt.Errorf("bundle single file: %v", err)
}
res.FullPathBeforeStrip = res.FullPath
} else {
res.FullPath, res.FullPathBeforeStrip, err = a.Bundle(ctx, unar, params.TransferDir, params.Key, params.TempFile, params.StripTopLevelDir)
Expand Down Expand Up @@ -150,25 +153,27 @@ func (a *BundleActivity) Unarchiver(key, filename string) archiver.Unarchiver {
}

// SingleFile bundles a transfer with the downloaded blob in it.
//
// TODO: Write metadata.csv and checksum files to the metadata dir.
func (a *BundleActivity) SingleFile(ctx context.Context, transferDir, key, tempFile string) (string, error) {
b, err := bundler.NewBundlerWithTempDir(transferDir)
if err != nil {
return "", fmt.Errorf("error creating bundle: %v", err)
return "", fmt.Errorf("create bundler: %v", err)
}

dest, err := b.Create(filepath.Join("objects", key))
src, err := os.Open(tempFile)
if err != nil {
return "", fmt.Errorf("error creating file: %v", err)
return "", fmt.Errorf("open source file: %v", err)
}
defer dest.Close()
defer src.Close()

path, _ := securejoin.SecureJoin(transferDir, dest.Name())
if err := os.Rename(tempFile, path); err != nil {
return "", fmt.Errorf("error moving file (from %s to %s): %v", tempFile, path, err)
err = b.Write(filepath.Join("objects", key), src)
if err != nil {
return "", fmt.Errorf("write file: %v", err)
}

if err := b.Bundle(); err != nil {
return "", fmt.Errorf("error bundling the transfer: %v", err)
return "", fmt.Errorf("write bundle: %v", err)
}

return b.FullBaseFsPath(), nil
Expand Down

0 comments on commit 55c7b97

Please sign in to comment.