Skip to content

Commit

Permalink
ensure file moves do not occur across filesystems
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
  • Loading branch information
wagoodman committed Dec 18, 2023
1 parent cca5e66 commit 1ad6a8d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (s *Store) AddTool(toolName string, resolvedVersion, pathOutsideRoot string
// move the file into the store at root/basename
targetName := toolName
targetPath := filepath.Join(s.root, toolName)

if err := os.Rename(pathOutsideRoot, targetPath); err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion tool/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ func Install(tool binny.Tool, intent binny.VersionIntent, store *binny.Store, ve
}
}()

tmpdir, err := os.MkdirTemp("", fmt.Sprintf("binny-install-%s-", tool.Name()))
// note: we choose a staging directory that is within the store to ensure the final move is atomic
// and not across filesystems (which would not succeed). We do this instead of a copy in case one of the binaries
// being managed is in use, in which case a copy would fail with "text file busy" error, whereas a move would
// allow for the in-use binary to continue to be used (since an unlink is performed on the path to the binary
// or the path to the parent of the binary).
tmpdir, err := os.MkdirTemp(store.Root(), fmt.Sprintf("binny-install-%s-", tool.Name()))
if err != nil {
return fmt.Errorf("failed to create temp directory: %w", err)
}
Expand Down

0 comments on commit 1ad6a8d

Please sign in to comment.