lotus/extern/sector-storage/stores/util_unix.go
2020-08-10 17:25:46 +02:00

44 lines
988 B
Go

package stores
import (
"bytes"
"os/exec"
"path/filepath"
"strings"
"github.com/mitchellh/go-homedir"
"golang.org/x/xerrors"
)
func move(from, to string) error {
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
cmd := exec.Command("/usr/bin/env", "mv", "-t", toDir, from)
cmd.Stderr = &errOut
if err := cmd.Run(); err != nil {
return xerrors.Errorf("exec mv (stderr: %s): %w", strings.TrimSpace(errOut.String()), err)
}
return nil
}