blob: 1ec0554ed64406cf5e9feee62fdb3782ab857cb7 [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 Morelandf7fa0682017-05-11 16:14:55 -070094 std::string path = mRootPath + getPackagePath(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 {
125 std::string ifaceName;
126 if (ast->isInterface(&ifaceName)) {
127 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(),
132 ifaceName.c_str());
Andreas Hubera2723d22016-07-29 15:36:07 -0700133
134 err = UNKNOWN_ERROR;
135 } else if (ifaceName != 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
Yifan Hongf619fc72017-04-07 15:40:06 -0700175 if (enforce) {
176 // For each .hal file that hidl-gen parses, the whole package will be checked.
177 err = enforceRestrictionsOnPackage(fqName);
178 if (err != OK) {
179 mCache[fqName] = nullptr;
180 delete ast;
181 ast = nullptr;
182 return nullptr;
183 }
Yifan Hong78b38d12017-02-13 18:14:46 +0000184 }
185
Andreas Huber5345ec22016-07-29 13:33:27 -0700186 return ast;
187}
188
Andreas Huberdca261f2016-08-04 13:47:51 -0700189std::vector<std::string>::const_iterator
190Coordinator::findPackageRoot(const FQName &fqName) const {
Andreas Huber5345ec22016-07-29 13:33:27 -0700191 CHECK(!fqName.package().empty());
192 CHECK(!fqName.version().empty());
Andreas Huber5345ec22016-07-29 13:33:27 -0700193
Andreas Huberdca261f2016-08-04 13:47:51 -0700194 // Find the right package prefix and path for this FQName. For
195 // example, if FQName is "android.hardware.nfc@1.0::INfc", and the
196 // prefix:root is set to [ "android.hardware:hardware/interfaces",
197 // "vendor.qcom.hardware:vendor/qcom"], then we will identify the
198 // prefix "android.hardware" and the package root
199 // "hardware/interfaces".
200
Andreas Huberdca261f2016-08-04 13:47:51 -0700201 auto it = mPackageRoots.begin();
Steven Moreland62709d72017-01-18 10:14:51 -0800202 auto ret = mPackageRoots.end();
Andreas Huberdca261f2016-08-04 13:47:51 -0700203 for (; it != mPackageRoots.end(); it++) {
Steven Moreland62709d72017-01-18 10:14:51 -0800204 if (!fqName.inPackage(*it)) {
205 continue;
Andreas Huberdca261f2016-08-04 13:47:51 -0700206 }
Steven Moreland62709d72017-01-18 10:14:51 -0800207
208 CHECK(ret == mPackageRoots.end())
209 << "Multiple package roots found for " << fqName.string()
210 << " (" << *it << " and " << *ret << ")";
211
212 ret = it;
Andreas Huberdca261f2016-08-04 13:47:51 -0700213 }
Steven Moreland62709d72017-01-18 10:14:51 -0800214 CHECK(ret != mPackageRoots.end())
Andreas Huber401cd162016-08-26 10:40:30 -0700215 << "Unable to find package root for " << fqName.string();
Andreas Huberdca261f2016-08-04 13:47:51 -0700216
Steven Moreland62709d72017-01-18 10:14:51 -0800217 return ret;
Andreas Huberdca261f2016-08-04 13:47:51 -0700218}
219
220std::string Coordinator::getPackageRoot(const FQName &fqName) const {
221 auto it = findPackageRoot(fqName);
222 auto prefix = *it;
223 return prefix;
224}
225
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700226std::string Coordinator::getPackageRootPath(const FQName &fqName) const {
227 auto it = findPackageRoot(fqName);
228 auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)];
229 return root;
230}
231
Yifan Hongc8934042016-11-17 17:10:52 -0800232std::string Coordinator::getPackageRootOption(const FQName &fqName) const {
233 return getPackageRoot(fqName) + ":" + getPackageRootPath(fqName);
234}
235
Andreas Huberdca261f2016-08-04 13:47:51 -0700236std::string Coordinator::getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -0800237 const FQName &fqName, bool relative, bool sanitized) const {
Andreas Huberdca261f2016-08-04 13:47:51 -0700238
239 auto it = findPackageRoot(fqName);
240 auto prefix = *it;
241 auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)];
242
243 // Make sure the prefix ends on a '.' and the root path on a '/'
244 if ((*--prefix.end()) != '.') {
245 prefix += '.';
246 }
247
248 if ((*--root.end()) != '/') {
249 root += '/';
250 }
251
252 // Given FQName of "android.hardware.nfc@1.0::IFoo" and a prefix
253 // "android.hardware.", the suffix is "nfc@1.0::IFoo".
254 const std::string packageSuffix = fqName.package().substr(prefix.length());
Andreas Huber5345ec22016-07-29 13:33:27 -0700255
Andreas Huber881227d2016-08-02 14:20:21 -0700256 std::string packagePath;
257 if (!relative) {
Andreas Huberdca261f2016-08-04 13:47:51 -0700258 packagePath = root;
Andreas Huber881227d2016-08-02 14:20:21 -0700259 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700260
261 size_t startPos = 0;
262 size_t dotPos;
263 while ((dotPos = packageSuffix.find('.', startPos)) != std::string::npos) {
264 packagePath.append(packageSuffix.substr(startPos, dotPos - startPos));
265 packagePath.append("/");
266
267 startPos = dotPos + 1;
268 }
269 CHECK_LT(startPos + 1, packageSuffix.length());
270 packagePath.append(packageSuffix.substr(startPos));
271 packagePath.append("/");
272
Yifan Hong97288ac2016-12-12 16:03:51 -0800273 packagePath.append(sanitized ? fqName.sanitizedVersion() : fqName.version());
Andreas Huber5345ec22016-07-29 13:33:27 -0700274 packagePath.append("/");
275
276 return packagePath;
277}
278
Andreas Huberd2943e12016-08-05 11:59:31 -0700279status_t Coordinator::getPackageInterfaceFiles(
280 const FQName &package,
281 std::vector<std::string> *fileNames) const {
282 fileNames->clear();
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700283
Steven Morelandf7fa0682017-05-11 16:14:55 -0700284 const std::string packagePath = mRootPath + getPackagePath(package);
Andreas Huberd2943e12016-08-05 11:59:31 -0700285
286 DIR *dir = opendir(packagePath.c_str());
287
288 if (dir == NULL) {
Steven Moreland028d5a32017-05-12 15:45:56 -0700289 fprintf(stderr,
290 "ERROR: Could not open package path %s for package %s:\n%s\n",
291 getPackagePath(package).c_str(),
292 package.string().c_str(),
293 packagePath.c_str());
Andreas Huberd2943e12016-08-05 11:59:31 -0700294 return -errno;
295 }
296
297 struct dirent *ent;
298 while ((ent = readdir(dir)) != NULL) {
299 if (ent->d_type != DT_REG) {
Andreas Huber6cb08cf2016-08-03 15:44:51 -0700300 continue;
301 }
302
Andreas Huberd2943e12016-08-05 11:59:31 -0700303 const auto suffix = ".hal";
304 const auto suffix_len = std::strlen(suffix);
305 const auto d_namelen = strlen(ent->d_name);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700306
Andreas Huberd2943e12016-08-05 11:59:31 -0700307 if (d_namelen < suffix_len
308 || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) {
309 continue;
Andreas Hubere61e3f72016-08-03 10:22:03 -0700310 }
Andreas Huberd2943e12016-08-05 11:59:31 -0700311
312 fileNames->push_back(std::string(ent->d_name, d_namelen - suffix_len));
313 }
314
315 closedir(dir);
316 dir = NULL;
317
Iliyan Malchev5e8bcfa2016-09-09 16:19:57 -0700318 std::sort(fileNames->begin(), fileNames->end(),
319 [](const std::string& lhs, const std::string& rhs) -> bool {
320 if (lhs == "types") {
321 return true;
322 }
323 if (rhs == "types") {
324 return false;
325 }
326 return lhs < rhs;
327 });
328
Andreas Huberd2943e12016-08-05 11:59:31 -0700329 return OK;
330}
331
Steven Morelandaa186832016-09-26 13:51:43 -0700332status_t Coordinator::appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -0700333 const FQName &package,
334 std::vector<FQName> *packageInterfaces) const {
335 packageInterfaces->clear();
336
337 std::vector<std::string> fileNames;
338 status_t err = getPackageInterfaceFiles(package, &fileNames);
339
340 if (err != OK) {
341 return err;
342 }
343
344 for (const auto &fileName : fileNames) {
345 FQName subFQName(
Yifan Hong90ea87f2016-11-01 14:25:47 -0700346 package.package() + package.atVersion() + "::" + fileName);
Andreas Huberd2943e12016-08-05 11:59:31 -0700347
348 if (!subFQName.isValid()) {
349 LOG(WARNING)
350 << "Whole-package import encountered invalid filename '"
351 << fileName
352 << "' in package "
353 << package.package()
Yifan Hong90ea87f2016-11-01 14:25:47 -0700354 << package.atVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700355
356 continue;
357 }
358
359 packageInterfaces->push_back(subFQName);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700360 }
361
362 return OK;
363}
364
Andreas Huberd2943e12016-08-05 11:59:31 -0700365std::string Coordinator::convertPackageRootToPath(const FQName &fqName) const {
366 std::string packageRoot = getPackageRoot(fqName);
367
368 if (*(packageRoot.end()--) != '.') {
369 packageRoot += '.';
370 }
371
372 std::replace(packageRoot.begin(), packageRoot.end(), '.', '/');
373
374 return packageRoot; // now converted to a path
375}
376
Yifan Hong78b38d12017-02-13 18:14:46 +0000377
Steven Moreland28b9b532017-05-12 17:02:58 -0700378status_t Coordinator::enforceRestrictionsOnPackage(const FQName &fqName) const {
Yifan Hong78b38d12017-02-13 18:14:46 +0000379 // need fqName to be something like android.hardware.foo@1.0.
380 // name and valueName is ignored.
381 if (fqName.package().empty() || fqName.version().empty()) {
382 LOG(ERROR) << "Cannot enforce restrictions on package " << fqName.string()
383 << ": package or version is missing.";
384 return BAD_VALUE;
385 }
386 FQName package = fqName.getPackageAndVersion();
387 // look up cache.
388 if (mPackagesEnforced.find(package) != mPackagesEnforced.end()) {
389 return OK;
390 }
391
392 // enforce all rules.
Steven Moreland218625a2017-04-18 22:31:50 -0700393 status_t err;
394
395 err = enforceMinorVersionUprevs(package);
396 if (err != OK) {
397 return err;
398 }
399
400 err = enforceHashes(package);
Yifan Hong78b38d12017-02-13 18:14:46 +0000401 if (err != OK) {
402 return err;
403 }
404
405 // cache it so that it won't need to be enforced again.
406 mPackagesEnforced.insert(package);
407 return OK;
408}
409
Steven Moreland28b9b532017-05-12 17:02:58 -0700410status_t Coordinator::enforceMinorVersionUprevs(const FQName &currentPackage) const {
Yifan Hong78b38d12017-02-13 18:14:46 +0000411 if(!currentPackage.hasVersion()) {
412 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
413 << ": missing version.";
414 return UNKNOWN_ERROR;
415 }
416
417 if (currentPackage.getPackageMinorVersion() == 0) {
418 return OK; // ignore for @x.0
419 }
420
421 bool hasPrevPackage = false;
422 FQName prevPacakge = currentPackage;
423 while (prevPacakge.getPackageMinorVersion() > 0) {
424 prevPacakge = prevPacakge.downRev();
Steven Morelandf7fa0682017-05-11 16:14:55 -0700425 if (existdir((mRootPath + getPackagePath(prevPacakge)).c_str())) {
Yifan Hong78b38d12017-02-13 18:14:46 +0000426 hasPrevPackage = true;
427 break;
428 }
429 }
430 if (!hasPrevPackage) {
431 // no @x.z, where z < y, exist.
432 return OK;
433 }
434
435 if (prevPacakge != currentPackage.downRev()) {
436 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
437 << ": Found package " << prevPacakge.string() << " but missing "
438 << currentPackage.downRev().string() << "; you cannot skip a minor version.";
439 return UNKNOWN_ERROR;
440 }
441
442 status_t err;
443 std::vector<FQName> packageInterfaces;
444 err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces);
445 if (err != OK) {
446 return err;
447 }
448
Yifan Hongfe07bff2017-02-15 14:55:48 -0800449 bool extendedInterface = false;
Yifan Hong78b38d12017-02-13 18:14:46 +0000450 for (const FQName &currentFQName : packageInterfaces) {
451 if (currentFQName.name() == "types") {
452 continue; // ignore types.hal
453 }
454 // Assume that currentFQName == android.hardware.foo@2.2::IFoo.
455 // Then prevFQName == android.hardware.foo@2.1::IFoo.
456 const Interface *iface = nullptr;
457 AST *currentAST = parse(currentFQName);
458 if (currentAST != nullptr) {
459 iface = currentAST->getInterface();
460 }
461 if (iface == nullptr) {
Yifan Hongfe07bff2017-02-15 14:55:48 -0800462 if (currentAST == nullptr) {
463 LOG(WARNING) << "Warning: Skipping " << currentFQName.string()
464 << " because it could not be found or parsed"
465 << " or " << currentPackage.string()
466 << " doesn't pass all requirements.";
467 } else {
468 LOG(WARNING) << "Warning: Skipping " << currentFQName.string()
469 << " because the file might contain more than one interface.";
470 }
Yifan Hong78b38d12017-02-13 18:14:46 +0000471 continue;
472 }
473
474 // android.hardware.foo@2.2::IFoo exists. Now make sure
475 // @2.2::IFoo extends @2.1::IFoo. If any interface IFoo in @2.2
476 // ensures this, @2.2 passes the enforcement.
477 FQName prevFQName(prevPacakge.package(), prevPacakge.version(),
478 currentFQName.name());
479 if (iface->superType() == nullptr) {
480 // @2.2::IFoo doesn't extend anything. (This is probably IBase.)
481 continue;
482 }
483 if (iface->superType()->fqName() != prevFQName) {
484 // @2.2::IFoo doesn't extend @2.1::IFoo.
Yifan Hongfe07bff2017-02-15 14:55:48 -0800485 if (iface->superType()->fqName().getPackageAndVersion() ==
486 prevPacakge.getPackageAndVersion()) {
Yifan Hong78b38d12017-02-13 18:14:46 +0000487 LOG(ERROR) << "Cannot enforce minor version uprevs for " << currentPackage.string()
488 << ": " << iface->fqName().string() << " extends "
489 << iface->superType()->fqName().string() << ", which is not allowed.";
490 return UNKNOWN_ERROR;
491 }
492 // @2.2::IFoo extends something from a package with a different package name.
493 // Check the next interface.
494 continue;
495 }
496
Yifan Hongfe07bff2017-02-15 14:55:48 -0800497 // @2.2::IFoo passes. Check next interface.
498 extendedInterface = true;
Yifan Hong78b38d12017-02-13 18:14:46 +0000499 LOG(VERBOSE) << "enforceMinorVersionUprevs: " << currentFQName.string() << " passes.";
Yifan Hong78b38d12017-02-13 18:14:46 +0000500 }
501
Yifan Hongfe07bff2017-02-15 14:55:48 -0800502 if (!extendedInterface) {
503 // No interface extends the interface with the same name in @x.(y-1).
504 LOG(ERROR) << currentPackage.string() << " doesn't pass minor version uprev requirement. "
505 << "Requires at least one interface to extend an interface with the same name "
506 << "from " << prevPacakge.string() << ".";
507 return UNKNOWN_ERROR;
508 }
509
510 return OK;
Yifan Hong78b38d12017-02-13 18:14:46 +0000511}
512
Steven Moreland28b9b532017-05-12 17:02:58 -0700513status_t Coordinator::enforceHashes(const FQName &currentPackage) const {
Steven Moreland218625a2017-04-18 22:31:50 -0700514 status_t err = OK;
515 std::vector<FQName> packageInterfaces;
516 err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces);
517 if (err != OK) {
518 return err;
519 }
520
521 for (const FQName &currentFQName : packageInterfaces) {
522 AST *ast = parse(currentFQName);
523
524 if (ast == nullptr) {
525 err = UNKNOWN_ERROR;
526 continue;
527 }
528
Steven Moreland5bdfa702017-04-18 23:20:39 -0700529 std::string hashPath = getPackageRootPath(currentFQName) + "/current.txt";
Steven Moreland218625a2017-04-18 22:31:50 -0700530 std::string error;
Steven Moreland5bdfa702017-04-18 23:20:39 -0700531 std::vector<std::string> frozen = Hash::lookupHash(hashPath, currentFQName.string(), &error);
Steven Moreland218625a2017-04-18 22:31:50 -0700532
533 if (error.size() > 0) {
534 LOG(ERROR) << error;
535 err = UNKNOWN_ERROR;
536 continue;
537 }
538
539 // hash not define, interface not frozen
540 if (frozen.size() == 0) {
541 continue;
542 }
543
544 std::string currentHash = Hash::getHash(ast->getFilename()).hexString();
545
546 if(std::find(frozen.begin(), frozen.end(), currentHash) == frozen.end()) {
547 LOG(ERROR) << currentFQName.string() << " has hash " << currentHash
548 << " which does not match hash on record. This interface has "
549 << "been frozen. Do not change it!";
550 err = UNKNOWN_ERROR;
551 continue;
552 }
553 }
554
555 return err;
556}
557
Andreas Huberd2943e12016-08-05 11:59:31 -0700558bool Coordinator::MakeParentHierarchy(const std::string &path) {
559 static const mode_t kMode = 0755;
560
561 size_t start = 1; // Ignore leading '/'
562 size_t slashPos;
563 while ((slashPos = path.find("/", start)) != std::string::npos) {
564 std::string partial = path.substr(0, slashPos);
565
566 struct stat st;
567 if (stat(partial.c_str(), &st) < 0) {
568 if (errno != ENOENT) {
569 return false;
570 }
571
572 int res = mkdir(partial.c_str(), kMode);
573 if (res < 0) {
574 return false;
575 }
576 } else if (!S_ISDIR(st.st_mode)) {
577 return false;
578 }
579
580 start = slashPos + 1;
581 }
582
583 return true;
584}
585
Andreas Huber5345ec22016-07-29 13:33:27 -0700586} // namespace android
587