-
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
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package conda
import (
"archive/tar"
"archive/zip"
"compress/bzip2"
"io"
"strings"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/validation"
"github.com/klauspost/compress/zstd"
)
var (
ErrInvalidStructure = util.SilentWrap{Message: "package structure is invalid", Err: util.ErrInvalidArgument}
ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument}
ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument}
)
const (
PropertyName = "conda.name"
PropertyChannel = "conda.channel"
PropertySubdir = "conda.subdir"
PropertyMetadata = "conda.metdata"
)
// Package represents a Conda package
type Package struct {
Name string
Version string
Subdir string
VersionMetadata *VersionMetadata
FileMetadata *FileMetadata
}
// VersionMetadata represents the metadata of a Conda package
type VersionMetadata struct {
Description string `json:"description,omitempty"`
Summary string `json:"summary,omitempty"`
ProjectURL string `json:"project_url,omitempty"`
RepositoryURL string `json:"repository_url,omitempty"`
DocumentationURL string `json:"documentation_url,omitempty"`
License string `json:"license,omitempty"`
LicenseFamily string `json:"license_family,omitempty"`
}
// FileMetadata represents the metadata of a Conda package file
type FileMetadata struct {
IsCondaPackage bool `json:"is_conda"`
Architecture string `json:"architecture,omitempty"`
NoArch string `json:"noarch,omitempty"`
Build string `json:"build,omitempty"`
BuildNumber int64 `json:"build_number,omitempty"`
Dependencies []string `json:"dependencies,omitempty"`
Platform string `json:"platform,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
}
type index struct {
Name string `json:"name"`
Version string `json:"version"`
Architecture string `json:"arch"`
NoArch string `json:"noarch"`
Build string `json:"build"`
BuildNumber int64 `json:"build_number"`
Dependencies []string `json:"depends"`
License string `json:"license"`
LicenseFamily string `json:"license_family"`
Platform string `json:"platform"`
Subdir string `json:"subdir"`
Timestamp int64 `json:"timestamp"`
}
type about struct {
Description string `json:"description"`
Summary string `json:"summary"`
ProjectURL string `json:"home"`
RepositoryURL string `json:"dev_url"`
DocumentationURL string `json:"doc_url"`
}
type ReaderAndReaderAt interface {
io.Reader
io.ReaderAt
}
// ParsePackageBZ2 parses the Conda package file compressed with bzip2
func ParsePackageBZ2(r io.Reader) (*Package, error) {
gzr := bzip2.NewReader(r)
return parsePackageTar(gzr)
}
// ParsePackageConda parses the Conda package file compressed with zip and zstd
func ParsePackageConda(r io.ReaderAt, size int64) (*Package, error) {
zr, err := zip.NewReader(r, size)
if err != nil {
return nil, err
}
for _, file := range zr.File {
if strings.HasPrefix(file.Name, "info-") && strings.HasSuffix(file.Name, ".tar.zst") {
f, err := zr.Open(file.Name)
if err != nil {
return nil, err
}
defer f.Close()
dec, err := zstd.NewReader(f)
if err != nil {
return nil, err
}
defer dec.Close()
p, err := parsePackageTar(dec)
if p != nil {
p.FileMetadata.IsCondaPackage = true
}
return p, err
}
}
return nil, ErrInvalidStructure
}
func parsePackageTar(r io.Reader) (*Package, error) {
var i *index
var a *about
tr := tar.NewReader(r)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if hdr.Typeflag != tar.TypeReg {
continue
}
if hdr.Name == "info/index.json" {
if err := json.NewDecoder(tr).Decode(&i); err != nil {
return nil, err
}
if !checkName(i.Name) {
return nil, ErrInvalidName
}
if !checkVersion(i.Version) {
return nil, ErrInvalidVersion
}
if a != nil {
break // stop loop if both files were found
}
} else if hdr.Name == "info/about.json" {
if err := json.NewDecoder(tr).Decode(&a); err != nil {
return nil, err
}
if !validation.IsValidURL(a.ProjectURL) {
a.ProjectURL = ""
}
if !validation.IsValidURL(a.RepositoryURL) {
a.RepositoryURL = ""
}
if !validation.IsValidURL(a.DocumentationURL) {
a.DocumentationURL = ""
}
if i != nil {
break // stop loop if both files were found
}
}
}
if i == nil {
return nil, ErrInvalidStructure
}
if a == nil {
a = &about{}
}
return &Package{
Name: i.Name,
Version: i.Version,
Subdir: i.Subdir,
VersionMetadata: &VersionMetadata{
License: i.License,
LicenseFamily: i.LicenseFamily,
Description: a.Description,
Summary: a.Summary,
ProjectURL: a.ProjectURL,
RepositoryURL: a.RepositoryURL,
DocumentationURL: a.DocumentationURL,
},
FileMetadata: &FileMetadata{
Architecture: i.Architecture,
NoArch: i.NoArch,
Build: i.Build,
BuildNumber: i.BuildNumber,
Dependencies: i.Dependencies,
Platform: i.Platform,
Timestamp: i.Timestamp,
},
}, nil
}
// https://github.com/conda/conda-build/blob/db9a728a9e4e6cfc895637ca3221117970fc2663/conda_build/metadata.py#L1393
func checkName(name string) bool {
if name == "" {
return false
}
if name != strings.ToLower(name) {
return false
}
return !checkBadCharacters(name, "!")
}
// https://github.com/conda/conda-build/blob/db9a728a9e4e6cfc895637ca3221117970fc2663/conda_build/metadata.py#L1403
func checkVersion(version string) bool {
if version == "" {
return false
}
return !checkBadCharacters(version, "-")
}
func checkBadCharacters(s, additional string) bool {
if strings.ContainsAny(s, "=@#$%^&*:;\"'\\|<>?/ ") {
return true
}
return strings.ContainsAny(s, additional)
}