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