archive.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package fs
  2. import (
  3. "errors"
  4. "io"
  5. "io/ioutil"
  6. "os/exec"
  7. )
  8. type Compression uint32
  9. const (
  10. Uncompressed Compression = iota
  11. Bzip2
  12. Gzip
  13. )
  14. func (compression *Compression) Flag() string {
  15. switch *compression {
  16. case Bzip2:
  17. return "j"
  18. case Gzip:
  19. return "z"
  20. }
  21. return ""
  22. }
  23. func Tar(path string, compression Compression) (io.Reader, error) {
  24. cmd := exec.Command("bsdtar", "-f", "-", "-C", path, "-c"+compression.Flag(), ".")
  25. return CmdStream(cmd)
  26. }
  27. func Untar(archive io.Reader, path string) error {
  28. cmd := exec.Command("bsdtar", "-f", "-", "-C", path, "-x")
  29. cmd.Stdin = archive
  30. output, err := cmd.CombinedOutput()
  31. if err != nil {
  32. return errors.New(err.Error() + ": " + string(output))
  33. }
  34. return nil
  35. }
  36. func CmdStream(cmd *exec.Cmd) (io.Reader, error) {
  37. stdout, err := cmd.StdoutPipe()
  38. if err != nil {
  39. return nil, err
  40. }
  41. stderr, err := cmd.StderrPipe()
  42. if err != nil {
  43. return nil, err
  44. }
  45. pipeR, pipeW := io.Pipe()
  46. go func() {
  47. _, err := io.Copy(pipeW, stdout)
  48. if err != nil {
  49. pipeW.CloseWithError(err)
  50. }
  51. errText, e := ioutil.ReadAll(stderr)
  52. if e != nil {
  53. errText = []byte("(...couldn't fetch stderr: " + e.Error() + ")")
  54. }
  55. if err := cmd.Wait(); err != nil {
  56. // FIXME: can this block if stderr outputs more than the size of StderrPipe()'s buffer?
  57. pipeW.CloseWithError(errors.New(err.Error() + ": " + string(errText)))
  58. } else {
  59. pipeW.Close()
  60. }
  61. }()
  62. if err := cmd.Start(); err != nil {
  63. return nil, err
  64. }
  65. return pipeR, nil
  66. }