blob: 566bdb531fcf55298c00a8d029a0e38f8befb1bc [file] [log] [blame]
Idries Hamadi1ecee442018-01-29 16:30:36 +00001/*
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 Hamadi78330f02018-09-13 18:00:25 +010017#include "adb_install.h"
Idries Hamadi1ecee442018-01-29 16:30:36 +000018
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -070019#include <fcntl.h>
Josh Gaoa4dfc142020-02-19 13:50:57 -080020#include <inttypes.h>
Idries Hamadi1ecee442018-01-29 16:30:36 +000021#include <stdio.h>
22#include <stdlib.h>
Idries Hamadi78330f02018-09-13 18:00:25 +010023#include <unistd.h>
Idries Hamadi1ecee442018-01-29 16:30:36 +000024#include <algorithm>
Idries Hamadi1ecee442018-01-29 16:30:36 +000025#include <string>
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070026#include <string_view>
Idries Hamadi1ecee442018-01-29 16:30:36 +000027#include <vector>
28
Josh Gaob9155972019-01-11 13:13:20 -080029#include <android-base/file.h>
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070030#include <android-base/parsebool.h>
Josh Gaob9155972019-01-11 13:13:20 -080031#include <android-base/stringprintf.h>
32#include <android-base/strings.h>
33
Idries Hamadi78330f02018-09-13 18:00:25 +010034#include "adb.h"
35#include "adb_client.h"
Josh Gaob9155972019-01-11 13:13:20 -080036#include "adb_unique_fd.h"
Idries Hamadi78330f02018-09-13 18:00:25 +010037#include "adb_utils.h"
Idries Hamadi78330f02018-09-13 18:00:25 +010038#include "client/file_sync_client.h"
39#include "commandline.h"
40#include "fastdeploy.h"
Alex Buynytskyy175ce292020-02-13 06:52:04 -080041#include "incremental.h"
Dario Frenifce67172022-07-19 13:47:41 +000042#include "sysdeps.h"
Idries Hamadi1ecee442018-01-29 16:30:36 +000043
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070044using namespace std::literals;
45
Idries Hamadidc272242018-08-24 11:46:45 +010046static constexpr int kFastDeployMinApi = 24;
47
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -070048namespace {
49
50enum InstallMode {
51 INSTALL_DEFAULT,
52 INSTALL_PUSH,
53 INSTALL_STREAM,
Alex Buynytskyy175ce292020-02-13 06:52:04 -080054 INSTALL_INCREMENTAL,
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -070055};
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -070056
57enum class CmdlineOption { None, Enable, Disable };
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -070058}
59
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -070060static InstallMode best_install_mode() {
Andrew Scull4b243a52022-01-25 16:45:45 +000061 auto&& features = adb_get_feature_set_or_die();
62 if (CanUseFeature(*features, kFeatureCmd)) {
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -070063 return INSTALL_STREAM;
64 }
65 return INSTALL_PUSH;
Dario Frenidcb4c362018-10-04 16:26:40 +010066}
67
68static bool is_apex_supported() {
Andrew Scull4b243a52022-01-25 16:45:45 +000069 auto&& features = adb_get_feature_set_or_die();
70 return CanUseFeature(*features, kFeatureApex);
Idries Hamadi1ecee442018-01-29 16:30:36 +000071}
72
Alex Buynytskyy175ce292020-02-13 06:52:04 -080073static bool is_abb_exec_supported() {
Andrew Scull4b243a52022-01-25 16:45:45 +000074 auto&& features = adb_get_feature_set_or_die();
75 return CanUseFeature(*features, kFeatureAbbExec);
Alex Buynytskyy175ce292020-02-13 06:52:04 -080076}
77
Idries Hamadi1ecee442018-01-29 16:30:36 +000078static 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
88static 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
110static 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
131int uninstall_app(int argc, const char** argv) {
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -0700132 if (best_install_mode() == INSTALL_PUSH) {
Idries Hamadi1ecee442018-01-29 16:30:36 +0000133 return uninstall_app_legacy(argc, argv);
134 }
135 return uninstall_app_streamed(argc, argv);
136}
137
138static 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 Buynytskyy8594fcd2020-04-23 14:45:52 -0700152static unique_fd send_command(const std::vector<std::string>& cmd_args, std::string* error) {
Fabien Sanglarda78c78e2024-10-01 12:28:23 -0700153 VLOG(ADB) << "pm command: '" << android::base::Join(cmd_args, " ") << "'";
Alex Buynytskyy8594fcd2020-04-23 14:45:52 -0700154 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 Buynytskyy1af550e2019-09-16 12:10:54 -0700161static int install_app_streamed(int argc, const char** argv, bool use_fastdeploy) {
Idries Hamadi1ecee442018-01-29 16:30:36 +0000162 printf("Performing Streamed Install\n");
163
164 // The last argument must be the APK file
165 const char* file = argv[argc - 1];
Dario Frenidcb4c362018-10-04 16:26:40 +0100166 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 Hamadi1ecee442018-01-29 16:30:36 +0000181 }
182
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700183 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 Daitx7cdf4892019-01-17 16:11:20 +0000191 }
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700192 }
Idries Hamadi1ecee442018-01-29 16:30:36 +0000193
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700194 struct stat sb;
195 if (stat(file, &sb) == -1) {
Mark Hansen8d185a42024-10-30 01:41:32 +0000196 perror_exit("failed to stat %s", file);
Idries Hamadi1ecee442018-01-29 16:30:36 +0000197 }
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700198
199 unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC));
200 if (local_fd < 0) {
Mark Hansen8d185a42024-10-30 01:41:32 +0000201 perror_exit("failed to open %s", file);
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700202 }
203
204#ifdef __linux__
205 posix_fadvise(local_fd.get(), 0, 0, POSIX_FADV_SEQUENTIAL | POSIX_FADV_NOREUSE);
206#endif
207
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800208 const bool use_abb_exec = is_abb_exec_supported();
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700209 std::string error;
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800210 std::vector<std::string> cmd_args = {use_abb_exec ? "package" : "exec:cmd package"};
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700211 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 Buynytskyy175ce292020-02-13 06:52:04 -0800215 if (use_abb_exec) {
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700216 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 Buynytskyy8594fcd2020-04-23 14:45:52 -0700231 unique_fd remote_fd = send_command(cmd_args, &error);
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700232 if (remote_fd < 0) {
Mark Hansen8d185a42024-10-30 01:41:32 +0000233 error_exit("connect error for write: %s", error.c_str());
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700234 }
235
Josh Gaod7f1d0b2019-12-03 16:05:54 -0800236 if (!copy_to_file(local_fd.get(), remote_fd.get())) {
Mark Hansen8d185a42024-10-30 01:41:32 +0000237 perror_exit("failed to install: copy_to_file: %s", file);
Josh Gaod7f1d0b2019-12-03 16:05:54 -0800238 }
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700239
240 char buf[BUFSIZ];
241 read_status_line(remote_fd.get(), buf, sizeof(buf));
Josh Gaod7f1d0b2019-12-03 16:05:54 -0800242 if (strncmp("Success", buf, 7) != 0) {
Mark Hansen8d185a42024-10-30 01:41:32 +0000243 error_exit("failed to install %s: %s", file, buf);
Alex Buynytskyybdff85c2019-09-13 14:19:01 -0700244 }
Josh Gaod7f1d0b2019-12-03 16:05:54 -0800245
246 fputs(buf, stdout);
247 return 0;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000248}
249
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700250static int install_app_legacy(int argc, const char** argv, bool use_fastdeploy) {
Idries Hamadi1ecee442018-01-29 16:30:36 +0000251 printf("Performing Push Install\n");
252
Idries Hamadi1ecee442018-01-29 16:30:36 +0000253 // 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 Frenidcb4c362018-10-04 16:26:40 +0100257 if (android::base::EndsWithIgnoreCase(argv[i], ".apex")) {
258 error_exit("APEX packages are only compatible with Streamed Install");
259 }
Idries Hamadi1ecee442018-01-29 16:30:36 +0000260 if (android::base::EndsWithIgnoreCase(argv[i], ".apk")) {
261 last_apk = i;
262 break;
263 }
264 }
265
Elliott Hughes0119a912018-10-22 17:02:51 -0700266 if (last_apk == -1) error_exit("need APK file on command line");
Idries Hamadi1ecee442018-01-29 16:30:36 +0000267
268 int result = -1;
269 std::vector<const char*> apk_file = {argv[last_apk]};
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -0700270 std::string apk_dest = "/data/local/tmp/" + android::base::Basename(argv[last_apk]);
Idries Hamadi1ecee442018-01-29 16:30:36 +0000271 argv[last_apk] = apk_dest.c_str(); /* destination name, not source location */
Idries Hamadi1ecee442018-01-29 16:30:36 +0000272
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700273 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 Hamadib1702db2018-09-06 18:42:39 +0100284 }
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700285
Peter Collingbournea41eb3d2024-03-28 20:05:07 -0700286 if (do_sync_push(apk_file, apk_dest.c_str(), false, CompressionType::Any, false, false)) {
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700287 result = pm_command(argc, argv);
288 delete_device_file(apk_dest);
289 }
290
Idries Hamadi1ecee442018-01-29 16:30:36 +0000291 return result;
292}
293
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800294template <class TimePoint>
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700295static int ms_between(TimePoint start, TimePoint end) {
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800296 return std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
297}
298
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700299static int install_app_incremental(int argc, const char** argv, bool wait, bool silent) {
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800300 using clock = std::chrono::high_resolution_clock;
301 const auto start = clock::now();
302 int first_apk = -1;
303 int last_apk = -1;
Alex Buynytskyy31ff0ca2020-05-14 13:29:05 -0700304 incremental::Args passthrough_args = {};
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800305 for (int i = 0; i < argc; ++i) {
306 const auto arg = std::string_view(argv[i]);
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700307 if (android::base::EndsWithIgnoreCase(arg, ".apk"sv)) {
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800308 last_apk = i;
309 if (first_apk == -1) {
310 first_apk = i;
311 }
Alex Buynytskyy31ff0ca2020-05-14 13:29:05 -0700312 } else if (arg.starts_with("install"sv)) {
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800313 // 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 Buynytskyy175ce292020-02-13 06:52:04 -0800315 } else {
Alex Buynytskyy31ff0ca2020-05-14 13:29:05 -0700316 passthrough_args.push_back(arg);
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800317 }
318 }
319
Josh Gaofb139d92020-03-30 17:38:37 -0700320 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 Buynytskyy175ce292020-02-13 06:52:04 -0800326
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700327 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 Buynytskyy175ce292020-02-13 06:52:04 -0800335
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700336 printf("Performing Incremental Install\n");
Alex Buynytskyy31ff0ca2020-05-14 13:29:05 -0700337 auto server_process = incremental::install(files, passthrough_args, silent);
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800338 if (!server_process) {
339 return -1;
340 }
341
342 const auto end = clock::now();
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700343 printf("Install command complete in %d ms\n", ms_between(start, end));
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800344
345 if (wait) {
346 (*server_process).wait();
347 }
348
349 return 0;
350}
351
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700352static std::pair<InstallMode, std::optional<InstallMode>> calculate_install_mode(
353 InstallMode modeFromArgs, bool fastdeploy, CmdlineOption incremental_request) {
354 if (incremental_request == CmdlineOption::Enable) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700355 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 Buynytskyy7a4224f2020-05-18 11:27:44 -0700363 if (incremental_request == CmdlineOption::Enable) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700364 error_exit("--incremental is not compatible with other installation modes");
365 }
366 return {modeFromArgs, std::nullopt};
367 }
368
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700369 if (incremental_request != CmdlineOption::Disable && !is_abb_exec_supported()) {
370 if (incremental_request == CmdlineOption::None) {
371 incremental_request = CmdlineOption::Disable;
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700372 } else {
373 error_exit("Device doesn't support incremental installations");
374 }
375 }
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700376 if (incremental_request == CmdlineOption::None) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700377 // 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 Buynytskyy7a4224f2020-05-18 11:27:44 -0700382 incremental_request = CmdlineOption::Disable;
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700383 }
384 }
385 }
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700386 if (incremental_request == CmdlineOption::None) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700387 // 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;
LuK1337d9f5d7f2024-07-18 21:23:37 +0200390 std::vector<std::string> args = {"settings", "get", "global",
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700391 "enable_adb_incremental_install_default"};
392 auto fd = send_abb_exec_command(args, &error);
393 if (!fd.ok()) {
Mark Hansen8d185a42024-10-30 01:41:32 +0000394 fprintf(stderr, "adb: retrieving the default device installation mode failed: %s\n",
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700395 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 Buynytskyy7a4224f2020-05-18 11:27:44 -0700402 incremental_request = CmdlineOption::Disable;
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700403 }
404 }
405 }
406
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700407 if (incremental_request == CmdlineOption::Enable) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700408 // explicitly requested - no fallback
409 return {INSTALL_INCREMENTAL, std::nullopt};
410 }
411 const auto bestMode = best_install_mode();
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700412 if (incremental_request == CmdlineOption::None) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700413 // 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 Buynytskyy7a4224f2020-05-18 11:27:44 -0700420static 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 Hamadi1ecee442018-01-29 16:30:36 +0000427
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700428 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 Buynytskyy175ce292020-02-13 06:52:04 -0800442 }
443 }
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700444 return passthrough;
445}
Alex Buynytskyy175ce292020-02-13 06:52:04 -0800446
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700447static 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
472int 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 Zubrytskyi39ee3d82020-03-26 18:20:39 -0700489 best_install_mode() == INSTALL_PUSH) {
Elliott Hughes0119a912018-10-22 17:02:51 -0700490 error_exit("Attempting to use streaming install on unsupported device");
Idries Hamadi1ecee442018-01-29 16:30:36 +0000491 }
492
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700493 if (use_fastdeploy && get_device_api_level() < kFastDeployMinApi) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700494 fprintf(stderr,
495 "Fast Deploy is only compatible with devices of API version %d or higher, "
496 "ignoring.\n",
497 kFastDeployMinApi);
Idries Hamadi1ecee442018-01-29 16:30:36 +0000498 use_fastdeploy = false;
499 }
Alex Buynytskyy1af550e2019-09-16 12:10:54 -0700500 fastdeploy_set_agent_update_strategy(agent_update_strategy);
Idries Hamadi1ecee442018-01-29 16:30:36 +0000501
Henry Daitxe4cc4d92018-12-12 10:40:57 +0000502 if (passthrough_argv.size() < 2) {
503 error_exit("install requires an apk argument");
504 }
Idries Hamadi1ecee442018-01-29 16:30:36 +0000505
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700506 auto run_install_mode = [&](InstallMode install_mode, bool silent) {
507 switch (install_mode) {
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700508 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 Buynytskyy7a4224f2020-05-18 11:27:44 -0700516 incremental_wait, silent);
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700517 case INSTALL_DEFAULT:
518 default:
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700519 error_exit("invalid install mode");
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700520 }
521 };
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700522 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 Hamadi1ecee442018-01-29 16:30:36 +0000525 }
Yurii Zubrytskyi39ee3d82020-03-26 18:20:39 -0700526 return res;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000527}
528
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700529static int install_multiple_app_streamed(int argc, const char** argv) {
Idries Hamadi1ecee442018-01-29 16:30:36 +0000530 // 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 Frenidcb4c362018-10-04 16:26:40 +0100536 if (android::base::EndsWithIgnoreCase(argv[i], ".apex")) {
537 error_exit("APEX packages are not compatible with install-multiple");
538 }
Idries Hamadi1ecee442018-01-29 16:30:36 +0000539
Victor Hsieh88f14b82018-10-04 10:46:56 -0700540 if (android::base::EndsWithIgnoreCase(file, ".apk") ||
Jiakai Zhang7024f552025-02-21 17:22:27 +0000541 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 Zubrytskyiaac24222024-11-05 11:05:09 -0800545 android::base::EndsWithIgnoreCase(file, ".fsv_sig") ||
Jiakai Zhang7024f552025-02-21 17:22:27 +0000546 android::base::EndsWithIgnoreCase(file, ".idsig")) { // v4 external signature
Idries Hamadi1ecee442018-01-29 16:30:36 +0000547 struct stat sb;
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700548 if (stat(file, &sb) == -1) perror_exit("failed to stat \"%s\"", file);
549 total_size += sb.st_size;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000550 first_apk = i;
551 } else {
552 break;
553 }
554 }
555
Elliott Hughes0119a912018-10-22 17:02:51 -0700556 if (first_apk == -1) error_exit("need APK file on command line");
Idries Hamadi1ecee442018-01-29 16:30:36 +0000557
Alex Buynytskyy8594fcd2020-04-23 14:45:52 -0700558 const bool use_abb_exec = is_abb_exec_supported();
Alex Buynytskyy8594fcd2020-04-23 14:45:52 -0700559 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 Wenff2446d2022-02-09 16:15:45 -0500566 for (int i = 0; i < first_apk; i++) {
Alex Buynytskyy8594fcd2020-04-23 14:45:52 -0700567 if (use_abb_exec) {
568 cmd_args.push_back(argv[i]);
569 } else {
570 cmd_args.push_back(escape_arg(argv[i]));
571 }
Idries Hamadi1ecee442018-01-29 16:30:36 +0000572 }
573
574 // Create install session
575 std::string error;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000576 char buf[BUFSIZ];
Josh Gaob9155972019-01-11 13:13:20 -0800577 {
Alex Buynytskyy8594fcd2020-04-23 14:45:52 -0700578 unique_fd fd = send_command(cmd_args, &error);
Josh Gaob9155972019-01-11 13:13:20 -0800579 if (fd < 0) {
Mark Hansen8d185a42024-10-30 01:41:32 +0000580 perror_exit("connect error for create: %s", error.c_str());
Josh Gaob9155972019-01-11 13:13:20 -0800581 }
582 read_status_line(fd.get(), buf, sizeof(buf));
583 }
Idries Hamadi1ecee442018-01-29 16:30:36 +0000584
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 Buynytskyy8594fcd2020-04-23 14:45:52 -0700599 const auto session_id_str = std::to_string(session_id);
Idries Hamadi1ecee442018-01-29 16:30:36 +0000600
601 // Valid session, now stream the APKs
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700602 bool success = true;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000603 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 Hughesb6ebbe22019-08-05 17:06:04 -0700607 fprintf(stderr, "adb: failed to stat \"%s\": %s\n", file, strerror(errno));
608 success = false;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000609 goto finalize_session;
610 }
611
Alex Buynytskyy8594fcd2020-04-23 14:45:52 -0700612 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 Hamadi1ecee442018-01-29 16:30:36 +0000621
Josh Gaob9155972019-01-11 13:13:20 -0800622 unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC));
623 if (local_fd < 0) {
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700624 fprintf(stderr, "adb: failed to open \"%s\": %s\n", file, strerror(errno));
625 success = false;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000626 goto finalize_session;
627 }
628
629 std::string error;
Alex Buynytskyy8594fcd2020-04-23 14:45:52 -0700630 unique_fd remote_fd = send_command(cmd_args, &error);
Josh Gaob9155972019-01-11 13:13:20 -0800631 if (remote_fd < 0) {
Idries Hamadi1ecee442018-01-29 16:30:36 +0000632 fprintf(stderr, "adb: connect error for write: %s\n", error.c_str());
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700633 success = false;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000634 goto finalize_session;
635 }
636
Josh Gaod7f1d0b2019-12-03 16:05:54 -0800637 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 Gaob9155972019-01-11 13:13:20 -0800643 read_status_line(remote_fd.get(), buf, sizeof(buf));
Idries Hamadi1ecee442018-01-29 16:30:36 +0000644
645 if (strncmp("Success", buf, 7)) {
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700646 fprintf(stderr, "adb: failed to write \"%s\"\n", file);
Idries Hamadi1ecee442018-01-29 16:30:36 +0000647 fputs(buf, stderr);
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700648 success = false;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000649 goto finalize_session;
650 }
651 }
652
653finalize_session:
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700654 // Commit session if we streamed everything okay; otherwise abandon.
Alex Buynytskyy8594fcd2020-04-23 14:45:52 -0700655 std::vector<std::string> service_args = {
656 install_cmd,
657 success ? "install-commit" : "install-abandon",
658 session_id_str,
659 };
Josh Gaob9155972019-01-11 13:13:20 -0800660 {
Alex Buynytskyy8594fcd2020-04-23 14:45:52 -0700661 unique_fd fd = send_command(service_args, &error);
Josh Gaob9155972019-01-11 13:13:20 -0800662 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 Hamadi1ecee442018-01-29 16:30:36 +0000667 }
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700668 if (!success) return EXIT_FAILURE;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000669
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700670 if (strncmp("Success", buf, 7)) {
671 fprintf(stderr, "adb: failed to finalize session\n");
672 fputs(buf, stderr);
673 return EXIT_FAILURE;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000674 }
Elliott Hughesb6ebbe22019-08-05 17:06:04 -0700675
676 fputs(buf, stdout);
677 return EXIT_SUCCESS;
Idries Hamadi1ecee442018-01-29 16:30:36 +0000678}
679
Alex Buynytskyy7a4224f2020-05-18 11:27:44 -0700680int 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 Baumann810ee9b2018-10-09 10:45:03 -0700718int install_multi_package(int argc, const char** argv) {
719 // Find all APK arguments starting at end.
720 // All other arguments passed through verbatim.
Dario Freni907ef682019-01-14 17:06:32 +0000721 bool apex_found = false;
722 int first_package = -1;
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700723 for (int i = argc - 1; i >= 0; i--) {
724 const char* file = argv[i];
Dario Freni907ef682019-01-14 17:06:32 +0000725 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 Baumann810ee9b2018-10-09 10:45:03 -0700731 } else {
732 break;
733 }
734 }
735
Dario Freni907ef682019-01-14 17:06:32 +0000736 if (first_package == -1) error_exit("need APK or APEX files on command line");
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700737
Yurii Zubrytskyie59c1bd2019-06-27 13:47:34 -0700738 if (best_install_mode() == INSTALL_PUSH) {
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700739 fprintf(stderr, "adb: multi-package install is not supported on this device\n");
740 return EXIT_FAILURE;
741 }
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700742
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700743 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 Freni2042fd22019-01-29 14:17:05 +0000750 for (int i = 1; i < first_package; i++) {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700751 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 Freni2042fd22019-01-29 14:17:05 +0000756 }
757
Dario Freni907ef682019-01-14 17:06:32 +0000758 if (apex_found) {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700759 multi_package_cmd_args.emplace_back("--staged");
Dario Freni907ef682019-01-14 17:06:32 +0000760 }
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700761
762 // Create multi-package install session
763 std::string error;
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700764 char buf[BUFSIZ];
Josh Gaob9155972019-01-11 13:13:20 -0800765 {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700766 unique_fd fd = send_command(multi_package_cmd_args, &error);
Josh Gaob9155972019-01-11 13:13:20 -0800767 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 Baumann810ee9b2018-10-09 10:45:03 -0700773
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 Buynytskyy77138ae2020-05-18 16:48:14 -0700788 const auto parent_session_id_str = std::to_string(parent_session_id);
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700789
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 Buynytskyy77138ae2020-05-18 16:48:14 -0700796 std::vector<std::string> individual_cmd_args = {install_cmd, "install-create"};
Dario Freni907ef682019-01-14 17:06:32 +0000797 for (int i = 1; i < first_package; i++) {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700798 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 Baumann810ee9b2018-10-09 10:45:03 -0700803 }
Dario Freni907ef682019-01-14 17:06:32 +0000804 if (apex_found) {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700805 individual_cmd_args.emplace_back("--staged");
Dario Freni907ef682019-01-14 17:06:32 +0000806 }
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700807
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 Freni907ef682019-01-14 17:06:32 +0000820 for (int i = first_package; i < argc; i++) {
821 const char* file = argv[i];
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700822 char buf[BUFSIZ];
Josh Gaob9155972019-01-11 13:13:20 -0800823 {
Dario Freni907ef682019-01-14 17:06:32 +0000824 unique_fd fd;
825 // Create individual install session
826 if (android::base::EndsWithIgnoreCase(file, ".apex")) {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700827 fd = send_command(individual_apex_cmd_args, &error);
Dario Freni907ef682019-01-14 17:06:32 +0000828 } else {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700829 fd = send_command(individual_cmd_args, &error);
Dario Freni907ef682019-01-14 17:06:32 +0000830 }
Josh Gaob9155972019-01-11 13:13:20 -0800831 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 Baumann810ee9b2018-10-09 10:45:03 -0700837
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 Buynytskyy77138ae2020-05-18 16:48:14 -0700852 const auto session_id_str = std::to_string(session_id);
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700853
854 fprintf(stdout, "Created child session ID %d.\n", session_id);
855 session_ids.push_back(session_id);
856
Dario Freni7d5a5e12019-02-25 12:00:32 +0000857 // Support splitAPKs by allowing the notation split1.apk:split2.apk:split3.apk as argument.
Dario Frenifce67172022-07-19 13:47:41 +0000858 // 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 Freni7d5a5e12019-02-25 12:00:32 +0000860
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 Buynytskyy77138ae2020-05-18 16:48:14 -0700868 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 Buynytskyy83e11f52020-07-14 17:55:22 -0700874 android::base::StringPrintf("%d_%s", i, android::base::Basename(split).c_str()),
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700875 "-",
876 };
Dario Freni7d5a5e12019-02-25 12:00:32 +0000877
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 Buynytskyy77138ae2020-05-18 16:48:14 -0700885 unique_fd remote_fd = send_command(cmd_args, &error);
Dario Freni7d5a5e12019-02-25 12:00:32 +0000886 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 Gaod7f1d0b2019-12-03 16:05:54 -0800891 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 Freni7d5a5e12019-02-25 12:00:32 +0000896 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 Baumann810ee9b2018-10-09 10:45:03 -0700903 }
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700904 add_session_cmd_args.push_back(std::to_string(session_id));
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700905 }
906
Josh Gaob9155972019-01-11 13:13:20 -0800907 {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700908 unique_fd fd = send_command(add_session_cmd_args, &error);
Josh Gaob9155972019-01-11 13:13:20 -0800909 if (fd < 0) {
Dario Freni907ef682019-01-14 17:06:32 +0000910 fprintf(stderr, "adb: connect error for install-add-session: %s\n", error.c_str());
Josh Gaob9155972019-01-11 13:13:20 -0800911 goto finalize_multi_package_session;
912 }
913 read_status_line(fd.get(), buf, sizeof(buf));
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700914 }
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700915
916 if (strncmp("Success", buf, 7)) {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700917 fprintf(stderr, "adb: failed to link sessions (%s)\n",
918 android::base::Join(add_session_cmd_args, " ").c_str());
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700919 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
926finalize_multi_package_session:
927 // Commit session if we streamed everything okay; otherwise abandon
Samiul Islam7a582452021-06-03 11:18:11 +0100928 std::vector<std::string> service_args;
Samiul Islam283c5db2021-06-10 13:37:35 +0100929 if (success == 0) {
Samiul Islam7a582452021-06-03 11:18:11 +0100930 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 Buynytskyy77138ae2020-05-18 16:48:14 -0700944
Josh Gaob9155972019-01-11 13:13:20 -0800945 {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700946 unique_fd fd = send_command(service_args, &error);
Josh Gaob9155972019-01-11 13:13:20 -0800947 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 Baumann810ee9b2018-10-09 10:45:03 -0700952 }
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700953
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 Freni7d5a5e12019-02-25 12:00:32 +0000964 session_ids.push_back(parent_session_id);
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700965 // try to abandon all remaining sessions
966 for (std::size_t i = 0; i < session_ids.size(); i++) {
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700967 std::vector<std::string> service_args = {
968 install_cmd,
969 "install-abandon",
970 std::to_string(session_ids[i]),
971 };
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700972 fprintf(stderr, "Attempting to abandon session ID %d\n", session_ids[i]);
Alex Buynytskyy77138ae2020-05-18 16:48:14 -0700973 unique_fd fd = send_command(service_args, &error);
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700974 if (fd < 0) {
975 fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
976 continue;
977 }
Josh Gaob9155972019-01-11 13:13:20 -0800978 read_status_line(fd.get(), buf, sizeof(buf));
Patrick Baumann810ee9b2018-10-09 10:45:03 -0700979 }
980 return EXIT_FAILURE;
981}
982
Idries Hamadi1ecee442018-01-29 16:30:36 +0000983int delete_device_file(const std::string& filename) {
Elliott Hughes203f5372019-08-02 16:06:06 -0700984 // 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 Hamadi1ecee442018-01-29 16:30:36 +00001000}