srpmproc/internal/directives/lookaside.go

128 lines
3.3 KiB
Go
Raw Normal View History

package directives
import (
2021-02-26 07:54:13 +00:00
"archive/tar"
"bytes"
"compress/gzip"
"crypto/sha256"
"errors"
"fmt"
"git.rockylinux.org/release-engineering/public/srpmproc/internal/data"
srpmprocpb "git.rockylinux.org/release-engineering/public/srpmproc/pb"
"github.com/go-git/go-git/v5"
2021-02-26 07:54:13 +00:00
"io/ioutil"
"os"
"path/filepath"
"time"
)
2021-02-26 07:54:13 +00:00
func lookaside(cfg *srpmprocpb.Cfg, _ *data.ProcessData, md *data.ModeData, patchTree *git.Worktree, pushTree *git.Worktree) error {
for _, directive := range cfg.Lookaside {
var buf bytes.Buffer
writer := tar.NewWriter(&buf)
w := pushTree
if directive.FromPatchTree {
w = patchTree
}
for _, file := range directive.File {
if directive.Tar && directive.ArchiveName == "" {
return errors.New("TAR_NO_ARCHIVE_NAME")
}
path := filepath.Join("SOURCES", file)
if directive.FromPatchTree {
path = file
}
stat, err := w.Filesystem.Stat(path)
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_STAT_FILE:%s", path))
}
f, err := w.Filesystem.Open(path)
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_OPEN_FILE:%s", path))
}
bts, err := ioutil.ReadAll(f)
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_READ_FILE:%s", path))
}
if directive.Tar {
hdr := &tar.Header{
Name: file,
Mode: int64(stat.Mode()),
Size: stat.Size(),
}
err = writer.WriteHeader(hdr)
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_WRITE_TAR_HEADER:%s", file))
}
_, err = writer.Write(bts)
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_WRITE_TAR_FILE:%s", file))
}
} else {
if directive.FromPatchTree {
pushF, err := pushTree.Filesystem.OpenFile(filepath.Join("SOURCES", filepath.Base(file)), os.O_CREATE|os.O_TRUNC|os.O_RDWR, stat.Mode())
2021-02-26 07:54:13 +00:00
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_CREATE_FILE_IN_PUSH_TREE:%s", file))
}
_, err = pushF.Write(bts)
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_WRITE_FILE_IN_PUSH_TREE:%s", file))
}
}
md.SourcesToIgnore = append(md.SourcesToIgnore, &data.IgnoredSource{
Name: filepath.Join("SOURCES", file),
HashFunction: sha256.New(),
})
}
}
if directive.Tar {
err := writer.Close()
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_CLOSE_TAR:%s", directive.ArchiveName))
}
var gbuf bytes.Buffer
gw := gzip.NewWriter(&gbuf)
gw.Name = fmt.Sprintf("%s.tar.gz", directive.ArchiveName)
gw.ModTime = time.Now()
_, err = gw.Write(buf.Bytes())
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_WRITE_GZIP:%s", directive.ArchiveName))
}
err = gw.Close()
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_CLOSE_GZIP:%s", directive.ArchiveName))
}
path := filepath.Join("SOURCES", fmt.Sprintf("%s.tar.gz", directive.ArchiveName))
pushF, err := pushTree.Filesystem.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644)
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_CREATE_TAR_FILE:%s", path))
}
_, err = pushF.Write(gbuf.Bytes())
if err != nil {
return errors.New(fmt.Sprintf("COULD_NOT_WRITE_TAR_FILE:%s", path))
}
md.SourcesToIgnore = append(md.SourcesToIgnore, &data.IgnoredSource{
Name: path,
HashFunction: sha256.New(),
})
}
}
return nil
}