Exemple #1
0
func archiveChecksums(tw *tar.Writer, upd []string, dir string) error {
	for _, u := range upd {
		sum, err := calcChecksum(u)
		if err != nil {
			return err
		}
		a := archiver.NewStreamArchiver(sum, filepath.Join(dir, withoutExt(u)+".sha256sum"))
		if err := a.Archive(tw); err != nil {
			return errors.Wrapf(err, "parser: error storing checksum")
		}
	}
	return nil
}
Exemple #2
0
func (rp *RootfsParser) ArchiveHeader(tw *tar.Writer, dstDir string, update *UpdateData) error {
	if update == nil {
		return errors.New("paser: empty update")
	}

	e := new(HeaderElems)
	if update.Data != nil {
		var ok bool
		e, ok = update.Data.(*HeaderElems)
		if !ok {
			return errors.New("invalid header elements type")
		}
	}

	// create a updates map with the key being the update file name (without extension)
	rp.updates = make(map[string]UpdateFile, len(update.DataFiles))
	for _, f := range update.DataFiles {
		rp.updates[withoutExt(f)] =
			UpdateFile{
				Name: filepath.Base(f),
				Path: f,
			}
	}
	if err := archiveFiles(tw, update.DataFiles, dstDir); err != nil {
		return errors.Wrapf(err, "parser: can not store files")
	}

	if e.TypeInfo == nil {
		tInfo := metadata.TypeInfo{Type: update.Type}
		info, err := json.Marshal(&tInfo)
		if err != nil {
			return errors.Wrapf(err, "parser: can not create type-info")
		}
		e.TypeInfo = info
	}

	a := archiver.NewStreamArchiver(e.TypeInfo, filepath.Join(dstDir, "type-info"))
	if err := a.Archive(tw); err != nil {
		return errors.Wrapf(err, "parser: can not store type-info")
	}

	// if metadata info is not provided we need to have one stored in file
	if e.Metadata == nil {
		a := archiver.NewFileArchiver(filepath.Join(update.Path, "meta-data"),
			filepath.Join(dstDir, "meta-data"))
		if err := a.Archive(tw); err != nil {
			return errors.Wrapf(err, "parser: can not store meta-data")
		}
	} else {
		a = archiver.NewStreamArchiver(e.Metadata, filepath.Join(dstDir, "meta-data"))
		if err := a.Archive(tw); err != nil {
			return errors.Wrapf(err, "parser: can not store meta-data")
		}
	}

	if err := archiveChecksums(tw, update.DataFiles,
		filepath.Join(dstDir, "checksums")); err != nil {
		return err
	}

	// scripts
	if len(e.Scripts) > 0 {
		for _, scr := range e.Scripts {
			scrRelPath, err := filepath.Rel(scr, filepath.Dir(filepath.Dir(scr)))
			if err != nil {
				return err
			}
			a := archiver.NewFileArchiver(scr, filepath.Join(dstDir, "scripts", scrRelPath))
			if err := a.Archive(tw); err != nil {
				return err
			}
		}
	} else {
		sa := scriptArch{
			w:   tw,
			dst: filepath.Join(dstDir, "scripts"),
		}
		if err := filepath.Walk(filepath.Join(update.Path, "scripts"),
			sa.archScrpt); err != nil {
			return errors.Wrapf(err, "parser: can not archive scripts")
		}
	}
	return nil
}