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