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