summaryrefslogtreecommitdiff
path: root/apex/sdkext/derive_sdk/derive_sdk.cpp
blob: 0a97116770159f6b5de649cb1d43a074f9c494e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "derive_sdk"

#include <algorithm>
#include <dirent.h>
#include <iostream>
#include <sys/stat.h>
#include <vector>

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>

#include "frameworks/base/apex/sdkext/derive_sdk/sdk.pb.h"

using com::android::sdkext::proto::SdkVersion;

int main(int, char**) {
    std::unique_ptr<DIR, decltype(&closedir)> apex(opendir("/apex"), closedir);
    if (!apex) {
        LOG(ERROR) << "Could not read /apex";
        return EXIT_FAILURE;
    }
    struct dirent* de;
    std::vector<std::string> paths;
    while ((de = readdir(apex.get()))) {
        std::string name = de->d_name;
        if (name[0] == '.' || name.find('@') != std::string::npos) {
            // Skip <name>@<ver> dirs, as they are bind-mounted to <name>
            continue;
        }
        std::string path = "/apex/" + name + "/etc/sdkinfo.binarypb";
        struct stat statbuf;
        if (stat(path.c_str(), &statbuf) == 0) {
            paths.push_back(path);
        }
    }

    std::vector<int> versions;
    for (const auto& path : paths) {
        std::string contents;
        if (!android::base::ReadFileToString(path, &contents, true)) {
            LOG(ERROR) << "failed to read " << path;
            continue;
        }
        SdkVersion sdk_version;
        if (!sdk_version.ParseFromString(contents)) {
            LOG(ERROR) << "failed to parse " << path;
            continue;
        }
        LOG(INFO) << "Read version " << sdk_version.version() << " from " << path;
        versions.push_back(sdk_version.version());
    }
    auto itr = std::min_element(versions.begin(), versions.end());
    std::string prop_value = itr == versions.end() ? "0" : std::to_string(*itr);

    if (!android::base::SetProperty("ro.build.version.extensions.r", prop_value)) {
        LOG(ERROR) << "failed to set sdk_info prop";
        return EXIT_FAILURE;
    }

    LOG(INFO) << "R extension version is " << prop_value;
    return EXIT_SUCCESS;
}