Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package 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 Badour | 3fe369c | 2022-12-18 14:15:02 -0800 | [diff] [blame] | 19 | func ShippedNodes(lg *LicenseGraph) TargetNodeSet { |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 20 | lg.mu.Lock() |
| 21 | shipped := lg.shippedNodes |
| 22 | lg.mu.Unlock() |
| 23 | if shipped != nil { |
Bob Badour | 3fe369c | 2022-12-18 14:15:02 -0800 | [diff] [blame] | 24 | return *shipped |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Bob Badour | 3fe369c | 2022-12-18 14:15:02 -0800 | [diff] [blame] | 27 | tset := make(TargetNodeSet) |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 28 | |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 29 | WalkTopDown(NoEdgeContext{}, lg, func(lg *LicenseGraph, tn *TargetNode, path TargetEdgePath) bool { |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 30 | if _, alreadyWalked := tset[tn]; alreadyWalked { |
| 31 | return false |
| 32 | } |
| 33 | if len(path) > 0 { |
Bob Badour | 103eb0f | 2022-01-10 13:50:57 -0800 | [diff] [blame] | 34 | if !edgeIsDerivation(path[len(path)-1].edge) { |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 35 | return false |
| 36 | } |
| 37 | } |
Bob Badour | 5446a6f | 2022-01-10 18:44:59 -0800 | [diff] [blame] | 38 | tset[tn] = struct{}{} |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 39 | return true |
| 40 | }) |
| 41 | |
Bob Badour | 3fe369c | 2022-12-18 14:15:02 -0800 | [diff] [blame] | 42 | shipped = &tset |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 43 | |
| 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 Badour | 3fe369c | 2022-12-18 14:15:02 -0800 | [diff] [blame] | 53 | return *shipped |
Bob Badour | 9ee7d03 | 2021-10-25 16:51:48 -0700 | [diff] [blame] | 54 | } |