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

Function extractTarGz

pkg/cmd/copilot/copilot.go:413–451  ·  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 absDestDirPath, err := safepaths.ParseAbsolute(destDir)
421 if err != nil {
422 return err
423 }
424
425 tr := tar.NewReader(gzr)
426 for {
427 header, err := tr.Next()
428 if err == io.EOF {
429 break
430 }
431 if err != nil {
432 return fmt.Errorf("failed to read tar: %w", err)
433 }
434
435 absFilePath, err := absDestDirPath.Join(header.Name)
436 if err != nil {
437 return err
438 }
439 target := absFilePath.String()
440
441 if header.Typeflag == tar.TypeReg {
442 if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
443 return fmt.Errorf("failed to create parent directory: %w", err)
444 }
445 if err := extractFile(target, os.FileMode(header.Mode)&0777, tr); err != nil {
446 return err
447 }
448 }
449 }
450 return nil
451}
452
453// extractFile creates a file at target with the given mode and copies content from r.
454func extractFile(target string, mode os.FileMode, r io.Reader) (err error) {

Callers 2

TestExtractTarGzFunction · 0.85
downloadCopilotFunction · 0.85

Calls 6

ParseAbsoluteFunction · 0.92
extractFileFunction · 0.85
JoinMethod · 0.80
ErrorfMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.65

Tested by 1

TestExtractTarGzFunction · 0.68