-
Notifications
You must be signed in to change notification settings - Fork 8
/
rpm_header.go
293 lines (251 loc) · 9.06 KB
/
rpm_header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
71
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package pack
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"github.com/apex/log"
"github.com/tarantool/tt/cli/cmdcontext"
"github.com/tarantool/tt/cli/config"
"github.com/tarantool/tt/cli/util"
"github.com/tarantool/tt/cli/version"
)
type filesInfo struct {
BaseNames []string
DirNames []string
DirIndexes []int32
FileUserNames []string
FileGroupNames []string
FileSizes []int32
FileModes []int16
FileInodes []int32
FileDevices []int32
FileMtimes []int32
FileLangs []string
FileRdevs []int16
FileLinkTos []string
FileFlags []int32
FileDigests []string
}
// getRPMRelation accepts the string representation of package relation
// and returns its according code for rpm header.
func getRPMRelation(relation string) int32 {
switch relation {
case ">":
return rpmSenseGreater
case ">=":
return rpmSenseGreater | rpmSenseEqual
case "<":
return rpmSenseLess
case "<=":
return rpmSenseLess | rpmSenseEqual
case "=", "==":
return rpmSenseEqual
}
return 0
}
// addDependenciesRPM writes all passed dependencies to the special rpm header.
func addDependenciesRPM(rpmHeader *rpmTagSetType, deps PackDependencies) {
if len(deps) == 0 {
return
}
var names []string
var versions []string
var relations []int32
for _, dep := range deps {
for _, r := range dep.Relations {
names = append(names, dep.Name)
relations = append(relations, getRPMRelation(r.Relation))
versions = append(versions, r.Version)
}
if len(dep.Relations) == 0 {
names = append(names, dep.Name)
relations = append(relations, 0)
versions = append(versions, "")
}
}
rpmHeader.addTags([]rpmTagType{
{ID: tagRequireName, Type: rpmTypeStringArray,
Value: names},
{ID: tagRequireFlags, Type: rpmTypeInt32,
Value: relations},
{ID: tagRequireVersion, Type: rpmTypeStringArray,
Value: versions},
}...)
}
// addPreAndPostInstallScriptsRPM writes passed paths of pre-install
// and post-install scripts to the rpm header.
func addPreAndPostInstallScriptsRPM(rpmHeader *rpmTagSetType, preInst string, postInst string) {
rpmHeader.addTags([]rpmTagType{
{ID: tagPrein, Type: rpmTypeString,
Value: strings.Join([]string{PreInstScriptContent, preInst}, "\n")},
{ID: tagPostin, Type: rpmTypeString,
Value: postInst},
}...)
}
// genRpmHeader generates rpm headers.
func genRpmHeader(relPaths []string, cpioPath, compresedCpioPath, packageFilesDir string,
cmdCtx *cmdcontext.CmdCtx, packCtx *PackCtx, opts *config.CliOpts) (rpmTagSetType, error) {
rpmHeader := rpmTagSetType{}
// Compute payload digest.
payloadDigestAlgo := hashAlgoSHA256
payloadDigest, err := util.FileSHA256Hex(compresedCpioPath)
if err != nil {
return nil, fmt.Errorf("failed to get payload digest: %s", err)
}
cpioFileInfo, err := os.Stat(cpioPath)
if err != nil {
return nil, fmt.Errorf("failed to get payload size: %s", err)
}
payloadSize := cpioFileInfo.Size()
// Generate fileinfo.
filesInfo, err := getFilesInfo(relPaths, packageFilesDir)
if err != nil {
return nil, fmt.Errorf("failed to get files info: %s", err)
}
versionString := getVersion(packCtx, opts, defaultVersion)
ver, err := version.Parse(versionString)
if err != nil {
return nil, err
}
name, err := getPackageName(packCtx, opts, "", false)
if err != nil {
return nil, err
}
versionStr := strings.Join([]string{
strconv.FormatUint(ver.Major, 10),
strconv.FormatUint(ver.Minor, 10),
strconv.FormatUint(ver.Patch, 10),
}, ".")
releaseStr := strconv.FormatUint(ver.Release.Num, 10)
arch := getArch()
rpmHeader.addTags([]rpmTagType{
{ID: tagName, Type: rpmTypeString, Value: name},
{ID: tagVersion, Type: rpmTypeString, Value: versionStr},
{ID: tagRelease, Type: rpmTypeString, Value: releaseStr},
{ID: tagSummary, Type: rpmTypeString, Value: ""},
{ID: tagDescription, Type: rpmTypeString, Value: ""},
{ID: tagLicense, Type: rpmTypeString, Value: "N/A"},
{ID: tagGroup, Type: rpmTypeString, Value: "None"},
{ID: tagOs, Type: rpmTypeString, Value: "linux"},
{ID: tagArch, Type: rpmTypeString, Value: arch},
{ID: tagPayloadFormat, Type: rpmTypeString, Value: "cpio"},
{ID: tagPayloadCompressor, Type: rpmTypeString, Value: "gzip"},
{ID: tagPayloadFlags, Type: rpmTypeString, Value: "5"},
{ID: tagPreinProg, Type: rpmTypeString, Value: "/bin/sh"},
{ID: tagPostinProg, Type: rpmTypeString, Value: "/bin/sh"},
{ID: tagDirNames, Type: rpmTypeStringArray, Value: filesInfo.DirNames},
{ID: tagBaseNames, Type: rpmTypeStringArray, Value: filesInfo.BaseNames},
{ID: tagDirIndexes, Type: rpmTypeInt32, Value: filesInfo.DirIndexes},
{ID: tagFileUsernames, Type: rpmTypeStringArray, Value: filesInfo.FileUserNames},
{ID: tagFileGroupnames, Type: rpmTypeStringArray, Value: filesInfo.FileGroupNames},
{ID: tagFileSizes, Type: rpmTypeInt32, Value: filesInfo.FileSizes},
{ID: tagFileModes, Type: rpmTypeInt16, Value: filesInfo.FileModes},
{ID: tagFileInodes, Type: rpmTypeInt32, Value: filesInfo.FileInodes},
{ID: tagFileDevices, Type: rpmTypeInt32, Value: filesInfo.FileDevices},
{ID: tagFileRdevs, Type: rpmTypeInt16, Value: filesInfo.FileRdevs},
{ID: tagFileMtimes, Type: rpmTypeInt32, Value: filesInfo.FileMtimes},
{ID: tagFileFlags, Type: rpmTypeInt32, Value: filesInfo.FileFlags},
{ID: tagFileLangs, Type: rpmTypeStringArray, Value: filesInfo.FileLangs},
{ID: tagFileDigests, Type: rpmTypeStringArray, Value: filesInfo.FileDigests},
{ID: tagFileLinkTos, Type: rpmTypeStringArray, Value: filesInfo.FileLinkTos},
{ID: tagSize, Type: rpmTypeInt32, Value: []int32{int32(payloadSize)}},
{ID: tagPayloadDigest, Type: rpmTypeStringArray, Value: []string{payloadDigest}},
{ID: tagPayloadDigestAlgo, Type: rpmTypeInt32, Value: []int32{int32(payloadDigestAlgo)}},
}...)
deps, err := parseAllDependencies(cmdCtx, packCtx)
if err != nil {
return nil, err
}
addDependenciesRPM(&rpmHeader, deps)
addPreAndPostInstallScriptsRPM(&rpmHeader, packCtx.RpmDeb.PreInst,
packCtx.RpmDeb.PostInst)
return rpmHeader, nil
}
// getFilesInfo returns the meta information about all items inside the passed
// directory needed for packing it into rpm headers.
func getFilesInfo(relPaths []string, dirPath string) (filesInfo, error) {
info := filesInfo{}
for _, relPath := range relPaths {
fullFilePath := filepath.Join(dirPath, relPath)
fileInfo, err := os.Lstat(fullFilePath)
if err != nil {
return info, err
}
if fileInfo.Mode().IsRegular() {
info.FileFlags = append(info.FileFlags, fileFlag)
fileDigest, err := util.FileMD5Hex(fullFilePath)
if err != nil {
return info, fmt.Errorf("failed to get file MD5 hex: %s", err)
}
info.FileDigests = append(info.FileDigests, fileDigest)
} else {
info.FileFlags = append(info.FileFlags, dirFlag)
info.FileDigests = append(info.FileDigests, emptyDigest)
}
fileDir := filepath.Dir(relPath)
fileDir = fmt.Sprintf("/%s/", fileDir)
dirIndex := addDirAndGetIndex(&info.DirNames, fileDir)
info.DirIndexes = append(info.DirIndexes, int32(dirIndex))
info.BaseNames = append(info.BaseNames, filepath.Base(relPath))
info.FileMtimes = append(info.FileMtimes, int32(fileInfo.ModTime().Unix()))
info.FileUserNames = append(info.FileUserNames, defaultFileUser)
info.FileGroupNames = append(info.FileGroupNames, defaultFileGroup)
info.FileLangs = append(info.FileLangs, defaultFileLang)
sysFileInfo, ok := fileInfo.Sys().(*syscall.Stat_t)
if !ok {
return info, fmt.Errorf("failed to get file info")
}
// If file is a symlink, it is needed to be the relative one.
if fileInfo.Mode().Type() == fs.ModeSymlink {
currentDir, err := os.Getwd()
if err != nil {
return filesInfo{}, err
}
os.Chdir(filepath.Dir(fullFilePath))
path, err := filepath.EvalSymlinks(filepath.Base(fullFilePath))
if err != nil {
os.Chdir(currentDir)
return filesInfo{}, err
}
info.FileLinkTos = append(info.FileLinkTos, path)
os.Chdir(currentDir)
} else {
info.FileLinkTos = append(info.FileLinkTos, defaultFileLinkTo)
}
info.FileSizes = append(info.FileSizes, int32(sysFileInfo.Size))
info.FileModes = append(info.FileModes, int16(sysFileInfo.Mode))
info.FileInodes = append(info.FileInodes, int32(sysFileInfo.Ino))
info.FileDevices = append(info.FileDevices, int32(sysFileInfo.Dev))
info.FileRdevs = append(info.FileRdevs, int16(sysFileInfo.Rdev))
}
return info, nil
}
// addDirAndGetIndex accepts a slice of directory names and a file directory name,
// checks if the passed file directory name is already included in slice and returns
// its index. Otherwise, it appends this name to the slice and returns its index too.
func addDirAndGetIndex(dirNames *[]string, fileDir string) int {
for i, dirName := range *dirNames {
if dirName == fileDir {
return i
}
}
*dirNames = append(*dirNames, fileDir)
return len(*dirNames) - 1
}
// getArch returns the architecture for an RPM package.
// Depends on runtime.GOARCH constant.
func getArch() string {
switch runtime.GOARCH {
case "amd64":
return "x86_64"
case "386":
return "i386"
}
log.Warn("The RPM package is going to be built with no architecture specified.")
return "noarch"
}