lotus/lib/systar/systar.go

48 lines
922 B
Go
Raw Normal View History

package systar
import (
2019-12-04 16:53:32 +00:00
"golang.org/x/xerrors"
"io"
2019-12-04 16:53:32 +00:00
"os"
"os/exec"
"path/filepath"
2019-12-04 16:53:32 +00:00
logging "github.com/ipfs/go-log"
)
2019-12-04 16:53:32 +00:00
var log = logging.Logger("systar")
func ExtractTar(body io.Reader, dest string) error {
2019-12-04 16:53:32 +00:00
if err := os.MkdirAll(dest, 0755); err != nil {
return xerrors.Errorf("creating dest directory: %w", err)
}
cmd := exec.Command("tar", "-xS", "-C", dest)
cmd.Stdin = body
return cmd.Run()
}
func TarDirectory(file string) (io.ReadCloser, error) {
// use system builtin tar, golang one doesn't support sparse files
dir := filepath.Dir(file)
base := filepath.Base(file)
i, o := io.Pipe()
// don't bother with compression, it's mostly random data
cmd := exec.Command("tar", "-cSf", "-", "-C", dir, base)
cmd.Stdout = o
if err := cmd.Start(); err != nil {
return nil, err
}
2019-12-04 16:53:32 +00:00
go func() {
if err := o.CloseWithError(cmd.Wait()); err != nil {
log.Error(err)
}
}()
2019-12-04 16:53:32 +00:00
return i, nil
}