blob: 6f589cf4fe4e7c7307935a57fb724f93c3907caa [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#ifndef COORDINATOR_H_
18
19#define COORDINATOR_H_
20
21#include <android-base/macros.h>
Steven Morelandd537ab02016-09-12 10:32:01 -070022#include <map>
Andreas Huber39fa7182016-08-19 14:27:33 -070023#include <set>
Andreas Huber5345ec22016-07-29 13:33:27 -070024#include <string>
Steven Morelandd537ab02016-09-12 10:32:01 -070025#include <utils/Errors.h>
Andreas Huberdca261f2016-08-04 13:47:51 -070026#include <vector>
Andreas Huber5345ec22016-07-29 13:33:27 -070027
28namespace android {
29
30struct AST;
31struct FQName;
Andreas Huberfd4afab2016-08-03 13:02:57 -070032struct Type;
Andreas Huber5345ec22016-07-29 13:33:27 -070033
34struct Coordinator {
Andreas Huberdca261f2016-08-04 13:47:51 -070035 Coordinator(
36 const std::vector<std::string> &packageRootPaths,
Steven Morelandf7fa0682017-05-11 16:14:55 -070037 const std::vector<std::string> &packageRoots,
38 const std::string &rootPath);
Andreas Huberdca261f2016-08-04 13:47:51 -070039
Andreas Huber5345ec22016-07-29 13:33:27 -070040 ~Coordinator();
41
Steven Moreland25c81662017-05-12 14:57:36 -070042 // adds path only if it doesn't exist
43 void addDefaultPackagePath(const std::string& root, const std::string& path);
44
Steven Morelandc59326e2017-06-20 15:19:30 -070045 enum class Enforce {
46 FULL, // default
47 NO_HASH, // only for use with -Lhash
48 NONE, // only for use during enforcement
49 };
50
Andreas Huber39fa7182016-08-19 14:27:33 -070051 // Attempts to parse the interface/types referred to by fqName.
52 // Parsing an interface also parses the associated package's types.hal
53 // file if it exists.
54 // If "parsedASTs" is non-NULL, successfully parsed ASTs are inserted
55 // into the set.
Yifan Hongf619fc72017-04-07 15:40:06 -070056 // If !enforce, enforceRestrictionsOnPackage won't be run.
Steven Morelandc59326e2017-06-20 15:19:30 -070057 AST* parse(const FQName& fqName, std::set<AST*>* parsedASTs = nullptr,
58 Enforce enforcement = Enforce::FULL) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070059
Andreas Huberdca261f2016-08-04 13:47:51 -070060 // Given package-root paths of ["hardware/interfaces",
61 // "vendor/<something>/interfaces"], package roots of
62 // ["android.hardware", "vendor.<something>.hardware"], and a
63 // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath()
Yifan Hong97288ac2016-12-12 16:03:51 -080064 // will return "hardware/interfaces/nfc/1.0" (if sanitized = false)
65 // or "hardware/interfaces/nfc/V1_0" (if sanitized = true).
Andreas Huber881227d2016-08-02 14:20:21 -070066 std::string getPackagePath(
Yifan Hong97288ac2016-12-12 16:03:51 -080067 const FQName &fqName, bool relative = false,
68 bool sanitized = false) const;
Andreas Huber5345ec22016-07-29 13:33:27 -070069
Andreas Huberdca261f2016-08-04 13:47:51 -070070 // Given package roots of ["android.hardware",
71 // "vendor.<something>.hardware"] and a FQName of
Iliyan Malchev5bb14022016-08-09 15:04:39 -070072 // "android.hardware.nfc@1.0::INfc, then getPackageRoot() will
Andreas Huberdca261f2016-08-04 13:47:51 -070073 // return "android.hardware".
Andreas Huberdca261f2016-08-04 13:47:51 -070074 std::string getPackageRoot(const FQName &fqName) const;
75
Iliyan Malchev5bb14022016-08-09 15:04:39 -070076 // Given package-root paths of ["hardware/interfaces",
77 // "vendor/<something>/interfaces"], package roots of
78 // ["android.hardware", "vendor.<something>.hardware"], and a
79 // FQName of "android.hardware.nfc@1.0::INfc, then getPackageRootPath()
80 // will return "hardware/interfaces".
Iliyan Malchev5bb14022016-08-09 15:04:39 -070081 std::string getPackageRootPath(const FQName &fqName) const;
82
Yifan Hongc8934042016-11-17 17:10:52 -080083 // return getPackageRoot + ":" + getPackageRootPath
84 std::string getPackageRootOption(const FQName &fqName) const;
85
Andreas Huberd2943e12016-08-05 11:59:31 -070086 // Given an FQName of "android.hardware.nfc@1.0::INfc", return
87 // "android/hardware/".
88 std::string convertPackageRootToPath(const FQName &fqName) const;
89
90 status_t getPackageInterfaceFiles(
91 const FQName &package,
92 std::vector<std::string> *fileNames) const;
93
Steven Morelandaa186832016-09-26 13:51:43 -070094 status_t appendPackageInterfacesToVector(
Andreas Huberd2943e12016-08-05 11:59:31 -070095 const FQName &package,
96 std::vector<FQName> *packageInterfaces) const;
97
Yifan Hong78b38d12017-02-13 18:14:46 +000098 // Enforce a set of restrictions on a set of packages. These include:
99 // - minor version upgrades
100 // "packages" contains names like "android.hardware.nfc@1.1".
Steven Moreland218625a2017-04-18 22:31:50 -0700101 // - hashing restrictions
Steven Morelandc59326e2017-06-20 15:19:30 -0700102 status_t enforceRestrictionsOnPackage(const FQName& fqName,
103 Enforce enforcement = Enforce::FULL) const;
Yifan Hong78b38d12017-02-13 18:14:46 +0000104
Andreas Huberd2943e12016-08-05 11:59:31 -0700105 static bool MakeParentHierarchy(const std::string &path);
Andreas Hubere61e3f72016-08-03 10:22:03 -0700106
Andreas Huber5345ec22016-07-29 13:33:27 -0700107private:
Andreas Huberdca261f2016-08-04 13:47:51 -0700108 // A list of top-level directories (mPackageRootPaths)
109 // corresponding to a list of package roots (mPackageRoots). For
110 // example, if mPackageRootPaths[0] == "hardware/interfaces" and
111 // mPackageRoots[0] == "android.hardware" this means that all
112 // packages starting with "android.hardware" will be looked up in
113 // "hardware/interfaces".
114 std::vector<std::string> mPackageRootPaths;
115 std::vector<std::string> mPackageRoots;
Yifan Hong78b38d12017-02-13 18:14:46 +0000116
Steven Morelandf7fa0682017-05-11 16:14:55 -0700117 std::string mRootPath;
118
Yifan Hong78b38d12017-02-13 18:14:46 +0000119 // cache to parse().
Steven Moreland28b9b532017-05-12 17:02:58 -0700120 mutable std::map<FQName, AST *> mCache;
Andreas Huber5345ec22016-07-29 13:33:27 -0700121
Yifan Hong78b38d12017-02-13 18:14:46 +0000122 // cache to enforceRestrictionsOnPackage().
Steven Moreland28b9b532017-05-12 17:02:58 -0700123 mutable std::set<FQName> mPackagesEnforced;
Yifan Hong78b38d12017-02-13 18:14:46 +0000124
Andreas Huberdca261f2016-08-04 13:47:51 -0700125 std::vector<std::string>::const_iterator findPackageRoot(
126 const FQName &fqName) const;
127
Steven Morelandb90d3272017-05-26 10:02:39 -0700128 // Returns abs package path by prepending the root path if a package
129 // path is non-absolute.
130 // If root is '/android/master' and getPackagePath returns 'h/i/nfc/V1_0'
131 // this will return '/android/master/h/i/nfc/V1_0'.
132 // If root is '/android/master' and getPackagePath returns '/abs/path/to/nfc/V1_0'
133 // this will return '/abs/path/to/nfc/V1_0'
134 std::string getAbsolutePackagePath(const FQName& fqName) const;
135
Yifan Hong78b38d12017-02-13 18:14:46 +0000136 // Rules of enforceRestrictionsOnPackage are listed below.
Steven Moreland28b9b532017-05-12 17:02:58 -0700137 status_t enforceMinorVersionUprevs(const FQName &fqName) const;
138 status_t enforceHashes(const FQName &fqName) const;
Yifan Hong78b38d12017-02-13 18:14:46 +0000139
Andreas Huber5345ec22016-07-29 13:33:27 -0700140 DISALLOW_COPY_AND_ASSIGN(Coordinator);
141};
142
143} // namespace android
144
145#endif // COORDINATOR_H_