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 | #include "Coordinator.h" |
| 18 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 21 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | #include <iterator> |
| 24 | |
| 25 | #include <android-base/logging.h> |
Steven Moreland | 5bdfa70 | 2017-04-18 23:20:39 -0700 | [diff] [blame] | 26 | #include <hidl-hash/Hash.h> |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 27 | #include <hidl-util/StringHelper.h> |
| 28 | |
| 29 | #include "AST.h" |
| 30 | #include "Interface.h" |
| 31 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 32 | extern android::status_t parseFile(android::AST *ast); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 33 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 34 | static bool existdir(const char *name) { |
| 35 | DIR *dir = opendir(name); |
| 36 | if (dir == NULL) { |
| 37 | return false; |
| 38 | } |
| 39 | closedir(dir); |
| 40 | return true; |
| 41 | } |
| 42 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 45 | Coordinator::Coordinator( |
| 46 | const std::vector<std::string> &packageRootPaths, |
Steven Moreland | f7fa068 | 2017-05-11 16:14:55 -0700 | [diff] [blame] | 47 | const std::vector<std::string> &packageRoots, |
| 48 | const std::string &rootPath) |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 49 | : mPackageRootPaths(packageRootPaths), |
Steven Moreland | f7fa068 | 2017-05-11 16:14:55 -0700 | [diff] [blame] | 50 | mPackageRoots(packageRoots), |
| 51 | mRootPath(rootPath) { |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 52 | // empty |
Andreas Huber | dc98133 | 2016-07-29 15:46:54 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 55 | Coordinator::~Coordinator() { |
| 56 | // empty |
| 57 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 58 | |
Steven Moreland | 25c8166 | 2017-05-12 14:57:36 -0700 | [diff] [blame] | 59 | void Coordinator::addDefaultPackagePath(const std::string& root, const std::string& path) { |
| 60 | if (std::find(mPackageRoots.begin(), mPackageRoots.end(), root) == mPackageRoots.end()) { |
| 61 | mPackageRoots.push_back(root); |
| 62 | mPackageRootPaths.push_back(path); |
| 63 | } |
| 64 | } |
| 65 | |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 66 | AST *Coordinator::parse(const FQName &fqName, std::set<AST *> *parsedASTs, bool enforce) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 67 | CHECK(fqName.isFullyQualified()); |
| 68 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 69 | auto it = mCache.find(fqName); |
| 70 | if (it != mCache.end()) { |
| 71 | AST *ast = (*it).second; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 72 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 73 | if (ast != nullptr && parsedASTs != nullptr) { |
| 74 | parsedASTs->insert(ast); |
| 75 | } |
| 76 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 77 | return ast; |
| 78 | } |
| 79 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 80 | // Add this to the cache immediately, so we can discover circular imports. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 81 | mCache[fqName] = nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 82 | |
| 83 | AST *typesAST = nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 84 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 85 | if (fqName.name() != "types") { |
| 86 | // Any interface file implicitly imports its package's types.hal. |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 87 | FQName typesName = fqName.getTypesForPackage(); |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 88 | // Do not enforce on imports. |
| 89 | typesAST = parse(typesName, parsedASTs, false /* enforce */); |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 90 | |
| 91 | // fall through. |
| 92 | } |
| 93 | |
Steven Moreland | f7fa068 | 2017-05-11 16:14:55 -0700 | [diff] [blame] | 94 | std::string path = mRootPath + getPackagePath(fqName); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 95 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 96 | path.append(fqName.name()); |
| 97 | path.append(".hal"); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 98 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 99 | AST *ast = new AST(this, path); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 100 | |
| 101 | if (typesAST != NULL) { |
| 102 | // If types.hal for this AST's package existed, make it's defined |
| 103 | // types available to the (about to be parsed) AST right away. |
| 104 | ast->addImportedAST(typesAST); |
| 105 | } |
| 106 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 107 | status_t err = parseFile(ast); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 108 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 109 | if (err != OK) { |
| 110 | delete ast; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 111 | ast = nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 112 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 113 | return nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 116 | if (ast->package().package() != fqName.package() |
| 117 | || ast->package().version() != fqName.version()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 118 | fprintf(stderr, |
| 119 | "ERROR: File at '%s' does not match expected package and/or " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 120 | "version.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 121 | path.c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 122 | |
| 123 | err = UNKNOWN_ERROR; |
| 124 | } else { |
| 125 | std::string ifaceName; |
| 126 | if (ast->isInterface(&ifaceName)) { |
| 127 | if (fqName.name() == "types") { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 128 | fprintf(stderr, |
| 129 | "ERROR: File at '%s' declares an interface '%s' " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 130 | "instead of the expected types common to the package.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 131 | path.c_str(), |
| 132 | ifaceName.c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 133 | |
| 134 | err = UNKNOWN_ERROR; |
| 135 | } else if (ifaceName != fqName.name()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 136 | fprintf(stderr, |
| 137 | "ERROR: File at '%s' does not declare interface type " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 138 | "'%s'.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 139 | path.c_str(), |
| 140 | fqName.name().c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 141 | |
| 142 | err = UNKNOWN_ERROR; |
| 143 | } |
| 144 | } else if (fqName.name() != "types") { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 145 | fprintf(stderr, |
| 146 | "ERROR: File at '%s' declares types rather than the " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 147 | "expected interface type '%s'.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 148 | path.c_str(), |
| 149 | fqName.name().c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 150 | |
| 151 | err = UNKNOWN_ERROR; |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 152 | } else if (ast->containsInterfaces()) { |
| 153 | fprintf(stderr, |
| 154 | "ERROR: types.hal file at '%s' declares at least one " |
| 155 | "interface type.\n", |
| 156 | path.c_str()); |
| 157 | |
| 158 | err = UNKNOWN_ERROR; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | if (err != OK) { |
| 163 | delete ast; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 164 | ast = nullptr; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 165 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 166 | return nullptr; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 169 | if (parsedASTs != nullptr) { parsedASTs->insert(ast); } |
| 170 | |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 171 | // put it into the cache now, so that enforceRestrictionsOnPackage can |
| 172 | // parse fqName. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 173 | mCache[fqName] = ast; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 174 | |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 175 | if (enforce) { |
| 176 | // For each .hal file that hidl-gen parses, the whole package will be checked. |
| 177 | err = enforceRestrictionsOnPackage(fqName); |
| 178 | if (err != OK) { |
| 179 | mCache[fqName] = nullptr; |
| 180 | delete ast; |
| 181 | ast = nullptr; |
| 182 | return nullptr; |
| 183 | } |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 186 | return ast; |
| 187 | } |
| 188 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 189 | std::vector<std::string>::const_iterator |
| 190 | Coordinator::findPackageRoot(const FQName &fqName) const { |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 191 | CHECK(!fqName.package().empty()); |
| 192 | CHECK(!fqName.version().empty()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 193 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 194 | // Find the right package prefix and path for this FQName. For |
| 195 | // example, if FQName is "android.hardware.nfc@1.0::INfc", and the |
| 196 | // prefix:root is set to [ "android.hardware:hardware/interfaces", |
| 197 | // "vendor.qcom.hardware:vendor/qcom"], then we will identify the |
| 198 | // prefix "android.hardware" and the package root |
| 199 | // "hardware/interfaces". |
| 200 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 201 | auto it = mPackageRoots.begin(); |
Steven Moreland | 62709d7 | 2017-01-18 10:14:51 -0800 | [diff] [blame] | 202 | auto ret = mPackageRoots.end(); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 203 | for (; it != mPackageRoots.end(); it++) { |
Steven Moreland | 62709d7 | 2017-01-18 10:14:51 -0800 | [diff] [blame] | 204 | if (!fqName.inPackage(*it)) { |
| 205 | continue; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 206 | } |
Steven Moreland | 62709d7 | 2017-01-18 10:14:51 -0800 | [diff] [blame] | 207 | |
| 208 | CHECK(ret == mPackageRoots.end()) |
| 209 | << "Multiple package roots found for " << fqName.string() |
| 210 | << " (" << *it << " and " << *ret << ")"; |
| 211 | |
| 212 | ret = it; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 213 | } |
Steven Moreland | 62709d7 | 2017-01-18 10:14:51 -0800 | [diff] [blame] | 214 | CHECK(ret != mPackageRoots.end()) |
Andreas Huber | 401cd16 | 2016-08-26 10:40:30 -0700 | [diff] [blame] | 215 | << "Unable to find package root for " << fqName.string(); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 216 | |
Steven Moreland | 62709d7 | 2017-01-18 10:14:51 -0800 | [diff] [blame] | 217 | return ret; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | std::string Coordinator::getPackageRoot(const FQName &fqName) const { |
| 221 | auto it = findPackageRoot(fqName); |
| 222 | auto prefix = *it; |
| 223 | return prefix; |
| 224 | } |
| 225 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 226 | std::string Coordinator::getPackageRootPath(const FQName &fqName) const { |
| 227 | auto it = findPackageRoot(fqName); |
| 228 | auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)]; |
| 229 | return root; |
| 230 | } |
| 231 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 232 | std::string Coordinator::getPackageRootOption(const FQName &fqName) const { |
| 233 | return getPackageRoot(fqName) + ":" + getPackageRootPath(fqName); |
| 234 | } |
| 235 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 236 | std::string Coordinator::getPackagePath( |
Yifan Hong | 97288ac | 2016-12-12 16:03:51 -0800 | [diff] [blame] | 237 | const FQName &fqName, bool relative, bool sanitized) const { |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 238 | |
| 239 | auto it = findPackageRoot(fqName); |
| 240 | auto prefix = *it; |
| 241 | auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)]; |
| 242 | |
| 243 | // Make sure the prefix ends on a '.' and the root path on a '/' |
| 244 | if ((*--prefix.end()) != '.') { |
| 245 | prefix += '.'; |
| 246 | } |
| 247 | |
| 248 | if ((*--root.end()) != '/') { |
| 249 | root += '/'; |
| 250 | } |
| 251 | |
| 252 | // Given FQName of "android.hardware.nfc@1.0::IFoo" and a prefix |
| 253 | // "android.hardware.", the suffix is "nfc@1.0::IFoo". |
| 254 | const std::string packageSuffix = fqName.package().substr(prefix.length()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 255 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 256 | std::string packagePath; |
| 257 | if (!relative) { |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 258 | packagePath = root; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 259 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 260 | |
| 261 | size_t startPos = 0; |
| 262 | size_t dotPos; |
| 263 | while ((dotPos = packageSuffix.find('.', startPos)) != std::string::npos) { |
| 264 | packagePath.append(packageSuffix.substr(startPos, dotPos - startPos)); |
| 265 | packagePath.append("/"); |
| 266 | |
| 267 | startPos = dotPos + 1; |
| 268 | } |
| 269 | CHECK_LT(startPos + 1, packageSuffix.length()); |
| 270 | packagePath.append(packageSuffix.substr(startPos)); |
| 271 | packagePath.append("/"); |
| 272 | |
Yifan Hong | 97288ac | 2016-12-12 16:03:51 -0800 | [diff] [blame] | 273 | packagePath.append(sanitized ? fqName.sanitizedVersion() : fqName.version()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 274 | packagePath.append("/"); |
| 275 | |
| 276 | return packagePath; |
| 277 | } |
| 278 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 279 | status_t Coordinator::getPackageInterfaceFiles( |
| 280 | const FQName &package, |
| 281 | std::vector<std::string> *fileNames) const { |
| 282 | fileNames->clear(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 283 | |
Steven Moreland | f7fa068 | 2017-05-11 16:14:55 -0700 | [diff] [blame] | 284 | const std::string packagePath = mRootPath + getPackagePath(package); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 285 | |
| 286 | DIR *dir = opendir(packagePath.c_str()); |
| 287 | |
| 288 | if (dir == NULL) { |
Steven Moreland | 028d5a3 | 2017-05-12 15:45:56 -0700 | [diff] [blame^] | 289 | fprintf(stderr, |
| 290 | "ERROR: Could not open package path %s for package %s:\n%s\n", |
| 291 | getPackagePath(package).c_str(), |
| 292 | package.string().c_str(), |
| 293 | packagePath.c_str()); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 294 | return -errno; |
| 295 | } |
| 296 | |
| 297 | struct dirent *ent; |
| 298 | while ((ent = readdir(dir)) != NULL) { |
| 299 | if (ent->d_type != DT_REG) { |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 300 | continue; |
| 301 | } |
| 302 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 303 | const auto suffix = ".hal"; |
| 304 | const auto suffix_len = std::strlen(suffix); |
| 305 | const auto d_namelen = strlen(ent->d_name); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 306 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 307 | if (d_namelen < suffix_len |
| 308 | || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) { |
| 309 | continue; |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 310 | } |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 311 | |
| 312 | fileNames->push_back(std::string(ent->d_name, d_namelen - suffix_len)); |
| 313 | } |
| 314 | |
| 315 | closedir(dir); |
| 316 | dir = NULL; |
| 317 | |
Iliyan Malchev | 5e8bcfa | 2016-09-09 16:19:57 -0700 | [diff] [blame] | 318 | std::sort(fileNames->begin(), fileNames->end(), |
| 319 | [](const std::string& lhs, const std::string& rhs) -> bool { |
| 320 | if (lhs == "types") { |
| 321 | return true; |
| 322 | } |
| 323 | if (rhs == "types") { |
| 324 | return false; |
| 325 | } |
| 326 | return lhs < rhs; |
| 327 | }); |
| 328 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 329 | return OK; |
| 330 | } |
| 331 | |
Steven Moreland | aa18683 | 2016-09-26 13:51:43 -0700 | [diff] [blame] | 332 | status_t Coordinator::appendPackageInterfacesToVector( |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 333 | const FQName &package, |
| 334 | std::vector<FQName> *packageInterfaces) const { |
| 335 | packageInterfaces->clear(); |
| 336 | |
| 337 | std::vector<std::string> fileNames; |
| 338 | status_t err = getPackageInterfaceFiles(package, &fileNames); |
| 339 | |
| 340 | if (err != OK) { |
| 341 | return err; |
| 342 | } |
| 343 | |
| 344 | for (const auto &fileName : fileNames) { |
| 345 | FQName subFQName( |
Yifan Hong | 90ea87f | 2016-11-01 14:25:47 -0700 | [diff] [blame] | 346 | package.package() + package.atVersion() + "::" + fileName); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 347 | |
| 348 | if (!subFQName.isValid()) { |
| 349 | LOG(WARNING) |
| 350 | << "Whole-package import encountered invalid filename '" |
| 351 | << fileName |
| 352 | << "' in package " |
| 353 | << package.package() |
Yifan Hong | 90ea87f | 2016-11-01 14:25:47 -0700 | [diff] [blame] | 354 | << package.atVersion(); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 355 | |
| 356 | continue; |
| 357 | } |
| 358 | |
| 359 | packageInterfaces->push_back(subFQName); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | return OK; |
| 363 | } |
| 364 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 365 | std::string Coordinator::convertPackageRootToPath(const FQName &fqName) const { |
| 366 | std::string packageRoot = getPackageRoot(fqName); |
| 367 | |
| 368 | if (*(packageRoot.end()--) != '.') { |
| 369 | packageRoot += '.'; |
| 370 | } |
| 371 | |
| 372 | std::replace(packageRoot.begin(), packageRoot.end(), '.', '/'); |
| 373 | |
| 374 | return packageRoot; // now converted to a path |
| 375 | } |
| 376 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 377 | |
| 378 | status_t Coordinator::enforceRestrictionsOnPackage(const FQName &fqName) { |
| 379 | // need fqName to be something like android.hardware.foo@1.0. |
| 380 | // name and valueName is ignored. |
| 381 | if (fqName.package().empty() || fqName.version().empty()) { |
| 382 | LOG(ERROR) << "Cannot enforce restrictions on package " << fqName.string() |
| 383 | << ": package or version is missing."; |
| 384 | return BAD_VALUE; |
| 385 | } |
| 386 | FQName package = fqName.getPackageAndVersion(); |
| 387 | // look up cache. |
| 388 | if (mPackagesEnforced.find(package) != mPackagesEnforced.end()) { |
| 389 | return OK; |
| 390 | } |
| 391 | |
| 392 | // enforce all rules. |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 393 | status_t err; |
| 394 | |
| 395 | err = enforceMinorVersionUprevs(package); |
| 396 | if (err != OK) { |
| 397 | return err; |
| 398 | } |
| 399 | |
| 400 | err = enforceHashes(package); |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 401 | if (err != OK) { |
| 402 | return err; |
| 403 | } |
| 404 | |
| 405 | // cache it so that it won't need to be enforced again. |
| 406 | mPackagesEnforced.insert(package); |
| 407 | return OK; |
| 408 | } |
| 409 | |
| 410 | status_t Coordinator::enforceMinorVersionUprevs(const FQName ¤tPackage) { |
| 411 | if(!currentPackage.hasVersion()) { |
| 412 | LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string() |
| 413 | << ": missing version."; |
| 414 | return UNKNOWN_ERROR; |
| 415 | } |
| 416 | |
| 417 | if (currentPackage.getPackageMinorVersion() == 0) { |
| 418 | return OK; // ignore for @x.0 |
| 419 | } |
| 420 | |
| 421 | bool hasPrevPackage = false; |
| 422 | FQName prevPacakge = currentPackage; |
| 423 | while (prevPacakge.getPackageMinorVersion() > 0) { |
| 424 | prevPacakge = prevPacakge.downRev(); |
Steven Moreland | f7fa068 | 2017-05-11 16:14:55 -0700 | [diff] [blame] | 425 | if (existdir((mRootPath + getPackagePath(prevPacakge)).c_str())) { |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 426 | hasPrevPackage = true; |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | if (!hasPrevPackage) { |
| 431 | // no @x.z, where z < y, exist. |
| 432 | return OK; |
| 433 | } |
| 434 | |
| 435 | if (prevPacakge != currentPackage.downRev()) { |
| 436 | LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string() |
| 437 | << ": Found package " << prevPacakge.string() << " but missing " |
| 438 | << currentPackage.downRev().string() << "; you cannot skip a minor version."; |
| 439 | return UNKNOWN_ERROR; |
| 440 | } |
| 441 | |
| 442 | status_t err; |
| 443 | std::vector<FQName> packageInterfaces; |
| 444 | err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces); |
| 445 | if (err != OK) { |
| 446 | return err; |
| 447 | } |
| 448 | |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 449 | bool extendedInterface = false; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 450 | for (const FQName ¤tFQName : packageInterfaces) { |
| 451 | if (currentFQName.name() == "types") { |
| 452 | continue; // ignore types.hal |
| 453 | } |
| 454 | // Assume that currentFQName == android.hardware.foo@2.2::IFoo. |
| 455 | // Then prevFQName == android.hardware.foo@2.1::IFoo. |
| 456 | const Interface *iface = nullptr; |
| 457 | AST *currentAST = parse(currentFQName); |
| 458 | if (currentAST != nullptr) { |
| 459 | iface = currentAST->getInterface(); |
| 460 | } |
| 461 | if (iface == nullptr) { |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 462 | if (currentAST == nullptr) { |
| 463 | LOG(WARNING) << "Warning: Skipping " << currentFQName.string() |
| 464 | << " because it could not be found or parsed" |
| 465 | << " or " << currentPackage.string() |
| 466 | << " doesn't pass all requirements."; |
| 467 | } else { |
| 468 | LOG(WARNING) << "Warning: Skipping " << currentFQName.string() |
| 469 | << " because the file might contain more than one interface."; |
| 470 | } |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 471 | continue; |
| 472 | } |
| 473 | |
| 474 | // android.hardware.foo@2.2::IFoo exists. Now make sure |
| 475 | // @2.2::IFoo extends @2.1::IFoo. If any interface IFoo in @2.2 |
| 476 | // ensures this, @2.2 passes the enforcement. |
| 477 | FQName prevFQName(prevPacakge.package(), prevPacakge.version(), |
| 478 | currentFQName.name()); |
| 479 | if (iface->superType() == nullptr) { |
| 480 | // @2.2::IFoo doesn't extend anything. (This is probably IBase.) |
| 481 | continue; |
| 482 | } |
| 483 | if (iface->superType()->fqName() != prevFQName) { |
| 484 | // @2.2::IFoo doesn't extend @2.1::IFoo. |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 485 | if (iface->superType()->fqName().getPackageAndVersion() == |
| 486 | prevPacakge.getPackageAndVersion()) { |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 487 | LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string() |
| 488 | << ": " << iface->fqName().string() << " extends " |
| 489 | << iface->superType()->fqName().string() << ", which is not allowed."; |
| 490 | return UNKNOWN_ERROR; |
| 491 | } |
| 492 | // @2.2::IFoo extends something from a package with a different package name. |
| 493 | // Check the next interface. |
| 494 | continue; |
| 495 | } |
| 496 | |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 497 | // @2.2::IFoo passes. Check next interface. |
| 498 | extendedInterface = true; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 499 | LOG(VERBOSE) << "enforceMinorVersionUprevs: " << currentFQName.string() << " passes."; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 502 | if (!extendedInterface) { |
| 503 | // No interface extends the interface with the same name in @x.(y-1). |
| 504 | LOG(ERROR) << currentPackage.string() << " doesn't pass minor version uprev requirement. " |
| 505 | << "Requires at least one interface to extend an interface with the same name " |
| 506 | << "from " << prevPacakge.string() << "."; |
| 507 | return UNKNOWN_ERROR; |
| 508 | } |
| 509 | |
| 510 | return OK; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 513 | status_t Coordinator::enforceHashes(const FQName ¤tPackage) { |
| 514 | status_t err = OK; |
| 515 | std::vector<FQName> packageInterfaces; |
| 516 | err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces); |
| 517 | if (err != OK) { |
| 518 | return err; |
| 519 | } |
| 520 | |
| 521 | for (const FQName ¤tFQName : packageInterfaces) { |
| 522 | AST *ast = parse(currentFQName); |
| 523 | |
| 524 | if (ast == nullptr) { |
| 525 | err = UNKNOWN_ERROR; |
| 526 | continue; |
| 527 | } |
| 528 | |
Steven Moreland | 5bdfa70 | 2017-04-18 23:20:39 -0700 | [diff] [blame] | 529 | std::string hashPath = getPackageRootPath(currentFQName) + "/current.txt"; |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 530 | std::string error; |
Steven Moreland | 5bdfa70 | 2017-04-18 23:20:39 -0700 | [diff] [blame] | 531 | std::vector<std::string> frozen = Hash::lookupHash(hashPath, currentFQName.string(), &error); |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 532 | |
| 533 | if (error.size() > 0) { |
| 534 | LOG(ERROR) << error; |
| 535 | err = UNKNOWN_ERROR; |
| 536 | continue; |
| 537 | } |
| 538 | |
| 539 | // hash not define, interface not frozen |
| 540 | if (frozen.size() == 0) { |
| 541 | continue; |
| 542 | } |
| 543 | |
| 544 | std::string currentHash = Hash::getHash(ast->getFilename()).hexString(); |
| 545 | |
| 546 | if(std::find(frozen.begin(), frozen.end(), currentHash) == frozen.end()) { |
| 547 | LOG(ERROR) << currentFQName.string() << " has hash " << currentHash |
| 548 | << " which does not match hash on record. This interface has " |
| 549 | << "been frozen. Do not change it!"; |
| 550 | err = UNKNOWN_ERROR; |
| 551 | continue; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | return err; |
| 556 | } |
| 557 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 558 | bool Coordinator::MakeParentHierarchy(const std::string &path) { |
| 559 | static const mode_t kMode = 0755; |
| 560 | |
| 561 | size_t start = 1; // Ignore leading '/' |
| 562 | size_t slashPos; |
| 563 | while ((slashPos = path.find("/", start)) != std::string::npos) { |
| 564 | std::string partial = path.substr(0, slashPos); |
| 565 | |
| 566 | struct stat st; |
| 567 | if (stat(partial.c_str(), &st) < 0) { |
| 568 | if (errno != ENOENT) { |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | int res = mkdir(partial.c_str(), kMode); |
| 573 | if (res < 0) { |
| 574 | return false; |
| 575 | } |
| 576 | } else if (!S_ISDIR(st.st_mode)) { |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | start = slashPos + 1; |
| 581 | } |
| 582 | |
| 583 | return true; |
| 584 | } |
| 585 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 586 | } // namespace android |
| 587 | |