Ejemplo n.º 1
0
func parseDataWithHandler(r io.Reader, handler parseDataHandlerFunc, uFiles map[string]UpdateFile) error {
	if r == nil {
		return errors.New("rootfs updater: uninitialized tar reader")
	}
	//[data.tar].gz
	gz, err := gzip.NewReader(r)
	if err != nil {
		return err
	}
	defer gz.Close()

	//data[.tar].gz
	tar := tar.NewReader(gz)
	// iterate over the files in tar archive
	for {
		hdr, err := tar.Next()
		if err == io.EOF {
			// once we reach end of archive break the loop
			break
		} else if err != nil {
			return errors.Wrapf(err, "parser: error reading archive")
		}
		fh, ok := uFiles[withoutExt(hdr.Name)]
		if !ok {
			return errors.New("parser: can not find header info for data file")
		}
		fh.Date = hdr.ModTime
		fh.Size = hdr.Size

		h := sha256.New()
		// use tee reader to pass read data for checksum calculation
		tr := io.TeeReader(tar, h)

		if err := handler(tr, fh); err != nil {
			return errors.Wrapf(err, "parser: data handler failed")
		}

		sum := h.Sum(nil)
		hSum := make([]byte, hex.EncodedLen(len(sum)))
		hex.Encode(hSum, h.Sum(nil))

		if bytes.Compare(hSum, fh.Checksum) != 0 {
			return errors.Errorf("parser: invalid data file [%s] checksum (%s) -> (%s)",
				hdr.Name, hSum, fh.Checksum)
		}

		uFiles[withoutExt(hdr.Name)] = fh
	}
	return nil
}
Ejemplo n.º 2
0
func extractTemplates() {
	tarFile, _ := os.Open(TEMPLATE_DIR + "templates.tar")
	defer tarFile.Close()

	tar := tar.NewReader(tarFile)

	for {
		header, err := tar.Next()
		if err == io.EOF {
			break
		}

		if err != nil {
			util.LogError(err)
		}

		// Remove first path part
		filePath := strings.Join(strings.Split(header.Name, "/")[1:], "/")

		// Check if we should create a folder or a file
		if header.Size == 0 {
			err := os.MkdirAll(TEMPLATE_DIR+filePath, 0755)
			if err != nil {
				util.LogError(err)
			}
		} else {
			f, err := os.Create(TEMPLATE_DIR + filePath)
			if err != nil {
				util.LogError(err)
			}
			defer f.Close()

			_, err = io.Copy(f, tar)
			if err != nil {
				util.LogError(err)
			}
		}
	}

	os.Remove(TEMPLATE_DIR + "templates.tar")
}