Created
May 4, 2019 21:41
-
-
Save kcmannem/c823da052f80cf20b3536d4c270d7f31 to your computer and use it in GitHub Desktop.
baggage claim with zstd streaming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
//"bufio" | |
"bytes" | |
"fmt" | |
"github.com/datadog/zstd" | |
"io" | |
"io/ioutil" | |
"os" | |
"os/exec" | |
"path/filepath" | |
) | |
func In(tgzStream io.Reader, dest string, privileged bool) (bool, error) { | |
tarCommand, dirFd, err := tarIn(privileged, dest, "xf", "-") | |
if err != nil { | |
return false, err | |
} | |
defer dirFd.Close() | |
tarCommand.Stdin = tgzStream | |
tarCommand.Stdout = os.Stderr | |
tarCommand.Stderr = os.Stderr | |
err = tarCommand.Run() | |
if err != nil { | |
if _, ok := err.(*exec.ExitError); ok { | |
return true, err | |
} | |
return false, err | |
} | |
return false, nil | |
} | |
func Out(w io.Writer, src string, privileged bool) error { | |
fileInfo, err := os.Stat(src) | |
if err != nil { | |
return err | |
} | |
var tarCommandPath, tarCommandDir string | |
if fileInfo.IsDir() { | |
tarCommandPath = "." | |
tarCommandDir = src | |
} else { | |
tarCommandPath = filepath.Base(src) | |
tarCommandDir = filepath.Dir(src) | |
} | |
tarCommand, dirFd, err := tarIn(privileged, tarCommandDir, "-cf", "-", tarCommandPath) | |
if err != nil { | |
return err | |
} | |
defer dirFd.Close() | |
zstdCompressor := zstd.NewWriter(w) | |
tarCommand.Stdout = zstdCompressor | |
tarCommand.Stderr = os.Stderr | |
err = tarCommand.Run() | |
if err != nil { | |
return err | |
} | |
err = zstdCompressor.Close() | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func tarIn(privileged bool, dir string, args ...string) (*exec.Cmd, *os.File, error) { | |
// 'tar' may run as MAX_UID in order to remap UIDs when streaming into an | |
// unprivileged volume. this may cause permission issues when exec'ing as it | |
// may not be able to even see the destination directory as non-root. | |
// | |
// so, open the directory while we're root, and pass it as a fd to the | |
// process. | |
dirFd, err := os.Open(dir) | |
if err != nil { | |
return nil, nil, err | |
} | |
tarCommand := exec.Command("tar", append([]string{"--exclude=./sys", "--exclude=./proc", "--exclude=./tmp", "-C", "/dev/fd/3"}, args...)...) | |
fmt.Println(tarCommand) | |
tarCommand.ExtraFiles = []*os.File{dirFd} | |
return tarCommand, dirFd, nil | |
} | |
func main() { | |
var writer bytes.Buffer | |
//bw := bufio.NewWriterSize(&writer, 50000000) | |
err := Out(&writer, "/", true) | |
fmt.Println("eerr happend", err) | |
err = ioutil.WriteFile("/tmp/test/wholeworld.tar.zstd", writer.Bytes(), 0644) | |
fmt.Println("eerr happend", err) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment