blob: 19cb381db5878866f8754ec5dbbef803b5314b0e [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huber5345ec22016-07-29 13:33:27 -070017#include "Coordinator.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <dirent.h>
Andreas Huberd2943e12016-08-05 11:59:31 -070020#include <sys/stat.h>
Andreas Huber5345ec22016-07-29 13:33:27 -070021
Yifan Hong78b38d12017-02-13 18:14:46 +000022#include <algorithm>
23#include <iterator>
24
25#include <android-base/logging.h>
Steven Moreland5bdfa702017-04-18 23:20:39 -070026#include <hidl-hash/Hash.h>
Yifan Hong78b38d12017-02-13 18:14:46 +000027#include <hidl-util/StringHelper.h>
28
29#include "AST.h"
30#include "Interface.h"
31
Andreas Huber0d0f9a22016-08-17 10:26:11 -070032extern android::status_t parseFile(android::AST *ast);
Andreas Huber5345ec22016-07-29 13:33:27 -070033
Yifan Hong78b38d12017-02-13 18:14:46 +000034static 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 Huber5345ec22016-07-29 13:33:27 -070043namespace android {
44
Andreas Huberdca261f2016-08-04 13:47:51 -070045Coordinator::Coordinator(
46 const std::vector<std::string> &packageRootPaths,
Steven Morelandf7fa0682017-05-11 16:14:55 -070047 const std::vector<std::string> &packageRoots,
48 const std::string &rootPath)
Andreas Huberdca261f2016-08-04 13:47:51 -070049 : mPackageRootPaths(packageRootPaths),
Steven Morelandf7fa0682017-05-11 16:14:55 -070050 mPackageRoots(packageRoots),
51 mRootPath(rootPath) {
Andreas Huberdca261f2016-08-04 13:47:51 -070052 // empty
Andreas Huberdc981332016-07-29 15:46:54 -070053}
54
Andreas Huberdca261f2016-08-04 13:47:51 -070055Coordinator::~Coordinator() {
56 // empty
57}
Andreas Huber5345ec22016-07-29 13:33:27 -070058
Steven Moreland25c81662017-05-12 14:57:36 -070059void 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 Moreland28b9b532017-05-12 17:02:58 -070066AST *Coordinator::parse(const FQName &fqName, std::set<AST *> *parsedASTs, bool enforce) const {
Andreas Huber68f24592016-07-29 14:53:48 -070067 CHECK(fqName.isFullyQualified());
68
Steven Morelandd537ab02016-09-12 10:32:01 -070069 auto it = mCache.find(fqName);
70 if (it != mCache.end()) {
71 AST *ast = (*it).second;
Andreas Huber5345ec22016-07-29 13:33:27 -070072
Andreas Huber39fa7182016-08-19 14:27:33 -070073 if (ast != nullptr && parsedASTs != nullptr) {
74 parsedASTs->insert(ast);
75 }
76
Andreas Huber5345ec22016-07-29 13:33:27 -070077 return ast;
78 }
79
Andreas Huber68f24592016-07-29 14:53:48 -070080 // Add this to the cache immediately, so we can discover circular imports.
Steven Morelandd537ab02016-09-12 10:32:01 -070081 mCache[fqName] = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -070082
83 AST *typesAST = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -070084
Andreas Huber68f24592016-07-29 14:53:48 -070085 if (fqName.name() != "types") {
86 // Any interface file implicitly imports its package's types.hal.
Yifan Hongfece6ec2017-01-12 17:04:04 -080087 FQName typesName = fqName.getTypesForPackage();
Yifan Hongf619fc72017-04-07 15:40:06 -070088 // Do not enforce on imports.
89 typesAST = parse(typesName, parsedASTs, false /* enforce */);
Andreas Huber68f24592016-07-29 14:53:48 -070090
91 // fall through.
92 }
93
Steven Morelandb90d3272017-05-26 10:02:39 -070094 std::string path = getAbsolutePackagePath(fqName);
Andreas Huberdca261f2016-08-04 13:47:51 -070095
Andreas Huber68f24592016-07-29 14:53:48 -070096 path.append(fqName.name());
97 path.append(".hal");
Andreas Huber5345ec22016-07-29 13:33:27 -070098
Andreas Huber0d0f9a22016-08-17 10:26:11 -070099 AST *ast = new AST(this, path);
Andreas Huber39fa7182016-08-19 14:27:33 -0700100
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 Huber0d0f9a22016-08-17 10:26:11 -0700107 status_t err = parseFile(ast);
Andreas Huber5345ec22016-07-29 13:33:27 -0700108
Andreas Huber68f24592016-07-29 14:53:48 -0700109 if (err != OK) {
110 delete ast;
Andreas Huber39fa7182016-08-19 14:27:33 -0700111 ast = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700112
Andreas Huber39fa7182016-08-19 14:27:33 -0700113 return nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700114 }
115
Andreas Hubera2723d22016-07-29 15:36:07 -0700116 if (ast->package().package() != fqName.package()
117 || ast->package().version() != fqName.version()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700118 fprintf(stderr,
119 "ERROR: File at '%s' does not match expected package and/or "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700120 "version.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700121 path.c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700122
123 err = UNKNOWN_ERROR;
124 } else {
Steven Moreland19f11b52017-05-12 18:22:21 -0700125 if (ast->isInterface()) {
Andreas Hubera2723d22016-07-29 15:36:07 -0700126 if (fqName.name() == "types") {
Andreas Huber70a59e12016-08-16 12:57:01 -0700127 fprintf(stderr,
128 "ERROR: File at '%s' declares an interface '%s' "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700129 "instead of the expected types common to the package.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700130 path.c_str(),
Steven Moreland19f11b52017-05-12 18:22:21 -0700131 ast->getInterface()->localName().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700132
133 err = UNKNOWN_ERROR;
Steven Moreland19f11b52017-05-12 18:22:21 -0700134 } else if (ast->getInterface()->localName() != fqName.name()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700135 fprintf(stderr,
136 "ERROR: File at '%s' does not declare interface type "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700137 "'%s'.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700138 path.c_str(),
139 fqName.name().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700140
141 err = UNKNOWN_ERROR;
142 }
143 } else if (fqName.name() != "types") {
Andreas Huber70a59e12016-08-16 12:57:01 -0700144 fprintf(stderr,
145 "ERROR: File at '%s' declares types rather than the "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700146 "expected interface type '%s'.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700147 path.c_str(),
148 fqName.name().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700149
150 err = UNKNOWN_ERROR;
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700151 } 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 Hubera2723d22016-07-29 15:36:07 -0700158 }
159 }
160
161 if (err != OK) {
162 delete ast;
Andreas Huber39fa7182016-08-19 14:27:33 -0700163 ast = nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -0700164
Andreas Huber39fa7182016-08-19 14:27:33 -0700165 return nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -0700166 }
167
Andreas Huber39fa7182016-08-19 14:27:33 -0700168 if (parsedASTs != nullptr) { parsedASTs->insert(ast); }
169
Yifan Hongfe07bff2017-02-15 14:55:48 -0800170 // put it into the cache now, so that enforceRestrictionsOnPackage can
171 // parse fqName.
Steven Morelandd537ab02016-09-12 10:32:01 -0700172 mCache[fqName] = ast;
Andreas Huber5345ec22016-07-29 13:33:27 -0700173
Yifan Hongf619fc72017-04-07 15:40:06 -0700174 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 Hong78b38d12017-02-13 18:14:46 +0000183 }
184
Andreas Huber5345ec22016-07-29 13:33:27 -0700185 return ast;
186}
187
Andreas Huberdca261f2016-08-04 13:47:51 -0700188std::vector<std::string>::const_iterator
189Coordinator::findPackageRoot(const FQName &fqName) const {
Andreas Huber5345ec22016-07-29 13:33:27 -0700190 CHECK(!fqName.package().empty());
191 CHECK(!fqName.version().empty());
Andreas Huber5345ec22016-07-29 13:33:27 -0700192
Andreas Huberdca261f2016-08-04 13:47:51 -0700193 // 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 Huberdca261f2016-08-04 13:47:51 -0700200 auto it = mPackageRoots.begin();
Steven Moreland62709d72017-01-18 10:14:51 -0800201 auto ret = mPackageRoots.end();
Andreas Huberdca261f2016-08-04 13:47:51 -0700202 for (; it != mPackageRoots.end(); it++) {
Steven Moreland62709d72017-01-18 10:14:51 -0800203 if (!fqName.inPackage(*it)) {
204 continue;
Andreas Huberdca261f2016-08-04 13:47:51 -0700205 }
Steven Moreland62709d72017-01-18 10:14:51 -0800206
207 CHECK(ret == mPackageRoots.end())
208 << "Multiple package roots found for " << fqName.string()
209 << " (" << *it << " and " << *ret << ")";
210
211 ret = it;
Andreas Huberdca261f2016-08-04 13:47:51 -0700212 }
Steven Moreland62709d72017-01-18 10:14:51 -0800213 CHECK(ret != mPackageRoots.end())
Andreas Huber401cd162016-08-26 10:40:30 -0700214 << "Unable to find package root for " << fqName.string();
Andreas Huberdca261f2016-08-04 13:47:51 -0700215
Steven Moreland62709d72017-01-18 10:14:51 -0800216 return ret;
Andreas Huberdca261f2016-08-04 13:47:51 -0700217}
218
Steven Morelandb90d3272017-05-26 10:02:39 -0700219std::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 Huberdca261f2016-08-04 13:47:51 -0700229std::string Coordinator::getPackageRoot(const FQName &fqName) const {
230 auto it = findPackageRoot(fqName);
231 auto prefix = *it;
232 return prefix;
233}
234
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700235std::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 Hongc8934042016-11-17 17:10:52 -0800241std::string Coordinator::getPackageRootOption(const FQName &fqName) const {
242 return getPackageRoot(fqName) + ":" + getPackageRootPath(fqName);
243}
244
Andreas Huberdca261f2016-08-04 13:47:51 -0700245std::string Coordinator::getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -0800246 const FQName &fqName, bool relative, bool sanitized) const {
Andreas Huberdca261f2016-08-04 13:47:51 -0700247
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 Huber5345ec22016-07-29 13:33:27 -0700264
Andreas Huber881227d2016-08-02 14:20:21 -0700265 std::string packagePath;
266 if (!relative) {
Andreas Huberdca261f2016-08-04 13:47:51 -0700267 packagePath = root;
Andreas Huber881227d2016-08-02 14:20:21 -0700268 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700269
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 Hong97288ac2016-12-12 16:03:51 -0800282 packagePath.append(sanitized ? fqName.sanitizedVersion() : fqName.version());
Andreas Huber5345ec22016-07-29 13:33:27 -0700283 packagePath.append("/");
284
285 return packagePath;
286}
287
Andreas Huberd2943e12016-08-05 11:59:31 -0700288status_t Coordinator::getPackageInterfaceFiles(
289 const FQName &package,
290 std::vector<std::string> *fileNames) const {
291 fileNames->clear();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700292
Steven Morelandb90d3272017-05-26 10:02:39 -0700293 const std::string packagePath = getAbsolutePackagePath(package);
Andreas Huberd2943e12016-08-05 11:59:31 -0700294
295 DIR *dir = opendir(packagePath.c_str());
296
297 if (dir == NULL) {
Steven Moreland028d5a32017-05-12 15:45:56 -0700298 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 Huberd2943e12016-08-05 11:59:31 -0700303 return -errno;
304 }
305
306 struct dirent *ent;
307 while ((ent = readdir(dir)) != NULL) {
308 if (ent->d_type != DT_REG) {
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700309 continue;
310 }
311
Andreas Huberd2943e12016-08-05 11:59:31 -0700312 const auto suffix = ".hal";
313 const auto suffix_len = std::strlen(suffix);
314 const auto d_namelen = strlen(ent->d_name);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700315
Andreas Huberd2943e12016-08-05 11:59:31 -0700316 if (d_namelen < suffix_len
317 || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) {
318 continue;
Andreas Hubere61e3f72016-08-03 10:22:03 -0700319 }
Andreas Huberd2943e12016-08-05 11:59:31 -0700320
321 fileNames->push_back(std::string(ent->d_name, d_namelen - suffix_len));
322 }
323
324 closedir(dir);
325 dir = NULL;
326
Iliyan Malchev5e8bcfa2016-09-09 16:19:57 -0700327 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 Huberd2943e12016-08-05 11:59:31 -0700338 return OK;
339}
340
Steven Morelandaa186832016-09-26 13:51:43 -0700341status_t Coordinator::appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -0700342 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 Hong90ea87f2016-11-01 14:25:47 -0700355 package.package() + package.atVersion() + "::" + fileName);
Andreas Huberd2943e12016-08-05 11:59:31 -0700356
357 if (!subFQName.isValid()) {
358 LOG(WARNING)
359 << "Whole-package import encountered invalid filename '"
360 << fileName
361 << "' in package "
362 << package.package()
Yifan Hong90ea87f2016-11-01 14:25:47 -0700363 << package.atVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700364
365 continue;
366 }
367
368 packageInterfaces->push_back(subFQName);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700369 }
370
371 return OK;
372}
373
Andreas Huberd2943e12016-08-05 11:59:31 -0700374std::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 Hong78b38d12017-02-13 18:14:46 +0000386
Steven Moreland28b9b532017-05-12 17:02:58 -0700387status_t Coordinator::enforceRestrictionsOnPackage(const FQName &fqName) const {
Yifan Hong78b38d12017-02-13 18:14:46 +0000388 // 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 Moreland218625a2017-04-18 22:31:50 -0700402 status_t err;
403
404 err = enforceMinorVersionUprevs(package);
405 if (err != OK) {
406 return err;
407 }
408
409 err = enforceHashes(package);
Yifan Hong78b38d12017-02-13 18:14:46 +0000410 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 Moreland28b9b532017-05-12 17:02:58 -0700419status_t Coordinator::enforceMinorVersionUprevs(const FQName &currentPackage) const {
Yifan Hong78b38d12017-02-13 18:14:46 +0000420 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 Morelandad79f562017-05-11 14:36:54 -0700431 FQName prevPackage = currentPackage;
432 while (prevPackage.getPackageMinorVersion() > 0) {
433 prevPackage = prevPackage.downRev();
Steven Morelandb90d3272017-05-26 10:02:39 -0700434 if (existdir(getAbsolutePackagePath(prevPackage).c_str())) {
Yifan Hong78b38d12017-02-13 18:14:46 +0000435 hasPrevPackage = true;
436 break;
437 }
438 }
439 if (!hasPrevPackage) {
440 // no @x.z, where z < y, exist.
441 return OK;
442 }
443
Steven Morelandad79f562017-05-11 14:36:54 -0700444 if (prevPackage != currentPackage.downRev()) {
Yifan Hong78b38d12017-02-13 18:14:46 +0000445 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
Steven Morelandad79f562017-05-11 14:36:54 -0700446 << ": Found package " << prevPackage.string() << " but missing "
Yifan Hong78b38d12017-02-13 18:14:46 +0000447 << 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 Hongfe07bff2017-02-15 14:55:48 -0800458 bool extendedInterface = false;
Yifan Hong78b38d12017-02-13 18:14:46 +0000459 for (const FQName &currentFQName : packageInterfaces) {
460 if (currentFQName.name() == "types") {
461 continue; // ignore types.hal
462 }
Steven Morelandad79f562017-05-11 14:36:54 -0700463
Yifan Hong78b38d12017-02-13 18:14:46 +0000464 const Interface *iface = nullptr;
465 AST *currentAST = parse(currentFQName);
466 if (currentAST != nullptr) {
467 iface = currentAST->getInterface();
468 }
469 if (iface == nullptr) {
Yifan Hongfe07bff2017-02-15 14:55:48 -0800470 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 Hong78b38d12017-02-13 18:14:46 +0000479 continue;
480 }
481
Yifan Hong78b38d12017-02-13 18:14:46 +0000482 if (iface->superType() == nullptr) {
Steven Morelandad79f562017-05-11 14:36:54 -0700483 CHECK(iface->isIBase());
Yifan Hong78b38d12017-02-13 18:14:46 +0000484 continue;
485 }
486
Steven Morelandad79f562017-05-11 14:36:54 -0700487 // 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 Hong78b38d12017-02-13 18:14:46 +0000516 LOG(VERBOSE) << "enforceMinorVersionUprevs: " << currentFQName.string() << " passes.";
Yifan Hong78b38d12017-02-13 18:14:46 +0000517 }
518
Yifan Hongfe07bff2017-02-15 14:55:48 -0800519 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 Morelandad79f562017-05-11 14:36:54 -0700523 << "from " << prevPackage.string() << ".";
Yifan Hongfe07bff2017-02-15 14:55:48 -0800524 return UNKNOWN_ERROR;
525 }
526
527 return OK;
Yifan Hong78b38d12017-02-13 18:14:46 +0000528}
529
Steven Moreland28b9b532017-05-12 17:02:58 -0700530status_t Coordinator::enforceHashes(const FQName &currentPackage) const {
Steven Moreland218625a2017-04-18 22:31:50 -0700531 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 &currentFQName : packageInterfaces) {
539 AST *ast = parse(currentFQName);
540
541 if (ast == nullptr) {
542 err = UNKNOWN_ERROR;
543 continue;
544 }
545
Steven Moreland5bdfa702017-04-18 23:20:39 -0700546 std::string hashPath = getPackageRootPath(currentFQName) + "/current.txt";
Steven Moreland218625a2017-04-18 22:31:50 -0700547 std::string error;
Steven Moreland5bdfa702017-04-18 23:20:39 -0700548 std::vector<std::string> frozen = Hash::lookupHash(hashPath, currentFQName.string(), &error);
Steven Moreland218625a2017-04-18 22:31:50 -0700549
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 Huberd2943e12016-08-05 11:59:31 -0700575bool 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 Huber5345ec22016-07-29 13:33:27 -0700603} // namespace android
604