├── adb.exe ├── AdbWinApi.dll ├── AdbWinUsbApi.dll ├── README └── file_sync_client.c /adb.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LunaticTian/adb/master/adb.exe -------------------------------------------------------------------------------- /AdbWinApi.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LunaticTian/adb/master/AdbWinApi.dll -------------------------------------------------------------------------------- /AdbWinUsbApi.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LunaticTian/adb/master/AdbWinUsbApi.dll -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ############################################################### 2 | this is adb-1.3.1 build for windows under ubuntu 12.04 (64) 3 | with android source code 4.2.2 4 | 5 | ### 6 | why this: 7 | windows系统下的文件(夹)命名所采用的是GBK编码,而linux是采用的UTF-8编码,使用adb的 8 | push和pull命令时由于编码方式的不同会产生错误,因此需要修改adb的源代码来支持编码转换。 9 | 10 | ### comile 11 | 1. replace file_sync_client.c with this modified one, thanks for the guy who shared it. 12 | 13 | 2. sudo apt-get install mingw32 安装linux下的windws交叉编译工具 14 | 15 | 3. in your android source code 16 | 17 | source build/envsetup.sh 18 | make USE_MINGW=y adb 19 | 20 | if no error happen, you should see this: 21 | host Executable: adb (out/host/windows-x86/obj/EXECUTABLES/adb_intermediates/adb.exe) 22 | Killing adb server so we can replace AdbWinApi.dll 23 | Install: out/host/windows-x86/bin/adb.exe 24 | 25 | 4. copy out/host/windows-x86/bin/* to your windows pc , you can enjoying it. 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /file_sync_client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2007 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 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "sysdeps.h" 30 | #include "adb.h" 31 | #include "adb_client.h" 32 | #include "file_sync_service.h" 33 | 34 | #ifdef USE_MINGW 35 | #include 36 | #endif 37 | 38 | static unsigned total_bytes; 39 | static long long start_time; 40 | 41 | static long long NOW() 42 | { 43 | struct timeval tv; 44 | gettimeofday(&tv, 0); 45 | return ((long long) tv.tv_usec) + 46 | 1000000LL * ((long long) tv.tv_sec); 47 | } 48 | 49 | static void BEGIN() 50 | { 51 | total_bytes = 0; 52 | start_time = NOW(); 53 | } 54 | 55 | static void END() 56 | { 57 | long long t = NOW() - start_time; 58 | if(total_bytes == 0) return; 59 | 60 | if (t == 0) /* prevent division by 0 :-) */ 61 | t = 1000000; 62 | 63 | fprintf(stderr,"%lld KB/s (%lld bytes in %lld.%03llds)\n", 64 | ((((long long) total_bytes) * 1000000LL) / t) / 1024LL, 65 | (long long) total_bytes, (t / 1000000LL), (t % 1000000LL) / 1000LL); 66 | } 67 | 68 | void sync_quit(int fd) 69 | { 70 | syncmsg msg; 71 | 72 | msg.req.id = ID_QUIT; 73 | msg.req.namelen = 0; 74 | 75 | writex(fd, &msg.req, sizeof(msg.req)); 76 | } 77 | 78 | typedef void (*sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char *name, void *cookie); 79 | 80 | int sync_ls(int fd, const char *path, sync_ls_cb func, void *cookie) 81 | { 82 | syncmsg msg; 83 | char buf[257]; 84 | int len; 85 | 86 | len = strlen(path); 87 | if(len > 1024) goto fail; 88 | 89 | msg.req.id = ID_LIST; 90 | msg.req.namelen = htoll(len); 91 | 92 | if(writex(fd, &msg.req, sizeof(msg.req)) || 93 | writex(fd, path, len)) { 94 | goto fail; 95 | } 96 | 97 | for(;;) { 98 | if(readx(fd, &msg.dent, sizeof(msg.dent))) break; 99 | if(msg.dent.id == ID_DONE) return 0; 100 | if(msg.dent.id != ID_DENT) break; 101 | 102 | len = ltohl(msg.dent.namelen); 103 | if(len > 256) break; 104 | 105 | if(readx(fd, buf, len)) break; 106 | buf[len] = 0; 107 | 108 | func(ltohl(msg.dent.mode), 109 | ltohl(msg.dent.size), 110 | ltohl(msg.dent.time), 111 | buf, cookie); 112 | } 113 | 114 | fail: 115 | adb_close(fd); 116 | return -1; 117 | } 118 | 119 | typedef struct syncsendbuf syncsendbuf; 120 | 121 | struct syncsendbuf { 122 | unsigned id; 123 | unsigned size; 124 | char data[SYNC_DATA_MAX]; 125 | }; 126 | 127 | static syncsendbuf send_buffer; 128 | 129 | int sync_readtime(int fd, const char *path, unsigned *timestamp) 130 | { 131 | syncmsg msg; 132 | int len = strlen(path); 133 | 134 | msg.req.id = ID_STAT; 135 | msg.req.namelen = htoll(len); 136 | 137 | if(writex(fd, &msg.req, sizeof(msg.req)) || 138 | writex(fd, path, len)) { 139 | return -1; 140 | } 141 | 142 | if(readx(fd, &msg.stat, sizeof(msg.stat))) { 143 | return -1; 144 | } 145 | 146 | if(msg.stat.id != ID_STAT) { 147 | return -1; 148 | } 149 | 150 | *timestamp = ltohl(msg.stat.time); 151 | return 0; 152 | } 153 | 154 | static int sync_start_readtime(int fd, const char *path) 155 | { 156 | syncmsg msg; 157 | int len = strlen(path); 158 | 159 | msg.req.id = ID_STAT; 160 | msg.req.namelen = htoll(len); 161 | 162 | if(writex(fd, &msg.req, sizeof(msg.req)) || 163 | writex(fd, path, len)) { 164 | return -1; 165 | } 166 | 167 | return 0; 168 | } 169 | 170 | static int sync_finish_readtime(int fd, unsigned int *timestamp, 171 | unsigned int *mode, unsigned int *size) 172 | { 173 | syncmsg msg; 174 | 175 | if(readx(fd, &msg.stat, sizeof(msg.stat))) 176 | return -1; 177 | 178 | if(msg.stat.id != ID_STAT) 179 | return -1; 180 | 181 | *timestamp = ltohl(msg.stat.time); 182 | *mode = ltohl(msg.stat.mode); 183 | *size = ltohl(msg.stat.size); 184 | 185 | return 0; 186 | } 187 | 188 | int sync_readmode(int fd, const char *path, unsigned *mode) 189 | { 190 | syncmsg msg; 191 | int len = strlen(path); 192 | 193 | msg.req.id = ID_STAT; 194 | msg.req.namelen = htoll(len); 195 | 196 | if(writex(fd, &msg.req, sizeof(msg.req)) || 197 | writex(fd, path, len)) { 198 | return -1; 199 | } 200 | 201 | if(readx(fd, &msg.stat, sizeof(msg.stat))) { 202 | return -1; 203 | } 204 | 205 | if(msg.stat.id != ID_STAT) { 206 | return -1; 207 | } 208 | 209 | *mode = ltohl(msg.stat.mode); 210 | return 0; 211 | } 212 | 213 | static int write_data_file(int fd, const char *path, syncsendbuf *sbuf) 214 | { 215 | int lfd, err = 0; 216 | 217 | lfd = adb_open(path, O_RDONLY); 218 | if(lfd < 0) { 219 | fprintf(stderr,"cannot open '%s': %s\n", path, strerror(errno)); 220 | return -1; 221 | } 222 | 223 | sbuf->id = ID_DATA; 224 | for(;;) { 225 | int ret; 226 | 227 | ret = adb_read(lfd, sbuf->data, SYNC_DATA_MAX); 228 | if(!ret) 229 | break; 230 | 231 | if(ret < 0) { 232 | if(errno == EINTR) 233 | continue; 234 | fprintf(stderr,"cannot read '%s': %s\n", path, strerror(errno)); 235 | break; 236 | } 237 | 238 | sbuf->size = htoll(ret); 239 | if(writex(fd, sbuf, sizeof(unsigned) * 2 + ret)){ 240 | err = -1; 241 | break; 242 | } 243 | total_bytes += ret; 244 | } 245 | 246 | adb_close(lfd); 247 | return err; 248 | } 249 | 250 | static int write_data_buffer(int fd, char* file_buffer, int size, syncsendbuf *sbuf) 251 | { 252 | int err = 0; 253 | int total = 0; 254 | 255 | sbuf->id = ID_DATA; 256 | while (total < size) { 257 | int count = size - total; 258 | if (count > SYNC_DATA_MAX) { 259 | count = SYNC_DATA_MAX; 260 | } 261 | 262 | memcpy(sbuf->data, &file_buffer[total], count); 263 | sbuf->size = htoll(count); 264 | if(writex(fd, sbuf, sizeof(unsigned) * 2 + count)){ 265 | err = -1; 266 | break; 267 | } 268 | total += count; 269 | total_bytes += count; 270 | } 271 | 272 | return err; 273 | } 274 | 275 | #ifdef HAVE_SYMLINKS 276 | static int write_data_link(int fd, const char *path, syncsendbuf *sbuf) 277 | { 278 | int len, ret; 279 | 280 | len = readlink(path, sbuf->data, SYNC_DATA_MAX-1); 281 | if(len < 0) { 282 | fprintf(stderr, "error reading link '%s': %s\n", path, strerror(errno)); 283 | return -1; 284 | } 285 | sbuf->data[len] = '\0'; 286 | 287 | sbuf->size = htoll(len + 1); 288 | sbuf->id = ID_DATA; 289 | 290 | ret = writex(fd, sbuf, sizeof(unsigned) * 2 + len + 1); 291 | if(ret) 292 | return -1; 293 | 294 | total_bytes += len + 1; 295 | 296 | return 0; 297 | } 298 | #endif 299 | 300 | static int GBKToUTF8(char *lpGBKStr, char *lpUTF8Str, int nUTF8StrLen) 301 | { 302 | wchar_t *lpUnicodeStr = NULL; 303 | int nRetLen = 0; 304 | 305 | if (!lpGBKStr) return 0; 306 | 307 | nRetLen = MultiByteToWideChar(CP_ACP, 0, (char *)lpGBKStr, -1, NULL, 0); 308 | lpUnicodeStr = (wchar_t *)malloc(sizeof(WCHAR) * (nRetLen + 1)); 309 | nRetLen = MultiByteToWideChar(CP_ACP, 0, (char *)lpGBKStr, -1, lpUnicodeStr, nRetLen); 310 | 311 | if (!nRetLen) return 0; 312 | 313 | nRetLen = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, NULL, 0, NULL, NULL); 314 | if (!lpUTF8Str) 315 | { 316 | if (lpUnicodeStr) free(lpUnicodeStr); 317 | return nRetLen; 318 | } 319 | 320 | if (nUTF8StrLen < nRetLen) 321 | { 322 | if (lpUnicodeStr) free(lpUnicodeStr); 323 | return 0; 324 | } 325 | 326 | nRetLen = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, (char *)lpUTF8Str,nUTF8StrLen, NULL, NULL); 327 | 328 | if (lpUnicodeStr) free(lpUnicodeStr); 329 | 330 | return nRetLen; 331 | } 332 | 333 | static int UTF8ToGBK(char *lpGBKStr,char *lpUTF8Str, int nGBKStrLen) 334 | { 335 | wchar_t *lpUnicodeStr=NULL; 336 | int nRetLen =0; 337 | 338 | if (!lpUTF8Str)return 0; 339 | 340 | nRetLen=MultiByteToWideChar(CP_UTF8,0,(char*)lpUTF8Str,-1,NULL,0); 341 | lpUnicodeStr = (wchar_t *)malloc(sizeof(WCHAR) * (nRetLen + 1)); 342 | nRetLen = MultiByteToWideChar(CP_UTF8, 0, (char *)lpUTF8Str, -1, lpUnicodeStr, nRetLen); 343 | 344 | if (!nRetLen)return 0; 345 | nRetLen = WideCharToMultiByte(CP_UTF8, 0, lpUnicodeStr, -1, NULL, 0, NULL, NULL); 346 | 347 | if (!lpGBKStr) 348 | { 349 | if (lpUnicodeStr) free(lpUnicodeStr); 350 | return nRetLen; 351 | } 352 | 353 | if (nGBKStrLen < nRetLen) 354 | { 355 | if (lpUnicodeStr) free(lpUnicodeStr); 356 | return 0; 357 | } 358 | 359 | nRetLen = WideCharToMultiByte(CP_ACP, 0, lpUnicodeStr, -1, (char *)lpGBKStr,nGBKStrLen, NULL, NULL); 360 | 361 | if (lpUnicodeStr) free(lpUnicodeStr); 362 | 363 | return nRetLen; 364 | } 365 | 366 | static int sync_send(int fd, const char *lpath, const char *rpath, 367 | unsigned mtime, mode_t mode, int verifyApk) 368 | { 369 | syncmsg msg; 370 | int len, r; 371 | syncsendbuf *sbuf = &send_buffer; 372 | char* file_buffer = NULL; 373 | int size = 0; 374 | char tmp[64]; 375 | 376 | len = strlen(rpath); 377 | if(len > 1024) goto fail; 378 | 379 | snprintf(tmp, sizeof(tmp), ",%d", mode); 380 | r = strlen(tmp); 381 | 382 | if (verifyApk) { 383 | int lfd; 384 | zipfile_t zip; 385 | zipentry_t entry; 386 | int amt; 387 | 388 | // if we are transferring an APK file, then sanity check to make sure 389 | // we have a real zip file that contains an AndroidManifest.xml 390 | // this requires that we read the entire file into memory. 391 | lfd = adb_open(lpath, O_RDONLY); 392 | if(lfd < 0) { 393 | fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno)); 394 | return -1; 395 | } 396 | 397 | size = adb_lseek(lfd, 0, SEEK_END); 398 | if (size == -1 || -1 == adb_lseek(lfd, 0, SEEK_SET)) { 399 | fprintf(stderr, "error seeking in file '%s'\n", lpath); 400 | adb_close(lfd); 401 | return 1; 402 | } 403 | 404 | file_buffer = (char *)malloc(size); 405 | if (file_buffer == NULL) { 406 | fprintf(stderr, "could not allocate buffer for '%s'\n", 407 | lpath); 408 | adb_close(lfd); 409 | return 1; 410 | } 411 | amt = adb_read(lfd, file_buffer, size); 412 | if (amt != size) { 413 | fprintf(stderr, "error reading from file: '%s'\n", lpath); 414 | adb_close(lfd); 415 | free(file_buffer); 416 | return 1; 417 | } 418 | 419 | adb_close(lfd); 420 | 421 | zip = init_zipfile(file_buffer, size); 422 | if (zip == NULL) { 423 | fprintf(stderr, "file '%s' is not a valid zip file\n", 424 | lpath); 425 | free(file_buffer); 426 | return 1; 427 | } 428 | 429 | entry = lookup_zipentry(zip, "AndroidManifest.xml"); 430 | release_zipfile(zip); 431 | if (entry == NULL) { 432 | fprintf(stderr, "file '%s' does not contain AndroidManifest.xml\n", 433 | lpath); 434 | free(file_buffer); 435 | return 1; 436 | } 437 | } 438 | 439 | msg.req.id = ID_SEND; 440 | msg.req.namelen = htoll(len + r); 441 | 442 | if(writex(fd, &msg.req, sizeof(msg.req)) || 443 | writex(fd, rpath, len) || writex(fd, tmp, r)) { 444 | free(file_buffer); 445 | goto fail; 446 | } 447 | 448 | if (file_buffer) { 449 | write_data_buffer(fd, file_buffer, size, sbuf); 450 | free(file_buffer); 451 | } else if (S_ISREG(mode)) 452 | write_data_file(fd, lpath, sbuf); 453 | #ifdef HAVE_SYMLINKS 454 | else if (S_ISLNK(mode)) 455 | write_data_link(fd, lpath, sbuf); 456 | #endif 457 | else 458 | goto fail; 459 | 460 | msg.data.id = ID_DONE; 461 | msg.data.size = htoll(mtime); 462 | if(writex(fd, &msg.data, sizeof(msg.data))) 463 | goto fail; 464 | 465 | if(readx(fd, &msg.status, sizeof(msg.status))) 466 | return -1; 467 | 468 | if(msg.status.id != ID_OKAY) { 469 | if(msg.status.id == ID_FAIL) { 470 | len = ltohl(msg.status.msglen); 471 | if(len > 256) len = 256; 472 | if(readx(fd, sbuf->data, len)) { 473 | return -1; 474 | } 475 | sbuf->data[len] = 0; 476 | } else 477 | strcpy(sbuf->data, "unknown reason"); 478 | 479 | fprintf(stderr,"failed to copy '%s' to '%s': %s\n", lpath, rpath, sbuf->data); 480 | return -1; 481 | } 482 | 483 | return 0; 484 | 485 | fail: 486 | fprintf(stderr,"protocol failure\n"); 487 | adb_close(fd); 488 | return -1; 489 | } 490 | 491 | static int mkdirs(char *name) 492 | { 493 | int ret; 494 | char *x = name + 1; 495 | 496 | for(;;) { 497 | x = adb_dirstart(x); 498 | if(x == 0) return 0; 499 | *x = 0; 500 | ret = adb_mkdir(name, 0775); 501 | *x = OS_PATH_SEPARATOR; 502 | if((ret < 0) && (errno != EEXIST)) { 503 | return ret; 504 | } 505 | x++; 506 | } 507 | return 0; 508 | } 509 | 510 | int sync_recv(int fd, const char *rpath, const char *lpath) 511 | { 512 | syncmsg msg; 513 | int len; 514 | int lfd = -1; 515 | char *buffer = send_buffer.data; 516 | unsigned id; 517 | 518 | len = strlen(rpath); 519 | if(len > 1024) return -1; 520 | 521 | msg.req.id = ID_RECV; 522 | msg.req.namelen = htoll(len); 523 | if(writex(fd, &msg.req, sizeof(msg.req)) || 524 | writex(fd, rpath, len)) { 525 | return -1; 526 | } 527 | 528 | if(readx(fd, &msg.data, sizeof(msg.data))) { 529 | return -1; 530 | } 531 | id = msg.data.id; 532 | 533 | if((id == ID_DATA) || (id == ID_DONE)) { 534 | adb_unlink(lpath); 535 | mkdirs((char *)lpath); 536 | lfd = adb_creat(lpath, 0644); 537 | if(lfd < 0) { 538 | fprintf(stderr,"cannot create '%s': %s\n", lpath, strerror(errno)); 539 | return -1; 540 | } 541 | goto handle_data; 542 | } else { 543 | goto remote_error; 544 | } 545 | 546 | for(;;) { 547 | if(readx(fd, &msg.data, sizeof(msg.data))) { 548 | return -1; 549 | } 550 | id = msg.data.id; 551 | 552 | handle_data: 553 | len = ltohl(msg.data.size); 554 | if(id == ID_DONE) break; 555 | if(id != ID_DATA) goto remote_error; 556 | if(len > SYNC_DATA_MAX) { 557 | fprintf(stderr,"data overrun\n"); 558 | adb_close(lfd); 559 | return -1; 560 | } 561 | 562 | if(readx(fd, buffer, len)) { 563 | adb_close(lfd); 564 | return -1; 565 | } 566 | 567 | if(writex(lfd, buffer, len)) { 568 | fprintf(stderr,"cannot write '%s': %s\n", rpath, strerror(errno)); 569 | adb_close(lfd); 570 | return -1; 571 | } 572 | 573 | total_bytes += len; 574 | } 575 | 576 | adb_close(lfd); 577 | return 0; 578 | 579 | remote_error: 580 | adb_close(lfd); 581 | adb_unlink(lpath); 582 | 583 | if(id == ID_FAIL) { 584 | len = ltohl(msg.data.size); 585 | if(len > 256) len = 256; 586 | if(readx(fd, buffer, len)) { 587 | return -1; 588 | } 589 | buffer[len] = 0; 590 | } else { 591 | memcpy(buffer, &id, 4); 592 | buffer[4] = 0; 593 | // strcpy(buffer,"unknown reason"); 594 | } 595 | fprintf(stderr,"failed to copy '%s' to '%s': %s\n", rpath, lpath, buffer); 596 | return 0; 597 | } 598 | 599 | 600 | 601 | /* --- */ 602 | 603 | 604 | static void do_sync_ls_cb(unsigned mode, unsigned size, unsigned time, 605 | const char *name, void *cookie) 606 | { 607 | printf("%08x %08x %08x %s\n", mode, size, time, name); 608 | } 609 | 610 | int do_sync_ls(const char *path) 611 | { 612 | int fd = adb_connect("sync:"); 613 | if(fd < 0) { 614 | fprintf(stderr,"error: %s\n", adb_error()); 615 | return 1; 616 | } 617 | 618 | if(sync_ls(fd, path, do_sync_ls_cb, 0)) { 619 | return 1; 620 | } else { 621 | sync_quit(fd); 622 | return 0; 623 | } 624 | } 625 | 626 | typedef struct copyinfo copyinfo; 627 | 628 | struct copyinfo 629 | { 630 | copyinfo *next; 631 | const char *src; 632 | const char *dst; 633 | unsigned int time; 634 | unsigned int mode; 635 | unsigned int size; 636 | int flag; 637 | //char data[0]; 638 | }; 639 | copyinfo *mkcopyinfo(const char *spath, const char *dpath, 640 | const char *sname, const char *dname,int isdir) 641 | { 642 | int slen = strlen(spath); 643 | int dlen = strlen(dpath); 644 | int snlen = strlen(sname); 645 | int dnlen = strlen(dname); 646 | int ssize = slen + snlen + 2; 647 | int dsize = dlen + dnlen + 2; 648 | 649 | 650 | 651 | copyinfo *ci = malloc(sizeof(copyinfo) + ssize + dsize); 652 | if(ci == 0) { 653 | fprintf(stderr,"out of memory\n"); 654 | abort(); 655 | } 656 | 657 | ci->next = 0; 658 | ci->time = 0; 659 | ci->mode = 0; 660 | ci->size = 0; 661 | ci->flag = 0; 662 | ci->src = (const char*)(ci + 1); 663 | ci->dst = ci->src + ssize; 664 | snprintf((char*) ci->src, ssize, isdir ? "%s%s/" : "%s%s", spath, sname); 665 | snprintf((char*) ci->dst, dsize, isdir ? "%s%s/" : "%s%s", dpath, dname); 666 | 667 | // fprintf(stderr,"mkcopyinfo('%s','%s')\n", ci->src, ci->dst); 668 | return ci; 669 | } 670 | 671 | 672 | 673 | static int local_build_list(copyinfo **filelist, 674 | const char *lpath, const char *rpath) 675 | { 676 | DIR *d; 677 | struct dirent *de; 678 | struct stat st; 679 | copyinfo *dirlist = 0; 680 | copyinfo *ci, *next; 681 | 682 | // fprintf(stderr,"local_build_list('%s','%s')\n", lpath, rpath); 683 | 684 | d = opendir(lpath); 685 | if(d == 0) { 686 | fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno)); 687 | return -1; 688 | } 689 | 690 | while((de = readdir(d))) { 691 | char stat_path[PATH_MAX]; 692 | char *name = de->d_name; 693 | 694 | if(name[0] == '.') { 695 | if(name[1] == 0) continue; 696 | if((name[1] == '.') && (name[2] == 0)) continue; 697 | } 698 | 699 | #ifdef USE_MINGW 700 | char utf8name[260]; 701 | int name_len=GBKToUTF8(name,NULL,0); 702 | name_len=GBKToUTF8(name,utf8name,name_len); 703 | #endif 704 | 705 | /* 706 | * We could use d_type if HAVE_DIRENT_D_TYPE is defined, but reiserfs 707 | * always returns DT_UNKNOWN, so we just use stat() for all cases. 708 | */ 709 | if (strlen(lpath) + strlen(de->d_name) + 1 > sizeof(stat_path)) 710 | continue; 711 | strcpy(stat_path, lpath); 712 | strcat(stat_path, de->d_name); 713 | stat(stat_path, &st); 714 | 715 | if (S_ISDIR(st.st_mode)) { 716 | ci = mkcopyinfo(lpath, rpath, name,name, 1); 717 | #ifdef USE_MINGW 718 | ci = mkcopyinfo(lpath,rpath,name,utf8name,1); 719 | #endif 720 | ci->next = dirlist; 721 | dirlist = ci; 722 | } else { 723 | ci = mkcopyinfo(lpath, rpath, name, name,0); 724 | #ifdef USE_MINGW 725 | ci = mkcopyinfo(lpath,rpath,name,utf8name,0); 726 | #endif 727 | if(lstat(ci->src, &st)) { 728 | fprintf(stderr,"cannot stat '%s': %s\n", ci->src, strerror(errno)); 729 | closedir(d); 730 | 731 | return -1; 732 | } 733 | if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) { 734 | fprintf(stderr, "skipping special file '%s'\n", ci->src); 735 | free(ci); 736 | } else { 737 | ci->time = st.st_mtime; 738 | ci->mode = st.st_mode; 739 | ci->size = st.st_size; 740 | ci->next = *filelist; 741 | *filelist = ci; 742 | } 743 | } 744 | } 745 | 746 | closedir(d); 747 | 748 | for(ci = dirlist; ci != 0; ci = next) { 749 | next = ci->next; 750 | local_build_list(filelist, ci->src, ci->dst); 751 | free(ci); 752 | } 753 | 754 | return 0; 755 | } 756 | 757 | 758 | static int copy_local_dir_remote(int fd, const char *lpath, const char *rpath, int checktimestamps, int listonly) 759 | { 760 | copyinfo *filelist = 0; 761 | copyinfo *ci, *next; 762 | int pushed = 0; 763 | int skipped = 0; 764 | 765 | if((lpath[0] == 0) || (rpath[0] == 0)) return -1; 766 | if(lpath[strlen(lpath) - 1] != '/') { 767 | int tmplen = strlen(lpath)+2; 768 | char *tmp = malloc(tmplen); 769 | if(tmp == 0) return -1; 770 | snprintf(tmp, tmplen, "%s/",lpath); 771 | lpath = tmp; 772 | } 773 | if(rpath[strlen(rpath) - 1] != '/') { 774 | int tmplen = strlen(rpath)+2; 775 | char *tmp = malloc(tmplen); 776 | if(tmp == 0) return -1; 777 | snprintf(tmp, tmplen, "%s/",rpath); 778 | rpath = tmp; 779 | } 780 | 781 | if(local_build_list(&filelist, lpath, rpath)) { 782 | return -1; 783 | } 784 | 785 | if(checktimestamps){ 786 | for(ci = filelist; ci != 0; ci = ci->next) { 787 | if(sync_start_readtime(fd, ci->dst)) { 788 | return 1; 789 | } 790 | } 791 | for(ci = filelist; ci != 0; ci = ci->next) { 792 | unsigned int timestamp, mode, size; 793 | if(sync_finish_readtime(fd, ×tamp, &mode, &size)) 794 | return 1; 795 | if(size == ci->size) { 796 | /* for links, we cannot update the atime/mtime */ 797 | if((S_ISREG(ci->mode & mode) && timestamp == ci->time) || 798 | (S_ISLNK(ci->mode & mode) && timestamp >= ci->time)) 799 | ci->flag = 1; 800 | } 801 | } 802 | } 803 | for(ci = filelist; ci != 0; ci = next) { 804 | next = ci->next; 805 | if(ci->flag == 0) { 806 | fprintf(stderr,"%spush: %s -> %s\n", listonly ? "would " : "", ci->src, ci->dst); 807 | if(!listonly && 808 | sync_send(fd, ci->src, ci->dst, ci->time, ci->mode, 0 /* no verify APK */)){ 809 | return 1; 810 | } 811 | pushed++; 812 | } else { 813 | skipped++; 814 | } 815 | free(ci); 816 | } 817 | 818 | fprintf(stderr,"%d file%s pushed. %d file%s skipped.\n", 819 | pushed, (pushed == 1) ? "" : "s", 820 | skipped, (skipped == 1) ? "" : "s"); 821 | 822 | return 0; 823 | } 824 | 825 | 826 | int do_sync_push(const char *lpath, const char *rpath, int verifyApk) 827 | { 828 | struct stat st; 829 | unsigned mode; 830 | int fd; 831 | 832 | fd = adb_connect("sync:"); 833 | if(fd < 0) { 834 | fprintf(stderr,"error: %s\n", adb_error()); 835 | return 1; 836 | } 837 | 838 | if(stat(lpath, &st)) { 839 | fprintf(stderr,"cannot stat '%s': %s\n", lpath, strerror(errno)); 840 | sync_quit(fd); 841 | return 1; 842 | } 843 | 844 | if(S_ISDIR(st.st_mode)) { 845 | BEGIN(); 846 | if(copy_local_dir_remote(fd, lpath, rpath, 0, 0)) { 847 | return 1; 848 | } else { 849 | END(); 850 | sync_quit(fd); 851 | } 852 | } else { 853 | if(sync_readmode(fd, rpath, &mode)) { 854 | return 1; 855 | } 856 | if((mode != 0) && S_ISDIR(mode)) { 857 | /* if we're copying a local file to a remote directory, 858 | ** we *really* want to copy to remotedir + "/" + localfilename 859 | */ 860 | const char *name = adb_dirstop(lpath); 861 | if(name == 0) { 862 | name = lpath; 863 | } else { 864 | name++; 865 | } 866 | 867 | int tmplen = strlen(name) + strlen(rpath) + 2; 868 | 869 | #ifdef USE_MINGW 870 | char utf8name[260]; 871 | int name_len=GBKToUTF8(name,NULL,0); 872 | name_len=GBKToUTF8(name,utf8name,name_len); 873 | tmplen=strlen(utf8name)+strlen(rpath)+2; 874 | #endif 875 | 876 | char *tmp = malloc(tmplen); 877 | if(tmp == 0) return 1; 878 | snprintf(tmp, tmplen, "%s/%s", rpath, name); 879 | 880 | #ifdef USE_MINGW 881 | snprintf(tmp, tmplen, "%s/%s", rpath, utf8name); 882 | #endif 883 | 884 | rpath = tmp; 885 | } 886 | BEGIN(); 887 | if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, verifyApk)) { 888 | 889 | return 1; 890 | } else { 891 | END(); 892 | sync_quit(fd); 893 | return 0; 894 | } 895 | } 896 | 897 | return 0; 898 | } 899 | 900 | 901 | typedef struct { 902 | copyinfo **filelist; 903 | copyinfo **dirlist; 904 | const char *rpath; 905 | const char *lpath; 906 | } sync_ls_build_list_cb_args; 907 | 908 | void 909 | sync_ls_build_list_cb(unsigned mode, unsigned size, unsigned time, 910 | const char *name, void *cookie) 911 | { 912 | sync_ls_build_list_cb_args *args = (sync_ls_build_list_cb_args *)cookie; 913 | copyinfo *ci; 914 | 915 | #ifdef USE_MINGW 916 | char gbk_name[260]; 917 | int name_len=UTF8ToGBK(NULL,name,0); 918 | name_len=UTF8ToGBK(gbk_name,name,name_len); 919 | #endif 920 | 921 | if (S_ISDIR(mode)) { 922 | copyinfo **dirlist = args->dirlist; 923 | 924 | /* Don't try recursing down "." or ".." */ 925 | if (name[0] == '.') { 926 | if (name[1] == '\0') return; 927 | if ((name[1] == '.') && (name[2] == '\0')) return; 928 | } 929 | 930 | ci = mkcopyinfo(args->rpath, args->lpath, name, name,1); 931 | 932 | #ifdef USE_MINGW 933 | ci = mkcopyinfo(args->rpath, args->lpath, name, gbk_name,1); 934 | #endif 935 | 936 | ci->next = *dirlist; 937 | *dirlist = ci; 938 | } else if (S_ISREG(mode) || S_ISLNK(mode)) { 939 | copyinfo **filelist = args->filelist; 940 | 941 | ci = mkcopyinfo(args->rpath, args->lpath, name,name, 0); 942 | 943 | #ifdef USE_MINGW 944 | ci = mkcopyinfo(args->rpath, args->lpath, name, gbk_name,0); 945 | #endif 946 | ci->time = time; 947 | ci->mode = mode; 948 | ci->size = size; 949 | ci->next = *filelist; 950 | *filelist = ci; 951 | } else { 952 | fprintf(stderr, "skipping special file '%s'\n", name); 953 | } 954 | } 955 | 956 | static int remote_build_list(int syncfd, copyinfo **filelist, 957 | const char *rpath, const char *lpath) 958 | { 959 | copyinfo *dirlist = NULL; 960 | sync_ls_build_list_cb_args args; 961 | 962 | args.filelist = filelist; 963 | args.dirlist = &dirlist; 964 | args.rpath = rpath; 965 | args.lpath = lpath; 966 | 967 | /* Put the files/dirs in rpath on the lists. */ 968 | if (sync_ls(syncfd, rpath, sync_ls_build_list_cb, (void *)&args)) { 969 | return 1; 970 | } 971 | 972 | /* Recurse into each directory we found. */ 973 | while (dirlist != NULL) { 974 | copyinfo *next = dirlist->next; 975 | if (remote_build_list(syncfd, filelist, dirlist->src, dirlist->dst)) { 976 | return 1; 977 | } 978 | free(dirlist); 979 | dirlist = next; 980 | } 981 | 982 | return 0; 983 | } 984 | 985 | static int copy_remote_dir_local(int fd, const char *rpath, const char *lpath, 986 | int checktimestamps) 987 | { 988 | copyinfo *filelist = 0; 989 | copyinfo *ci, *next; 990 | int pulled = 0; 991 | int skipped = 0; 992 | 993 | /* Make sure that both directory paths end in a slash. */ 994 | if (rpath[0] == 0 || lpath[0] == 0) return -1; 995 | if (rpath[strlen(rpath) - 1] != '/') { 996 | int tmplen = strlen(rpath) + 2; 997 | char *tmp = malloc(tmplen); 998 | if (tmp == 0) return -1; 999 | snprintf(tmp, tmplen, "%s/", rpath); 1000 | rpath = tmp; 1001 | } 1002 | if (lpath[strlen(lpath) - 1] != '/') { 1003 | int tmplen = strlen(lpath) + 2; 1004 | char *tmp = malloc(tmplen); 1005 | if (tmp == 0) return -1; 1006 | snprintf(tmp, tmplen, "%s/", lpath); 1007 | lpath = tmp; 1008 | } 1009 | 1010 | fprintf(stderr, "pull: building file list...\n"); 1011 | /* Recursively build the list of files to copy. */ 1012 | if (remote_build_list(fd, &filelist, rpath, lpath)) { 1013 | return -1; 1014 | } 1015 | 1016 | #if 0 1017 | if (checktimestamps) { 1018 | for (ci = filelist; ci != 0; ci = ci->next) { 1019 | if (sync_start_readtime(fd, ci->dst)) { 1020 | return 1; 1021 | } 1022 | } 1023 | for (ci = filelist; ci != 0; ci = ci->next) { 1024 | unsigned int timestamp, mode, size; 1025 | if (sync_finish_readtime(fd, ×tamp, &mode, &size)) 1026 | return 1; 1027 | if (size == ci->size) { 1028 | /* for links, we cannot update the atime/mtime */ 1029 | if ((S_ISREG(ci->mode & mode) && timestamp == ci->time) || 1030 | (S_ISLNK(ci->mode & mode) && timestamp >= ci->time)) 1031 | ci->flag = 1; 1032 | } 1033 | } 1034 | } 1035 | #endif 1036 | for (ci = filelist; ci != 0; ci = next) { 1037 | next = ci->next; 1038 | if (ci->flag == 0) { 1039 | fprintf(stderr, "pull: %s -> %s\n", ci->src, ci->dst); 1040 | if (sync_recv(fd, ci->src, ci->dst)) { 1041 | return 1; 1042 | } 1043 | pulled++; 1044 | } else { 1045 | skipped++; 1046 | } 1047 | free(ci); 1048 | } 1049 | 1050 | fprintf(stderr, "%d file%s pulled. %d file%s skipped.\n", 1051 | pulled, (pulled == 1) ? "" : "s", 1052 | skipped, (skipped == 1) ? "" : "s"); 1053 | 1054 | return 0; 1055 | } 1056 | 1057 | int do_sync_pull(const char *rpath, const char *lpath) 1058 | { 1059 | unsigned mode; 1060 | struct stat st; 1061 | 1062 | int fd; 1063 | 1064 | fd = adb_connect("sync:"); 1065 | if(fd < 0) { 1066 | fprintf(stderr,"error: %s\n", adb_error()); 1067 | return 1; 1068 | } 1069 | 1070 | if(sync_readmode(fd, rpath, &mode)) { 1071 | return 1; 1072 | } 1073 | if(mode == 0) { 1074 | fprintf(stderr,"remote object '%s' does not exist\n", rpath); 1075 | return 1; 1076 | } 1077 | 1078 | if(S_ISREG(mode) || S_ISLNK(mode) || S_ISCHR(mode) || S_ISBLK(mode)) { 1079 | if(stat(lpath, &st) == 0) { 1080 | if(S_ISDIR(st.st_mode)) { 1081 | /* if we're copying a remote file to a local directory, 1082 | ** we *really* want to copy to localdir + "/" + remotefilename 1083 | */ 1084 | const char *name = adb_dirstop(rpath); 1085 | if(name == 0) { 1086 | name = rpath; 1087 | } else { 1088 | name++; 1089 | } 1090 | 1091 | 1092 | int tmplen = strlen(name) + strlen(lpath) + 2; 1093 | #ifdef USE_MINGW 1094 | char gbk_name[260]; 1095 | int name_len=UTF8ToGBK(NULL,name,0); 1096 | name_len=UTF8ToGBK(gbk_name,name,name_len); 1097 | tmplen=strlen(gbk_name)+strlen(lpath)+2; 1098 | #endif 1099 | char *tmp = malloc(tmplen); 1100 | if(tmp == 0) return 1; 1101 | snprintf(tmp, tmplen, "%s/%s", lpath, name); 1102 | 1103 | #ifdef USE_MINGW 1104 | snprintf(tmp, tmplen, "%s/%s", lpath, gbk_name); 1105 | #endif 1106 | 1107 | lpath = tmp; 1108 | } 1109 | } 1110 | BEGIN(); 1111 | if(sync_recv(fd, rpath, lpath)) { 1112 | return 1; 1113 | } else { 1114 | END(); 1115 | sync_quit(fd); 1116 | return 0; 1117 | } 1118 | } else if(S_ISDIR(mode)) { 1119 | BEGIN(); 1120 | if (copy_remote_dir_local(fd, rpath, lpath, 0)) { 1121 | return 1; 1122 | } else { 1123 | END(); 1124 | sync_quit(fd); 1125 | return 0; 1126 | } 1127 | } else { 1128 | fprintf(stderr,"remote object '%s' not a file or directory\n", rpath); 1129 | return 1; 1130 | } 1131 | } 1132 | 1133 | int do_sync_sync(const char *lpath, const char *rpath, int listonly) 1134 | { 1135 | fprintf(stderr,"syncing %s...\n",rpath); 1136 | 1137 | int fd = adb_connect("sync:"); 1138 | if(fd < 0) { 1139 | fprintf(stderr,"error: %s\n", adb_error()); 1140 | return 1; 1141 | } 1142 | 1143 | BEGIN(); 1144 | if(copy_local_dir_remote(fd, lpath, rpath, 1, listonly)){ 1145 | return 1; 1146 | } else { 1147 | END(); 1148 | sync_quit(fd); 1149 | return 0; 1150 | } 1151 | } 1152 | --------------------------------------------------------------------------------