2019-12-04 14:58:28 +00:00
|
|
|
package systar
|
|
|
|
|
|
|
|
import (
|
2019-12-04 16:53:32 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-12-04 14:58:28 +00:00
|
|
|
"io"
|
2019-12-04 16:53:32 +00:00
|
|
|
"os"
|
2019-12-04 14:58:28 +00:00
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2019-12-04 16:53:32 +00:00
|
|
|
|
|
|
|
logging "github.com/ipfs/go-log"
|
2019-12-04 14:58:28 +00:00
|
|
|
)
|
|
|
|
|
2019-12-04 16:53:32 +00:00
|
|
|
var log = logging.Logger("systar")
|
|
|
|
|
2019-12-04 14:58:28 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:58:28 +00:00
|
|
|
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 14:58:28 +00:00
|
|
|
|
2019-12-04 16:53:32 +00:00
|
|
|
return i, nil
|
2019-12-04 14:58:28 +00:00
|
|
|
}
|