blob: b21a95ae754805bfccd53537c552c30d65d1e799 [file] [log] [blame]
Bob Badour9ee7d032021-10-25 16:51:48 -07001// Copyright 2021 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 compliance
16
17// ShippedNodes returns the set of nodes in a license graph where the target or
18// a derivative work gets distributed. (caches result)
Bob Badour3fe369c2022-12-18 14:15:02 -080019func ShippedNodes(lg *LicenseGraph) TargetNodeSet {
Bob Badour9ee7d032021-10-25 16:51:48 -070020 lg.mu.Lock()
21 shipped := lg.shippedNodes
22 lg.mu.Unlock()
23 if shipped != nil {
Bob Badour3fe369c2022-12-18 14:15:02 -080024 return *shipped
Bob Badour9ee7d032021-10-25 16:51:48 -070025 }
26
Bob Badour3fe369c2022-12-18 14:15:02 -080027 tset := make(TargetNodeSet)
Bob Badour9ee7d032021-10-25 16:51:48 -070028
Bob Badour103eb0f2022-01-10 13:50:57 -080029 WalkTopDown(NoEdgeContext{}, lg, func(lg *LicenseGraph, tn *TargetNode, path TargetEdgePath) bool {
Bob Badour9ee7d032021-10-25 16:51:48 -070030 if _, alreadyWalked := tset[tn]; alreadyWalked {
31 return false
32 }
33 if len(path) > 0 {
Bob Badour103eb0f2022-01-10 13:50:57 -080034 if !edgeIsDerivation(path[len(path)-1].edge) {
Bob Badour9ee7d032021-10-25 16:51:48 -070035 return false
36 }
37 }
Bob Badour5446a6f2022-01-10 18:44:59 -080038 tset[tn] = struct{}{}
Bob Badour9ee7d032021-10-25 16:51:48 -070039 return true
40 })
41
Bob Badour3fe369c2022-12-18 14:15:02 -080042 shipped = &tset
Bob Badour9ee7d032021-10-25 16:51:48 -070043
44 lg.mu.Lock()
45 if lg.shippedNodes == nil {
46 lg.shippedNodes = shipped
47 } else {
48 // if we end up with 2, release the later for garbage collection.
49 shipped = lg.shippedNodes
50 }
51 lg.mu.Unlock()
52
Bob Badour3fe369c2022-12-18 14:15:02 -080053 return *shipped
Bob Badour9ee7d032021-10-25 16:51:48 -070054}