1 /*
2  * Copyright (C) 2020 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 #include "linkerconfig/apex.h"
17 
18 #include <unistd.h>
19 
20 #include <apexutil.h>
21 
22 #include "linkerconfig/stringutil.h"
23 
24 namespace {
25 
26 bool DirExists(const std::string& path) {
27   return access(path.c_str(), F_OK) == 0;
28 }
29 
30 }  // namespace
31 
32 namespace android {
33 namespace linkerconfig {
34 namespace modules {
35 
36 std::map<std::string, ApexInfo> ScanActiveApexes(const std::string& root) {
37   std::map<std::string, ApexInfo> apexes;
38   const auto apex_root = root + apex::kApexRoot;
39   for (const auto& [path, manifest] : apex::GetActivePackages(apex_root)) {
40     bool has_bin = DirExists(path + "/bin");
41     bool has_lib = DirExists(path + "/lib") || DirExists(path + "/lib64");
42     ApexInfo info(manifest.name(),
43                   TrimPrefix(path, root),
44                   {manifest.providenativelibs().begin(),
45                    manifest.providenativelibs().end()},
46                   {manifest.requirenativelibs().begin(),
47                    manifest.requirenativelibs().end()},
48                   has_bin,
49                   has_lib);
50     apexes.emplace(manifest.name(), std::move(info));
51   }
52   return apexes;
53 }
54 
55 }  // namespace modules
56 }  // namespace linkerconfig
57 }  // namespace android