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 
17 #include "apex_manifest.h"
18 #include <android-base/file.h>
19 
20 #include <memory>
21 #include <string>
22 
23 using android::base::Error;
24 using android::base::Result;
25 
26 namespace android {
27 namespace apex {
28 
ParseManifest(const std::string & content)29 Result<ApexManifest> ParseManifest(const std::string& content) {
30   ApexManifest apex_manifest;
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 
GetPackageId(const ApexManifest & apexManifest)49 std::string GetPackageId(const ApexManifest& apexManifest) {
50   return apexManifest.name() + "@" + std::to_string(apexManifest.version());
51 }
52 
ReadManifest(const std::string & path)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 
61 }  // namespace apex
62 }  // namespace android
63