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
17 #include "odr_common.h"
18
19 #include <sys/system_properties.h>
20
21 #include <functional>
22 #include <initializer_list>
23 #include <regex>
24 #include <sstream>
25 #include <string>
26 #include <string_view>
27
28 #include "android-base/logging.h"
29 #include "android-base/parseint.h"
30 #include "android-base/result.h"
31
32 namespace art {
33 namespace odrefresh {
34
35 namespace {
36
37 using ::android::base::Result;
38
39 }
40
Concatenate(std::initializer_list<std::string_view> args)41 std::string Concatenate(std::initializer_list<std::string_view> args) {
42 std::stringstream ss;
43 for (auto arg : args) {
44 ss << arg;
45 }
46 return ss.str();
47 }
48
QuotePath(std::string_view path)49 std::string QuotePath(std::string_view path) {
50 return Concatenate({"'", path, "'"});
51 }
52
ParseSecurityPatchStr(const std::string & security_patch_str)53 Result<int> ParseSecurityPatchStr(const std::string& security_patch_str) {
54 std::regex security_patch_regex(R"re((\d{4})-(\d{2})-(\d{2}))re");
55 std::smatch m;
56 if (!std::regex_match(security_patch_str, m, security_patch_regex)) {
57 return Errorf("Invalid security patch string \"{}\"", security_patch_str);
58 }
59 int year = 0, month = 0, day = 0;
60 if (!android::base::ParseInt(m[1], &year) ||
61 !android::base::ParseInt(m[2], &month) ||
62 !android::base::ParseInt(m[3], &day)) {
63 // This should never happen because the string already matches the regex.
64 return Errorf("Unknown error when parsing security patch string \"{}\"", security_patch_str);
65 }
66 return year * 10000 + month * 100 + day;
67 }
68
ShouldDisablePartialCompilation(const std::string & security_patch_str)69 bool ShouldDisablePartialCompilation(const std::string& security_patch_str) {
70 Result<int> security_patch_value = ParseSecurityPatchStr(security_patch_str);
71 if (!security_patch_value.ok()) {
72 LOG(ERROR) << security_patch_value.error();
73 return false;
74 }
75 return security_patch_value.value() < ParseSecurityPatchStr("2022-03-05").value();
76 }
77
ShouldDisableRefresh(const std::string & sdk_version_str)78 bool ShouldDisableRefresh(const std::string& sdk_version_str) {
79 int sdk_version = 0;
80 if (!android::base::ParseInt(sdk_version_str, &sdk_version)) {
81 return false;
82 }
83 return sdk_version >= 32;
84 }
85
SystemPropertyForeach(std::function<void (const char * name,const char * value)> action)86 void SystemPropertyForeach(std::function<void(const char* name, const char* value)> action) {
87 __system_property_foreach(
88 [](const prop_info* pi, void* cookie) {
89 __system_property_read_callback(
90 pi,
91 [](void* cookie, const char* name, const char* value, unsigned) {
92 auto action =
93 reinterpret_cast<std::function<void(const char* name, const char* value)>*>(
94 cookie);
95 (*action)(name, value);
96 },
97 cookie);
98 },
99 &action);
100 }
101
102 } // namespace odrefresh
103 } // namespace art
104