Skip to content

Commit f0f312a

Browse files
tdmcyngnmikeNG
authored andcommitted
adb: host: Provide better sideload status
* Show data transfer in MB and in multiple of the file size. * Show a spinner to indicate liveness, which is updated at least once per second regardless of data transfer. * Do not allow sideload of zero sized files. Change-Id: I1bd0df6a8183fad5a502fc26a7e789c27d24f71a
1 parent 85a9fd3 commit f0f312a

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

adb/client/commandline.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,8 @@ static int adb_sideload_legacy(const char* filename, int in_fd, int size) {
838838

839839
#define SIDELOAD_HOST_BLOCK_SIZE (CHUNK_SIZE)
840840

841+
#define MB (1024*1024)
842+
841843
// Connects to the sideload / rescue service on the device (served by minadbd) and sends over the
842844
// data in an OTA package.
843845
//
@@ -857,12 +859,21 @@ static int adb_sideload_legacy(const char* filename, int in_fd, int size) {
857859
// the data transfer.
858860
//
859861
static int adb_sideload_install(const char* filename, bool rescue_mode) {
862+
static const char spinner[] = "/-\\|";
863+
static const int spinlen = sizeof(spinner)-1;
864+
size_t last_xfer = 0;
865+
int spin_index = 0;
860866
// TODO: use a LinePrinter instead...
861867
struct stat sb;
862868
if (stat(filename, &sb) == -1) {
863869
fprintf(stderr, "adb: failed to stat file %s: %s\n", filename, strerror(errno));
864870
return -1;
865871
}
872+
if (sb.st_size == 0) {
873+
printf("\n");
874+
fprintf(stderr, "* '%s' is empty *\n", filename);
875+
return -1;
876+
}
866877
unique_fd package_fd(adb_open(filename, O_RDONLY));
867878
if (package_fd == -1) {
868879
fprintf(stderr, "adb: failed to open file %s: %s\n", filename, strerror(errno));
@@ -896,8 +907,25 @@ static int adb_sideload_install(const char* filename, bool rescue_mode) {
896907
char buf[SIDELOAD_HOST_BLOCK_SIZE];
897908

898909
int64_t xfer = 0;
899-
int last_percent = -1;
900910
while (true) {
911+
fd_set fds;
912+
struct timeval tv;
913+
FD_ZERO(&fds);
914+
FD_SET(device_fd, &fds);
915+
tv.tv_sec = 1;
916+
tv.tv_usec = 0;
917+
int rc = select(device_fd+1, &fds, NULL, NULL, &tv);
918+
size_t diff = xfer - last_xfer;
919+
if (rc == 0 || diff >= (1*MB)) {
920+
spin_index = (spin_index+1) % spinlen;
921+
printf("\rserving: '%s' %4umb %.2fx %c", filename,
922+
(unsigned)xfer/(1*MB), (double)xfer/sb.st_size, spinner[spin_index]);
923+
fflush(stdout);
924+
last_xfer = xfer;
925+
}
926+
if (rc == 0) {
927+
continue;
928+
}
901929
if (!ReadFdExactly(device_fd, buf, 8)) {
902930
fprintf(stderr, "adb: failed to read command: %s\n", strerror(errno));
903931
return -1;
@@ -945,20 +973,9 @@ static int adb_sideload_install(const char* filename, bool rescue_mode) {
945973
return -1;
946974
}
947975
xfer += to_write;
948-
949-
// For normal OTA packages, we expect to transfer every byte
950-
// twice, plus a bit of overhead (one read during
951-
// verification, one read of each byte for installation, plus
952-
// extra access to things like the zip central directory).
953-
// This estimate of the completion becomes 100% when we've
954-
// transferred ~2.13 (=100/47) times the package size.
955-
int percent = static_cast<int>(xfer * 47LL / (sb.st_size ? sb.st_size : 1));
956-
if (percent != last_percent) {
957-
printf("\rserving: '%s' (~%d%%) ", filename, percent);
958-
fflush(stdout);
959-
last_percent = percent;
960-
}
961976
}
977+
978+
printf("\ntotal xfer: %4umb %.2fx\n", (unsigned)xfer/(1*MB), (double)xfer/sb.st_size);
962979
}
963980

964981
static int adb_wipe_devices() {

0 commit comments

Comments
 (0)