1*33f37583SAndroid Build Coastguard Worker /* 2*33f37583SAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project 3*33f37583SAndroid Build Coastguard Worker * 4*33f37583SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*33f37583SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*33f37583SAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*33f37583SAndroid Build Coastguard Worker * 8*33f37583SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*33f37583SAndroid Build Coastguard Worker * 10*33f37583SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*33f37583SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*33f37583SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*33f37583SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*33f37583SAndroid Build Coastguard Worker * limitations under the License. 15*33f37583SAndroid Build Coastguard Worker */ 16*33f37583SAndroid Build Coastguard Worker 17*33f37583SAndroid Build Coastguard Worker #include "apex_manifest.h" 18*33f37583SAndroid Build Coastguard Worker #include <android-base/file.h> 19*33f37583SAndroid Build Coastguard Worker 20*33f37583SAndroid Build Coastguard Worker #include <memory> 21*33f37583SAndroid Build Coastguard Worker #include <string> 22*33f37583SAndroid Build Coastguard Worker 23*33f37583SAndroid Build Coastguard Worker using android::base::Error; 24*33f37583SAndroid Build Coastguard Worker using android::base::Result; 25*33f37583SAndroid Build Coastguard Worker using ::apex::proto::ApexManifest; 26*33f37583SAndroid Build Coastguard Worker 27*33f37583SAndroid Build Coastguard Worker namespace android { 28*33f37583SAndroid Build Coastguard Worker namespace apex { 29*33f37583SAndroid Build Coastguard Worker ParseManifest(const std::string & content)30*33f37583SAndroid Build Coastguard WorkerResult<ApexManifest> ParseManifest(const std::string& content) { 31*33f37583SAndroid Build Coastguard Worker ApexManifest apex_manifest; 32*33f37583SAndroid Build Coastguard Worker 33*33f37583SAndroid Build Coastguard Worker if (!apex_manifest.ParseFromString(content)) { 34*33f37583SAndroid Build Coastguard Worker return Error() << "Can't parse APEX manifest."; 35*33f37583SAndroid Build Coastguard Worker } 36*33f37583SAndroid Build Coastguard Worker 37*33f37583SAndroid Build Coastguard Worker // Verifying required fields. 38*33f37583SAndroid Build Coastguard Worker // name 39*33f37583SAndroid Build Coastguard Worker if (apex_manifest.name().empty()) { 40*33f37583SAndroid Build Coastguard Worker return Error() << "Missing required field \"name\" from APEX manifest."; 41*33f37583SAndroid Build Coastguard Worker } 42*33f37583SAndroid Build Coastguard Worker 43*33f37583SAndroid Build Coastguard Worker // version 44*33f37583SAndroid Build Coastguard Worker if (apex_manifest.version() == 0) { 45*33f37583SAndroid Build Coastguard Worker return Error() << "Missing required field \"version\" from APEX manifest."; 46*33f37583SAndroid Build Coastguard Worker } 47*33f37583SAndroid Build Coastguard Worker return apex_manifest; 48*33f37583SAndroid Build Coastguard Worker } 49*33f37583SAndroid Build Coastguard Worker GetPackageId(const ApexManifest & apex_manifest)50*33f37583SAndroid Build Coastguard Workerstd::string GetPackageId(const ApexManifest& apex_manifest) { 51*33f37583SAndroid Build Coastguard Worker return apex_manifest.name() + "@" + std::to_string(apex_manifest.version()); 52*33f37583SAndroid Build Coastguard Worker } 53*33f37583SAndroid Build Coastguard Worker ReadManifest(const std::string & path)54*33f37583SAndroid Build Coastguard WorkerResult<ApexManifest> ReadManifest(const std::string& path) { 55*33f37583SAndroid Build Coastguard Worker std::string content; 56*33f37583SAndroid Build Coastguard Worker if (!android::base::ReadFileToString(path, &content)) { 57*33f37583SAndroid Build Coastguard Worker return Error() << "Failed to read manifest file: " << path; 58*33f37583SAndroid Build Coastguard Worker } 59*33f37583SAndroid Build Coastguard Worker return ParseManifest(content); 60*33f37583SAndroid Build Coastguard Worker } 61*33f37583SAndroid Build Coastguard Worker 62*33f37583SAndroid Build Coastguard Worker } // namespace apex 63*33f37583SAndroid Build Coastguard Worker } // namespace android 64