2022-06-14 18:25:52 +00:00
|
|
|
package paths
|
2020-03-25 18:21:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-05-21 13:45:05 +00:00
|
|
|
"os"
|
2020-03-25 18:21:53 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2021-05-20 16:25:49 +00:00
|
|
|
"runtime"
|
2020-03-25 18:21:53 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2023-09-21 15:37:02 +00:00
|
|
|
func Move(from, to string) error {
|
2020-03-25 18:21:53 +00:00
|
|
|
from, err := homedir.Expand(from)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("move: expanding from: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
to, err = homedir.Expand(to)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("move: expanding to: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if filepath.Base(from) != filepath.Base(to) {
|
|
|
|
return xerrors.Errorf("move: base names must match ('%s' != '%s')", filepath.Base(from), filepath.Base(to))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugw("move sector data", "from", from, "to", to)
|
|
|
|
|
|
|
|
toDir := filepath.Dir(to)
|
|
|
|
|
|
|
|
// `mv` has decades of experience in moving files quickly; don't pretend we
|
|
|
|
// can do better
|
|
|
|
|
|
|
|
var errOut bytes.Buffer
|
2021-05-20 16:25:49 +00:00
|
|
|
|
|
|
|
var cmd *exec.Cmd
|
|
|
|
if runtime.GOOS == "darwin" {
|
2021-05-21 13:45:05 +00:00
|
|
|
if err := os.MkdirAll(toDir, 0777); err != nil {
|
|
|
|
return xerrors.Errorf("failed exec MkdirAll: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-05-20 16:25:49 +00:00
|
|
|
cmd = exec.Command("/usr/bin/env", "mv", from, toDir) // nolint
|
|
|
|
} else {
|
|
|
|
cmd = exec.Command("/usr/bin/env", "mv", "-t", toDir, from) // nolint
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:21:53 +00:00
|
|
|
cmd.Stderr = &errOut
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return xerrors.Errorf("exec mv (stderr: %s): %w", strings.TrimSpace(errOut.String()), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|