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" |
Joshua Gilpatrick | af3ce08 | 2019-07-19 09:42:12 -0700 | [diff] [blame] | 30 | #include "deployagent.inc" // Generated include via build rule. |
| 31 | #include "deployagentscript.inc" // Generated include via build rule. |
| 32 | #include "fastdeploy/deploypatchgenerator/deploy_patch_generator.h" |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 33 | #include "fastdeploy/deploypatchgenerator/patch_utils.h" |
| 34 | #include "fastdeploy/proto/ApkEntry.pb.h" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 35 | #include "fastdeploycallbacks.h" |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 36 | #include "sysdeps.h" |
Elliott Hughes | d2aaede | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 37 | |
| 38 | #include "adb_utils.h" |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 39 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 40 | static constexpr long kRequiredAgentVersion = 0x00000003; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 41 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 42 | static constexpr int kPackageMissing = 3; |
| 43 | static constexpr int kInvalidAgentVersion = 4; |
| 44 | |
Joshua Gilpatrick | af3ce08 | 2019-07-19 09:42:12 -0700 | [diff] [blame] | 45 | static constexpr const char* kDeviceAgentFile = "/data/local/tmp/deployagent.jar"; |
| 46 | static constexpr const char* kDeviceAgentScript = "/data/local/tmp/deployagent"; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 47 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 48 | static constexpr bool g_verbose_timings = false; |
| 49 | static FastDeploy_AgentUpdateStrategy g_agent_update_strategy = |
| 50 | FastDeploy_AgentUpdateDifferentVersion; |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 51 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 52 | using APKMetaData = com::android::fastdeploy::APKMetaData; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 53 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 54 | namespace { |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 55 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 56 | struct TimeReporter { |
| 57 | TimeReporter(const char* label) : label_(label) {} |
| 58 | ~TimeReporter() { |
| 59 | if (g_verbose_timings) { |
| 60 | auto duration = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 61 | std::chrono::steady_clock::now() - start_); |
| 62 | fprintf(stderr, "%s finished in %lldms\n", label_, |
| 63 | static_cast<long long>(duration.count())); |
| 64 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 67 | private: |
| 68 | const char* label_; |
| 69 | std::chrono::steady_clock::time_point start_ = std::chrono::steady_clock::now(); |
| 70 | }; |
| 71 | #define REPORT_FUNC_TIME() TimeReporter reporter(__func__) |
| 72 | |
| 73 | struct FileDeleter { |
| 74 | FileDeleter(const char* path) : path_(path) {} |
| 75 | ~FileDeleter() { adb_unlink(path_); } |
| 76 | |
| 77 | private: |
| 78 | const char* const path_; |
| 79 | }; |
| 80 | |
| 81 | } // namespace |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 82 | |
| 83 | int get_device_api_level() { |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 84 | REPORT_FUNC_TIME(); |
| 85 | std::vector<char> sdk_version_output_buffer; |
| 86 | std::vector<char> sdk_version_error_buffer; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 87 | int api_level = -1; |
| 88 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 89 | int statusCode = capture_shell_command("getprop ro.build.version.sdk", |
| 90 | &sdk_version_output_buffer, &sdk_version_error_buffer); |
| 91 | if (statusCode == 0 && sdk_version_output_buffer.size() > 0) { |
| 92 | api_level = strtol((char*)sdk_version_output_buffer.data(), NULL, 10); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | return api_level; |
| 96 | } |
| 97 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 98 | void fastdeploy_set_agent_update_strategy(FastDeploy_AgentUpdateStrategy agent_update_strategy) { |
| 99 | g_agent_update_strategy = agent_update_strategy; |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 102 | static void push_to_device(const void* data, size_t byte_count, const char* dst, bool sync) { |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 103 | std::vector<const char*> srcs; |
Elliott Hughes | 74f0fc6 | 2019-11-07 15:38:00 -0800 | [diff] [blame] | 104 | TemporaryFile tf; |
| 105 | android::base::WriteFully(tf.fd, data, byte_count); |
| 106 | srcs.push_back(tf.path); |
| 107 | // On Windows, the file needs to be flushed before pushing to device, |
| 108 | // but can't be removed until after the push. |
| 109 | unix_close(tf.release()); |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 110 | |
| 111 | if (!do_sync_push(srcs, dst, sync)) { |
Joshua Gilpatrick | af3ce08 | 2019-07-19 09:42:12 -0700 | [diff] [blame] | 112 | error_exit("Failed to push fastdeploy agent to device."); |
| 113 | } |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static bool deploy_agent(bool check_time_stamps) { |
| 117 | REPORT_FUNC_TIME(); |
| 118 | |
| 119 | push_to_device(kDeployAgent, sizeof(kDeployAgent), kDeviceAgentFile, check_time_stamps); |
| 120 | push_to_device(kDeployAgentScript, sizeof(kDeployAgentScript), kDeviceAgentScript, |
| 121 | check_time_stamps); |
| 122 | |
Joshua Gilpatrick | af3ce08 | 2019-07-19 09:42:12 -0700 | [diff] [blame] | 123 | // on windows the shell script might have lost execute permission |
| 124 | // so need to set this explicitly |
| 125 | const char* kChmodCommandPattern = "chmod 777 %s"; |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 126 | std::string chmod_command = |
Joshua Gilpatrick | af3ce08 | 2019-07-19 09:42:12 -0700 | [diff] [blame] | 127 | android::base::StringPrintf(kChmodCommandPattern, kDeviceAgentScript); |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 128 | int ret = send_shell_command(chmod_command); |
Joshua Gilpatrick | af3ce08 | 2019-07-19 09:42:12 -0700 | [diff] [blame] | 129 | if (ret != 0) { |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 130 | error_exit("Error executing %s returncode: %d", chmod_command.c_str(), ret); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 131 | } |
Idries Hamadi | 4af6ee4 | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 132 | |
| 133 | return true; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 136 | static std::string get_string_from_utf16(const char16_t* input, int input_len) { |
| 137 | ssize_t utf8_length = utf16_to_utf8_length(input, input_len); |
| 138 | if (utf8_length <= 0) { |
| 139 | return {}; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 140 | } |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 141 | std::string utf8; |
| 142 | utf8.resize(utf8_length); |
| 143 | utf16_to_utf8(input, input_len, &*utf8.begin(), utf8_length + 1); |
| 144 | return utf8; |
Idries Hamadi | 5b6bf94 | 2018-08-28 12:58:09 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 147 | static std::string get_package_name_from_apk(const char* apk_path) { |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 148 | #undef open |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 149 | std::unique_ptr<android::ZipFileRO> zip_file((android::ZipFileRO::open)(apk_path)); |
Elliott Hughes | 4679a39 | 2018-10-19 13:59:44 -0700 | [diff] [blame] | 150 | #define open ___xxx_unix_open |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 151 | if (zip_file == nullptr) { |
| 152 | perror_exit("Could not open %s", apk_path); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 153 | } |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 154 | android::ZipEntryRO entry = zip_file->findEntryByName("AndroidManifest.xml"); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 155 | if (entry == nullptr) { |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 156 | error_exit("Could not find AndroidManifest.xml inside %s", apk_path); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 157 | } |
| 158 | uint32_t manifest_len = 0; |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 159 | if (!zip_file->getEntryInfo(entry, NULL, &manifest_len, NULL, NULL, NULL, NULL)) { |
| 160 | error_exit("Could not read AndroidManifest.xml inside %s", apk_path); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 161 | } |
| 162 | std::vector<char> manifest_data(manifest_len); |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 163 | if (!zip_file->uncompressEntry(entry, manifest_data.data(), manifest_len)) { |
| 164 | error_exit("Could not uncompress AndroidManifest.xml inside %s", apk_path); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 165 | } |
| 166 | android::ResXMLTree tree; |
| 167 | android::status_t setto_status = tree.setTo(manifest_data.data(), manifest_len, true); |
| 168 | if (setto_status != android::OK) { |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 169 | error_exit("Could not parse AndroidManifest.xml inside %s", apk_path); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 170 | } |
| 171 | android::ResXMLParser::event_code_t code; |
| 172 | while ((code = tree.next()) != android::ResXMLParser::BAD_DOCUMENT && |
| 173 | code != android::ResXMLParser::END_DOCUMENT) { |
| 174 | switch (code) { |
| 175 | case android::ResXMLParser::START_TAG: { |
| 176 | size_t element_name_length; |
| 177 | const char16_t* element_name = tree.getElementName(&element_name_length); |
| 178 | if (element_name == nullptr) { |
| 179 | continue; |
| 180 | } |
| 181 | std::u16string element_name_string(element_name, element_name_length); |
| 182 | if (element_name_string == u"manifest") { |
| 183 | for (size_t i = 0; i < tree.getAttributeCount(); i++) { |
| 184 | size_t attribute_name_length; |
| 185 | const char16_t* attribute_name_text = |
| 186 | tree.getAttributeName(i, &attribute_name_length); |
| 187 | if (attribute_name_text == nullptr) { |
| 188 | continue; |
| 189 | } |
| 190 | std::u16string attribute_name_string(attribute_name_text, |
| 191 | attribute_name_length); |
| 192 | if (attribute_name_string == u"package") { |
| 193 | size_t attribute_value_length; |
| 194 | const char16_t* attribute_value_text = |
| 195 | tree.getAttributeStringValue(i, &attribute_value_length); |
| 196 | if (attribute_value_text == nullptr) { |
| 197 | continue; |
| 198 | } |
| 199 | return get_string_from_utf16(attribute_value_text, |
| 200 | attribute_value_length); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | break; |
| 205 | } |
| 206 | default: |
| 207 | break; |
| 208 | } |
| 209 | } |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 210 | error_exit("Could not find package name tag in AndroidManifest.xml inside %s", apk_path); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 213 | static long parse_agent_version(const std::vector<char>& version_buffer) { |
| 214 | long version = -1; |
| 215 | if (!version_buffer.empty()) { |
| 216 | version = strtol((char*)version_buffer.data(), NULL, 16); |
| 217 | } |
| 218 | return version; |
| 219 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 220 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 221 | static void update_agent_if_necessary() { |
| 222 | switch (g_agent_update_strategy) { |
| 223 | case FastDeploy_AgentUpdateAlways: |
| 224 | deploy_agent(/*check_time_stamps=*/false); |
| 225 | break; |
| 226 | case FastDeploy_AgentUpdateNewerTimeStamp: |
| 227 | deploy_agent(/*check_time_stamps=*/true); |
| 228 | break; |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | std::optional<APKMetaData> extract_metadata(const char* apk_path) { |
| 235 | // Update agent if there is a command line argument forcing to do so. |
| 236 | update_agent_if_necessary(); |
| 237 | |
| 238 | REPORT_FUNC_TIME(); |
| 239 | |
| 240 | std::string package_name = get_package_name_from_apk(apk_path); |
| 241 | |
| 242 | // Dump apk command checks the required vs current agent version and if they match then returns |
| 243 | // the APK dump for package. Doing this in a single call saves round-trip and agent launch time. |
| 244 | constexpr const char* kAgentDumpCommandPattern = "/data/local/tmp/deployagent dump %ld %s"; |
| 245 | std::string dump_command = android::base::StringPrintf( |
| 246 | kAgentDumpCommandPattern, kRequiredAgentVersion, package_name.c_str()); |
| 247 | |
| 248 | std::vector<char> dump_out_buffer; |
| 249 | std::vector<char> dump_error_buffer; |
| 250 | int returnCode = |
| 251 | capture_shell_command(dump_command.c_str(), &dump_out_buffer, &dump_error_buffer); |
| 252 | if (returnCode >= kInvalidAgentVersion) { |
| 253 | // Agent has wrong version or missing. |
| 254 | long agent_version = parse_agent_version(dump_out_buffer); |
| 255 | if (agent_version < 0) { |
| 256 | printf("Could not detect agent on device, deploying\n"); |
| 257 | } else { |
| 258 | printf("Device agent version is (%ld), (%ld) is required, re-deploying\n", |
| 259 | agent_version, kRequiredAgentVersion); |
| 260 | } |
| 261 | deploy_agent(/*check_time_stamps=*/false); |
| 262 | |
| 263 | // Retry with new agent. |
| 264 | dump_out_buffer.clear(); |
| 265 | dump_error_buffer.clear(); |
| 266 | returnCode = |
| 267 | capture_shell_command(dump_command.c_str(), &dump_out_buffer, &dump_error_buffer); |
| 268 | } |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 269 | if (returnCode != 0) { |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 270 | if (returnCode == kInvalidAgentVersion) { |
| 271 | long agent_version = parse_agent_version(dump_out_buffer); |
| 272 | error_exit( |
| 273 | "After update agent version remains incorrect! Expected %ld but version is %ld", |
| 274 | kRequiredAgentVersion, agent_version); |
| 275 | } |
| 276 | if (returnCode == kPackageMissing) { |
| 277 | fprintf(stderr, "Package %s not found, falling back to install\n", |
| 278 | package_name.c_str()); |
| 279 | return {}; |
| 280 | } |
| 281 | fprintf(stderr, "Executing %s returned %d\n", dump_command.c_str(), returnCode); |
| 282 | fprintf(stderr, "%*s\n", int(dump_error_buffer.size()), dump_error_buffer.data()); |
Henry Daitx | f21edf3 | 2018-11-30 18:02:43 +0000 | [diff] [blame] | 283 | error_exit("Aborting"); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 284 | } |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 285 | |
| 286 | com::android::fastdeploy::APKDump dump; |
| 287 | if (!dump.ParseFromArray(dump_out_buffer.data(), dump_out_buffer.size())) { |
| 288 | fprintf(stderr, "Can't parse output of %s\n", dump_command.c_str()); |
| 289 | error_exit("Aborting"); |
| 290 | } |
| 291 | |
| 292 | return PatchUtils::GetDeviceAPKMetaData(dump); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 295 | unique_fd install_patch(int argc, const char** argv) { |
| 296 | REPORT_FUNC_TIME(); |
| 297 | constexpr char kAgentApplyServicePattern[] = "shell:/data/local/tmp/deployagent apply - -pm %s"; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 298 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 299 | std::vector<unsigned char> apply_output_buffer; |
| 300 | std::vector<unsigned char> apply_error_buffer; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 301 | std::string argsString; |
| 302 | |
Henry Daitx | ee01c80 | 2018-12-12 10:40:57 +0000 | [diff] [blame] | 303 | bool rSwitchPresent = false; |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 304 | for (int i = 0; i < argc; i++) { |
| 305 | argsString.append(argv[i]); |
| 306 | argsString.append(" "); |
Henry Daitx | ee01c80 | 2018-12-12 10:40:57 +0000 | [diff] [blame] | 307 | if (!strcmp(argv[i], "-r")) { |
| 308 | rSwitchPresent = true; |
| 309 | } |
| 310 | } |
| 311 | if (!rSwitchPresent) { |
| 312 | argsString.append("-r"); |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 315 | std::string error; |
| 316 | std::string apply_patch_service_string = |
| 317 | android::base::StringPrintf(kAgentApplyServicePattern, argsString.c_str()); |
| 318 | unique_fd fd{adb_connect(apply_patch_service_string, &error)}; |
| 319 | if (fd < 0) { |
| 320 | error_exit("Executing %s returned %s", apply_patch_service_string.c_str(), error.c_str()); |
| 321 | } |
| 322 | return fd; |
| 323 | } |
| 324 | |
| 325 | unique_fd apply_patch_on_device(const char* output_path) { |
| 326 | REPORT_FUNC_TIME(); |
| 327 | constexpr char kAgentApplyServicePattern[] = "shell:/data/local/tmp/deployagent apply - -o %s"; |
| 328 | |
| 329 | std::string error; |
| 330 | std::string apply_patch_service_string = |
| 331 | android::base::StringPrintf(kAgentApplyServicePattern, output_path); |
| 332 | unique_fd fd{adb_connect(apply_patch_service_string, &error)}; |
| 333 | if (fd < 0) { |
| 334 | error_exit("Executing %s returned %s", apply_patch_service_string.c_str(), error.c_str()); |
| 335 | } |
| 336 | return fd; |
| 337 | } |
| 338 | |
| 339 | static void create_patch(const char* apk_path, APKMetaData metadata, borrowed_fd patch_fd) { |
| 340 | REPORT_FUNC_TIME(); |
| 341 | DeployPatchGenerator generator(/*is_verbose=*/false); |
| 342 | bool success = generator.CreatePatch(apk_path, std::move(metadata), patch_fd); |
| 343 | if (!success) { |
| 344 | error_exit("Failed to create patch for %s", apk_path); |
Idries Hamadi | 269a4b4 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 345 | } |
Idries Hamadi | ed409ea | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 346 | } |
Henry Daitx | ee01c80 | 2018-12-12 10:40:57 +0000 | [diff] [blame] | 347 | |
Alex Buynytskyy | 665f3ff | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 348 | int stream_patch(const char* apk_path, APKMetaData metadata, unique_fd patch_fd) { |
| 349 | create_patch(apk_path, std::move(metadata), patch_fd); |
| 350 | |
| 351 | REPORT_FUNC_TIME(); |
| 352 | return read_and_dump(patch_fd.get()); |
Henry Daitx | ee01c80 | 2018-12-12 10:40:57 +0000 | [diff] [blame] | 353 | } |