adb: switch install to unique_fd.

Test: mma
Change-Id: I9474c003e0eb4a8cb183090a2e525d78932d3bea
This commit is contained in:
Josh Gao 2019-01-11 13:13:20 -08:00
parent dc58b0a388
commit 61f872f2a8

View file

@ -24,12 +24,14 @@
#include <string>
#include <vector>
#include <android-base/file.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include "adb.h"
#include "adb_client.h"
#include "adb_unique_fd.h"
#include "adb_utils.h"
#include "android-base/file.h"
#include "android-base/stringprintf.h"
#include "android-base/strings.h"
#include "client/file_sync_client.h"
#include "commandline.h"
#include "fastdeploy.h"
@ -179,8 +181,8 @@ static int install_app_streamed(int argc, const char** argv, bool use_fastdeploy
return 1;
}
int localFd = adb_open(file, O_RDONLY);
if (localFd < 0) {
unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC));
if (local_fd < 0) {
fprintf(stderr, "adb: failed to open %s: %s\n", file, strerror(errno));
return 1;
}
@ -201,19 +203,15 @@ static int install_app_streamed(int argc, const char** argv, bool use_fastdeploy
cmd += " --apex";
}
int remoteFd = adb_connect(cmd, &error);
if (remoteFd < 0) {
unique_fd remote_fd(adb_connect(cmd, &error));
if (remote_fd < 0) {
fprintf(stderr, "adb: connect error for write: %s\n", error.c_str());
adb_close(localFd);
return 1;
}
char buf[BUFSIZ];
copy_to_file(localFd, remoteFd);
read_status_line(remoteFd, buf, sizeof(buf));
adb_close(localFd);
adb_close(remoteFd);
copy_to_file(local_fd.get(), remote_fd.get());
read_status_line(remote_fd.get(), buf, sizeof(buf));
if (!strncmp("Success", buf, 7)) {
fputs(buf, stdout);
@ -410,14 +408,15 @@ int install_multiple_app(int argc, const char** argv) {
// Create install session
std::string error;
int fd = adb_connect(cmd, &error);
if (fd < 0) {
fprintf(stderr, "adb: connect error for create: %s\n", error.c_str());
return EXIT_FAILURE;
}
char buf[BUFSIZ];
read_status_line(fd, buf, sizeof(buf));
adb_close(fd);
{
unique_fd fd(adb_connect(cmd, &error));
if (fd < 0) {
fprintf(stderr, "adb: connect error for create: %s\n", error.c_str());
return EXIT_FAILURE;
}
read_status_line(fd.get(), buf, sizeof(buf));
}
int session_id = -1;
if (!strncmp("Success", buf, 7)) {
@ -450,27 +449,23 @@ int install_multiple_app(int argc, const char** argv) {
install_cmd.c_str(), static_cast<uint64_t>(sb.st_size),
session_id, android::base::Basename(file).c_str());
int localFd = adb_open(file, O_RDONLY);
if (localFd < 0) {
unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC));
if (local_fd < 0) {
fprintf(stderr, "adb: failed to open %s: %s\n", file, strerror(errno));
success = 0;
goto finalize_session;
}
std::string error;
int remoteFd = adb_connect(cmd, &error);
if (remoteFd < 0) {
unique_fd remote_fd(adb_connect(cmd, &error));
if (remote_fd < 0) {
fprintf(stderr, "adb: connect error for write: %s\n", error.c_str());
adb_close(localFd);
success = 0;
goto finalize_session;
}
copy_to_file(localFd, remoteFd);
read_status_line(remoteFd, buf, sizeof(buf));
adb_close(localFd);
adb_close(remoteFd);
copy_to_file(local_fd.get(), remote_fd.get());
read_status_line(remote_fd.get(), buf, sizeof(buf));
if (strncmp("Success", buf, 7)) {
fprintf(stderr, "adb: failed to write %s\n", file);
@ -484,13 +479,14 @@ finalize_session:
// Commit session if we streamed everything okay; otherwise abandon
std::string service = android::base::StringPrintf("%s install-%s %d", install_cmd.c_str(),
success ? "commit" : "abandon", session_id);
fd = adb_connect(service, &error);
if (fd < 0) {
fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
return EXIT_FAILURE;
{
unique_fd fd(adb_connect(service, &error));
if (fd < 0) {
fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
return EXIT_FAILURE;
}
read_status_line(fd.get(), buf, sizeof(buf));
}
read_status_line(fd, buf, sizeof(buf));
adb_close(fd);
if (!strncmp("Success", buf, 7)) {
fputs(buf, stdout);
@ -527,14 +523,15 @@ int install_multi_package(int argc, const char** argv) {
// Create multi-package install session
std::string error;
int fd = adb_connect(multi_package_cmd, &error);
if (fd < 0) {
fprintf(stderr, "adb: connect error for create multi-package: %s\n", error.c_str());
return EXIT_FAILURE;
}
char buf[BUFSIZ];
read_status_line(fd, buf, sizeof(buf));
adb_close(fd);
{
unique_fd fd(adb_connect(multi_package_cmd, &error));
if (fd < 0) {
fprintf(stderr, "adb: connect error for create multi-package: %s\n", error.c_str());
return EXIT_FAILURE;
}
read_status_line(fd.get(), buf, sizeof(buf));
}
int parent_session_id = -1;
if (!strncmp("Success", buf, 7)) {
@ -566,14 +563,15 @@ int install_multi_package(int argc, const char** argv) {
std::string cmd = "";
for (int i = first_apk; i < argc; i++) {
// Create individual install session
fd = adb_connect(individual_cmd, &error);
if (fd < 0) {
fprintf(stderr, "adb: connect error for create: %s\n", error.c_str());
goto finalize_multi_package_session;
}
char buf[BUFSIZ];
read_status_line(fd, buf, sizeof(buf));
adb_close(fd);
{
unique_fd fd(adb_connect(individual_cmd, &error));
if (fd < 0) {
fprintf(stderr, "adb: connect error for create: %s\n", error.c_str());
goto finalize_multi_package_session;
}
read_status_line(fd.get(), buf, sizeof(buf));
}
int session_id = -1;
if (!strncmp("Success", buf, 7)) {
@ -605,25 +603,21 @@ int install_multi_package(int argc, const char** argv) {
install_cmd.c_str(), static_cast<uint64_t>(sb.st_size),
session_id, i, android::base::Basename(file).c_str());
int localFd = adb_open(file, O_RDONLY);
if (localFd < 0) {
unique_fd local_fd(adb_open(file, O_RDONLY | O_CLOEXEC));
if (local_fd < 0) {
fprintf(stderr, "adb: failed to open %s: %s\n", file, strerror(errno));
goto finalize_multi_package_session;
}
std::string error;
int remoteFd = adb_connect(cmd, &error);
if (remoteFd < 0) {
unique_fd remote_fd(adb_connect(cmd, &error));
if (remote_fd < 0) {
fprintf(stderr, "adb: connect error for write: %s\n", error.c_str());
adb_close(localFd);
goto finalize_multi_package_session;
}
copy_to_file(localFd, remoteFd);
read_status_line(remoteFd, buf, sizeof(buf));
adb_close(localFd);
adb_close(remoteFd);
copy_to_file(local_fd.get(), remote_fd.get());
read_status_line(remote_fd.get(), buf, sizeof(buf));
if (strncmp("Success", buf, 7)) {
fprintf(stderr, "adb: failed to write %s\n", file);
@ -636,13 +630,14 @@ int install_multi_package(int argc, const char** argv) {
cmd = android::base::StringPrintf("%s install-add-session %d%s", install_cmd.c_str(),
parent_session_id, all_session_ids.c_str());
fd = adb_connect(cmd, &error);
if (fd < 0) {
fprintf(stderr, "adb: connect error for create: %s\n", error.c_str());
goto finalize_multi_package_session;
{
unique_fd fd(adb_connect(cmd, &error));
if (fd < 0) {
fprintf(stderr, "adb: connect error for create: %s\n", error.c_str());
goto finalize_multi_package_session;
}
read_status_line(fd.get(), buf, sizeof(buf));
}
read_status_line(fd, buf, sizeof(buf));
adb_close(fd);
if (strncmp("Success", buf, 7)) {
fprintf(stderr, "adb: failed to link sessions (%s)\n", cmd.c_str());
@ -658,13 +653,14 @@ finalize_multi_package_session:
std::string service =
android::base::StringPrintf("%s install-%s %d", install_cmd.c_str(),
success == 0 ? "commit" : "abandon", parent_session_id);
fd = adb_connect(service, &error);
if (fd < 0) {
fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
return EXIT_FAILURE;
{
unique_fd fd(adb_connect(service, &error));
if (fd < 0) {
fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
return EXIT_FAILURE;
}
read_status_line(fd.get(), buf, sizeof(buf));
}
read_status_line(fd, buf, sizeof(buf));
adb_close(fd);
if (!strncmp("Success", buf, 7)) {
fputs(buf, stdout);
@ -681,13 +677,12 @@ finalize_multi_package_session:
service = android::base::StringPrintf("%s install-abandon %d", install_cmd.c_str(),
session_ids[i]);
fprintf(stderr, "Attempting to abandon session ID %d\n", session_ids[i]);
fd = adb_connect(service, &error);
unique_fd fd(adb_connect(service, &error));
if (fd < 0) {
fprintf(stderr, "adb: connect error for finalize: %s\n", error.c_str());
continue;
}
read_status_line(fd, buf, sizeof(buf));
adb_close(fd);
read_status_line(fd.get(), buf, sizeof(buf));
}
return EXIT_FAILURE;
}