blob: 1994633c3cd7eb6383fb70fad37aaf3d8f10a287 [file] [log] [blame]
Dario Freniaeb233c2018-08-28 12:48:42 +01001/*
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 Freniaeb233c2018-08-28 12:48:42 +010017#include "apex_manifest.h"
Nikita Ioffe264c4212019-09-13 16:30:17 +010018#include <android-base/file.h>
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000019
Jiyong Park69c0f112018-11-22 20:38:05 +090020#include <memory>
21#include <string>
22
Mohammad Samiul Islambd6ab0f2019-06-20 15:55:27 +010023using android::base::Error;
24using android::base::Result;
Abhijeet Kaur216e36c2019-01-04 10:15:01 +000025
Dario Freniaeb233c2018-08-28 12:48:42 +010026namespace android {
27namespace apex {
Dario Freniaeb233c2018-08-28 12:48:42 +010028
Dario Frenia277bdf2019-11-05 22:37:49 +000029Result<ApexManifest> ParseManifest(const std::string& content) {
30 ApexManifest apex_manifest;
Dario Frenia277bdf2019-11-05 22:37:49 +000031
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 Kaur216e36c2019-01-04 10:15:01 +000049std::string GetPackageId(const ApexManifest& apexManifest) {
50 return apexManifest.name() + "@" + std::to_string(apexManifest.version());
Dario Freniaeb233c2018-08-28 12:48:42 +010051}
52
Nikita Ioffe264c4212019-09-13 16:30:17 +010053Result<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 Freniaeb233c2018-08-28 12:48:42 +010061} // namespace apex
62} // namespace android