xref: /aosp_15_r20/system/apex/apexd/apex_blocklist.cpp (revision 33f3758387333dbd2962d7edbd98681940d895da)
1 /*
2  * Copyright (C) 2024 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_blocklist.h"
18 
19 #include <android-base/file.h>
20 #include <google/protobuf/util/json_util.h>
21 
22 #include <memory>
23 #include <string>
24 
25 using android::base::Error;
26 using android::base::Result;
27 using ::apex::proto::ApexBlocklist;
28 
29 namespace android::apex {
30 
ParseBlocklist(const std::string & content)31 Result<ApexBlocklist> ParseBlocklist(const std::string& content) {
32   ApexBlocklist apex_blocklist;
33   google::protobuf::util::JsonParseOptions options;
34   options.ignore_unknown_fields = true;
35   auto parse_result = google::protobuf::util::JsonStringToMessage(
36       content, &apex_blocklist, options);
37   if (!parse_result.ok()) {
38     return Error() << "Can't parse APEX blocklist: " << parse_result.message();
39   }
40 
41   for (const auto& apex : apex_blocklist.blocked_apex()) {
42     // Verifying required fields.
43     // name
44     if (apex.name().empty()) {
45       return Error() << "Missing required field \"name\" from APEX blocklist.";
46     }
47 
48     // version
49     if (apex.version() <= 0) {
50       return Error() << "Missing positive value for field \"version\" "
51                         "from APEX blocklist.";
52     }
53   }
54 
55   return apex_blocklist;
56 }
57 
ReadBlocklist(const std::string & path)58 Result<ApexBlocklist> ReadBlocklist(const std::string& path) {
59   std::string content;
60   if (!android::base::ReadFileToString(path, &content)) {
61     return Error() << "Failed to read blocklist file: " << path;
62   }
63   return ParseBlocklist(content);
64 }
65 
66 }  // namespace android::apex
67