Skip to content

Commit

Permalink
ensure file moves do not occur across filesystems (#14)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman authored Dec 19, 2023
1 parent cca5e66 commit c51f257
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
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
43 changes: 33 additions & 10 deletions tool/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/wagoodman/go-partybus"
Expand All @@ -27,18 +28,11 @@ func Install(tool binny.Tool, intent binny.VersionIntent, store *binny.Store, ve
prog.SetCompleted()
}
}()

tmpdir, err := os.MkdirTemp("", fmt.Sprintf("binny-install-%s-", tool.Name()))
tmpdir, cleanup, err := makeStagingArea(store, tool.Name())
defer cleanup()
if err != nil {
return fmt.Errorf("failed to create temp directory: %w", err)
return err
}
defer func() {
err := os.RemoveAll(tmpdir)
if err != nil {
log.WithFields("tool", tool.Name(), "dir", tmpdir).
Warnf("failed to remove temp directory: %v", err)
}
}()

log.WithFields("tool", tool.Name(), "dir", tmpdir).Trace("tool staging directory")

Expand Down Expand Up @@ -83,6 +77,35 @@ func Install(tool binny.Tool, intent binny.VersionIntent, store *binny.Store, ve
return nil
}

// makeStagingArea creates a temporary directory within the store root to stage the installation of a tool. A valid
// function should always be returned for cleanup, even if an error is returned.
func makeStagingArea(store *binny.Store, toolName string) (string, func(), error) {
cleanup := func() {}
// 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). We use the absolute path to the store root to ensure that there is
// no confusion on how to interpret this path across different installer implementations.
absRoot, err := filepath.Abs(store.Root())
if err != nil {
return "", cleanup, fmt.Errorf("failed to get absolute path for store root: %w", err)
}
tmpdir, err := os.MkdirTemp(absRoot, fmt.Sprintf("binny-install-%s-", toolName))
if err != nil {
return "", cleanup, fmt.Errorf("failed to create temp directory: %w", err)
}
cleanup = func() {
err := os.RemoveAll(tmpdir)
if err != nil {
log.WithFields("tool", toolName, "dir", tmpdir).
Warnf("failed to remove temp directory: %v", err)
}
}

return tmpdir, cleanup, nil
}

func trackInstallation(repo, version string) (*progress.Manual, *progress.AtomicStage) {
fields := strings.Split(repo, "/")
p := progress.NewManual(-1)
Expand Down

0 comments on commit c51f257

Please sign in to comment.