lookaside.go 3.32 KB
Newer Older
1
2
3
package directives

import (
Mustafa Gezen's avatar
Mustafa Gezen committed
4
5
6
7
8
9
	"archive/tar"
	"bytes"
	"compress/gzip"
	"crypto/sha256"
	"errors"
	"fmt"
10
11
12
	"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"
Mustafa Gezen's avatar
Mustafa Gezen committed
13
14
15
16
	"io/ioutil"
	"os"
	"path/filepath"
	"time"
17
18
)

Mustafa Gezen's avatar
Mustafa Gezen committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 {
71
					pushF, err := pushTree.Filesystem.OpenFile(filepath.Join("SOURCES", filepath.Base(file)), os.O_CREATE|os.O_TRUNC|os.O_RDWR, stat.Mode())
Mustafa Gezen's avatar
Mustafa Gezen committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
					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(),
			})
		}
	}
126
127
	return nil
}