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