forked from Tasssadar/multirom_adbd
-
Notifications
You must be signed in to change notification settings - Fork 4
/
commandline.c
1614 lines (1396 loc) · 46.2 KB
/
commandline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <assert.h>
#include "sysdeps.h"
#ifdef HAVE_TERMIO_H
#include <termios.h>
#endif
#define TRACE_TAG TRACE_ADB
#include "adb.h"
#include "adb_client.h"
#include "file_sync_service.h"
static int do_cmd(transport_type ttype, char* serial, char *cmd, ...);
void get_my_path(char *s, size_t maxLen);
int find_sync_dirs(const char *srcarg,
char **android_srcdir_out, char **data_srcdir_out);
int install_app(transport_type transport, char* serial, int argc, char** argv);
int uninstall_app(transport_type transport, char* serial, int argc, char** argv);
static const char *gProductOutPath = NULL;
static char *product_file(const char *extra)
{
int n;
char *x;
if (gProductOutPath == NULL) {
fprintf(stderr, "adb: Product directory not specified; "
"use -p or define ANDROID_PRODUCT_OUT\n");
exit(1);
}
n = strlen(gProductOutPath) + strlen(extra) + 2;
x = malloc(n);
if (x == 0) {
fprintf(stderr, "adb: Out of memory (product_file())\n");
exit(1);
}
snprintf(x, (size_t)n, "%s" OS_PATH_SEPARATOR_STR "%s", gProductOutPath, extra);
return x;
}
void version(FILE * out) {
fprintf(out, "Android Debug Bridge version %d.%d.%d\n",
ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION);
}
void help()
{
version(stderr);
fprintf(stderr,
"\n"
" -d - directs command to the only connected USB device\n"
" returns an error if more than one USB device is present.\n"
" -e - directs command to the only running emulator.\n"
" returns an error if more than one emulator is running.\n"
" -s <serial number> - directs command to the USB device or emulator with\n"
" the given serial number. Overrides ANDROID_SERIAL\n"
" environment variable.\n"
" -p <product name or path> - simple product name like 'sooner', or\n"
" a relative/absolute path to a product\n"
" out directory like 'out/target/product/sooner'.\n"
" If -p is not specified, the ANDROID_PRODUCT_OUT\n"
" environment variable is used, which must\n"
" be an absolute path.\n"
" devices - list all connected devices\n"
" connect <host>[:<port>] - connect to a device via TCP/IP\n"
" Port 5555 is used by default if no port number is specified.\n"
" disconnect [<host>[:<port>]] - disconnect from a TCP/IP device.\n"
" Port 5555 is used by default if no port number is specified.\n"
" Using this command with no additional arguments\n"
" will disconnect from all connected TCP/IP devices.\n"
"\n"
"device commands:\n"
" adb push <local> <remote> - copy file/dir to device\n"
" adb pull <remote> [<local>] - copy file/dir from device\n"
" adb sync [ <directory> ] - copy host->device only if changed\n"
" (-l means list but don't copy)\n"
" (see 'adb help all')\n"
" adb shell - run remote shell interactively\n"
" adb shell <command> - run remote shell command\n"
" adb emu <command> - run emulator console command\n"
" adb logcat [ <filter-spec> ] - View device log\n"
" adb forward <local> <remote> - forward socket connections\n"
" forward specs are one of: \n"
" tcp:<port>\n"
" localabstract:<unix domain socket name>\n"
" localreserved:<unix domain socket name>\n"
" localfilesystem:<unix domain socket name>\n"
" dev:<character device name>\n"
" jdwp:<process pid> (remote only)\n"
" adb jdwp - list PIDs of processes hosting a JDWP transport\n"
" adb install [-l] [-r] [-s] [--algo <algorithm name> --key <hex-encoded key> --iv <hex-encoded iv>] <file>\n"
" - push this package file to the device and install it\n"
" ('-l' means forward-lock the app)\n"
" ('-r' means reinstall the app, keeping its data)\n"
" ('-s' means install on SD card instead of internal storage)\n"
" ('--algo', '--key', and '--iv' mean the file is encrypted already)\n"
" adb uninstall [-k] <package> - remove this app package from the device\n"
" ('-k' means keep the data and cache directories)\n"
" adb bugreport - return all information from the device\n"
" that should be included in a bug report.\n"
"\n"
" adb backup [-f <file>] [-apk|-noapk] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]\n"
" - write an archive of the device's data to <file>.\n"
" If no -f option is supplied then the data is written\n"
" to \"backup.ab\" in the current directory.\n"
" (-apk|-noapk enable/disable backup of the .apks themselves\n"
" in the archive; the default is noapk.)\n"
" (-shared|-noshared enable/disable backup of the device's\n"
" shared storage / SD card contents; the default is noshared.)\n"
" (-all means to back up all installed applications)\n"
" (-system|-nosystem toggles whether -all automatically includes\n"
" system applications; the default is to include system apps)\n"
" (<packages...> is the list of applications to be backed up. If\n"
" the -all or -shared flags are passed, then the package\n"
" list is optional. Applications explicitly given on the\n"
" command line will be included even if -nosystem would\n"
" ordinarily cause them to be omitted.)\n"
"\n"
" adb restore <file> - restore device contents from the <file> backup archive\n"
"\n"
" adb help - show this help message\n"
" adb version - show version num\n"
"\n"
"scripting:\n"
" adb wait-for-device - block until device is online\n"
" adb start-server - ensure that there is a server running\n"
" adb kill-server - kill the server if it is running\n"
" adb get-state - prints: offline | bootloader | device\n"
" adb get-serialno - prints: <serial-number>\n"
" adb status-window - continuously print device status for a specified device\n"
" adb remount - remounts the /system partition on the device read-write\n"
" adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program\n"
" adb reboot-bootloader - reboots the device into the bootloader\n"
" adb root - restarts the adbd daemon with root permissions\n"
" adb usb - restarts the adbd daemon listening on USB\n"
" adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port"
"\n"
"networking:\n"
" adb ppp <tty> [parameters] - Run PPP over USB.\n"
" Note: you should not automatically start a PPP connection.\n"
" <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1\n"
" [parameters] - Eg. defaultroute debug dump local notty usepeerdns\n"
"\n"
"adb sync notes: adb sync [ <directory> ]\n"
" <localdir> can be interpreted in several ways:\n"
"\n"
" - If <directory> is not specified, both /system and /data partitions will be updated.\n"
"\n"
" - If it is \"system\" or \"data\", only the corresponding partition\n"
" is updated.\n"
"\n"
"environmental variables:\n"
" ADB_TRACE - Print debug information. A comma separated list of the following values\n"
" 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp\n"
" ANDROID_SERIAL - The serial number to connect to. -s takes priority over this if given.\n"
" ANDROID_LOG_TAGS - When used with the logcat option, only these debug tags are printed.\n"
);
}
int usage()
{
help();
return 1;
}
#ifdef HAVE_TERMIO_H
static struct termios tio_save;
static void stdin_raw_init(int fd)
{
struct termios tio;
if(tcgetattr(fd, &tio)) return;
if(tcgetattr(fd, &tio_save)) return;
tio.c_lflag = 0; /* disable CANON, ECHO*, etc */
/* no timeout but request at least one character per read */
tio.c_cc[VTIME] = 0;
tio.c_cc[VMIN] = 1;
tcsetattr(fd, TCSANOW, &tio);
tcflush(fd, TCIFLUSH);
}
static void stdin_raw_restore(int fd)
{
tcsetattr(fd, TCSANOW, &tio_save);
tcflush(fd, TCIFLUSH);
}
#endif
static void read_and_dump(int fd)
{
char buf[4096];
int len;
while(fd >= 0) {
D("read_and_dump(): pre adb_read(fd=%d)\n", fd);
len = adb_read(fd, buf, 4096);
D("read_and_dump(): post adb_read(fd=%d): len=%d\n", fd, len);
if(len == 0) {
break;
}
if(len < 0) {
if(errno == EINTR) continue;
break;
}
fwrite(buf, 1, len, stdout);
fflush(stdout);
}
}
static void copy_to_file(int inFd, int outFd) {
const size_t BUFSIZE = 32 * 1024;
char* buf = (char*) malloc(BUFSIZE);
int len;
long total = 0;
D("copy_to_file(%d -> %d)\n", inFd, outFd);
for (;;) {
len = adb_read(inFd, buf, BUFSIZE);
if (len == 0) {
D("copy_to_file() : read 0 bytes; exiting\n");
break;
}
if (len < 0) {
if (errno == EINTR) {
D("copy_to_file() : EINTR, retrying\n");
continue;
}
D("copy_to_file() : error %d\n", errno);
break;
}
adb_write(outFd, buf, len);
total += len;
}
D("copy_to_file() finished after %lu bytes\n", total);
free(buf);
}
static void *stdin_read_thread(void *x)
{
int fd, fdi;
unsigned char buf[1024];
int r, n;
int state = 0;
int *fds = (int*) x;
fd = fds[0];
fdi = fds[1];
free(fds);
for(;;) {
/* fdi is really the client's stdin, so use read, not adb_read here */
D("stdin_read_thread(): pre unix_read(fdi=%d,...)\n", fdi);
r = unix_read(fdi, buf, 1024);
D("stdin_read_thread(): post unix_read(fdi=%d,...)\n", fdi);
if(r == 0) break;
if(r < 0) {
if(errno == EINTR) continue;
break;
}
for(n = 0; n < r; n++){
switch(buf[n]) {
case '\n':
state = 1;
break;
case '\r':
state = 1;
break;
case '~':
if(state == 1) state++;
break;
case '.':
if(state == 2) {
fprintf(stderr,"\n* disconnect *\n");
#ifdef HAVE_TERMIO_H
stdin_raw_restore(fdi);
#endif
exit(0);
}
default:
state = 0;
}
}
r = adb_write(fd, buf, r);
if(r <= 0) {
break;
}
}
return 0;
}
int interactive_shell(void)
{
adb_thread_t thr;
int fdi, fd;
int *fds;
fd = adb_connect("shell:");
if(fd < 0) {
fprintf(stderr,"error: %s\n", adb_error());
return 1;
}
fdi = 0; //dup(0);
fds = malloc(sizeof(int) * 2);
fds[0] = fd;
fds[1] = fdi;
#ifdef HAVE_TERMIO_H
stdin_raw_init(fdi);
#endif
adb_thread_create(&thr, stdin_read_thread, fds);
read_and_dump(fd);
#ifdef HAVE_TERMIO_H
stdin_raw_restore(fdi);
#endif
return 0;
}
static void format_host_command(char* buffer, size_t buflen, const char* command, transport_type ttype, const char* serial)
{
if (serial) {
snprintf(buffer, buflen, "host-serial:%s:%s", serial, command);
} else {
const char* prefix = "host";
if (ttype == kTransportUsb)
prefix = "host-usb";
else if (ttype == kTransportLocal)
prefix = "host-local";
snprintf(buffer, buflen, "%s:%s", prefix, command);
}
}
int adb_download_buffer(const char *service, const void* data, int sz,
unsigned progress)
{
char buf[4096];
unsigned total;
int fd;
const unsigned char *ptr;
sprintf(buf,"%s:%d", service, sz);
fd = adb_connect(buf);
if(fd < 0) {
fprintf(stderr,"error: %s\n", adb_error());
return -1;
}
int opt = CHUNK_SIZE;
opt = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));
total = sz;
ptr = data;
if(progress) {
char *x = strrchr(service, ':');
if(x) service = x + 1;
}
while(sz > 0) {
unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
if(writex(fd, ptr, xfer)) {
adb_status(fd);
fprintf(stderr,"* failed to write data '%s' *\n", adb_error());
return -1;
}
sz -= xfer;
ptr += xfer;
if(progress) {
printf("sending: '%s' %4d%% \r", service, (int)(100LL - ((100LL * sz) / (total))));
fflush(stdout);
}
}
if(progress) {
printf("\n");
}
if(readx(fd, buf, 4)){
fprintf(stderr,"* error reading response *\n");
adb_close(fd);
return -1;
}
if(memcmp(buf, "OKAY", 4)) {
buf[4] = 0;
fprintf(stderr,"* error response '%s' *\n", buf);
adb_close(fd);
return -1;
}
adb_close(fd);
return 0;
}
int adb_download(const char *service, const char *fn, unsigned progress)
{
void *data;
unsigned sz;
data = load_file(fn, &sz);
if(data == 0) {
fprintf(stderr,"* cannot read '%s' *\n", service);
return -1;
}
int status = adb_download_buffer(service, data, sz, progress);
free(data);
return status;
}
static void status_window(transport_type ttype, const char* serial)
{
char command[4096];
char *state = 0;
char *laststate = 0;
/* silence stderr */
#ifdef _WIN32
/* XXX: TODO */
#else
int fd;
fd = unix_open("/dev/null", O_WRONLY);
dup2(fd, 2);
adb_close(fd);
#endif
format_host_command(command, sizeof command, "get-state", ttype, serial);
for(;;) {
adb_sleep_ms(250);
if(state) {
free(state);
state = 0;
}
state = adb_query(command);
if(state) {
if(laststate && !strcmp(state,laststate)){
continue;
} else {
if(laststate) free(laststate);
laststate = strdup(state);
}
}
printf("%c[2J%c[2H", 27, 27);
printf("Android Debug Bridge\n");
printf("State: %s\n", state ? state : "offline");
fflush(stdout);
}
}
/** duplicate string and quote all \ " ( ) chars + space character. */
static char *
dupAndQuote(const char *s)
{
const char *ts;
size_t alloc_len;
char *ret;
char *dest;
ts = s;
alloc_len = 0;
for( ;*ts != '\0'; ts++) {
alloc_len++;
if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
alloc_len++;
}
}
ret = (char *)malloc(alloc_len + 1);
ts = s;
dest = ret;
for ( ;*ts != '\0'; ts++) {
if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
*dest++ = '\\';
}
*dest++ = *ts;
}
*dest++ = '\0';
return ret;
}
/**
* Run ppp in "notty" mode against a resource listed as the first parameter
* eg:
*
* ppp dev:/dev/omap_csmi_tty0 <ppp options>
*
*/
int ppp(int argc, char **argv)
{
#ifdef HAVE_WIN32_PROC
fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
return -1;
#else
char *adb_service_name;
pid_t pid;
int fd;
if (argc < 2) {
fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n",
argv[0]);
return 1;
}
adb_service_name = argv[1];
fd = adb_connect(adb_service_name);
if(fd < 0) {
fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n",
adb_service_name, adb_error());
return 1;
}
pid = fork();
if (pid < 0) {
perror("from fork()");
return 1;
} else if (pid == 0) {
int err;
int i;
const char **ppp_args;
// copy args
ppp_args = (const char **) alloca(sizeof(char *) * argc + 1);
ppp_args[0] = "pppd";
for (i = 2 ; i < argc ; i++) {
//argv[2] and beyond become ppp_args[1] and beyond
ppp_args[i - 1] = argv[i];
}
ppp_args[i-1] = NULL;
// child side
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
adb_close(STDERR_FILENO);
adb_close(fd);
err = execvp("pppd", (char * const *)ppp_args);
if (err < 0) {
perror("execing pppd");
}
exit(-1);
} else {
// parent side
adb_close(fd);
return 0;
}
#endif /* !HAVE_WIN32_PROC */
}
static int send_shellcommand(transport_type transport, char* serial, char* buf)
{
int fd, ret;
for(;;) {
fd = adb_connect(buf);
if(fd >= 0)
break;
fprintf(stderr,"- waiting for device -\n");
adb_sleep_ms(1000);
do_cmd(transport, serial, "wait-for-device", 0);
}
read_and_dump(fd);
ret = adb_close(fd);
if (ret)
perror("close");
return ret;
}
static int logcat(transport_type transport, char* serial, int argc, char **argv)
{
char buf[4096];
char *log_tags;
char *quoted_log_tags;
log_tags = getenv("ANDROID_LOG_TAGS");
quoted_log_tags = dupAndQuote(log_tags == NULL ? "" : log_tags);
snprintf(buf, sizeof(buf),
"shell:export ANDROID_LOG_TAGS=\"\%s\" ; exec logcat",
quoted_log_tags);
free(quoted_log_tags);
if (!strcmp(argv[0],"longcat")) {
strncat(buf, " -v long", sizeof(buf)-1);
}
argc -= 1;
argv += 1;
while(argc-- > 0) {
char *quoted;
quoted = dupAndQuote (*argv++);
strncat(buf, " ", sizeof(buf)-1);
strncat(buf, quoted, sizeof(buf)-1);
free(quoted);
}
send_shellcommand(transport, serial, buf);
return 0;
}
static int mkdirs(char *path)
{
int ret;
char *x = path + 1;
for(;;) {
x = adb_dirstart(x);
if(x == 0) return 0;
*x = 0;
ret = adb_mkdir(path, 0775);
*x = OS_PATH_SEPARATOR;
if((ret < 0) && (errno != EEXIST)) {
return ret;
}
x++;
}
return 0;
}
static int backup(int argc, char** argv) {
char buf[4096];
char default_name[32];
const char* filename = strcpy(default_name, "./backup.ab");
int fd, outFd;
int i, j;
/* find, extract, and use any -f argument */
for (i = 1; i < argc; i++) {
if (!strcmp("-f", argv[i])) {
if (i == argc-1) {
fprintf(stderr, "adb: -f passed with no filename\n");
return usage();
}
filename = argv[i+1];
for (j = i+2; j <= argc; ) {
argv[i++] = argv[j++];
}
argc -= 2;
argv[argc] = NULL;
}
}
/* bare "adb backup" or "adb backup -f filename" are not valid invocations */
if (argc < 2) return usage();
adb_unlink(filename);
mkdirs((char *)filename);
outFd = adb_creat(filename, 0640);
if (outFd < 0) {
fprintf(stderr, "adb: unable to open file %s\n", filename);
return -1;
}
snprintf(buf, sizeof(buf), "backup");
for (argc--, argv++; argc; argc--, argv++) {
strncat(buf, ":", sizeof(buf) - strlen(buf) - 1);
strncat(buf, argv[0], sizeof(buf) - strlen(buf) - 1);
}
D("backup. filename=%s buf=%s\n", filename, buf);
fd = adb_connect(buf);
if (fd < 0) {
fprintf(stderr, "adb: unable to connect for backup\n");
adb_close(outFd);
return -1;
}
printf("Now unlock your device and confirm the backup operation.\n");
copy_to_file(fd, outFd);
adb_close(fd);
adb_close(outFd);
return 0;
}
static int restore(int argc, char** argv) {
const char* filename;
int fd, tarFd;
if (argc != 2) return usage();
filename = argv[1];
tarFd = adb_open(filename, O_RDONLY);
if (tarFd < 0) {
fprintf(stderr, "adb: unable to open file %s\n", filename);
return -1;
}
fd = adb_connect("restore:");
if (fd < 0) {
fprintf(stderr, "adb: unable to connect for backup\n");
adb_close(tarFd);
return -1;
}
printf("Now unlock your device and confirm the restore operation.\n");
copy_to_file(tarFd, fd);
adb_close(fd);
adb_close(tarFd);
return 0;
}
#define SENTINEL_FILE "config" OS_PATH_SEPARATOR_STR "envsetup.make"
static int top_works(const char *top)
{
if (top != NULL && adb_is_absolute_host_path(top)) {
char path_buf[PATH_MAX];
snprintf(path_buf, sizeof(path_buf),
"%s" OS_PATH_SEPARATOR_STR SENTINEL_FILE, top);
return access(path_buf, F_OK) == 0;
}
return 0;
}
static char *find_top_from(const char *indir, char path_buf[PATH_MAX])
{
strcpy(path_buf, indir);
while (1) {
if (top_works(path_buf)) {
return path_buf;
}
char *s = adb_dirstop(path_buf);
if (s != NULL) {
*s = '\0';
} else {
path_buf[0] = '\0';
return NULL;
}
}
}
static char *find_top(char path_buf[PATH_MAX])
{
char *top = getenv("ANDROID_BUILD_TOP");
if (top != NULL && top[0] != '\0') {
if (!top_works(top)) {
fprintf(stderr, "adb: bad ANDROID_BUILD_TOP value \"%s\"\n", top);
return NULL;
}
} else {
top = getenv("TOP");
if (top != NULL && top[0] != '\0') {
if (!top_works(top)) {
fprintf(stderr, "adb: bad TOP value \"%s\"\n", top);
return NULL;
}
} else {
top = NULL;
}
}
if (top != NULL) {
/* The environment pointed to a top directory that works.
*/
strcpy(path_buf, top);
return path_buf;
}
/* The environment didn't help. Walk up the tree from the CWD
* to see if we can find the top.
*/
char dir[PATH_MAX];
top = find_top_from(getcwd(dir, sizeof(dir)), path_buf);
if (top == NULL) {
/* If the CWD isn't under a good-looking top, see if the
* executable is.
*/
get_my_path(dir, PATH_MAX);
top = find_top_from(dir, path_buf);
}
return top;
}
/* <hint> may be:
* - A simple product name
* e.g., "sooner"
TODO: debug? sooner-debug, sooner:debug?
* - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir
* e.g., "out/target/product/sooner"
* - An absolute path to the PRODUCT_OUT dir
* e.g., "/src/device/out/target/product/sooner"
*
* Given <hint>, try to construct an absolute path to the
* ANDROID_PRODUCT_OUT dir.
*/
static const char *find_product_out_path(const char *hint)
{
static char path_buf[PATH_MAX];
if (hint == NULL || hint[0] == '\0') {
return NULL;
}
/* If it's already absolute, don't bother doing any work.
*/
if (adb_is_absolute_host_path(hint)) {
strcpy(path_buf, hint);
return path_buf;
}
/* If there are any slashes in it, assume it's a relative path;
* make it absolute.
*/
if (adb_dirstart(hint) != NULL) {
if (getcwd(path_buf, sizeof(path_buf)) == NULL) {
fprintf(stderr, "adb: Couldn't get CWD: %s\n", strerror(errno));
return NULL;
}
if (strlen(path_buf) + 1 + strlen(hint) >= sizeof(path_buf)) {
fprintf(stderr, "adb: Couldn't assemble path\n");
return NULL;
}
strcat(path_buf, OS_PATH_SEPARATOR_STR);
strcat(path_buf, hint);
return path_buf;
}
/* It's a string without any slashes. Try to do something with it.
*
* Try to find the root of the build tree, and build a PRODUCT_OUT
* path from there.
*/
char top_buf[PATH_MAX];
const char *top = find_top(top_buf);
if (top == NULL) {
fprintf(stderr, "adb: Couldn't find top of build tree\n");
return NULL;
}
//TODO: if we have a way to indicate debug, look in out/debug/target/...
snprintf(path_buf, sizeof(path_buf),
"%s" OS_PATH_SEPARATOR_STR
"out" OS_PATH_SEPARATOR_STR
"target" OS_PATH_SEPARATOR_STR
"product" OS_PATH_SEPARATOR_STR
"%s", top_buf, hint);
if (access(path_buf, F_OK) < 0) {
fprintf(stderr, "adb: Couldn't find a product dir "
"based on \"-p %s\"; \"%s\" doesn't exist\n", hint, path_buf);
return NULL;
}
return path_buf;
}
int adb_commandline(int argc, char **argv)
{
char buf[4096];
int no_daemon = 0;
int is_daemon = 0;
int is_server = 0;
int persist = 0;
int r;
int quote;
transport_type ttype = kTransportAny;
char* serial = NULL;
char* server_port_str = NULL;
/* If defined, this should be an absolute path to
* the directory containing all of the various system images
* for a particular product. If not defined, and the adb
* command requires this information, then the user must
* specify the path using "-p".
*/
gProductOutPath = getenv("ANDROID_PRODUCT_OUT");
if (gProductOutPath == NULL || gProductOutPath[0] == '\0') {
gProductOutPath = NULL;
}
// TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
serial = getenv("ANDROID_SERIAL");
/* Validate and assign the server port */
server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
int server_port = DEFAULT_ADB_PORT;
if (server_port_str && strlen(server_port_str) > 0) {
server_port = (int) strtol(server_port_str, NULL, 0);
if (server_port <= 0) {
fprintf(stderr,
"adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number. Got \"%s\"\n",
server_port_str);
return usage();
}
}
/* modifiers and flags */
while(argc > 0) {
if(!strcmp(argv[0],"server")) {
is_server = 1;
} else if(!strcmp(argv[0],"nodaemon")) {
no_daemon = 1;
} else if (!strcmp(argv[0], "fork-server")) {
/* this is a special flag used only when the ADB client launches the ADB Server */
is_daemon = 1;
} else if(!strcmp(argv[0],"persist")) {
persist = 1;
} else if(!strncmp(argv[0], "-p", 2)) {
const char *product = NULL;
if (argv[0][2] == '\0') {
if (argc < 2) return usage();
product = argv[1];
argc--;
argv++;
} else {
product = argv[1] + 2;
}
gProductOutPath = find_product_out_path(product);
if (gProductOutPath == NULL) {
fprintf(stderr, "adb: could not resolve \"-p %s\"\n",
product);
return usage();
}
} else if (argv[0][0]=='-' && argv[0][1]=='s') {
if (isdigit(argv[0][2])) {
serial = argv[0] + 2;
} else {
if(argc < 2 || argv[0][2] != '\0') return usage();
serial = argv[1];
argc--;
argv++;
}
} else if (!strcmp(argv[0],"-d")) {
ttype = kTransportUsb;
} else if (!strcmp(argv[0],"-e")) {
ttype = kTransportLocal;
} else {
/* out of recognized modifiers and flags */
break;
}
argc--;
argv++;
}
adb_set_transport(ttype, serial);
adb_set_tcp_specifics(server_port);
if (is_server) {
if (no_daemon || is_daemon) {
r = adb_main(is_daemon, server_port);