• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 using ::apex::proto::ApexManifest;
26 
27 namespace android {
28 namespace apex {
29 
ParseManifest(const std::string & content)30 Result<ApexManifest> ParseManifest(const std::string& content) {
31   ApexManifest apex_manifest;
32 
33   if (!apex_manifest.ParseFromString(content)) {
34     return Error() << "Can't parse APEX manifest.";
35   }
36 
37   // Verifying required fields.
38   // name
39   if (apex_manifest.name().empty()) {
40     return Error() << "Missing required field \"name\" from APEX manifest.";
41   }
42 
43   // version
44   if (apex_manifest.version() == 0) {
45     return Error() << "Missing required field \"version\" from APEX manifest.";
46   }
47   return apex_manifest;
48 }
49 
GetPackageId(const ApexManifest & apex_manifest)50 std::string GetPackageId(const ApexManifest& apex_manifest) {
51   return apex_manifest.name() + "@" + std::to_string(apex_manifest.version());
52 }
53 
ReadManifest(const std::string & path)54 Result<ApexManifest> ReadManifest(const std::string& path) {
55   std::string content;
56   if (!android::base::ReadFileToString(path, &content)) {
57     return Error() << "Failed to read manifest file: " << path;
58   }
59   return ParseManifest(content);
60 }
61 
62 }  // namespace apex
63 }  // namespace android
64