Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 17 | #ifndef COORDINATOR_H_ |
| 18 | |
| 19 | #define COORDINATOR_H_ |
| 20 | |
| 21 | #include <android-base/macros.h> |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 22 | #include <functional> |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 23 | #include <map> |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 24 | #include <set> |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 25 | #include <string> |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 26 | #include <utils/Errors.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 27 | #include <vector> |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | struct AST; |
| 32 | struct FQName; |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 33 | struct Type; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 34 | |
| 35 | struct Coordinator { |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 36 | Coordinator( |
| 37 | const std::vector<std::string> &packageRootPaths, |
Steven Moreland | f7fa068 | 2017-05-11 16:14:55 -0700 | [diff] [blame] | 38 | const std::vector<std::string> &packageRoots, |
| 39 | const std::string &rootPath); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 40 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 41 | ~Coordinator(); |
| 42 | |
Steven Moreland | 25c8166 | 2017-05-12 14:57:36 -0700 | [diff] [blame] | 43 | // adds path only if it doesn't exist |
| 44 | void addDefaultPackagePath(const std::string& root, const std::string& path); |
| 45 | |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame^] | 46 | enum class Enforce { |
| 47 | FULL, // default |
| 48 | NO_HASH, // only for use with -Lhash |
| 49 | NONE, // only for use during enforcement |
| 50 | }; |
| 51 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 52 | // Attempts to parse the interface/types referred to by fqName. |
| 53 | // Parsing an interface also parses the associated package's types.hal |
| 54 | // file if it exists. |
| 55 | // If "parsedASTs" is non-NULL, successfully parsed ASTs are inserted |
| 56 | // into the set. |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 57 | // If !enforce, enforceRestrictionsOnPackage won't be run. |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame^] | 58 | AST* parse(const FQName& fqName, std::set<AST*>* parsedASTs = nullptr, |
| 59 | Enforce enforcement = Enforce::FULL) const; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 60 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 61 | // Given package-root paths of ["hardware/interfaces", |
| 62 | // "vendor/<something>/interfaces"], package roots of |
| 63 | // ["android.hardware", "vendor.<something>.hardware"], and a |
| 64 | // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath() |
Yifan Hong | 97288ac | 2016-12-12 16:03:51 -0800 | [diff] [blame] | 65 | // will return "hardware/interfaces/nfc/1.0" (if sanitized = false) |
| 66 | // or "hardware/interfaces/nfc/V1_0" (if sanitized = true). |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 67 | std::string getPackagePath( |
Yifan Hong | 97288ac | 2016-12-12 16:03:51 -0800 | [diff] [blame] | 68 | const FQName &fqName, bool relative = false, |
| 69 | bool sanitized = false) const; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 70 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 71 | // Given package roots of ["android.hardware", |
| 72 | // "vendor.<something>.hardware"] and a FQName of |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 73 | // "android.hardware.nfc@1.0::INfc, then getPackageRoot() will |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 74 | // return "android.hardware". |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 75 | std::string getPackageRoot(const FQName &fqName) const; |
| 76 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 77 | // Given package-root paths of ["hardware/interfaces", |
| 78 | // "vendor/<something>/interfaces"], package roots of |
| 79 | // ["android.hardware", "vendor.<something>.hardware"], and a |
| 80 | // FQName of "android.hardware.nfc@1.0::INfc, then getPackageRootPath() |
| 81 | // will return "hardware/interfaces". |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 82 | std::string getPackageRootPath(const FQName &fqName) const; |
| 83 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 84 | // return getPackageRoot + ":" + getPackageRootPath |
| 85 | std::string getPackageRootOption(const FQName &fqName) const; |
| 86 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 87 | // Given an FQName of "android.hardware.nfc@1.0::INfc", return |
| 88 | // "android/hardware/". |
| 89 | std::string convertPackageRootToPath(const FQName &fqName) const; |
| 90 | |
| 91 | status_t getPackageInterfaceFiles( |
| 92 | const FQName &package, |
| 93 | std::vector<std::string> *fileNames) const; |
| 94 | |
Steven Moreland | aa18683 | 2016-09-26 13:51:43 -0700 | [diff] [blame] | 95 | status_t appendPackageInterfacesToVector( |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 96 | const FQName &package, |
| 97 | std::vector<FQName> *packageInterfaces) const; |
| 98 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 99 | // Enforce a set of restrictions on a set of packages. These include: |
| 100 | // - minor version upgrades |
| 101 | // "packages" contains names like "android.hardware.nfc@1.1". |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 102 | // - hashing restrictions |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame^] | 103 | status_t enforceRestrictionsOnPackage(const FQName& fqName, |
| 104 | Enforce enforcement = Enforce::FULL) const; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 105 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 106 | static bool MakeParentHierarchy(const std::string &path); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 107 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 108 | private: |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 109 | // A list of top-level directories (mPackageRootPaths) |
| 110 | // corresponding to a list of package roots (mPackageRoots). For |
| 111 | // example, if mPackageRootPaths[0] == "hardware/interfaces" and |
| 112 | // mPackageRoots[0] == "android.hardware" this means that all |
| 113 | // packages starting with "android.hardware" will be looked up in |
| 114 | // "hardware/interfaces". |
| 115 | std::vector<std::string> mPackageRootPaths; |
| 116 | std::vector<std::string> mPackageRoots; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 117 | |
Steven Moreland | f7fa068 | 2017-05-11 16:14:55 -0700 | [diff] [blame] | 118 | std::string mRootPath; |
| 119 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 120 | // cache to parse(). |
Steven Moreland | 28b9b53 | 2017-05-12 17:02:58 -0700 | [diff] [blame] | 121 | mutable std::map<FQName, AST *> mCache; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 122 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 123 | // cache to enforceRestrictionsOnPackage(). |
Steven Moreland | 28b9b53 | 2017-05-12 17:02:58 -0700 | [diff] [blame] | 124 | mutable std::set<FQName> mPackagesEnforced; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 125 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 126 | std::vector<std::string>::const_iterator findPackageRoot( |
| 127 | const FQName &fqName) const; |
| 128 | |
Steven Moreland | b90d327 | 2017-05-26 10:02:39 -0700 | [diff] [blame] | 129 | // Returns abs package path by prepending the root path if a package |
| 130 | // path is non-absolute. |
| 131 | // If root is '/android/master' and getPackagePath returns 'h/i/nfc/V1_0' |
| 132 | // this will return '/android/master/h/i/nfc/V1_0'. |
| 133 | // If root is '/android/master' and getPackagePath returns '/abs/path/to/nfc/V1_0' |
| 134 | // this will return '/abs/path/to/nfc/V1_0' |
| 135 | std::string getAbsolutePackagePath(const FQName& fqName) const; |
| 136 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 137 | // Rules of enforceRestrictionsOnPackage are listed below. |
Steven Moreland | 28b9b53 | 2017-05-12 17:02:58 -0700 | [diff] [blame] | 138 | status_t enforceMinorVersionUprevs(const FQName &fqName) const; |
| 139 | status_t enforceHashes(const FQName &fqName) const; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 140 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 141 | DISALLOW_COPY_AND_ASSIGN(Coordinator); |
| 142 | }; |
| 143 | |
| 144 | } // namespace android |
| 145 | |
| 146 | #endif // COORDINATOR_H_ |