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