Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 17 | #include "fastdeploy.h" |
| 18 | |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 20 | #include <array> |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 21 | #include <memory> |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 22 | |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 23 | #include "android-base/file.h" |
| 24 | #include "android-base/strings.h" |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 25 | #include "androidfw/ResourceTypes.h" |
| 26 | #include "androidfw/ZipFileRO.h" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 27 | #include "client/file_sync_client.h" |
| 28 | #include "commandline.h" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 29 | #include "fastdeploycallbacks.h" |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 30 | #include "sysdeps.h" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 31 | #include "utils/String16.h" |
| 32 | |
Elliott Hughes | 86ab9ff | 2018-09-05 12:13:11 -0700 | [diff] [blame] | 33 | static constexpr long kRequiredAgentVersion = 0x00000001; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 34 | |
Elliott Hughes | 86ab9ff | 2018-09-05 12:13:11 -0700 | [diff] [blame] | 35 | static constexpr const char* kDeviceAgentPath = "/data/local/tmp/"; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 36 | |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 37 | static bool g_use_localagent = false; |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 38 | |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 39 | long get_agent_version() { |
| 40 | std::vector<char> versionOutputBuffer; |
| 41 | std::vector<char> versionErrorBuffer; |
| 42 | |
Idries Hamadi | 7575cd9 | 2018-08-24 11:46:45 +0100 | [diff] [blame] | 43 | int statusCode = capture_shell_command("/data/local/tmp/deployagent version", |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 44 | &versionOutputBuffer, &versionErrorBuffer); |
| 45 | long version = -1; |
| 46 | |
| 47 | if (statusCode == 0 && versionOutputBuffer.size() > 0) { |
| 48 | version = strtol((char*)versionOutputBuffer.data(), NULL, 16); |
| 49 | } |
| 50 | |
| 51 | return version; |
| 52 | } |
| 53 | |
| 54 | int get_device_api_level() { |
| 55 | std::vector<char> sdkVersionOutputBuffer; |
| 56 | std::vector<char> sdkVersionErrorBuffer; |
| 57 | int api_level = -1; |
| 58 | |
| 59 | int statusCode = capture_shell_command("getprop ro.build.version.sdk", &sdkVersionOutputBuffer, |
| 60 | &sdkVersionErrorBuffer); |
Idries Hamadi | 7f51e00 | 2018-08-23 17:22:21 +0100 | [diff] [blame] | 61 | if (statusCode == 0 && sdkVersionOutputBuffer.size() > 0) { |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 62 | api_level = strtol((char*)sdkVersionOutputBuffer.data(), NULL, 10); |
| 63 | } |
| 64 | |
| 65 | return api_level; |
| 66 | } |
| 67 | |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 68 | void fastdeploy_set_local_agent(bool use_localagent) { |
| 69 | g_use_localagent = use_localagent; |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 72 | // local_path - must start with a '/' and be relative to $ANDROID_PRODUCT_OUT |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 73 | static std::string get_agent_component_host_path(const char* local_path, const char* sdk_path) { |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 74 | std::string adb_dir = android::base::GetExecutableDirectory(); |
| 75 | if (adb_dir.empty()) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 76 | error(1, 0, "Could not determine location of adb!"); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 79 | if (g_use_localagent) { |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 80 | const char* product_out = getenv("ANDROID_PRODUCT_OUT"); |
| 81 | if (product_out == nullptr) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 82 | error(1, 0, "Could not locate %s because $ANDROID_PRODUCT_OUT is not defined", |
| 83 | local_path); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 84 | } |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 85 | return android::base::StringPrintf("%s%s", product_out, local_path); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 86 | } else { |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 87 | return adb_dir + sdk_path; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 88 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 91 | static bool deploy_agent(bool checkTimeStamps) { |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 92 | std::vector<const char*> srcs; |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 93 | std::string jar_path = |
| 94 | get_agent_component_host_path("/system/framework/deployagent.jar", "/deployagent.jar"); |
| 95 | std::string script_path = |
| 96 | get_agent_component_host_path("/system/bin/deployagent", "/deployagent"); |
| 97 | srcs.push_back(jar_path.c_str()); |
| 98 | srcs.push_back(script_path.c_str()); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 99 | |
| 100 | if (do_sync_push(srcs, kDeviceAgentPath, checkTimeStamps)) { |
| 101 | // on windows the shell script might have lost execute permission |
| 102 | // so need to set this explicitly |
Idries Hamadi | 7575cd9 | 2018-08-24 11:46:45 +0100 | [diff] [blame] | 103 | const char* kChmodCommandPattern = "chmod 777 %sdeployagent"; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 104 | std::string chmodCommand = |
| 105 | android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentPath); |
Idries Hamadi | 7f51e00 | 2018-08-23 17:22:21 +0100 | [diff] [blame] | 106 | int ret = send_shell_command(chmodCommand); |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 107 | if (ret != 0) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 108 | error(1, 0, "Error executing %s returncode: %d", chmodCommand.c_str(), ret); |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 109 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 110 | } else { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 111 | error(1, 0, "Error pushing agent files to device"); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 112 | } |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 113 | |
| 114 | return true; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 117 | void update_agent(FastDeploy_AgentUpdateStrategy agentUpdateStrategy) { |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 118 | long agent_version = get_agent_version(); |
| 119 | switch (agentUpdateStrategy) { |
| 120 | case FastDeploy_AgentUpdateAlways: |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 121 | deploy_agent(false); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 122 | break; |
| 123 | case FastDeploy_AgentUpdateNewerTimeStamp: |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 124 | deploy_agent(true); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 125 | break; |
| 126 | case FastDeploy_AgentUpdateDifferentVersion: |
| 127 | if (agent_version != kRequiredAgentVersion) { |
| 128 | if (agent_version < 0) { |
| 129 | printf("Could not detect agent on device, deploying\n"); |
| 130 | } else { |
| 131 | printf("Device agent version is (%ld), (%ld) is required, re-deploying\n", |
| 132 | agent_version, kRequiredAgentVersion); |
| 133 | } |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 134 | deploy_agent(false); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 135 | } |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | agent_version = get_agent_version(); |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 140 | if (agent_version != kRequiredAgentVersion) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 141 | error(1, 0, "After update agent version remains incorrect! Expected %ld but version is %ld", |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 142 | kRequiredAgentVersion, agent_version); |
| 143 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 146 | static std::string get_string_from_utf16(const char16_t* input, int input_len) { |
| 147 | ssize_t utf8_length = utf16_to_utf8_length(input, input_len); |
| 148 | if (utf8_length <= 0) { |
| 149 | return {}; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 150 | } |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 151 | std::string utf8; |
| 152 | utf8.resize(utf8_length); |
| 153 | utf16_to_utf8(input, input_len, &*utf8.begin(), utf8_length + 1); |
| 154 | return utf8; |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 157 | static std::string get_packagename_from_apk(const char* apkPath) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 158 | #undef open |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 159 | std::unique_ptr<android::ZipFileRO> zipFile(android::ZipFileRO::open(apkPath)); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 160 | #define open ___xxx_unix_open |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 161 | if (zipFile == nullptr) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 162 | error(1, errno, "Could not open %s", apkPath); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 163 | } |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 164 | android::ZipEntryRO entry = zipFile->findEntryByName("AndroidManifest.xml"); |
| 165 | if (entry == nullptr) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 166 | error(1, 0, "Could not find AndroidManifest.xml inside %s", apkPath); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 167 | } |
| 168 | uint32_t manifest_len = 0; |
| 169 | if (!zipFile->getEntryInfo(entry, NULL, &manifest_len, NULL, NULL, NULL, NULL)) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 170 | error(1, 0, "Could not read AndroidManifest.xml inside %s", apkPath); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 171 | } |
| 172 | std::vector<char> manifest_data(manifest_len); |
| 173 | if (!zipFile->uncompressEntry(entry, manifest_data.data(), manifest_len)) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 174 | error(1, 0, "Could not uncompress AndroidManifest.xml inside %s", apkPath); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 175 | } |
| 176 | android::ResXMLTree tree; |
| 177 | android::status_t setto_status = tree.setTo(manifest_data.data(), manifest_len, true); |
| 178 | if (setto_status != android::OK) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 179 | error(1, 0, "Could not parse AndroidManifest.xml inside %s", apkPath); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 180 | } |
| 181 | android::ResXMLParser::event_code_t code; |
| 182 | while ((code = tree.next()) != android::ResXMLParser::BAD_DOCUMENT && |
| 183 | code != android::ResXMLParser::END_DOCUMENT) { |
| 184 | switch (code) { |
| 185 | case android::ResXMLParser::START_TAG: { |
| 186 | size_t element_name_length; |
| 187 | const char16_t* element_name = tree.getElementName(&element_name_length); |
| 188 | if (element_name == nullptr) { |
| 189 | continue; |
| 190 | } |
| 191 | std::u16string element_name_string(element_name, element_name_length); |
| 192 | if (element_name_string == u"manifest") { |
| 193 | for (size_t i = 0; i < tree.getAttributeCount(); i++) { |
| 194 | size_t attribute_name_length; |
| 195 | const char16_t* attribute_name_text = |
| 196 | tree.getAttributeName(i, &attribute_name_length); |
| 197 | if (attribute_name_text == nullptr) { |
| 198 | continue; |
| 199 | } |
| 200 | std::u16string attribute_name_string(attribute_name_text, |
| 201 | attribute_name_length); |
| 202 | if (attribute_name_string == u"package") { |
| 203 | size_t attribute_value_length; |
| 204 | const char16_t* attribute_value_text = |
| 205 | tree.getAttributeStringValue(i, &attribute_value_length); |
| 206 | if (attribute_value_text == nullptr) { |
| 207 | continue; |
| 208 | } |
| 209 | return get_string_from_utf16(attribute_value_text, |
| 210 | attribute_value_length); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | break; |
| 215 | } |
| 216 | default: |
| 217 | break; |
| 218 | } |
| 219 | } |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 220 | error(1, 0, "Could not find package name tag in AndroidManifest.xml inside %s", apkPath); |
| 221 | __builtin_unreachable(); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 224 | void extract_metadata(const char* apkPath, FILE* outputFp) { |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 225 | std::string packageName = get_packagename_from_apk(apkPath); |
Idries Hamadi | 7575cd9 | 2018-08-24 11:46:45 +0100 | [diff] [blame] | 226 | const char* kAgentExtractCommandPattern = "/data/local/tmp/deployagent extract %s"; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 227 | std::string extractCommand = |
| 228 | android::base::StringPrintf(kAgentExtractCommandPattern, packageName.c_str()); |
| 229 | |
| 230 | std::vector<char> extractErrorBuffer; |
| 231 | int statusCode; |
| 232 | DeployAgentFileCallback cb(outputFp, &extractErrorBuffer, &statusCode); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 233 | int returnCode = send_shell_command(extractCommand, false, &cb); |
| 234 | if (returnCode != 0) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 235 | error(1, 0, "Executing %s returned %d", extractCommand.c_str(), returnCode); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 236 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 239 | static std::string get_patch_generator_command() { |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 240 | if (g_use_localagent) { |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 241 | // This should never happen on a Windows machine |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 242 | const char* host_out = getenv("ANDROID_HOST_OUT"); |
| 243 | if (host_out == nullptr) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 244 | error(1, 0, |
| 245 | "Could not locate deploypatchgenerator.jar because $ANDROID_HOST_OUT " |
| 246 | "is not defined"); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 247 | } |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 248 | return android::base::StringPrintf("java -jar %s/framework/deploypatchgenerator.jar", |
| 249 | host_out); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 250 | } |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 251 | |
| 252 | std::string adb_dir = android::base::GetExecutableDirectory(); |
| 253 | if (adb_dir.empty()) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 254 | error(1, 0, "Could not locate deploypatchgenerator.jar"); |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 255 | } |
| 256 | return android::base::StringPrintf(R"(java -jar "%s/deploypatchgenerator.jar")", |
| 257 | adb_dir.c_str()); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 260 | void create_patch(const char* apkPath, const char* metadataPath, const char* patchPath) { |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 261 | std::string generatePatchCommand = android::base::StringPrintf( |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 262 | R"(%s "%s" "%s" > "%s")", get_patch_generator_command().c_str(), apkPath, metadataPath, |
| 263 | patchPath); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 264 | int returnCode = system(generatePatchCommand.c_str()); |
| 265 | if (returnCode != 0) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 266 | error(1, 0, "Executing %s returned %d", generatePatchCommand.c_str(), returnCode); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 267 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | std::string get_patch_path(const char* apkPath) { |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 271 | std::string packageName = get_packagename_from_apk(apkPath); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 272 | std::string patchDevicePath = |
| 273 | android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str()); |
| 274 | return patchDevicePath; |
| 275 | } |
| 276 | |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 277 | void apply_patch_on_device(const char* apkPath, const char* patchPath, const char* outputPath) { |
Idries Hamadi | 7575cd9 | 2018-08-24 11:46:45 +0100 | [diff] [blame] | 278 | const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -o %s"; |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 279 | std::string packageName = get_packagename_from_apk(apkPath); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 280 | std::string patchDevicePath = get_patch_path(apkPath); |
| 281 | |
| 282 | std::vector<const char*> srcs = {patchPath}; |
| 283 | bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 284 | if (!push_ok) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 285 | error(1, 0, "Error pushing %s to %s returned", patchPath, patchDevicePath.c_str()); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | std::string applyPatchCommand = |
| 289 | android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(), |
| 290 | patchDevicePath.c_str(), outputPath); |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 291 | |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 292 | int returnCode = send_shell_command(applyPatchCommand); |
| 293 | if (returnCode != 0) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 294 | error(1, 0, "Executing %s returned %d", applyPatchCommand.c_str(), returnCode); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 295 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 298 | void install_patch(const char* apkPath, const char* patchPath, int argc, const char** argv) { |
Idries Hamadi | 7575cd9 | 2018-08-24 11:46:45 +0100 | [diff] [blame] | 299 | const std::string kAgentApplyCommandPattern = "/data/local/tmp/deployagent apply %s %s -pm %s"; |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 300 | std::string packageName = get_packagename_from_apk(apkPath); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 301 | |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 302 | std::string patchDevicePath = |
| 303 | android::base::StringPrintf("%s%s.patch", kDeviceAgentPath, packageName.c_str()); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 304 | |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 305 | std::vector<const char*> srcs{patchPath}; |
| 306 | bool push_ok = do_sync_push(srcs, patchDevicePath.c_str(), false); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 307 | if (!push_ok) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 308 | error(1, 0, "Error pushing %s to %s returned", patchPath, patchDevicePath.c_str()); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | std::vector<unsigned char> applyOutputBuffer; |
| 312 | std::vector<unsigned char> applyErrorBuffer; |
| 313 | std::string argsString; |
| 314 | |
| 315 | for (int i = 0; i < argc; i++) { |
| 316 | argsString.append(argv[i]); |
| 317 | argsString.append(" "); |
| 318 | } |
| 319 | |
| 320 | std::string applyPatchCommand = |
| 321 | android::base::StringPrintf(kAgentApplyCommandPattern.c_str(), packageName.c_str(), |
| 322 | patchDevicePath.c_str(), argsString.c_str()); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 323 | int returnCode = send_shell_command(applyPatchCommand); |
| 324 | if (returnCode != 0) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 325 | error(1, 0, "Executing %s returned %d", applyPatchCommand.c_str(), returnCode); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 326 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 327 | } |