blob: f9ddbae8a1dfbd1cf2e0abd163817a525332efdb [file] [log] [blame]
Bob Badourdc62de42022-10-12 20:10:17 -07001// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package projectmetadata
16
17import (
18 "fmt"
19 "io"
20 "io/fs"
21 "path/filepath"
22 "strings"
23 "sync"
24
25 "android/soong/compliance/project_metadata_proto"
26
27 "google.golang.org/protobuf/encoding/prototext"
28)
29
30var (
31 // ConcurrentReaders is the size of the task pool for limiting resource usage e.g. open files.
32 ConcurrentReaders = 5
33)
34
35// ProjectMetadata contains the METADATA for a git project.
36type ProjectMetadata struct {
37 proto project_metadata_proto.Metadata
38
39 // project is the path to the directory containing the METADATA file.
40 project string
41}
42
Ibrahim Kanouche776ad802022-10-21 20:47:42 +000043// ProjectUrlMap maps url type name to url value
44type ProjectUrlMap map[string]string
45
46// DownloadUrl returns the address of a download location
47func (m ProjectUrlMap) DownloadUrl() string {
48 for _, urlType := range []string{"GIT", "SVN", "HG", "DARCS"} {
49 if url, ok := m[urlType]; ok {
50 return url
51 }
52 }
53 return ""
54}
55
Bob Badourdc62de42022-10-12 20:10:17 -070056// String returns a string representation of the metadata for error messages.
57func (pm *ProjectMetadata) String() string {
58 return fmt.Sprintf("project: %q\n%s", pm.project, pm.proto.String())
59}
60
Ibrahim Kanouche776ad802022-10-21 20:47:42 +000061// ProjectName returns the name of the project.
62func (pm *ProjectMetadata) Name() string {
63 return pm.proto.GetName()
64}
65
66// ProjectVersion returns the version of the project if available.
67func (pm *ProjectMetadata) Version() string {
68 tp := pm.proto.GetThirdParty()
69 if tp != nil {
70 version := tp.GetVersion()
71 return version
72 }
73 return ""
74}
75
Bob Badourdc62de42022-10-12 20:10:17 -070076// VersionedName returns the name of the project including the version if any.
77func (pm *ProjectMetadata) VersionedName() string {
78 name := pm.proto.GetName()
79 if name != "" {
80 tp := pm.proto.GetThirdParty()
81 if tp != nil {
82 version := tp.GetVersion()
83 if version != "" {
84 if version[0] == 'v' || version[0] == 'V' {
85 return name + "_" + version
86 } else {
87 return name + "_v_" + version
88 }
89 }
90 }
91 return name
92 }
93 return pm.proto.GetDescription()
94}
95
Ibrahim Kanouche776ad802022-10-21 20:47:42 +000096// UrlsByTypeName returns a map of URLs by Type Name
97func (pm *ProjectMetadata) UrlsByTypeName() ProjectUrlMap {
98 tp := pm.proto.GetThirdParty()
99 if tp == nil {
100 return nil
101 }
102 if len(tp.Url) == 0 {
103 return nil
104 }
105 urls := make(ProjectUrlMap)
106
107 for _, url := range tp.Url {
108 uri := url.GetValue()
109 if uri == "" {
110 continue
111 }
112 urls[project_metadata_proto.URL_Type_name[int32(url.GetType())]] = uri
113 }
114 return urls
115}
116
Bob Badourdc62de42022-10-12 20:10:17 -0700117// projectIndex describes a project to be read; after `wg.Wait()`, will contain either
118// a `ProjectMetadata`, pm (can be nil even without error), or a non-nil `err`.
119type projectIndex struct {
120 project string
Bob Badourd6574e52022-10-27 15:19:58 -0700121 path string
Ibrahim Kanouche776ad802022-10-21 20:47:42 +0000122 pm *ProjectMetadata
123 err error
124 done chan struct{}
Bob Badourdc62de42022-10-12 20:10:17 -0700125}
126
127// finish marks the task to read the `projectIndex` completed.
128func (pi *projectIndex) finish() {
129 close(pi.done)
130}
131
132// wait suspends execution until the `projectIndex` task completes.
133func (pi *projectIndex) wait() {
134 <-pi.done
135}
136
137// Index reads and caches ProjectMetadata (thread safe)
138type Index struct {
139 // projecs maps project name to a wait group if read has already started, and
140 // to a `ProjectMetadata` or to an `error` after the read completes.
141 projects sync.Map
142
143 // task provides a fixed-size task pool to limit concurrent open files etc.
144 task chan bool
145
146 // rootFS locates the root of the file system from which to read the files.
147 rootFS fs.FS
148}
149
150// NewIndex constructs a project metadata `Index` for the given file system.
151func NewIndex(rootFS fs.FS) *Index {
152 ix := &Index{task: make(chan bool, ConcurrentReaders), rootFS: rootFS}
153 for i := 0; i < ConcurrentReaders; i++ {
154 ix.task <- true
155 }
156 return ix
157}
158
159// MetadataForProjects returns 0..n ProjectMetadata for n `projects`, or an error.
160// Each project that has a METADATA.android or a METADATA file in the root of the project will have
161// a corresponding ProjectMetadata in the result. Projects with neither file get skipped. A nil
162// result with no error indicates none of the given `projects` has a METADATA file.
163// (thread safe -- can be called concurrently from multiple goroutines)
164func (ix *Index) MetadataForProjects(projects ...string) ([]*ProjectMetadata, error) {
165 if ConcurrentReaders < 1 {
166 return nil, fmt.Errorf("need at least one task in project metadata pool")
167 }
168 if len(projects) == 0 {
169 return nil, nil
170 }
171 // Identify the projects that have never been read
172 projectsToRead := make([]*projectIndex, 0, len(projects))
173 projectIndexes := make([]*projectIndex, 0, len(projects))
174 for _, p := range projects {
175 pi, loaded := ix.projects.LoadOrStore(p, &projectIndex{project: p, done: make(chan struct{})})
176 if !loaded {
177 projectsToRead = append(projectsToRead, pi.(*projectIndex))
178 }
179 projectIndexes = append(projectIndexes, pi.(*projectIndex))
180 }
181 // findMeta locates and reads the appropriate METADATA file, if any.
182 findMeta := func(pi *projectIndex) {
183 <-ix.task
184 defer func() {
185 ix.task <- true
186 pi.finish()
187 }()
188
189 // Support METADATA.android for projects that already have a different sort of METADATA file.
190 path := filepath.Join(pi.project, "METADATA.android")
191 fi, err := fs.Stat(ix.rootFS, path)
192 if err == nil {
193 if fi.Mode().IsRegular() {
194 ix.readMetadataFile(pi, path)
195 return
196 }
197 }
198 // No METADATA.android try METADATA file.
199 path = filepath.Join(pi.project, "METADATA")
200 fi, err = fs.Stat(ix.rootFS, path)
201 if err == nil {
202 if fi.Mode().IsRegular() {
203 ix.readMetadataFile(pi, path)
204 return
205 }
206 }
207 // no METADATA file exists -- leave nil and finish
208 }
209 // Look for the METADATA files to read, and record any missing.
210 for _, p := range projectsToRead {
211 go findMeta(p)
212 }
213 // Wait until all of the projects have been read.
214 var msg strings.Builder
215 result := make([]*ProjectMetadata, 0, len(projects))
216 for _, pi := range projectIndexes {
217 pi.wait()
218 // Combine any errors into a single error.
219 if pi.err != nil {
220 fmt.Fprintf(&msg, " %v\n", pi.err)
221 } else if pi.pm != nil {
222 result = append(result, pi.pm)
223 }
224 }
225 if msg.Len() > 0 {
226 return nil, fmt.Errorf("error reading project(s):\n%s", msg.String())
227 }
228 if len(result) == 0 {
229 return nil, nil
230 }
231 return result, nil
232}
233
Bob Badourd6574e52022-10-27 15:19:58 -0700234// AllMetadataFiles returns the sorted list of all METADATA files read thus far.
235func (ix *Index) AllMetadataFiles() []string {
236 files := []string(nil)
237 ix.projects.Range(func(key, value any) bool {
238 pi := value.(*projectIndex)
239 if pi.path != "" {
240 files = append(files, pi.path)
241 }
242 return true
243 })
244 return files
245}
246
Bob Badourdc62de42022-10-12 20:10:17 -0700247// readMetadataFile tries to read and parse a METADATA file at `path` for `project`.
248func (ix *Index) readMetadataFile(pi *projectIndex, path string) {
249 f, err := ix.rootFS.Open(path)
250 if err != nil {
251 pi.err = fmt.Errorf("error opening project %q metadata %q: %w", pi.project, path, err)
252 return
253 }
254
255 // read the file
256 data, err := io.ReadAll(f)
257 if err != nil {
258 pi.err = fmt.Errorf("error reading project %q metadata %q: %w", pi.project, path, err)
259 return
260 }
261 f.Close()
262
263 uo := prototext.UnmarshalOptions{DiscardUnknown: true}
264 pm := &ProjectMetadata{project: pi.project}
265 err = uo.Unmarshal(data, &pm.proto)
266 if err != nil {
Bob Badourd6574e52022-10-27 15:19:58 -0700267 pi.err = fmt.Errorf(`error in project %q METADATA %q: %v
268
269METADATA and METADATA.android files must parse as text protobufs
270defined by
271 build/soong/compliance/project_metadata_proto/project_metadata.proto
272
273* unknown fields don't matter
274* check invalid ENUM names
275* check quoting
276* check unescaped nested quotes
277* check the comment marker for protobuf is '#' not '//'
278
279if importing a library that uses a different sort of METADATA file, add
280a METADATA.android file beside it to parse instead
281`, pi.project, path, err)
Bob Badourdc62de42022-10-12 20:10:17 -0700282 return
283 }
284
Bob Badourd6574e52022-10-27 15:19:58 -0700285 pi.path = path
Bob Badourdc62de42022-10-12 20:10:17 -0700286 pi.pm = pm
287}