| Idries Hamadi | 1ecee44 | 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 | 78330f0 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 17 | #include "adb_install.h" |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 18 | |
| Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
| Josh Gao | a4dfc14 | 2020-02-19 13:50:57 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| Idries Hamadi | 78330f0 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 23 | #include <unistd.h> |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 24 | #include <algorithm> |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 25 | #include <string> |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 26 | #include <string_view> |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 29 | #include <android-base/file.h> |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 30 | #include <android-base/parsebool.h> |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> |
| 32 | #include <android-base/strings.h> |
| 33 | |
| Idries Hamadi | 78330f0 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 34 | #include "adb.h" |
| 35 | #include "adb_client.h" |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 36 | #include "adb_unique_fd.h" |
| Idries Hamadi | 78330f0 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 37 | #include "adb_utils.h" |
| Idries Hamadi | 78330f0 | 2018-09-13 18:00:25 +0100 | [diff] [blame] | 38 | #include "client/file_sync_client.h" |
| 39 | #include "commandline.h" |
| 40 | #include "fastdeploy.h" |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 41 | #include "incremental.h" |
| Dario Freni | fce6717 | 2022-07-19 13:47:41 +0000 | [diff] [blame] | 42 | #include "sysdeps.h" |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 43 | |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 44 | using namespace std::literals; |
| 45 | |
| Idries Hamadi | dc27224 | 2018-08-24 11:46:45 +0100 | [diff] [blame] | 46 | static constexpr int kFastDeployMinApi = 24; |
| 47 | |
| Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 48 | namespace { |
| 49 | |
| 50 | enum InstallMode { |
| 51 | INSTALL_DEFAULT, |
| 52 | INSTALL_PUSH, |
| 53 | INSTALL_STREAM, |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 54 | INSTALL_INCREMENTAL, |
| Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 55 | }; |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 56 | |
| 57 | enum class CmdlineOption { None, Enable, Disable }; |
| Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 60 | static InstallMode best_install_mode() { |
| Andrew Scull | 4b243a5 | 2022-01-25 16:45:45 +0000 | [diff] [blame] | 61 | auto&& features = adb_get_feature_set_or_die(); |
| 62 | if (CanUseFeature(*features, kFeatureCmd)) { |
| Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 63 | return INSTALL_STREAM; |
| 64 | } |
| 65 | return INSTALL_PUSH; |
| Dario Freni | dcb4c36 | 2018-10-04 16:26:40 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static bool is_apex_supported() { |
| Andrew Scull | 4b243a5 | 2022-01-25 16:45:45 +0000 | [diff] [blame] | 69 | auto&& features = adb_get_feature_set_or_die(); |
| 70 | return CanUseFeature(*features, kFeatureApex); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 73 | static bool is_abb_exec_supported() { |
| Andrew Scull | 4b243a5 | 2022-01-25 16:45:45 +0000 | [diff] [blame] | 74 | auto&& features = adb_get_feature_set_or_die(); |
| 75 | return CanUseFeature(*features, kFeatureAbbExec); |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 78 | static int pm_command(int argc, const char** argv) { |
| 79 | std::string cmd = "pm"; |
| 80 | |
| 81 | while (argc-- > 0) { |
| 82 | cmd += " " + escape_arg(*argv++); |
| 83 | } |
| 84 | |
| 85 | return send_shell_command(cmd); |
| 86 | } |
| 87 | |
| 88 | static int uninstall_app_streamed(int argc, const char** argv) { |
| 89 | // 'adb uninstall' takes the same arguments as 'cmd package uninstall' on device |
| 90 | std::string cmd = "cmd package"; |
| 91 | while (argc-- > 0) { |
| 92 | // deny the '-k' option until the remaining data/cache can be removed with adb/UI |
| 93 | if (strcmp(*argv, "-k") == 0) { |
| 94 | printf("The -k option uninstalls the application while retaining the " |
| 95 | "data/cache.\n" |
| 96 | "At the moment, there is no way to remove the remaining data.\n" |
| 97 | "You will have to reinstall the application with the same " |
| 98 | "signature, and fully " |
| 99 | "uninstall it.\n" |
| 100 | "If you truly wish to continue, execute 'adb shell cmd package " |
| 101 | "uninstall -k'.\n"); |
| 102 | return EXIT_FAILURE; |
| 103 | } |
| 104 | cmd += " " + escape_arg(*argv++); |
| 105 | } |
| 106 | |
| 107 | return send_shell_command(cmd); |
| 108 | } |
| 109 | |
| 110 | static int uninstall_app_legacy(int argc, const char** argv) { |
| 111 | /* if the user choose the -k option, we refuse to do it until devices are |
| 112 | out with the option to uninstall the remaining data somehow (adb/ui) */ |
| 113 | for (int i = 1; i < argc; i++) { |
| 114 | if (!strcmp(argv[i], "-k")) { |
| 115 | printf("The -k option uninstalls the application while retaining the " |
| 116 | "data/cache.\n" |
| 117 | "At the moment, there is no way to remove the remaining data.\n" |
| 118 | "You will have to reinstall the application with the same " |
| 119 | "signature, and fully " |
| 120 | "uninstall it.\n" |
| 121 | "If you truly wish to continue, execute 'adb shell pm uninstall " |
| 122 | "-k'\n."); |
| 123 | return EXIT_FAILURE; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* 'adb uninstall' takes the same arguments as 'pm uninstall' on device */ |
| 128 | return pm_command(argc, argv); |
| 129 | } |
| 130 | |
| 131 | int uninstall_app(int argc, const char** argv) { |
| Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 132 | if (best_install_mode() == INSTALL_PUSH) { |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 133 | return uninstall_app_legacy(argc, argv); |
| 134 | } |
| 135 | return uninstall_app_streamed(argc, argv); |
| 136 | } |
| 137 | |
| 138 | static void read_status_line(int fd, char* buf, size_t count) { |
| 139 | count--; |
| 140 | while (count > 0) { |
| 141 | int len = adb_read(fd, buf, count); |
| 142 | if (len <= 0) { |
| 143 | break; |
| 144 | } |
| 145 | |
| 146 | buf += len; |
| 147 | count -= len; |
| 148 | } |
| 149 | *buf = '\0'; |
| 150 | } |
| 151 | |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 152 | static unique_fd send_command(const std::vector<std::string>& cmd_args, std::string* error) { |
| Fabien Sanglard | a78c78e | 2024-10-01 12:28:23 -0700 | [diff] [blame] | 153 | VLOG(ADB) << "pm command: '" << android::base::Join(cmd_args, " ") << "'"; |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 154 | if (is_abb_exec_supported()) { |
| 155 | return send_abb_exec_command(cmd_args, error); |
| 156 | } else { |
| 157 | return unique_fd(adb_connect(android::base::Join(cmd_args, " "), error)); |
| 158 | } |
| 159 | } |
| 160 | |
| Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 161 | static int install_app_streamed(int argc, const char** argv, bool use_fastdeploy) { |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 162 | printf("Performing Streamed Install\n"); |
| 163 | |
| 164 | // The last argument must be the APK file |
| 165 | const char* file = argv[argc - 1]; |
| Dario Freni | dcb4c36 | 2018-10-04 16:26:40 +0100 | [diff] [blame] | 166 | if (!android::base::EndsWithIgnoreCase(file, ".apk") && |
| 167 | !android::base::EndsWithIgnoreCase(file, ".apex")) { |
| 168 | error_exit("filename doesn't end .apk or .apex: %s", file); |
| 169 | } |
| 170 | |
| 171 | bool is_apex = false; |
| 172 | if (android::base::EndsWithIgnoreCase(file, ".apex")) { |
| 173 | is_apex = true; |
| 174 | } |
| 175 | if (is_apex && !is_apex_supported()) { |
| 176 | error_exit(".apex is not supported on the target device"); |
| 177 | } |
| 178 | |
| 179 | if (is_apex && use_fastdeploy) { |
| 180 | error_exit("--fastdeploy doesn't support .apex files"); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 183 | if (use_fastdeploy) { |
| 184 | auto metadata = extract_metadata(file); |
| 185 | if (metadata.has_value()) { |
| 186 | // pass all but 1st (command) and last (apk path) parameters through to pm for |
| 187 | // session creation |
| 188 | std::vector<const char*> pm_args{argv + 1, argv + argc - 1}; |
| 189 | auto patchFd = install_patch(pm_args.size(), pm_args.data()); |
| 190 | return stream_patch(file, std::move(metadata.value()), std::move(patchFd)); |
| Henry Daitx | 7cdf489 | 2019-01-17 16:11:20 +0000 | [diff] [blame] | 191 | } |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 192 | } |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 193 | |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 194 | struct stat sb; |
| 195 | if (stat(file, &sb) == -1) { |
| Mark Hansen | 8d185a4 | 2024-10-30 01:41:32 +0000 | [diff] [blame] | 196 | perror_exit("failed to stat %s", file); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 197 | } |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 198 | |
| 199 | unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC)); |
| 200 | if (local_fd < 0) { |
| Mark Hansen | 8d185a4 | 2024-10-30 01:41:32 +0000 | [diff] [blame] | 201 | perror_exit("failed to open %s", file); |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | #ifdef __linux__ |
| 205 | posix_fadvise(local_fd.get(), 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE); |
| 206 | #endif |
| 207 | |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 208 | const bool use_abb_exec = is_abb_exec_supported(); |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 209 | std::string error; |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 210 | std::vector<std::string> cmd_args = {use_abb_exec ? "package" : "exec:cmd package"}; |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 211 | cmd_args.reserve(argc + 3); |
| 212 | |
| 213 | // don't copy the APK name, but, copy the rest of the arguments as-is |
| 214 | while (argc-- > 1) { |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 215 | if (use_abb_exec) { |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 216 | cmd_args.push_back(*argv++); |
| 217 | } else { |
| 218 | cmd_args.push_back(escape_arg(*argv++)); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // add size parameter [required for streaming installs] |
| 223 | // do last to override any user specified value |
| 224 | cmd_args.push_back("-S"); |
| 225 | cmd_args.push_back(android::base::StringPrintf("%" PRIu64, static_cast<uint64_t>(sb.st_size))); |
| 226 | |
| 227 | if (is_apex) { |
| 228 | cmd_args.push_back("--apex"); |
| 229 | } |
| 230 | |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 231 | unique_fd remote_fd = send_command(cmd_args, &error); |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 232 | if (remote_fd < 0) { |
| Mark Hansen | 8d185a4 | 2024-10-30 01:41:32 +0000 | [diff] [blame] | 233 | error_exit("connect error for write: %s", error.c_str()); |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| Josh Gao | d7f1d0b | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 236 | if (!copy_to_file(local_fd.get(), remote_fd.get())) { |
| Mark Hansen | 8d185a4 | 2024-10-30 01:41:32 +0000 | [diff] [blame] | 237 | perror_exit("failed to install: copy_to_file: %s", file); |
| Josh Gao | d7f1d0b | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 238 | } |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 239 | |
| 240 | char buf[BUFSIZ]; |
| 241 | read_status_line(remote_fd.get(), buf, sizeof(buf)); |
| Josh Gao | d7f1d0b | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 242 | if (strncmp("Success", buf, 7) != 0) { |
| Mark Hansen | 8d185a4 | 2024-10-30 01:41:32 +0000 | [diff] [blame] | 243 | error_exit("failed to install %s: %s", file, buf); |
| Alex Buynytskyy | bdff85c | 2019-09-13 14:19:01 -0700 | [diff] [blame] | 244 | } |
| Josh Gao | d7f1d0b | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 245 | |
| 246 | fputs(buf, stdout); |
| 247 | return 0; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 250 | static int install_app_legacy(int argc, const char** argv, bool use_fastdeploy) { |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 251 | printf("Performing Push Install\n"); |
| 252 | |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 253 | // Find last APK argument. |
| 254 | // All other arguments passed through verbatim. |
| 255 | int last_apk = -1; |
| 256 | for (int i = argc - 1; i >= 0; i--) { |
| Dario Freni | dcb4c36 | 2018-10-04 16:26:40 +0100 | [diff] [blame] | 257 | if (android::base::EndsWithIgnoreCase(argv[i], ".apex")) { |
| 258 | error_exit("APEX packages are only compatible with Streamed Install"); |
| 259 | } |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 260 | if (android::base::EndsWithIgnoreCase(argv[i], ".apk")) { |
| 261 | last_apk = i; |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
| Elliott Hughes | 0119a91 | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 266 | if (last_apk == -1) error_exit("need APK file on command line"); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 267 | |
| 268 | int result = -1; |
| 269 | std::vector<const char*> apk_file = {argv[last_apk]}; |
| Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 270 | std::string apk_dest = "/data/local/tmp/" + android::base::Basename(argv[last_apk]); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 271 | argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */ |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 272 | |
| Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 273 | if (use_fastdeploy) { |
| 274 | auto metadata = extract_metadata(apk_file[0]); |
| 275 | if (metadata.has_value()) { |
| 276 | auto patchFd = apply_patch_on_device(apk_dest.c_str()); |
| 277 | int status = stream_patch(apk_file[0], std::move(metadata.value()), std::move(patchFd)); |
| 278 | |
| 279 | result = pm_command(argc, argv); |
| 280 | delete_device_file(apk_dest); |
| 281 | |
| 282 | return status; |
| 283 | } |
| Idries Hamadi | b1702db | 2018-09-06 18:42:39 +0100 | [diff] [blame] | 284 | } |
| Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 285 | |
| Peter Collingbourne | a41eb3d | 2024-03-28 20:05:07 -0700 | [diff] [blame] | 286 | if (do_sync_push(apk_file, apk_dest.c_str(), false, CompressionType::Any, false, false)) { |
| Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 287 | result = pm_command(argc, argv); |
| 288 | delete_device_file(apk_dest); |
| 289 | } |
| 290 | |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 291 | return result; |
| 292 | } |
| 293 | |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 294 | template <class TimePoint> |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 295 | static int ms_between(TimePoint start, TimePoint end) { |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 296 | return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); |
| 297 | } |
| 298 | |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 299 | static int install_app_incremental(int argc, const char** argv, bool wait, bool silent) { |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 300 | using clock = std::chrono::high_resolution_clock; |
| 301 | const auto start = clock::now(); |
| 302 | int first_apk = -1; |
| 303 | int last_apk = -1; |
| Alex Buynytskyy | 31ff0ca | 2020-05-14 13:29:05 -0700 | [diff] [blame] | 304 | incremental::Args passthrough_args = {}; |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 305 | for (int i = 0; i < argc; ++i) { |
| 306 | const auto arg = std::string_view(argv[i]); |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 307 | if (android::base::EndsWithIgnoreCase(arg, ".apk"sv)) { |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 308 | last_apk = i; |
| 309 | if (first_apk == -1) { |
| 310 | first_apk = i; |
| 311 | } |
| Alex Buynytskyy | 31ff0ca | 2020-05-14 13:29:05 -0700 | [diff] [blame] | 312 | } else if (arg.starts_with("install"sv)) { |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 313 | // incremental installation command on the device is the same for all its variations in |
| 314 | // the adb, e.g. install-multiple or install-multi-package |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 315 | } else { |
| Alex Buynytskyy | 31ff0ca | 2020-05-14 13:29:05 -0700 | [diff] [blame] | 316 | passthrough_args.push_back(arg); |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
| Josh Gao | fb139d9 | 2020-03-30 17:38:37 -0700 | [diff] [blame] | 320 | if (first_apk == -1) { |
| 321 | if (!silent) { |
| 322 | fprintf(stderr, "error: need at least one APK file on command line\n"); |
| 323 | } |
| 324 | return -1; |
| 325 | } |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 326 | |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 327 | auto files = incremental::Files{argv + first_apk, argv + last_apk + 1}; |
| 328 | if (silent) { |
| 329 | // For a silent installation we want to do the lightweight check first and bail early and |
| 330 | // quietly if it fails. |
| 331 | if (!incremental::can_install(files)) { |
| 332 | return -1; |
| 333 | } |
| 334 | } |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 335 | |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 336 | printf("Performing Incremental Install\n"); |
| Alex Buynytskyy | 31ff0ca | 2020-05-14 13:29:05 -0700 | [diff] [blame] | 337 | auto server_process = incremental::install(files, passthrough_args, silent); |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 338 | if (!server_process) { |
| 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | const auto end = clock::now(); |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 343 | printf("Install command complete in %d ms\n", ms_between(start, end)); |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 344 | |
| 345 | if (wait) { |
| 346 | (*server_process).wait(); |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 352 | static std::pair<InstallMode, std::optional<InstallMode>> calculate_install_mode( |
| 353 | InstallMode modeFromArgs, bool fastdeploy, CmdlineOption incremental_request) { |
| 354 | if (incremental_request == CmdlineOption::Enable) { |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 355 | if (fastdeploy) { |
| 356 | error_exit( |
| 357 | "--incremental and --fast-deploy options are incompatible. " |
| 358 | "Please choose one"); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if (modeFromArgs != INSTALL_DEFAULT) { |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 363 | if (incremental_request == CmdlineOption::Enable) { |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 364 | error_exit("--incremental is not compatible with other installation modes"); |
| 365 | } |
| 366 | return {modeFromArgs, std::nullopt}; |
| 367 | } |
| 368 | |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 369 | if (incremental_request != CmdlineOption::Disable && !is_abb_exec_supported()) { |
| 370 | if (incremental_request == CmdlineOption::None) { |
| 371 | incremental_request = CmdlineOption::Disable; |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 372 | } else { |
| 373 | error_exit("Device doesn't support incremental installations"); |
| 374 | } |
| 375 | } |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 376 | if (incremental_request == CmdlineOption::None) { |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 377 | // check if the host is ok with incremental by default |
| 378 | if (const char* incrementalFromEnv = getenv("ADB_INSTALL_DEFAULT_INCREMENTAL")) { |
| 379 | using namespace android::base; |
| 380 | auto val = ParseBool(incrementalFromEnv); |
| 381 | if (val == ParseBoolResult::kFalse) { |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 382 | incremental_request = CmdlineOption::Disable; |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | } |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 386 | if (incremental_request == CmdlineOption::None) { |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 387 | // still ok: let's see if the device allows using incremental by default |
| 388 | // it starts feeling like we're looking for an excuse to not to use incremental... |
| 389 | std::string error; |
| LuK1337 | d9f5d7f | 2024-07-18 21:23:37 +0200 | [diff] [blame] | 390 | std::vector<std::string> args = {"settings", "get", "global", |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 391 | "enable_adb_incremental_install_default"}; |
| 392 | auto fd = send_abb_exec_command(args, &error); |
| 393 | if (!fd.ok()) { |
| Mark Hansen | 8d185a4 | 2024-10-30 01:41:32 +0000 | [diff] [blame] | 394 | fprintf(stderr, "adb: retrieving the default device installation mode failed: %s\n", |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 395 | error.c_str()); |
| 396 | } else { |
| 397 | char buf[BUFSIZ] = {}; |
| 398 | read_status_line(fd.get(), buf, sizeof(buf)); |
| 399 | using namespace android::base; |
| 400 | auto val = ParseBool(buf); |
| 401 | if (val == ParseBoolResult::kFalse) { |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 402 | incremental_request = CmdlineOption::Disable; |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 407 | if (incremental_request == CmdlineOption::Enable) { |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 408 | // explicitly requested - no fallback |
| 409 | return {INSTALL_INCREMENTAL, std::nullopt}; |
| 410 | } |
| 411 | const auto bestMode = best_install_mode(); |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 412 | if (incremental_request == CmdlineOption::None) { |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 413 | // no opinion - use incremental, fallback to regular on a failure. |
| 414 | return {INSTALL_INCREMENTAL, bestMode}; |
| 415 | } |
| 416 | // incremental turned off - use the regular best mode without a fallback. |
| 417 | return {bestMode, std::nullopt}; |
| 418 | } |
| 419 | |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 420 | static std::vector<const char*> parse_install_mode(std::vector<const char*> argv, |
| 421 | InstallMode* install_mode, |
| 422 | CmdlineOption* incremental_request, |
| 423 | bool* incremental_wait) { |
| 424 | *install_mode = INSTALL_DEFAULT; |
| 425 | *incremental_request = CmdlineOption::None; |
| 426 | *incremental_wait = false; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 427 | |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 428 | std::vector<const char*> passthrough; |
| 429 | for (auto&& arg : argv) { |
| 430 | if (arg == "--streaming"sv) { |
| 431 | *install_mode = INSTALL_STREAM; |
| 432 | } else if (arg == "--no-streaming"sv) { |
| 433 | *install_mode = INSTALL_PUSH; |
| 434 | } else if (strlen(arg) >= "--incr"sv.size() && "--incremental"sv.starts_with(arg)) { |
| 435 | *incremental_request = CmdlineOption::Enable; |
| 436 | } else if (strlen(arg) >= "--no-incr"sv.size() && "--no-incremental"sv.starts_with(arg)) { |
| 437 | *incremental_request = CmdlineOption::Disable; |
| 438 | } else if (arg == "--wait"sv) { |
| 439 | *incremental_wait = true; |
| 440 | } else { |
| 441 | passthrough.push_back(arg); |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 442 | } |
| 443 | } |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 444 | return passthrough; |
| 445 | } |
| Alex Buynytskyy | 175ce29 | 2020-02-13 06:52:04 -0800 | [diff] [blame] | 446 | |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 447 | static std::vector<const char*> parse_fast_deploy_mode( |
| 448 | std::vector<const char*> argv, bool* use_fastdeploy, |
| 449 | FastDeploy_AgentUpdateStrategy* agent_update_strategy) { |
| 450 | *use_fastdeploy = false; |
| 451 | *agent_update_strategy = FastDeploy_AgentUpdateDifferentVersion; |
| 452 | |
| 453 | std::vector<const char*> passthrough; |
| 454 | for (auto&& arg : argv) { |
| 455 | if (arg == "--fastdeploy"sv) { |
| 456 | *use_fastdeploy = true; |
| 457 | } else if (arg == "--no-fastdeploy"sv) { |
| 458 | *use_fastdeploy = false; |
| 459 | } else if (arg == "--force-agent"sv) { |
| 460 | *agent_update_strategy = FastDeploy_AgentUpdateAlways; |
| 461 | } else if (arg == "--date-check-agent"sv) { |
| 462 | *agent_update_strategy = FastDeploy_AgentUpdateNewerTimeStamp; |
| 463 | } else if (arg == "--version-check-agent"sv) { |
| 464 | *agent_update_strategy = FastDeploy_AgentUpdateDifferentVersion; |
| 465 | } else { |
| 466 | passthrough.push_back(arg); |
| 467 | } |
| 468 | } |
| 469 | return passthrough; |
| 470 | } |
| 471 | |
| 472 | int install_app(int argc, const char** argv) { |
| 473 | InstallMode install_mode = INSTALL_DEFAULT; |
| 474 | auto incremental_request = CmdlineOption::None; |
| 475 | bool incremental_wait = false; |
| 476 | |
| 477 | bool use_fastdeploy = false; |
| 478 | FastDeploy_AgentUpdateStrategy agent_update_strategy = FastDeploy_AgentUpdateDifferentVersion; |
| 479 | |
| 480 | auto unused_argv = parse_install_mode({argv, argv + argc}, &install_mode, &incremental_request, |
| 481 | &incremental_wait); |
| 482 | auto passthrough_argv = |
| 483 | parse_fast_deploy_mode(std::move(unused_argv), &use_fastdeploy, &agent_update_strategy); |
| 484 | |
| 485 | auto [primary_mode, fallback_mode] = |
| 486 | calculate_install_mode(install_mode, use_fastdeploy, incremental_request); |
| 487 | if ((primary_mode == INSTALL_STREAM || |
| 488 | fallback_mode.value_or(INSTALL_PUSH) == INSTALL_STREAM) && |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 489 | best_install_mode() == INSTALL_PUSH) { |
| Elliott Hughes | 0119a91 | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 490 | error_exit("Attempting to use streaming install on unsupported device"); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 493 | if (use_fastdeploy && get_device_api_level() < kFastDeployMinApi) { |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 494 | fprintf(stderr, |
| 495 | "Fast Deploy is only compatible with devices of API version %d or higher, " |
| 496 | "ignoring.\n", |
| 497 | kFastDeployMinApi); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 498 | use_fastdeploy = false; |
| 499 | } |
| Alex Buynytskyy | 1af550e | 2019-09-16 12:10:54 -0700 | [diff] [blame] | 500 | fastdeploy_set_agent_update_strategy(agent_update_strategy); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 501 | |
| Henry Daitx | e4cc4d9 | 2018-12-12 10:40:57 +0000 | [diff] [blame] | 502 | if (passthrough_argv.size() < 2) { |
| 503 | error_exit("install requires an apk argument"); |
| 504 | } |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 505 | |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 506 | auto run_install_mode = [&](InstallMode install_mode, bool silent) { |
| 507 | switch (install_mode) { |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 508 | case INSTALL_PUSH: |
| 509 | return install_app_legacy(passthrough_argv.size(), passthrough_argv.data(), |
| 510 | use_fastdeploy); |
| 511 | case INSTALL_STREAM: |
| 512 | return install_app_streamed(passthrough_argv.size(), passthrough_argv.data(), |
| 513 | use_fastdeploy); |
| 514 | case INSTALL_INCREMENTAL: |
| 515 | return install_app_incremental(passthrough_argv.size(), passthrough_argv.data(), |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 516 | incremental_wait, silent); |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 517 | case INSTALL_DEFAULT: |
| 518 | default: |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 519 | error_exit("invalid install mode"); |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 520 | } |
| 521 | }; |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 522 | auto res = run_install_mode(primary_mode, fallback_mode.has_value()); |
| 523 | if (res && fallback_mode.value_or(primary_mode) != primary_mode) { |
| 524 | res = run_install_mode(*fallback_mode, false); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 525 | } |
| Yurii Zubrytskyi | 39ee3d8 | 2020-03-26 18:20:39 -0700 | [diff] [blame] | 526 | return res; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 529 | static int install_multiple_app_streamed(int argc, const char** argv) { |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 530 | // Find all APK arguments starting at end. |
| 531 | // All other arguments passed through verbatim. |
| 532 | int first_apk = -1; |
| 533 | uint64_t total_size = 0; |
| 534 | for (int i = argc - 1; i >= 0; i--) { |
| 535 | const char* file = argv[i]; |
| Dario Freni | dcb4c36 | 2018-10-04 16:26:40 +0100 | [diff] [blame] | 536 | if (android::base::EndsWithIgnoreCase(argv[i], ".apex")) { |
| 537 | error_exit("APEX packages are not compatible with install-multiple"); |
| 538 | } |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 539 | |
| Victor Hsieh | 88f14b8 | 2018-10-04 10:46:56 -0700 | [diff] [blame] | 540 | if (android::base::EndsWithIgnoreCase(file, ".apk") || |
| Jiakai Zhang | 7024f55 | 2025-02-21 17:22:27 +0000 | [diff] [blame] | 541 | android::base::EndsWithIgnoreCase( |
| 542 | file, ".dm") || // dex metadata, for cloud profile and cloud verification |
| 543 | android::base::EndsWithIgnoreCase( |
| 544 | file, ".sdm") || // secure dex metadata, for cloud compilation |
| Yurii Zubrytskyi | aac2422 | 2024-11-05 11:05:09 -0800 | [diff] [blame] | 545 | android::base::EndsWithIgnoreCase(file, ".fsv_sig") || |
| Jiakai Zhang | 7024f55 | 2025-02-21 17:22:27 +0000 | [diff] [blame] | 546 | android::base::EndsWithIgnoreCase(file, ".idsig")) { // v4 external signature |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 547 | struct stat sb; |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 548 | if (stat(file, &sb) == -1) perror_exit("failed to stat \"%s\"", file); |
| 549 | total_size += sb.st_size; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 550 | first_apk = i; |
| 551 | } else { |
| 552 | break; |
| 553 | } |
| 554 | } |
| 555 | |
| Elliott Hughes | 0119a91 | 2018-10-22 17:02:51 -0700 | [diff] [blame] | 556 | if (first_apk == -1) error_exit("need APK file on command line"); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 557 | |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 558 | const bool use_abb_exec = is_abb_exec_supported(); |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 559 | const std::string install_cmd = |
| 560 | use_abb_exec ? "package" |
| 561 | : best_install_mode() == INSTALL_PUSH ? "exec:pm" : "exec:cmd package"; |
| 562 | |
| 563 | std::vector<std::string> cmd_args = {install_cmd, "install-create", "-S", |
| 564 | std::to_string(total_size)}; |
| 565 | cmd_args.reserve(first_apk + 4); |
| Peter Wen | ff2446d | 2022-02-09 16:15:45 -0500 | [diff] [blame] | 566 | for (int i = 0; i < first_apk; i++) { |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 567 | if (use_abb_exec) { |
| 568 | cmd_args.push_back(argv[i]); |
| 569 | } else { |
| 570 | cmd_args.push_back(escape_arg(argv[i])); |
| 571 | } |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | // Create install session |
| 575 | std::string error; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 576 | char buf[BUFSIZ]; |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 577 | { |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 578 | unique_fd fd = send_command(cmd_args, &error); |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 579 | if (fd < 0) { |
| Mark Hansen | 8d185a4 | 2024-10-30 01:41:32 +0000 | [diff] [blame] | 580 | perror_exit("connect error for create: %s", error.c_str()); |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 581 | } |
| 582 | read_status_line(fd.get(), buf, sizeof(buf)); |
| 583 | } |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 584 | |
| 585 | int session_id = -1; |
| 586 | if (!strncmp("Success", buf, 7)) { |
| 587 | char* start = strrchr(buf, '['); |
| 588 | char* end = strrchr(buf, ']'); |
| 589 | if (start && end) { |
| 590 | *end = '\0'; |
| 591 | session_id = strtol(start + 1, nullptr, 10); |
| 592 | } |
| 593 | } |
| 594 | if (session_id < 0) { |
| 595 | fprintf(stderr, "adb: failed to create session\n"); |
| 596 | fputs(buf, stderr); |
| 597 | return EXIT_FAILURE; |
| 598 | } |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 599 | const auto session_id_str = std::to_string(session_id); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 600 | |
| 601 | // Valid session, now stream the APKs |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 602 | bool success = true; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 603 | for (int i = first_apk; i < argc; i++) { |
| 604 | const char* file = argv[i]; |
| 605 | struct stat sb; |
| 606 | if (stat(file, &sb) == -1) { |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 607 | fprintf(stderr, "adb: failed to stat \"%s\": %s\n", file, strerror(errno)); |
| 608 | success = false; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 609 | goto finalize_session; |
| 610 | } |
| 611 | |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 612 | std::vector<std::string> cmd_args = { |
| 613 | install_cmd, |
| 614 | "install-write", |
| 615 | "-S", |
| 616 | std::to_string(sb.st_size), |
| 617 | session_id_str, |
| 618 | android::base::Basename(file), |
| 619 | "-", |
| 620 | }; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 621 | |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 622 | unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC)); |
| 623 | if (local_fd < 0) { |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 624 | fprintf(stderr, "adb: failed to open \"%s\": %s\n", file, strerror(errno)); |
| 625 | success = false; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 626 | goto finalize_session; |
| 627 | } |
| 628 | |
| 629 | std::string error; |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 630 | unique_fd remote_fd = send_command(cmd_args, &error); |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 631 | if (remote_fd < 0) { |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 632 | fprintf(stderr, "adb: connect error for write: %s\n", error.c_str()); |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 633 | success = false; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 634 | goto finalize_session; |
| 635 | } |
| 636 | |
| Josh Gao | d7f1d0b | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 637 | if (!copy_to_file(local_fd.get(), remote_fd.get())) { |
| 638 | fprintf(stderr, "adb: failed to write \"%s\": %s\n", file, strerror(errno)); |
| 639 | success = false; |
| 640 | goto finalize_session; |
| 641 | } |
| 642 | |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 643 | read_status_line(remote_fd.get(), buf, sizeof(buf)); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 644 | |
| 645 | if (strncmp("Success", buf, 7)) { |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 646 | fprintf(stderr, "adb: failed to write \"%s\"\n", file); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 647 | fputs(buf, stderr); |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 648 | success = false; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 649 | goto finalize_session; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | finalize_session: |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 654 | // Commit session if we streamed everything okay; otherwise abandon. |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 655 | std::vector<std::string> service_args = { |
| 656 | install_cmd, |
| 657 | success ? "install-commit" : "install-abandon", |
| 658 | session_id_str, |
| 659 | }; |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 660 | { |
| Alex Buynytskyy | 8594fcd | 2020-04-23 14:45:52 -0700 | [diff] [blame] | 661 | unique_fd fd = send_command(service_args, &error); |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 662 | if (fd < 0) { |
| 663 | fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str()); |
| 664 | return EXIT_FAILURE; |
| 665 | } |
| 666 | read_status_line(fd.get(), buf, sizeof(buf)); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 667 | } |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 668 | if (!success) return EXIT_FAILURE; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 669 | |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 670 | if (strncmp("Success", buf, 7)) { |
| 671 | fprintf(stderr, "adb: failed to finalize session\n"); |
| 672 | fputs(buf, stderr); |
| 673 | return EXIT_FAILURE; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 674 | } |
| Elliott Hughes | b6ebbe2 | 2019-08-05 17:06:04 -0700 | [diff] [blame] | 675 | |
| 676 | fputs(buf, stdout); |
| 677 | return EXIT_SUCCESS; |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| Alex Buynytskyy | 7a4224f | 2020-05-18 11:27:44 -0700 | [diff] [blame] | 680 | int install_multiple_app(int argc, const char** argv) { |
| 681 | InstallMode install_mode = INSTALL_DEFAULT; |
| 682 | auto incremental_request = CmdlineOption::None; |
| 683 | bool incremental_wait = false; |
| 684 | bool use_fastdeploy = false; |
| 685 | |
| 686 | auto passthrough_argv = parse_install_mode({argv + 1, argv + argc}, &install_mode, |
| 687 | &incremental_request, &incremental_wait); |
| 688 | |
| 689 | auto [primary_mode, fallback_mode] = |
| 690 | calculate_install_mode(install_mode, use_fastdeploy, incremental_request); |
| 691 | if ((primary_mode == INSTALL_STREAM || |
| 692 | fallback_mode.value_or(INSTALL_PUSH) == INSTALL_STREAM) && |
| 693 | best_install_mode() == INSTALL_PUSH) { |
| 694 | error_exit("Attempting to use streaming install on unsupported device"); |
| 695 | } |
| 696 | |
| 697 | auto run_install_mode = [&](InstallMode install_mode, bool silent) { |
| 698 | switch (install_mode) { |
| 699 | case INSTALL_PUSH: |
| 700 | case INSTALL_STREAM: |
| 701 | return install_multiple_app_streamed(passthrough_argv.size(), |
| 702 | passthrough_argv.data()); |
| 703 | case INSTALL_INCREMENTAL: |
| 704 | return install_app_incremental(passthrough_argv.size(), passthrough_argv.data(), |
| 705 | incremental_wait, silent); |
| 706 | case INSTALL_DEFAULT: |
| 707 | default: |
| 708 | error_exit("invalid install mode"); |
| 709 | } |
| 710 | }; |
| 711 | auto res = run_install_mode(primary_mode, fallback_mode.has_value()); |
| 712 | if (res && fallback_mode.value_or(primary_mode) != primary_mode) { |
| 713 | res = run_install_mode(*fallback_mode, false); |
| 714 | } |
| 715 | return res; |
| 716 | } |
| 717 | |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 718 | int install_multi_package(int argc, const char** argv) { |
| 719 | // Find all APK arguments starting at end. |
| 720 | // All other arguments passed through verbatim. |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 721 | bool apex_found = false; |
| 722 | int first_package = -1; |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 723 | for (int i = argc - 1; i >= 0; i--) { |
| 724 | const char* file = argv[i]; |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 725 | if (android::base::EndsWithIgnoreCase(file, ".apk") || |
| 726 | android::base::EndsWithIgnoreCase(file, ".apex")) { |
| 727 | first_package = i; |
| 728 | if (android::base::EndsWithIgnoreCase(file, ".apex")) { |
| 729 | apex_found = true; |
| 730 | } |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 731 | } else { |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 736 | if (first_package == -1) error_exit("need APK or APEX files on command line"); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 737 | |
| Yurii Zubrytskyi | e59c1bd | 2019-06-27 13:47:34 -0700 | [diff] [blame] | 738 | if (best_install_mode() == INSTALL_PUSH) { |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 739 | fprintf(stderr, "adb: multi-package install is not supported on this device\n"); |
| 740 | return EXIT_FAILURE; |
| 741 | } |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 742 | |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 743 | const bool use_abb_exec = is_abb_exec_supported(); |
| 744 | const std::string install_cmd = use_abb_exec ? "package" : "exec:cmd package"; |
| 745 | |
| 746 | std::vector<std::string> multi_package_cmd_args = {install_cmd, "install-create", |
| 747 | "--multi-package"}; |
| 748 | |
| 749 | multi_package_cmd_args.reserve(first_package + 4); |
| Dario Freni | 2042fd2 | 2019-01-29 14:17:05 +0000 | [diff] [blame] | 750 | for (int i = 1; i < first_package; i++) { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 751 | if (use_abb_exec) { |
| 752 | multi_package_cmd_args.push_back(argv[i]); |
| 753 | } else { |
| 754 | multi_package_cmd_args.push_back(escape_arg(argv[i])); |
| 755 | } |
| Dario Freni | 2042fd2 | 2019-01-29 14:17:05 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 758 | if (apex_found) { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 759 | multi_package_cmd_args.emplace_back("--staged"); |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 760 | } |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 761 | |
| 762 | // Create multi-package install session |
| 763 | std::string error; |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 764 | char buf[BUFSIZ]; |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 765 | { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 766 | unique_fd fd = send_command(multi_package_cmd_args, &error); |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 767 | if (fd < 0) { |
| 768 | fprintf(stderr, "adb: connect error for create multi-package: %s\n", error.c_str()); |
| 769 | return EXIT_FAILURE; |
| 770 | } |
| 771 | read_status_line(fd.get(), buf, sizeof(buf)); |
| 772 | } |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 773 | |
| 774 | int parent_session_id = -1; |
| 775 | if (!strncmp("Success", buf, 7)) { |
| 776 | char* start = strrchr(buf, '['); |
| 777 | char* end = strrchr(buf, ']'); |
| 778 | if (start && end) { |
| 779 | *end = '\0'; |
| 780 | parent_session_id = strtol(start + 1, nullptr, 10); |
| 781 | } |
| 782 | } |
| 783 | if (parent_session_id < 0) { |
| 784 | fprintf(stderr, "adb: failed to create multi-package session\n"); |
| 785 | fputs(buf, stderr); |
| 786 | return EXIT_FAILURE; |
| 787 | } |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 788 | const auto parent_session_id_str = std::to_string(parent_session_id); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 789 | |
| 790 | fprintf(stdout, "Created parent session ID %d.\n", parent_session_id); |
| 791 | |
| 792 | std::vector<int> session_ids; |
| 793 | |
| 794 | // Valid session, now create the individual sessions and stream the APKs |
| 795 | int success = EXIT_FAILURE; |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 796 | std::vector<std::string> individual_cmd_args = {install_cmd, "install-create"}; |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 797 | for (int i = 1; i < first_package; i++) { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 798 | if (use_abb_exec) { |
| 799 | individual_cmd_args.push_back(argv[i]); |
| 800 | } else { |
| 801 | individual_cmd_args.push_back(escape_arg(argv[i])); |
| 802 | } |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 803 | } |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 804 | if (apex_found) { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 805 | individual_cmd_args.emplace_back("--staged"); |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 806 | } |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 807 | |
| 808 | std::vector<std::string> individual_apex_cmd_args; |
| 809 | if (apex_found) { |
| 810 | individual_apex_cmd_args = individual_cmd_args; |
| 811 | individual_apex_cmd_args.emplace_back("--apex"); |
| 812 | } |
| 813 | |
| 814 | std::vector<std::string> add_session_cmd_args = { |
| 815 | install_cmd, |
| 816 | "install-add-session", |
| 817 | parent_session_id_str, |
| 818 | }; |
| 819 | |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 820 | for (int i = first_package; i < argc; i++) { |
| 821 | const char* file = argv[i]; |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 822 | char buf[BUFSIZ]; |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 823 | { |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 824 | unique_fd fd; |
| 825 | // Create individual install session |
| 826 | if (android::base::EndsWithIgnoreCase(file, ".apex")) { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 827 | fd = send_command(individual_apex_cmd_args, &error); |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 828 | } else { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 829 | fd = send_command(individual_cmd_args, &error); |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 830 | } |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 831 | if (fd < 0) { |
| 832 | fprintf(stderr, "adb: connect error for create: %s\n", error.c_str()); |
| 833 | goto finalize_multi_package_session; |
| 834 | } |
| 835 | read_status_line(fd.get(), buf, sizeof(buf)); |
| 836 | } |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 837 | |
| 838 | int session_id = -1; |
| 839 | if (!strncmp("Success", buf, 7)) { |
| 840 | char* start = strrchr(buf, '['); |
| 841 | char* end = strrchr(buf, ']'); |
| 842 | if (start && end) { |
| 843 | *end = '\0'; |
| 844 | session_id = strtol(start + 1, nullptr, 10); |
| 845 | } |
| 846 | } |
| 847 | if (session_id < 0) { |
| 848 | fprintf(stderr, "adb: failed to create multi-package session\n"); |
| 849 | fputs(buf, stderr); |
| 850 | goto finalize_multi_package_session; |
| 851 | } |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 852 | const auto session_id_str = std::to_string(session_id); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 853 | |
| 854 | fprintf(stdout, "Created child session ID %d.\n", session_id); |
| 855 | session_ids.push_back(session_id); |
| 856 | |
| Dario Freni | 7d5a5e1 | 2019-02-25 12:00:32 +0000 | [diff] [blame] | 857 | // Support splitAPKs by allowing the notation split1.apk:split2.apk:split3.apk as argument. |
| Dario Freni | fce6717 | 2022-07-19 13:47:41 +0000 | [diff] [blame] | 858 | // The character used as separator is OS-dependent, see ENV_PATH_SEPARATOR_STR. |
| 859 | std::vector<std::string> splits = android::base::Split(file, ENV_PATH_SEPARATOR_STR); |
| Dario Freni | 7d5a5e1 | 2019-02-25 12:00:32 +0000 | [diff] [blame] | 860 | |
| 861 | for (const std::string& split : splits) { |
| 862 | struct stat sb; |
| 863 | if (stat(split.c_str(), &sb) == -1) { |
| 864 | fprintf(stderr, "adb: failed to stat %s: %s\n", split.c_str(), strerror(errno)); |
| 865 | goto finalize_multi_package_session; |
| 866 | } |
| 867 | |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 868 | std::vector<std::string> cmd_args = { |
| 869 | install_cmd, |
| 870 | "install-write", |
| 871 | "-S", |
| 872 | std::to_string(sb.st_size), |
| 873 | session_id_str, |
| Alex Buynytskyy | 83e11f5 | 2020-07-14 17:55:22 -0700 | [diff] [blame] | 874 | android::base::StringPrintf("%d_%s", i, android::base::Basename(split).c_str()), |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 875 | "-", |
| 876 | }; |
| Dario Freni | 7d5a5e1 | 2019-02-25 12:00:32 +0000 | [diff] [blame] | 877 | |
| 878 | unique_fd local_fd(adb_open(split.c_str(), O_RDONLY | O_CLOEXEC)); |
| 879 | if (local_fd < 0) { |
| 880 | fprintf(stderr, "adb: failed to open %s: %s\n", split.c_str(), strerror(errno)); |
| 881 | goto finalize_multi_package_session; |
| 882 | } |
| 883 | |
| 884 | std::string error; |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 885 | unique_fd remote_fd = send_command(cmd_args, &error); |
| Dario Freni | 7d5a5e1 | 2019-02-25 12:00:32 +0000 | [diff] [blame] | 886 | if (remote_fd < 0) { |
| 887 | fprintf(stderr, "adb: connect error for write: %s\n", error.c_str()); |
| 888 | goto finalize_multi_package_session; |
| 889 | } |
| 890 | |
| Josh Gao | d7f1d0b | 2019-12-03 16:05:54 -0800 | [diff] [blame] | 891 | if (!copy_to_file(local_fd.get(), remote_fd.get())) { |
| 892 | fprintf(stderr, "adb: failed to write %s: %s\n", split.c_str(), strerror(errno)); |
| 893 | goto finalize_multi_package_session; |
| 894 | } |
| 895 | |
| Dario Freni | 7d5a5e1 | 2019-02-25 12:00:32 +0000 | [diff] [blame] | 896 | read_status_line(remote_fd.get(), buf, sizeof(buf)); |
| 897 | |
| 898 | if (strncmp("Success", buf, 7)) { |
| 899 | fprintf(stderr, "adb: failed to write %s\n", split.c_str()); |
| 900 | fputs(buf, stderr); |
| 901 | goto finalize_multi_package_session; |
| 902 | } |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 903 | } |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 904 | add_session_cmd_args.push_back(std::to_string(session_id)); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 907 | { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 908 | unique_fd fd = send_command(add_session_cmd_args, &error); |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 909 | if (fd < 0) { |
| Dario Freni | 907ef68 | 2019-01-14 17:06:32 +0000 | [diff] [blame] | 910 | fprintf(stderr, "adb: connect error for install-add-session: %s\n", error.c_str()); |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 911 | goto finalize_multi_package_session; |
| 912 | } |
| 913 | read_status_line(fd.get(), buf, sizeof(buf)); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 914 | } |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 915 | |
| 916 | if (strncmp("Success", buf, 7)) { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 917 | fprintf(stderr, "adb: failed to link sessions (%s)\n", |
| 918 | android::base::Join(add_session_cmd_args, " ").c_str()); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 919 | fputs(buf, stderr); |
| 920 | goto finalize_multi_package_session; |
| 921 | } |
| 922 | |
| 923 | // no failures means we can proceed with the assumption of success |
| 924 | success = 0; |
| 925 | |
| 926 | finalize_multi_package_session: |
| 927 | // Commit session if we streamed everything okay; otherwise abandon |
| Samiul Islam | 7a58245 | 2021-06-03 11:18:11 +0100 | [diff] [blame] | 928 | std::vector<std::string> service_args; |
| Samiul Islam | 283c5db | 2021-06-10 13:37:35 +0100 | [diff] [blame] | 929 | if (success == 0) { |
| Samiul Islam | 7a58245 | 2021-06-03 11:18:11 +0100 | [diff] [blame] | 930 | service_args.push_back(install_cmd); |
| 931 | service_args.push_back("install-commit"); |
| 932 | // If successful, we need to forward args to install-commit |
| 933 | for (int i = 1; i < first_package - 1; i++) { |
| 934 | if (strcmp(argv[i], "--staged-ready-timeout") == 0) { |
| 935 | service_args.push_back(argv[i]); |
| 936 | service_args.push_back(argv[i + 1]); |
| 937 | i++; |
| 938 | } |
| 939 | } |
| 940 | service_args.push_back(parent_session_id_str); |
| 941 | } else { |
| 942 | service_args = {install_cmd, "install-abandon", parent_session_id_str}; |
| 943 | } |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 944 | |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 945 | { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 946 | unique_fd fd = send_command(service_args, &error); |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 947 | if (fd < 0) { |
| 948 | fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str()); |
| 949 | return EXIT_FAILURE; |
| 950 | } |
| 951 | read_status_line(fd.get(), buf, sizeof(buf)); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 952 | } |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 953 | |
| 954 | if (!strncmp("Success", buf, 7)) { |
| 955 | fputs(buf, stdout); |
| 956 | if (success == 0) { |
| 957 | return 0; |
| 958 | } |
| 959 | } else { |
| 960 | fprintf(stderr, "adb: failed to finalize session\n"); |
| 961 | fputs(buf, stderr); |
| 962 | } |
| 963 | |
| Dario Freni | 7d5a5e1 | 2019-02-25 12:00:32 +0000 | [diff] [blame] | 964 | session_ids.push_back(parent_session_id); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 965 | // try to abandon all remaining sessions |
| 966 | for (std::size_t i = 0; i < session_ids.size(); i++) { |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 967 | std::vector<std::string> service_args = { |
| 968 | install_cmd, |
| 969 | "install-abandon", |
| 970 | std::to_string(session_ids[i]), |
| 971 | }; |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 972 | fprintf(stderr, "Attempting to abandon session ID %d\n", session_ids[i]); |
| Alex Buynytskyy | 77138ae | 2020-05-18 16:48:14 -0700 | [diff] [blame] | 973 | unique_fd fd = send_command(service_args, &error); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 974 | if (fd < 0) { |
| 975 | fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str()); |
| 976 | continue; |
| 977 | } |
| Josh Gao | b915597 | 2019-01-11 13:13:20 -0800 | [diff] [blame] | 978 | read_status_line(fd.get(), buf, sizeof(buf)); |
| Patrick Baumann | 810ee9b | 2018-10-09 10:45:03 -0700 | [diff] [blame] | 979 | } |
| 980 | return EXIT_FAILURE; |
| 981 | } |
| 982 | |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 983 | int delete_device_file(const std::string& filename) { |
| Elliott Hughes | 203f537 | 2019-08-02 16:06:06 -0700 | [diff] [blame] | 984 | // http://b/17339227 "Sideloading a Readonly File Results in a Prompt to |
| 985 | // Delete" caused us to add `-f` here, to avoid the equivalent of the `-i` |
| 986 | // prompt that you get from BSD rm (used in Android 5) if you have a |
| 987 | // non-writable file and stdin is a tty (which is true for old versions of |
| 988 | // adbd). |
| 989 | // |
| 990 | // Unfortunately, `rm -f` requires Android 4.3, so that workaround broke |
| 991 | // earlier Android releases. This was reported as http://b/37704384 "adb |
| 992 | // install -r passes invalid argument to rm on Android 4.1" and |
| 993 | // http://b/37035817 "ADB Fails: rm failed for -f, No such file or |
| 994 | // directory". |
| 995 | // |
| 996 | // Testing on a variety of devices and emulators shows that redirecting |
| 997 | // stdin is sufficient to avoid the pseudo-`-i`, and works on toolbox, |
| 998 | // BSD, and toybox versions of rm. |
| 999 | return send_shell_command("rm " + escape_arg(filename) + " </dev/null"); |
| Idries Hamadi | 1ecee44 | 2018-01-29 16:30:36 +0000 | [diff] [blame] | 1000 | } |