blob: 1f764106e2951779810a82c927333456fed5ef37 [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 Morelandc59326e2017-06-20 15:19:30 -070066AST* Coordinator::parse(const FQName& fqName, std::set<AST*>* parsedASTs,
67 Enforce enforcement) const {
Andreas Huber68f24592016-07-29 14:53:48 -070068 CHECK(fqName.isFullyQualified());
69
Steven Morelandd537ab02016-09-12 10:32:01 -070070 auto it = mCache.find(fqName);
71 if (it != mCache.end()) {
72 AST *ast = (*it).second;
Andreas Huber5345ec22016-07-29 13:33:27 -070073
Andreas Huber39fa7182016-08-19 14:27:33 -070074 if (ast != nullptr && parsedASTs != nullptr) {
75 parsedASTs->insert(ast);
76 }
77
Andreas Huber5345ec22016-07-29 13:33:27 -070078 return ast;
79 }
80
Andreas Huber68f24592016-07-29 14:53:48 -070081 // Add this to the cache immediately, so we can discover circular imports.
Steven Morelandd537ab02016-09-12 10:32:01 -070082 mCache[fqName] = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -070083
84 AST *typesAST = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -070085
Andreas Huber68f24592016-07-29 14:53:48 -070086 if (fqName.name() != "types") {
87 // Any interface file implicitly imports its package's types.hal.
Yifan Hongfece6ec2017-01-12 17:04:04 -080088 FQName typesName = fqName.getTypesForPackage();
Yifan Hongf619fc72017-04-07 15:40:06 -070089 // Do not enforce on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -070090 typesAST = parse(typesName, parsedASTs, Enforce::NONE);
Andreas Huber68f24592016-07-29 14:53:48 -070091
92 // fall through.
93 }
94
Steven Morelandb90d3272017-05-26 10:02:39 -070095 std::string path = getAbsolutePackagePath(fqName);
Andreas Huberdca261f2016-08-04 13:47:51 -070096
Andreas Huber68f24592016-07-29 14:53:48 -070097 path.append(fqName.name());
98 path.append(".hal");
Andreas Huber5345ec22016-07-29 13:33:27 -070099
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700100 AST *ast = new AST(this, path);
Andreas Huber39fa7182016-08-19 14:27:33 -0700101
102 if (typesAST != NULL) {
103 // If types.hal for this AST's package existed, make it's defined
104 // types available to the (about to be parsed) AST right away.
105 ast->addImportedAST(typesAST);
106 }
107
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700108 status_t err = parseFile(ast);
Andreas Huber5345ec22016-07-29 13:33:27 -0700109
Andreas Huber68f24592016-07-29 14:53:48 -0700110 if (err != OK) {
111 delete ast;
Andreas Huber39fa7182016-08-19 14:27:33 -0700112 ast = nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700113
Andreas Huber39fa7182016-08-19 14:27:33 -0700114 return nullptr;
Andreas Huber68f24592016-07-29 14:53:48 -0700115 }
116
Andreas Hubera2723d22016-07-29 15:36:07 -0700117 if (ast->package().package() != fqName.package()
118 || ast->package().version() != fqName.version()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700119 fprintf(stderr,
120 "ERROR: File at '%s' does not match expected package and/or "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700121 "version.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700122 path.c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700123
124 err = UNKNOWN_ERROR;
125 } else {
Steven Moreland19f11b52017-05-12 18:22:21 -0700126 if (ast->isInterface()) {
Andreas Hubera2723d22016-07-29 15:36:07 -0700127 if (fqName.name() == "types") {
Andreas Huber70a59e12016-08-16 12:57:01 -0700128 fprintf(stderr,
129 "ERROR: File at '%s' declares an interface '%s' "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700130 "instead of the expected types common to the package.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700131 path.c_str(),
Steven Moreland19f11b52017-05-12 18:22:21 -0700132 ast->getInterface()->localName().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700133
134 err = UNKNOWN_ERROR;
Steven Moreland19f11b52017-05-12 18:22:21 -0700135 } else if (ast->getInterface()->localName() != fqName.name()) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700136 fprintf(stderr,
137 "ERROR: File at '%s' does not declare interface type "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700138 "'%s'.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700139 path.c_str(),
140 fqName.name().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700141
142 err = UNKNOWN_ERROR;
143 }
144 } else if (fqName.name() != "types") {
Andreas Huber70a59e12016-08-16 12:57:01 -0700145 fprintf(stderr,
146 "ERROR: File at '%s' declares types rather than the "
Andreas Huber0d0f9a22016-08-17 10:26:11 -0700147 "expected interface type '%s'.\n",
Andreas Huber70a59e12016-08-16 12:57:01 -0700148 path.c_str(),
149 fqName.name().c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700150
151 err = UNKNOWN_ERROR;
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700152 } 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 Hubera2723d22016-07-29 15:36:07 -0700159 }
160 }
161
162 if (err != OK) {
163 delete ast;
Andreas Huber39fa7182016-08-19 14:27:33 -0700164 ast = nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -0700165
Andreas Huber39fa7182016-08-19 14:27:33 -0700166 return nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -0700167 }
168
Andreas Huber39fa7182016-08-19 14:27:33 -0700169 if (parsedASTs != nullptr) { parsedASTs->insert(ast); }
170
Yifan Hongfe07bff2017-02-15 14:55:48 -0800171 // put it into the cache now, so that enforceRestrictionsOnPackage can
172 // parse fqName.
Steven Morelandd537ab02016-09-12 10:32:01 -0700173 mCache[fqName] = ast;
Andreas Huber5345ec22016-07-29 13:33:27 -0700174
Steven Morelandc59326e2017-06-20 15:19:30 -0700175 // For each .hal file that hidl-gen parses, the whole package will be checked.
176 err = enforceRestrictionsOnPackage(fqName, enforcement);
177 if (err != OK) {
178 mCache[fqName] = nullptr;
179 delete ast;
180 ast = nullptr;
181 return nullptr;
Yifan Hong78b38d12017-02-13 18:14:46 +0000182 }
183
Andreas Huber5345ec22016-07-29 13:33:27 -0700184 return ast;
185}
186
Andreas Huberdca261f2016-08-04 13:47:51 -0700187std::vector<std::string>::const_iterator
188Coordinator::findPackageRoot(const FQName &fqName) const {
Andreas Huber5345ec22016-07-29 13:33:27 -0700189 CHECK(!fqName.package().empty());
Andreas Huber5345ec22016-07-29 13:33:27 -0700190
Andreas Huberdca261f2016-08-04 13:47:51 -0700191 // Find the right package prefix and path for this FQName. For
192 // example, if FQName is "android.hardware.nfc@1.0::INfc", and the
193 // prefix:root is set to [ "android.hardware:hardware/interfaces",
194 // "vendor.qcom.hardware:vendor/qcom"], then we will identify the
195 // prefix "android.hardware" and the package root
196 // "hardware/interfaces".
197
Andreas Huberdca261f2016-08-04 13:47:51 -0700198 auto it = mPackageRoots.begin();
Steven Moreland62709d72017-01-18 10:14:51 -0800199 auto ret = mPackageRoots.end();
Andreas Huberdca261f2016-08-04 13:47:51 -0700200 for (; it != mPackageRoots.end(); it++) {
Steven Moreland62709d72017-01-18 10:14:51 -0800201 if (!fqName.inPackage(*it)) {
202 continue;
Andreas Huberdca261f2016-08-04 13:47:51 -0700203 }
Steven Moreland62709d72017-01-18 10:14:51 -0800204
205 CHECK(ret == mPackageRoots.end())
206 << "Multiple package roots found for " << fqName.string()
207 << " (" << *it << " and " << *ret << ")";
208
209 ret = it;
Andreas Huberdca261f2016-08-04 13:47:51 -0700210 }
Steven Moreland62709d72017-01-18 10:14:51 -0800211 CHECK(ret != mPackageRoots.end())
Andreas Huber401cd162016-08-26 10:40:30 -0700212 << "Unable to find package root for " << fqName.string();
Andreas Huberdca261f2016-08-04 13:47:51 -0700213
Steven Moreland62709d72017-01-18 10:14:51 -0800214 return ret;
Andreas Huberdca261f2016-08-04 13:47:51 -0700215}
216
Steven Morelandb90d3272017-05-26 10:02:39 -0700217std::string Coordinator::getAbsolutePackagePath(const FQName& fqName) const {
218 const std::string packagePath = getPackagePath(fqName);
219
220 if (StringHelper::StartsWith(packagePath, "/") || mRootPath.empty()) {
221 return packagePath;
222 }
223
224 return StringHelper::RTrim(mRootPath, "/") + "/" + packagePath;
225}
226
Andreas Huberdca261f2016-08-04 13:47:51 -0700227std::string Coordinator::getPackageRoot(const FQName &fqName) const {
228 auto it = findPackageRoot(fqName);
229 auto prefix = *it;
230 return prefix;
231}
232
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700233std::string Coordinator::getPackageRootPath(const FQName &fqName) const {
234 auto it = findPackageRoot(fqName);
235 auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)];
236 return root;
237}
238
Yifan Hongc8934042016-11-17 17:10:52 -0800239std::string Coordinator::getPackageRootOption(const FQName &fqName) const {
240 return getPackageRoot(fqName) + ":" + getPackageRootPath(fqName);
241}
242
Andreas Huberdca261f2016-08-04 13:47:51 -0700243std::string Coordinator::getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -0800244 const FQName &fqName, bool relative, bool sanitized) const {
Andreas Huberdca261f2016-08-04 13:47:51 -0700245
246 auto it = findPackageRoot(fqName);
247 auto prefix = *it;
248 auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)];
249
250 // Make sure the prefix ends on a '.' and the root path on a '/'
251 if ((*--prefix.end()) != '.') {
252 prefix += '.';
253 }
254
255 if ((*--root.end()) != '/') {
256 root += '/';
257 }
258
259 // Given FQName of "android.hardware.nfc@1.0::IFoo" and a prefix
260 // "android.hardware.", the suffix is "nfc@1.0::IFoo".
261 const std::string packageSuffix = fqName.package().substr(prefix.length());
Andreas Huber5345ec22016-07-29 13:33:27 -0700262
Andreas Huber881227d2016-08-02 14:20:21 -0700263 std::string packagePath;
264 if (!relative) {
Andreas Huberdca261f2016-08-04 13:47:51 -0700265 packagePath = root;
Andreas Huber881227d2016-08-02 14:20:21 -0700266 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700267
268 size_t startPos = 0;
269 size_t dotPos;
270 while ((dotPos = packageSuffix.find('.', startPos)) != std::string::npos) {
271 packagePath.append(packageSuffix.substr(startPos, dotPos - startPos));
272 packagePath.append("/");
273
274 startPos = dotPos + 1;
275 }
276 CHECK_LT(startPos + 1, packageSuffix.length());
277 packagePath.append(packageSuffix.substr(startPos));
278 packagePath.append("/");
279
Yifan Hong97288ac2016-12-12 16:03:51 -0800280 packagePath.append(sanitized ? fqName.sanitizedVersion() : fqName.version());
Andreas Huber5345ec22016-07-29 13:33:27 -0700281 packagePath.append("/");
282
283 return packagePath;
284}
285
Andreas Huberd2943e12016-08-05 11:59:31 -0700286status_t Coordinator::getPackageInterfaceFiles(
287 const FQName &package,
288 std::vector<std::string> *fileNames) const {
289 fileNames->clear();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700290
Steven Morelandb90d3272017-05-26 10:02:39 -0700291 const std::string packagePath = getAbsolutePackagePath(package);
Andreas Huberd2943e12016-08-05 11:59:31 -0700292
293 DIR *dir = opendir(packagePath.c_str());
294
295 if (dir == NULL) {
Steven Moreland028d5a32017-05-12 15:45:56 -0700296 fprintf(stderr,
297 "ERROR: Could not open package path %s for package %s:\n%s\n",
298 getPackagePath(package).c_str(),
299 package.string().c_str(),
300 packagePath.c_str());
Andreas Huberd2943e12016-08-05 11:59:31 -0700301 return -errno;
302 }
303
304 struct dirent *ent;
305 while ((ent = readdir(dir)) != NULL) {
306 if (ent->d_type != DT_REG) {
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700307 continue;
308 }
309
Andreas Huberd2943e12016-08-05 11:59:31 -0700310 const auto suffix = ".hal";
311 const auto suffix_len = std::strlen(suffix);
312 const auto d_namelen = strlen(ent->d_name);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700313
Andreas Huberd2943e12016-08-05 11:59:31 -0700314 if (d_namelen < suffix_len
315 || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) {
316 continue;
Andreas Hubere61e3f72016-08-03 10:22:03 -0700317 }
Andreas Huberd2943e12016-08-05 11:59:31 -0700318
319 fileNames->push_back(std::string(ent->d_name, d_namelen - suffix_len));
320 }
321
322 closedir(dir);
323 dir = NULL;
324
Iliyan Malchev5e8bcfa2016-09-09 16:19:57 -0700325 std::sort(fileNames->begin(), fileNames->end(),
326 [](const std::string& lhs, const std::string& rhs) -> bool {
327 if (lhs == "types") {
328 return true;
329 }
330 if (rhs == "types") {
331 return false;
332 }
333 return lhs < rhs;
334 });
335
Andreas Huberd2943e12016-08-05 11:59:31 -0700336 return OK;
337}
338
Steven Morelandaa186832016-09-26 13:51:43 -0700339status_t Coordinator::appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -0700340 const FQName &package,
341 std::vector<FQName> *packageInterfaces) const {
342 packageInterfaces->clear();
343
344 std::vector<std::string> fileNames;
345 status_t err = getPackageInterfaceFiles(package, &fileNames);
346
347 if (err != OK) {
348 return err;
349 }
350
351 for (const auto &fileName : fileNames) {
352 FQName subFQName(
Yifan Hong90ea87f2016-11-01 14:25:47 -0700353 package.package() + package.atVersion() + "::" + fileName);
Andreas Huberd2943e12016-08-05 11:59:31 -0700354
355 if (!subFQName.isValid()) {
356 LOG(WARNING)
357 << "Whole-package import encountered invalid filename '"
358 << fileName
359 << "' in package "
360 << package.package()
Yifan Hong90ea87f2016-11-01 14:25:47 -0700361 << package.atVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700362
363 continue;
364 }
365
366 packageInterfaces->push_back(subFQName);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700367 }
368
369 return OK;
370}
371
Andreas Huberd2943e12016-08-05 11:59:31 -0700372std::string Coordinator::convertPackageRootToPath(const FQName &fqName) const {
373 std::string packageRoot = getPackageRoot(fqName);
374
375 if (*(packageRoot.end()--) != '.') {
376 packageRoot += '.';
377 }
378
379 std::replace(packageRoot.begin(), packageRoot.end(), '.', '/');
380
381 return packageRoot; // now converted to a path
382}
383
Steven Morelandc59326e2017-06-20 15:19:30 -0700384status_t Coordinator::enforceRestrictionsOnPackage(const FQName& fqName,
385 Enforce enforcement) const {
386 CHECK(enforcement == Enforce::FULL || enforcement == Enforce::NO_HASH ||
387 enforcement == Enforce::NONE);
Yifan Hong78b38d12017-02-13 18:14:46 +0000388
Yifan Hong78b38d12017-02-13 18:14:46 +0000389 // need fqName to be something like android.hardware.foo@1.0.
390 // name and valueName is ignored.
391 if (fqName.package().empty() || fqName.version().empty()) {
392 LOG(ERROR) << "Cannot enforce restrictions on package " << fqName.string()
393 << ": package or version is missing.";
394 return BAD_VALUE;
395 }
Steven Morelandc59326e2017-06-20 15:19:30 -0700396
397 if (enforcement == Enforce::NONE) {
398 return OK;
399 }
400
Yifan Hong78b38d12017-02-13 18:14:46 +0000401 FQName package = fqName.getPackageAndVersion();
402 // look up cache.
403 if (mPackagesEnforced.find(package) != mPackagesEnforced.end()) {
404 return OK;
405 }
406
407 // enforce all rules.
Steven Moreland218625a2017-04-18 22:31:50 -0700408 status_t err;
409
410 err = enforceMinorVersionUprevs(package);
411 if (err != OK) {
412 return err;
413 }
414
Steven Morelandc59326e2017-06-20 15:19:30 -0700415 if (enforcement != Enforce::NO_HASH) {
416 err = enforceHashes(package);
417 if (err != OK) {
418 return err;
419 }
Yifan Hong78b38d12017-02-13 18:14:46 +0000420 }
421
422 // cache it so that it won't need to be enforced again.
423 mPackagesEnforced.insert(package);
424 return OK;
425}
426
Steven Moreland28b9b532017-05-12 17:02:58 -0700427status_t Coordinator::enforceMinorVersionUprevs(const FQName &currentPackage) const {
Yifan Hong78b38d12017-02-13 18:14:46 +0000428 if(!currentPackage.hasVersion()) {
429 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
430 << ": missing version.";
431 return UNKNOWN_ERROR;
432 }
433
434 if (currentPackage.getPackageMinorVersion() == 0) {
435 return OK; // ignore for @x.0
436 }
437
438 bool hasPrevPackage = false;
Steven Morelandad79f562017-05-11 14:36:54 -0700439 FQName prevPackage = currentPackage;
440 while (prevPackage.getPackageMinorVersion() > 0) {
441 prevPackage = prevPackage.downRev();
Steven Morelandb90d3272017-05-26 10:02:39 -0700442 if (existdir(getAbsolutePackagePath(prevPackage).c_str())) {
Yifan Hong78b38d12017-02-13 18:14:46 +0000443 hasPrevPackage = true;
444 break;
445 }
446 }
447 if (!hasPrevPackage) {
448 // no @x.z, where z < y, exist.
449 return OK;
450 }
451
Steven Morelandad79f562017-05-11 14:36:54 -0700452 if (prevPackage != currentPackage.downRev()) {
Yifan Hong78b38d12017-02-13 18:14:46 +0000453 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
Steven Morelandad79f562017-05-11 14:36:54 -0700454 << ": Found package " << prevPackage.string() << " but missing "
Yifan Hong78b38d12017-02-13 18:14:46 +0000455 << currentPackage.downRev().string() << "; you cannot skip a minor version.";
456 return UNKNOWN_ERROR;
457 }
458
459 status_t err;
460 std::vector<FQName> packageInterfaces;
461 err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces);
462 if (err != OK) {
463 return err;
464 }
465
Yifan Hongfe07bff2017-02-15 14:55:48 -0800466 bool extendedInterface = false;
Yifan Hong78b38d12017-02-13 18:14:46 +0000467 for (const FQName &currentFQName : packageInterfaces) {
468 if (currentFQName.name() == "types") {
469 continue; // ignore types.hal
470 }
Steven Morelandad79f562017-05-11 14:36:54 -0700471
Yifan Hong78b38d12017-02-13 18:14:46 +0000472 const Interface *iface = nullptr;
473 AST *currentAST = parse(currentFQName);
474 if (currentAST != nullptr) {
475 iface = currentAST->getInterface();
476 }
477 if (iface == nullptr) {
Yifan Hongfe07bff2017-02-15 14:55:48 -0800478 if (currentAST == nullptr) {
479 LOG(WARNING) << "Warning: Skipping " << currentFQName.string()
480 << " because it could not be found or parsed"
481 << " or " << currentPackage.string()
482 << " doesn't pass all requirements.";
483 } else {
484 LOG(WARNING) << "Warning: Skipping " << currentFQName.string()
485 << " because the file might contain more than one interface.";
486 }
Yifan Hong78b38d12017-02-13 18:14:46 +0000487 continue;
488 }
489
Yifan Hong78b38d12017-02-13 18:14:46 +0000490 if (iface->superType() == nullptr) {
Steven Morelandad79f562017-05-11 14:36:54 -0700491 CHECK(iface->isIBase());
Yifan Hong78b38d12017-02-13 18:14:46 +0000492 continue;
493 }
494
Steven Morelandad79f562017-05-11 14:36:54 -0700495 // Assume that currentFQName == android.hardware.foo@2.2::IFoo.
496 FQName lastFQName(prevPackage.package(), prevPackage.version(),
497 currentFQName.name());
498 AST *lastAST = parse(lastFQName);
499
500 for (; lastFQName.getPackageMinorVersion() > 0 &&
501 (lastAST == nullptr || lastAST->getInterface() == nullptr)
502 ; lastFQName = lastFQName.downRev(), lastAST = parse(lastFQName)) {
503 // nothing
504 }
505
506 // Then lastFQName == android.hardware.foo@2.1::IFoo or
507 // lastFQName == android.hardware.foo@2.0::IFoo if 2.1 doesn't exist.
508
509 bool lastFQNameExists = lastAST != nullptr && lastAST->getInterface() != nullptr;
510
511 if (iface->superType()->fqName() != lastFQName && lastFQNameExists) {
512 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
513 << ": " << iface->fqName().string() << " extends "
514 << iface->superType()->fqName().string()
515 << ", which is not allowed. It must extend " << lastFQName.string();
516 return UNKNOWN_ERROR;
517 }
518
519 // at least one interface must extend the previous version
520 if (lastFQName.getPackageAndVersion() == prevPackage.getPackageAndVersion()) {
521 extendedInterface = true;
522 }
523
Yifan Hong78b38d12017-02-13 18:14:46 +0000524 LOG(VERBOSE) << "enforceMinorVersionUprevs: " << currentFQName.string() << " passes.";
Yifan Hong78b38d12017-02-13 18:14:46 +0000525 }
526
Yifan Hongfe07bff2017-02-15 14:55:48 -0800527 if (!extendedInterface) {
528 // No interface extends the interface with the same name in @x.(y-1).
529 LOG(ERROR) << currentPackage.string() << " doesn't pass minor version uprev requirement. "
530 << "Requires at least one interface to extend an interface with the same name "
Steven Morelandad79f562017-05-11 14:36:54 -0700531 << "from " << prevPackage.string() << ".";
Yifan Hongfe07bff2017-02-15 14:55:48 -0800532 return UNKNOWN_ERROR;
533 }
534
535 return OK;
Yifan Hong78b38d12017-02-13 18:14:46 +0000536}
537
Steven Moreland28b9b532017-05-12 17:02:58 -0700538status_t Coordinator::enforceHashes(const FQName &currentPackage) const {
Steven Moreland218625a2017-04-18 22:31:50 -0700539 status_t err = OK;
540 std::vector<FQName> packageInterfaces;
541 err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces);
542 if (err != OK) {
543 return err;
544 }
545
546 for (const FQName &currentFQName : packageInterfaces) {
547 AST *ast = parse(currentFQName);
548
549 if (ast == nullptr) {
550 err = UNKNOWN_ERROR;
551 continue;
552 }
553
Steven Moreland5bdfa702017-04-18 23:20:39 -0700554 std::string hashPath = getPackageRootPath(currentFQName) + "/current.txt";
Steven Moreland218625a2017-04-18 22:31:50 -0700555 std::string error;
Steven Moreland5bdfa702017-04-18 23:20:39 -0700556 std::vector<std::string> frozen = Hash::lookupHash(hashPath, currentFQName.string(), &error);
Steven Moreland218625a2017-04-18 22:31:50 -0700557
558 if (error.size() > 0) {
559 LOG(ERROR) << error;
560 err = UNKNOWN_ERROR;
561 continue;
562 }
563
564 // hash not define, interface not frozen
565 if (frozen.size() == 0) {
566 continue;
567 }
568
569 std::string currentHash = Hash::getHash(ast->getFilename()).hexString();
570
571 if(std::find(frozen.begin(), frozen.end(), currentHash) == frozen.end()) {
572 LOG(ERROR) << currentFQName.string() << " has hash " << currentHash
573 << " which does not match hash on record. This interface has "
574 << "been frozen. Do not change it!";
575 err = UNKNOWN_ERROR;
576 continue;
577 }
578 }
579
580 return err;
581}
582
Andreas Huberd2943e12016-08-05 11:59:31 -0700583bool Coordinator::MakeParentHierarchy(const std::string &path) {
584 static const mode_t kMode = 0755;
585
586 size_t start = 1; // Ignore leading '/'
587 size_t slashPos;
588 while ((slashPos = path.find("/", start)) != std::string::npos) {
589 std::string partial = path.substr(0, slashPos);
590
591 struct stat st;
592 if (stat(partial.c_str(), &st) < 0) {
593 if (errno != ENOENT) {
594 return false;
595 }
596
597 int res = mkdir(partial.c_str(), kMode);
598 if (res < 0) {
599 return false;
600 }
601 } else if (!S_ISDIR(st.st_mode)) {
602 return false;
603 }
604
605 start = slashPos + 1;
606 }
607
608 return true;
609}
610
Andreas Huber5345ec22016-07-29 13:33:27 -0700611} // namespace android
612