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