Beispiel #1
0
func ExtractArchive() {
	data, err := elf.ExtractSection(*input)
	if err != nil {
		log.Fatalf("failed extracting section from %s: %s", *input, err)
	}

	file, err := os.OpenFile(*extract, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		log.Fatalf("failed to open %s for writing: %s", *extract, err)
	}
	defer file.Close()

	_, err = io.Copy(file, bytes.NewReader(data))
	if err != nil {
		log.Fatalf("failed copy: %s", err)
	}
}
Beispiel #2
0
func Load() (*Goblet, error) {
	binary, err := osext.Executable()
	if err != nil {
		return nil, err
	}

	data, err := elf.ExtractSection(binary)
	if err != nil {
		return nil, err
	}

	r := tar.NewReader(bytes.NewReader(data))
	files := make(map[string]*Asset)
	for {
		hdr, err := r.Next()
		if err != nil {
			if err == io.EOF {
				break
			}
			return nil, fmt.Errorf("failed to get next entry: %s", err)
		}

		buffer := make([]byte, hdr.Size)
		_, err = io.ReadFull(r, buffer)
		if err != nil {
			return nil, fmt.Errorf("failed reading from archive %s: %s", hdr.Name, err)
		}

		files[hdr.Name] = &Asset{
			Name:         hdr.Name,
			Data:         buffer,
			LastModified: hdr.ModTime,
			Sha1:         fmt.Sprintf("%x", sha1.New().Sum(buffer)),
		}
	}

	return &Goblet{files}, nil
}