summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes2017-05-02 19:51:06 -0500
committerGerrit Code Review2017-05-02 19:51:09 -0500
commit66b25eb810eb542e7b6ef5d5b0f89cf5f23abe06 (patch)
tree3970250e46fa1522f15cdae201f12c1efc56bf47
parent8fc6cc8910dbb4c77333aa432b4732237157ed0b (diff)
parent09e794c05d392941e04d961e1c4072b8bcc4133a (diff)
downloadplatform-system-core-66b25eb810eb542e7b6ef5d5b0f89cf5f23abe06.tar.gz
platform-system-core-66b25eb810eb542e7b6ef5d5b0f89cf5f23abe06.tar.xz
platform-system-core-66b25eb810eb542e7b6ef5d5b0f89cf5f23abe06.zip
Merge "Improve "adb sideload" error reporting."
-rw-r--r--adb/commandline.cpp83
1 files changed, 35 insertions, 48 deletions
diff --git a/adb/commandline.cpp b/adb/commandline.cpp
index b8f790dcf..a9b1540d5 100644
--- a/adb/commandline.cpp
+++ b/adb/commandline.cpp
@@ -761,55 +761,46 @@ static int adb_shell(int argc, const char** argv) {
761 return RemoteShell(use_shell_protocol, shell_type_arg, escape_char, command); 761 return RemoteShell(use_shell_protocol, shell_type_arg, escape_char, command);
762} 762}
763 763
764static int adb_download_buffer(const char* service, const char* filename) { 764static int adb_sideload_legacy(const char* filename, int in_fd, int size) {
765 std::string content;
766 if (!android::base::ReadFileToString(filename, &content)) {
767 fprintf(stderr, "error: couldn't read %s: %s\n", filename, strerror(errno));
768 return -1;
769 }
770
771 const uint8_t* data = reinterpret_cast<const uint8_t*>(content.data());
772 unsigned sz = content.size();
773
774 std::string error; 765 std::string error;
775 int fd = adb_connect(android::base::StringPrintf("%s:%d", service, sz), &error); 766 int out_fd = adb_connect(android::base::StringPrintf("sideload:%d", size), &error);
776 if (fd < 0) { 767 if (out_fd < 0) {
777 fprintf(stderr,"error: %s\n", error.c_str()); 768 fprintf(stderr, "adb: pre-KitKat sideload connection failed: %s\n", error.c_str());
778 return -1; 769 return -1;
779 } 770 }
780 771
781 int opt = CHUNK_SIZE; 772 int opt = CHUNK_SIZE;
782 opt = adb_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (const void *) &opt, sizeof(opt)); 773 opt = adb_setsockopt(out_fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));
783 774
784 unsigned total = sz; 775 char buf[CHUNK_SIZE];
785 const uint8_t* ptr = reinterpret_cast<const uint8_t*>(data); 776 int total = size;
786 777 while (size > 0) {
787 const char* x = strrchr(service, ':'); 778 unsigned xfer = (size > CHUNK_SIZE) ? CHUNK_SIZE : size;
788 if (x) service = x + 1; 779 if (!ReadFdExactly(in_fd, buf, xfer)) {
789 780 fprintf(stderr, "adb: failed to read data from %s: %s\n", filename, strerror(errno));
790 while (sz > 0) { 781 adb_close(out_fd);
791 unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz; 782 return -1;
792 if (!WriteFdExactly(fd, ptr, xfer)) { 783 }
784 if (!WriteFdExactly(out_fd, buf, xfer)) {
793 std::string error; 785 std::string error;
794 adb_status(fd, &error); 786 adb_status(out_fd, &error);
795 fprintf(stderr,"* failed to write data '%s' *\n", error.c_str()); 787 fprintf(stderr, "adb: failed to write data: %s\n", error.c_str());
796 adb_close(fd); 788 adb_close(out_fd);
797 return -1; 789 return -1;
798 } 790 }
799 sz -= xfer; 791 size -= xfer;
800 ptr += xfer; 792 printf("sending: '%s' %4d%% \r", filename, (int)(100LL - ((100LL * size) / (total))));
801 printf("sending: '%s' %4d%% \r", filename, (int)(100LL - ((100LL * sz) / (total))));
802 fflush(stdout); 793 fflush(stdout);
803 } 794 }
804 printf("\n"); 795 printf("\n");
805 796
806 if (!adb_status(fd, &error)) { 797 if (!adb_status(out_fd, &error)) {
807 fprintf(stderr,"* error response '%s' *\n", error.c_str()); 798 fprintf(stderr, "adb: error response: %s\n", error.c_str());
808 adb_close(fd); 799 adb_close(out_fd);
809 return -1; 800 return -1;
810 } 801 }
811 802
812 adb_close(fd); 803 adb_close(out_fd);
813 return 0; 804 return 0;
814} 805}
815 806
@@ -836,22 +827,17 @@ static int adb_download_buffer(const char* service, const char* filename) {
836 */ 827 */
837static int adb_sideload_host(const char* filename) { 828static int adb_sideload_host(const char* filename) {
838 // TODO: use a LinePrinter instead... 829 // TODO: use a LinePrinter instead...
839 fprintf(stdout, "opening '%s'...\n", filename);
840 fflush(stdout);
841
842 struct stat sb; 830 struct stat sb;
843 if (stat(filename, &sb) == -1) { 831 if (stat(filename, &sb) == -1) {
844 fprintf(stderr, "failed to stat file %s: %s\n", filename, strerror(errno)); 832 fprintf(stderr, "adb: failed to stat file %s: %s\n", filename, strerror(errno));
845 return -1; 833 return -1;
846 } 834 }
847 unique_fd package_fd(adb_open(filename, O_RDONLY)); 835 unique_fd package_fd(adb_open(filename, O_RDONLY));
848 if (package_fd == -1) { 836 if (package_fd == -1) {
849 fprintf(stderr, "failed to open file %s: %s\n", filename, strerror(errno)); 837 fprintf(stderr, "adb: failed to open file %s: %s\n", filename, strerror(errno));
850 return -1; 838 return -1;
851 } 839 }
852 840
853 fprintf(stdout, "connecting...\n");
854 fflush(stdout);
855 std::string service = android::base::StringPrintf( 841 std::string service = android::base::StringPrintf(
856 "sideload-host:%d:%d", static_cast<int>(sb.st_size), SIDELOAD_HOST_BLOCK_SIZE); 842 "sideload-host:%d:%d", static_cast<int>(sb.st_size), SIDELOAD_HOST_BLOCK_SIZE);
857 std::string error; 843 std::string error;
@@ -859,8 +845,9 @@ static int adb_sideload_host(const char* filename) {
859 if (device_fd < 0) { 845 if (device_fd < 0) {
860 // Try falling back to the older (<= K) sideload method. Maybe this 846 // Try falling back to the older (<= K) sideload method. Maybe this
861 // is an older device that doesn't support sideload-host. 847 // is an older device that doesn't support sideload-host.
862 fprintf(stderr, "falling back to older sideload method...\n"); 848 fprintf(stderr, "adb: sideload connection failed: %s\n", error.c_str());
863 return adb_download_buffer("sideload", filename); 849 fprintf(stderr, "adb: trying pre-KitKat sideload method...\n");
850 return adb_sideload_legacy(filename, package_fd, static_cast<int>(sb.st_size));
864 } 851 }
865 852
866 int opt = SIDELOAD_HOST_BLOCK_SIZE; 853 int opt = SIDELOAD_HOST_BLOCK_SIZE;
@@ -872,7 +859,7 @@ static int adb_sideload_host(const char* filename) {
872 int last_percent = -1; 859 int last_percent = -1;
873 while (true) { 860 while (true) {
874 if (!ReadFdExactly(device_fd, buf, 8)) { 861 if (!ReadFdExactly(device_fd, buf, 8)) {
875 fprintf(stderr, "* failed to read command: %s\n", strerror(errno)); 862 fprintf(stderr, "adb: failed to read command: %s\n", strerror(errno));
876 return -1; 863 return -1;
877 } 864 }
878 buf[8] = '\0'; 865 buf[8] = '\0';
@@ -888,7 +875,7 @@ static int adb_sideload_host(const char* filename) {
888 875
889 size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE; 876 size_t offset = block * SIDELOAD_HOST_BLOCK_SIZE;
890 if (offset >= static_cast<size_t>(sb.st_size)) { 877 if (offset >= static_cast<size_t>(sb.st_size)) {
891 fprintf(stderr, "* attempt to read block %d past end\n", block); 878 fprintf(stderr, "adb: failed to read block %d past end\n", block);
892 return -1; 879 return -1;
893 } 880 }
894 881
@@ -898,17 +885,17 @@ static int adb_sideload_host(const char* filename) {
898 } 885 }
899 886
900 if (adb_lseek(package_fd, offset, SEEK_SET) != static_cast<int>(offset)) { 887 if (adb_lseek(package_fd, offset, SEEK_SET) != static_cast<int>(offset)) {
901 fprintf(stderr, "* failed to seek to package block: %s\n", strerror(errno)); 888 fprintf(stderr, "adb: failed to seek to package block: %s\n", strerror(errno));
902 return -1; 889 return -1;
903 } 890 }
904 if (!ReadFdExactly(package_fd, buf, to_write)) { 891 if (!ReadFdExactly(package_fd, buf, to_write)) {
905 fprintf(stderr, "* failed to read package block: %s\n", strerror(errno)); 892 fprintf(stderr, "adb: failed to read package block: %s\n", strerror(errno));
906 return -1; 893 return -1;
907 } 894 }
908 895
909 if (!WriteFdExactly(device_fd, buf, to_write)) { 896 if (!WriteFdExactly(device_fd, buf, to_write)) {
910 adb_status(device_fd, &error); 897 adb_status(device_fd, &error);
911 fprintf(stderr,"* failed to write data '%s' *\n", error.c_str()); 898 fprintf(stderr, "adb: failed to write data '%s' *\n", error.c_str());
912 return -1; 899 return -1;
913 } 900 }
914 xfer += to_write; 901 xfer += to_write;