Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 17 | #include "apex_manifest.h" |
Nikita Ioffe | 264c421 | 2019-09-13 16:30:17 +0100 | [diff] [blame] | 18 | #include <android-base/file.h> |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 19 | |
Jiyong Park | 69c0f11 | 2018-11-22 20:38:05 +0900 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
Mohammad Samiul Islam | bd6ab0f | 2019-06-20 15:55:27 +0100 | [diff] [blame] | 23 | using android::base::Error; |
| 24 | using android::base::Result; |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 25 | |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace apex { |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 28 | |
Dario Freni | a277bdf | 2019-11-05 22:37:49 +0000 | [diff] [blame] | 29 | Result<ApexManifest> ParseManifest(const std::string& content) { |
| 30 | ApexManifest apex_manifest; |
Dario Freni | a277bdf | 2019-11-05 22:37:49 +0000 | [diff] [blame] | 31 | |
| 32 | if (!apex_manifest.ParseFromString(content)) { |
| 33 | return Error() << "Can't parse APEX manifest."; |
| 34 | } |
| 35 | |
| 36 | // Verifying required fields. |
| 37 | // name |
| 38 | if (apex_manifest.name().empty()) { |
| 39 | return Error() << "Missing required field \"name\" from APEX manifest."; |
| 40 | } |
| 41 | |
| 42 | // version |
| 43 | if (apex_manifest.version() == 0) { |
| 44 | return Error() << "Missing required field \"version\" from APEX manifest."; |
| 45 | } |
| 46 | return apex_manifest; |
| 47 | } |
| 48 | |
Abhijeet Kaur | 216e36c | 2019-01-04 10:15:01 +0000 | [diff] [blame] | 49 | std::string GetPackageId(const ApexManifest& apexManifest) { |
| 50 | return apexManifest.name() + "@" + std::to_string(apexManifest.version()); |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Nikita Ioffe | 264c421 | 2019-09-13 16:30:17 +0100 | [diff] [blame] | 53 | Result<ApexManifest> ReadManifest(const std::string& path) { |
| 54 | std::string content; |
| 55 | if (!android::base::ReadFileToString(path, &content)) { |
| 56 | return Error() << "Failed to read manifest file: " << path; |
| 57 | } |
| 58 | return ParseManifest(content); |
| 59 | } |
| 60 | |
Dario Freni | aeb233c | 2018-08-28 12:48:42 +0100 | [diff] [blame] | 61 | } // namespace apex |
| 62 | } // namespace android |