Beispiel #1
0
//Gets next tar header for supported types
func (drdr *DebReader) NextTar() (string, *tar.Reader, error) {
	for {
		hdr, err := drdr.ArReader.Next()
		if err != nil {
			return "", nil, err
		}
		if hdr.Name == "debian-binary" {
			drdr.HasDebianVersion = true
			continue
		}
		if strings.HasSuffix(hdr.Name, ".tar.gz") {
			tgzr, err := targz.NewReader(drdr.ArReader)
			return hdr.Name, tgzr.Reader, err
		}
		// else return error
		return hdr.Name, nil, fmt.Errorf("Unsuported file type: %s", hdr.Name)
	}
}
Beispiel #2
0
func DebExtractFileL2(rdr io.Reader, topLevelFilename string, secondLevelFilename string, destination io.Writer) error {
	arr, err := ar.NewReader(rdr)
	if err != nil {
		return err
	}
	for {
		hdr, err := arr.Next()
		if err == io.EOF {
			// end of ar archive
			break
		}
		if err != nil {
			return err
		}
		if hdr.Name == topLevelFilename {
			tgzr, err := targz.NewReader(arr)
			if err != nil {
				tgzr.Close()
				return err
			}
			for {
				thdr, err := tgzr.Next()
				if err == io.EOF {
					// end of tar.gz archive
					break
				}
				if err != nil {
					tgzr.Close()
					return err
				}
				if thdr.Name == secondLevelFilename {
					_, err = io.Copy(destination, tgzr)
					tgzr.Close()
					return nil
				} else {
					//SKIP
					log.Printf("File %s", thdr.Name)
				}
			}
			tgzr.Close()
		}
	}
	return fmt.Errorf("File not found")
}
Beispiel #3
0
func DebGetContents(rdr io.Reader, topLevelFilename string) ([]string, error) {
	ret := []string{}
	fileNotFound := true
	arr, err := ar.NewReader(rdr)
	if err != nil {
		return nil, err
	}
	for {
		hdr, err := arr.Next()
		if err == io.EOF {
			// end of ar archive
			break
		}
		if err != nil {
			return nil, err
		}
		if hdr.Name == topLevelFilename {
			fileNotFound = false
			tgzr, err := targz.NewReader(arr)
			if err != nil {
				tgzr.Close()
				return nil, err
			}
			for {
				thdr, err := tgzr.Next()
				if err == io.EOF {
					// end of tar.gz archive
					break
				}
				if err != nil {
					tgzr.Close()
					return nil, err
				}
				ret = append(ret, thdr.Name)
			}
		}
	}
	if fileNotFound {
		return nil, fmt.Errorf("File not found")
	}
	return ret, nil
}
Beispiel #4
0
// ParseDebMetadata reads an artifact's contents.
func DebParseMetadata(rdr io.Reader) (*Package, error) {

	arr, err := ar.NewReader(rdr)
	if err != nil {
		return nil, err
	}

	var pkg *Package
	pkg = nil
	hasDataArchive := false
	hasControlArchive := false
	hasControlFile := false
	hasDebianBinaryFile := false

	// Iterate through the files in the archive.
	for {
		hdr, err := arr.Next()
		if err == io.EOF {
			// end of ar archive
			break
		}
		if err != nil {
			return nil, err
		}
		//		t.Logf("File %s:\n", hdr.Name)
		if hdr.Name == BinaryDataArchiveNameDefault {
			// SKIP!
			hasDataArchive = true
		} else if hdr.Name == BinaryControlArchiveNameDefault {
			// Find control file
			hasControlArchive = true
			tgzr, err := targz.NewReader(arr)
			if err != nil {
				return nil, err
			}
			for {
				thdr, err := tgzr.Next()
				if err == io.EOF {
					// end of tar.gz archive
					break
				}
				if err != nil {
					return nil, err
				}
				if path.Base(thdr.Name) == "control" {
					hasControlFile = true
					dscr := NewDscReader(tgzr)
					pkg, err = dscr.Parse()
					if err != nil {
						return nil, err
					}
				} else {
					//SKIP
					log.Printf("File %s", thdr.Name)
				}
			}

		} else if hdr.Name == "debian-binary" {
			b, err := ioutil.ReadAll(arr)
			if err != nil {
				return nil, err
			}
			hasDebianBinaryFile = true
			if string(b) != "2.0\n" {
				return nil, fmt.Errorf("Binary version not valid: %s", string(b))
			}
		} else {
			return nil, fmt.Errorf("Unsupported file %s", hdr.Name)
		}
	}

	if !hasDebianBinaryFile {
		return nil, fmt.Errorf("No debian-binary file in .deb archive")
	}
	if !hasDataArchive {
		return nil, fmt.Errorf("No data.tar.gz file in .deb archive")
	}
	if !hasControlArchive {
		return nil, fmt.Errorf("No control.tar.gz file in .deb archive")
	}
	if !hasControlFile {
		return nil, fmt.Errorf("No debian/control file in control.tar.gz")
	}
	return pkg, nil
}
Beispiel #5
0
func Example() {
	// Create a buffer to write our archive to.
	wtr := writer(isFs)

	// Create a new ar archive.
	tgzw := targz.NewWriter(wtr)

	// Add some files to the archive.
	var files = []struct {
		Name, Body string
	}{
		{"readme.txt", "This archive contains some text files."},
		{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
		{"todo.txt", "Get animal handling licence."},
	}
	for _, file := range files {
		hdr := &tar.Header{
			Name: file.Name,
			Size: int64(len(file.Body)),
		}
		if err := tgzw.WriteHeader(hdr); err != nil {
			log.Fatalln(err)
		}
		if _, err := tgzw.Write([]byte(file.Body)); err != nil {
			log.Fatalln(err)
		}
	}
	// Make sure to check the error on Close.
	if err := tgzw.Close(); err != nil {
		log.Fatalln(err)
	}
	rdr := reader(isFs, wtr)
	tgzr, err := targz.NewReader(rdr)
	if err != nil {
		log.Fatalln(err)
	}

	// Iterate through the files in the archive.
	for {
		hdr, err := tgzr.Next()
		if err == io.EOF {
			// end of ar archive
			break
		}
		if err != nil {
			log.Fatalln(err)
		}
		fmt.Printf("Contents of %s:\n", hdr.Name)
		if _, err := io.Copy(os.Stdout, tgzr); err != nil {
			log.Fatalln(err)
		}

		fmt.Println()
	}

	// Output:
	// Contents of readme.txt:
	// This archive contains some text files.
	// Contents of gopher.txt:
	// Gopher names:
	// George
	// Geoffrey
	// Gonzo
	// Contents of todo.txt:
	// Get animal handling licence.
}