Skip to content

Commit

Permalink
backport tar handling from #2410 (#2702)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Henriksen <[email protected]>
  • Loading branch information
mhenriks authored Apr 26, 2023
1 parent 49e3fc4 commit 2f8915d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
34 changes: 30 additions & 4 deletions cmd/cdi-cloner/clone-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,38 @@ func validateMount() {
}

func newTarReader(preallocation bool) (io.ReadCloser, error) {
args := "cv"
excludeMap := map[string]struct{}{
"lost+found": struct{}{},
}

args := []string{"/usr/bin/tar", "cv"}
if !preallocation {
// -S is used to handle sparse files. It can only be used when preallocation is not requested
args = "S" + args
args = append(args, "-S")
}
cmd := exec.Command("/usr/bin/tar", args, ".")

files, err := os.ReadDir(mountPoint)
if err != nil {
return nil, err
}

var tarFiles []string
for _, f := range files {
if _, ok := excludeMap[f.Name()]; ok {
continue
}
tarFiles = append(tarFiles, f.Name())
}

if len(tarFiles) > 0 {
args = append(args, tarFiles...)
} else {
args = append(args, "--files-from", "/dev/null")
}

klog.Infof("Executing %+v", args)

cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = mountPoint

stdout, err := cmd.StdoutPipe()
Expand All @@ -166,7 +192,7 @@ func newTarReader(preallocation bool) (io.ReadCloser, error) {
return nil, err
}

return &execReader{cmd: cmd, stdout: stdout, stderr: ioutil.NopCloser(&stderr)}, nil
return &execReader{cmd: cmd, stdout: stdout, stderr: io.NopCloser(&stderr)}, nil
}

func getInputStream(preallocation bool) (rc io.ReadCloser) {
Expand Down
19 changes: 7 additions & 12 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,27 +204,22 @@ func StreamDataToFile(r io.Reader, fileName string) error {

// UnArchiveTar unarchives a tar file and streams its files
// using the specified io.Reader to the specified destination.
func UnArchiveTar(reader io.Reader, destDir string, arg ...string) error {
func UnArchiveTar(reader io.Reader, destDir string) error {
klog.V(1).Infof("begin untar to %s...\n", destDir)

var tarOptions string
var args = arg
if len(arg) > 0 {
tarOptions = arg[0]
args = arg[1:]
}
options := fmt.Sprintf("-%s%s", tarOptions, "xvC")
untar := exec.Command("/usr/bin/tar", "--no-same-owner", options, destDir, strings.Join(args, ""))
untar := exec.Command("/usr/bin/tar", "--preserve-permissions", "--no-same-owner", "-xvC", destDir)
untar.Stdin = reader
var errBuf bytes.Buffer
var outBuf, errBuf bytes.Buffer
untar.Stdout = &outBuf
untar.Stderr = &errBuf
klog.V(1).Infof("running untar cmd: %v\n", untar.Args)
err := untar.Start()
if err != nil {
return err
}
err = untar.Wait()
if err != nil {
klog.V(3).Infof("%s\n", errBuf.String())
klog.V(3).Infof("STDOUT\n%s\n", outBuf.String())
klog.V(3).Infof("STDERR\n%s\n", errBuf.String())
klog.Errorf("%s\n", err.Error())
return err
}
Expand Down

0 comments on commit 2f8915d

Please sign in to comment.