MCPcopy Create free account
hub / github.com/cli/cli / extractTarGz

Function extractTarGz

pkg/cmd/copilot/copilot.go:413–446  ·  view source on GitHub ↗

extractTarGz reads a TAR.GZ archive from r and extracts its contents into destDir. It returns an error if the archive cannot be read, or if any file or directory within the archive cannot be created or written.

(r io.Reader, destDir string)

Source from the content-addressed store, hash-verified

411// It returns an error if the archive cannot be read,
412// or if any file or directory within the archive cannot be created or written.
413func extractTarGz(r io.Reader, destDir string) error {
414 gzr, err := gzip.NewReader(r)
415 if err != nil {
416 return fmt.Errorf("failed to create gzip reader: %w", err)
417 }
418 defer gzr.Close()
419
420 root, err := safepaths.OpenRoot(destDir)
421 if err != nil {
422 return err
423 }
424 defer root.Close()
425
426 tr := tar.NewReader(gzr)
427 for {
428 header, err := tr.Next()
429 if err == io.EOF {
430 break
431 }
432 if err != nil {
433 return fmt.Errorf("failed to read tar: %w", err)
434 }
435 if err := safepaths.ValidateChild(header.Name); err != nil {
436 return err
437 }
438
439 if header.Typeflag == tar.TypeReg {
440 if err := root.CopyFile(header.Name, tr, os.FileMode(header.Mode)&0o777, 0o755, true); err != nil {
441 return fmt.Errorf("failed to extract file: %w", err)
442 }
443 }
444 }
445 return nil
446}
447
448func removeCopilot(installDir string) error {
449 if _, err := os.Stat(installDir); os.IsNotExist(err) {

Callers 2

TestExtractTarGzFunction · 0.85
downloadCopilotFunction · 0.85

Calls 5

OpenRootFunction · 0.92
ValidateChildFunction · 0.92
CopyFileMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.65

Tested by 1

TestExtractTarGzFunction · 0.68