blob: fb5f1f5b606540f30870700bdbc7d3188f8a3bf7 [file] [log] [blame]
Anton Hanssonda4972f2020-01-08 09:48:18 +00001/*
2 * Copyright (C) 2019 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#define LOG_TAG "derive_sdk"
18
Anton Hanssondddcb572020-11-24 09:32:14 +000019#include "derive_sdk.h"
20
Anton Hanssonda4972f2020-01-08 09:48:18 +000021#include <android-base/file.h>
22#include <android-base/logging.h>
23#include <android-base/properties.h>
Anton Hanssonb1a217c2020-11-30 11:43:05 +000024#include <android-modules-utils/sdk_level.h>
Anton Hanssone4fd55c2020-12-29 17:46:30 +000025#include <dirent.h>
26#include <sys/stat.h>
27
28#include <algorithm>
29#include <iostream>
30#include <vector>
Anton Hanssonda4972f2020-01-08 09:48:18 +000031
Baligh Uddina1fed012020-05-22 16:48:01 +000032#include "packages/modules/SdkExtensions/derive_sdk/sdk.pb.h"
Anton Hanssonda4972f2020-01-08 09:48:18 +000033
34using com::android::sdkext::proto::SdkVersion;
35
Anton Hanssondddcb572020-11-24 09:32:14 +000036namespace android {
37namespace derivesdk {
38
39bool SetSdkLevels(const std::string& mountpath) {
40 std::unique_ptr<DIR, decltype(&closedir)> apex(opendir(mountpath.c_str()),
41 closedir);
Anton Hanssone4fd55c2020-12-29 17:46:30 +000042 if (!apex) {
Anton Hanssondddcb572020-11-24 09:32:14 +000043 LOG(ERROR) << "Could not read " + mountpath;
44 return false;
Anton Hanssone4fd55c2020-12-29 17:46:30 +000045 }
46 struct dirent* de;
47 std::vector<std::string> paths;
48 while ((de = readdir(apex.get()))) {
49 std::string name = de->d_name;
50 if (name[0] == '.' || name.find('@') != std::string::npos) {
51 // Skip <name>@<ver> dirs, as they are bind-mounted to <name>
52 continue;
Anton Hanssonda4972f2020-01-08 09:48:18 +000053 }
Anton Hanssondddcb572020-11-24 09:32:14 +000054 std::string path = mountpath + "/" + name + "/etc/sdkinfo.binarypb";
Anton Hanssone4fd55c2020-12-29 17:46:30 +000055 struct stat statbuf;
56 if (stat(path.c_str(), &statbuf) == 0) {
57 paths.push_back(path);
Anton Hanssonda4972f2020-01-08 09:48:18 +000058 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +000059 }
Anton Hanssonda4972f2020-01-08 09:48:18 +000060
Anton Hanssone4fd55c2020-12-29 17:46:30 +000061 std::vector<int> versions;
62 for (const auto& path : paths) {
63 std::string contents;
64 if (!android::base::ReadFileToString(path, &contents, true)) {
65 LOG(ERROR) << "failed to read " << path;
66 continue;
Anton Hanssonda4972f2020-01-08 09:48:18 +000067 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +000068 SdkVersion sdk_version;
69 if (!sdk_version.ParseFromString(contents)) {
70 LOG(ERROR) << "failed to parse " << path;
71 continue;
72 }
73 LOG(INFO) << "Read version " << sdk_version.version() << " from " << path;
74 versions.push_back(sdk_version.version());
75 }
76 auto itr = std::min_element(versions.begin(), versions.end());
77 std::string prop_value = itr == versions.end() ? "0" : std::to_string(*itr);
Anton Hanssonda4972f2020-01-08 09:48:18 +000078
Anton Hanssone4fd55c2020-12-29 17:46:30 +000079 if (!android::base::SetProperty("build.version.extensions.r", prop_value)) {
80 LOG(ERROR) << "failed to set r sdk_info prop";
Anton Hanssondddcb572020-11-24 09:32:14 +000081 return false;
Anton Hanssone4fd55c2020-12-29 17:46:30 +000082 }
83 if (android::modules::sdklevel::IsAtLeastS()) {
84 if (!android::base::SetProperty("build.version.extensions.s", prop_value)) {
85 LOG(ERROR) << "failed to set s sdk_info prop";
Anton Hanssondddcb572020-11-24 09:32:14 +000086 return false;
Anton Hanssonda4972f2020-01-08 09:48:18 +000087 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +000088 }
Anton Hanssonda4972f2020-01-08 09:48:18 +000089
Anton Hanssone4fd55c2020-12-29 17:46:30 +000090 LOG(INFO) << "Extension version is " << prop_value;
Anton Hanssondddcb572020-11-24 09:32:14 +000091 return true;
Anton Hanssonda4972f2020-01-08 09:48:18 +000092}
Anton Hanssondddcb572020-11-24 09:32:14 +000093
94} // namespace derivesdk
95} // namespace android