file_sync_client.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <sys/stat.h>
  21. #include <sys/time.h>
  22. #include <time.h>
  23. #include <dirent.h>
  24. #include <limits.h>
  25. #include <sys/types.h>
  26. #include <zipfile/zipfile.h>
  27. #include "sysdeps.h"
  28. #include "adb.h"
  29. #include "adb_client.h"
  30. #include "file_sync_service.h"
  31. static unsigned total_bytes;
  32. static long long start_time;
  33. static long long NOW()
  34. {
  35. struct timeval tv;
  36. gettimeofday(&tv, 0);
  37. return ((long long) tv.tv_usec) +
  38. 1000000LL * ((long long) tv.tv_sec);
  39. }
  40. static void BEGIN()
  41. {
  42. total_bytes = 0;
  43. start_time = NOW();
  44. }
  45. static void END()
  46. {
  47. long long t = NOW() - start_time;
  48. if(total_bytes == 0) return;
  49. if (t == 0) /* prevent division by 0 :-) */
  50. t = 1000000;
  51. fprintf(stderr,"%lld KB/s (%lld bytes in %lld.%03llds)\n",
  52. ((((long long) total_bytes) * 1000000LL) / t) / 1024LL,
  53. (long long) total_bytes, (t / 1000000LL), (t % 1000000LL) / 1000LL);
  54. }
  55. void sync_quit(int fd)
  56. {
  57. syncmsg msg;
  58. msg.req.id = ID_QUIT;
  59. msg.req.namelen = 0;
  60. writex(fd, &msg.req, sizeof(msg.req));
  61. }
  62. typedef void (*sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char *name, void *cookie);
  63. int sync_ls(int fd, const char *path, sync_ls_cb func, void *cookie)
  64. {
  65. syncmsg msg;
  66. char buf[257];
  67. int len;
  68. len = strlen(path);
  69. if(len > 1024) goto fail;
  70. msg.req.id = ID_LIST;
  71. msg.req.namelen = htoll(len);
  72. if(writex(fd, &msg.req, sizeof(msg.req)) ||
  73. writex(fd, path, len)) {
  74. goto fail;
  75. }
  76. for(;;) {
  77. if(readx(fd, &msg.dent, sizeof(msg.dent))) break;
  78. if(msg.dent.id == ID_DONE) return 0;
  79. if(msg.dent.id != ID_DENT) break;
  80. len = ltohl(msg.dent.namelen);
  81. if(len > 256) break;
  82. if(readx(fd, buf, len)) break;
  83. buf[len] = 0;
  84. func(ltohl(msg.dent.mode),
  85. ltohl(msg.dent.size),
  86. ltohl(msg.dent.time),
  87. buf, cookie);
  88. }
  89. fail:
  90. adb_close(fd);
  91. return -1;
  92. }
  93. typedef struct syncsendbuf syncsendbuf;
  94. struct syncsendbuf {
  95. unsigned id;
  96. unsigned size;
  97. char data[SYNC_DATA_MAX];
  98. };
  99. static syncsendbuf send_buffer;
  100. int sync_readtime(int fd, const char *path, unsigned *timestamp)
  101. {
  102. syncmsg msg;
  103. int len = strlen(path);
  104. msg.req.id = ID_STAT;
  105. msg.req.namelen = htoll(len);
  106. if(writex(fd, &msg.req, sizeof(msg.req)) ||
  107. writex(fd, path, len)) {
  108. return -1;
  109. }
  110. if(readx(fd, &msg.stat, sizeof(msg.stat))) {
  111. return -1;
  112. }
  113. if(msg.stat.id != ID_STAT) {
  114. return -1;
  115. }
  116. *timestamp = ltohl(msg.stat.time);
  117. return 0;
  118. }
  119. static int sync_start_readtime(int fd, const char *path)
  120. {
  121. syncmsg msg;
  122. int len = strlen(path);
  123. msg.req.id = ID_STAT;
  124. msg.req.namelen = htoll(len);
  125. if(writex(fd, &msg.req, sizeof(msg.req)) ||
  126. writex(fd, path, len)) {
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. static int sync_finish_readtime(int fd, unsigned int *timestamp,
  132. unsigned int *mode, unsigned int *size)
  133. {
  134. syncmsg msg;
  135. if(readx(fd, &msg.stat, sizeof(msg.stat)))
  136. return -1;
  137. if(msg.stat.id != ID_STAT)
  138. return -1;
  139. *timestamp = ltohl(msg.stat.time);
  140. *mode = ltohl(msg.stat.mode);
  141. *size = ltohl(msg.stat.size);
  142. return 0;
  143. }
  144. int sync_readmode(int fd, const char *path, unsigned *mode)
  145. {
  146. syncmsg msg;
  147. int len = strlen(path);
  148. msg.req.id = ID_STAT;
  149. msg.req.namelen = htoll(len);
  150. if(writex(fd, &msg.req, sizeof(msg.req)) ||
  151. writex(fd, path, len)) {
  152. return -1;
  153. }
  154. if(readx(fd, &msg.stat, sizeof(msg.stat))) {
  155. return -1;
  156. }
  157. if(msg.stat.id != ID_STAT) {
  158. return -1;
  159. }
  160. *mode = ltohl(msg.stat.mode);
  161. return 0;
  162. }
  163. static int write_data_file(int fd, const char *path, syncsendbuf *sbuf)
  164. {
  165. int lfd, err = 0;
  166. lfd = adb_open(path, O_RDONLY);
  167. if(lfd < 0) {
  168. fprintf(stderr,"cannot open '%s': %s\n", path, strerror(errno));
  169. return -1;
  170. }
  171. sbuf->id = ID_DATA;
  172. for(;;) {
  173. int ret;
  174. ret = adb_read(lfd, sbuf->data, SYNC_DATA_MAX);
  175. if(!ret)
  176. break;
  177. if(ret < 0) {
  178. if(errno == EINTR)
  179. continue;
  180. fprintf(stderr,"cannot read '%s': %s\n", path, strerror(errno));
  181. break;
  182. }
  183. sbuf->size = htoll(ret);
  184. if(writex(fd, sbuf, sizeof(unsigned) * 2 + ret)){
  185. err = -1;
  186. break;
  187. }
  188. total_bytes += ret;
  189. }
  190. adb_close(lfd);
  191. return err;
  192. }
  193. static int write_data_buffer(int fd, char* file_buffer, int size, syncsendbuf *sbuf)
  194. {
  195. int err = 0;
  196. int total = 0;
  197. sbuf->id = ID_DATA;
  198. while (total < size) {
  199. int count = size - total;
  200. if (count > SYNC_DATA_MAX) {
  201. count = SYNC_DATA_MAX;
  202. }
  203. memcpy(sbuf->data, &file_buffer[total], count);
  204. sbuf->size = htoll(count);
  205. if(writex(fd, sbuf, sizeof(unsigned) * 2 + count)){
  206. err = -1;
  207. break;
  208. }
  209. total += count;
  210. total_bytes += count;
  211. }
  212. return err;
  213. }
  214. #ifdef HAVE_SYMLINKS
  215. static int write_data_link(int fd, const char *path, syncsendbuf *sbuf)
  216. {
  217. int len, ret;
  218. len = readlink(path, sbuf->data, SYNC_DATA_MAX-1);
  219. if(len < 0) {
  220. fprintf(stderr, "error reading link '%s': %s\n", path, strerror(errno));
  221. return -1;
  222. }
  223. sbuf->data[len] = '\0';
  224. sbuf->size = htoll(len + 1);
  225. sbuf->id = ID_DATA;
  226. ret = writex(fd, sbuf, sizeof(unsigned) * 2 + len + 1);
  227. if(ret)
  228. return -1;
  229. total_bytes += len + 1;
  230. return 0;
  231. }
  232. #endif
  233. static int sync_send(int fd, const char *lpath, const char *rpath,
  234. unsigned mtime, mode_t mode, int verifyApk)
  235. {
  236. syncmsg msg;
  237. int len, r;
  238. syncsendbuf *sbuf = &send_buffer;
  239. char* file_buffer = NULL;
  240. int size = 0;
  241. char tmp[64];
  242. len = strlen(rpath);
  243. if(len > 1024) goto fail;
  244. snprintf(tmp, sizeof(tmp), ",%d", mode);
  245. r = strlen(tmp);
  246. if (verifyApk) {
  247. int lfd;
  248. zipfile_t zip;
  249. zipentry_t entry;
  250. int amt;
  251. // if we are transferring an APK file, then sanity check to make sure
  252. // we have a real zip file that contains an AndroidManifest.xml
  253. // this requires that we read the entire file into memory.
  254. lfd = adb_open(lpath, O_RDONLY);
  255. if(lfd < 0) {
  256. fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
  257. return -1;
  258. }
  259. size = adb_lseek(lfd, 0, SEEK_END);
  260. if (size == -1 || -1 == adb_lseek(lfd, 0, SEEK_SET)) {
  261. fprintf(stderr, "error seeking in file '%s'\n", lpath);
  262. adb_close(lfd);
  263. return 1;
  264. }
  265. file_buffer = (char *)malloc(size);
  266. if (file_buffer == NULL) {
  267. fprintf(stderr, "could not allocate buffer for '%s'\n",
  268. lpath);
  269. adb_close(lfd);
  270. return 1;
  271. }
  272. amt = adb_read(lfd, file_buffer, size);
  273. if (amt != size) {
  274. fprintf(stderr, "error reading from file: '%s'\n", lpath);
  275. adb_close(lfd);
  276. free(file_buffer);
  277. return 1;
  278. }
  279. adb_close(lfd);
  280. zip = init_zipfile(file_buffer, size);
  281. if (zip == NULL) {
  282. fprintf(stderr, "file '%s' is not a valid zip file\n",
  283. lpath);
  284. free(file_buffer);
  285. return 1;
  286. }
  287. entry = lookup_zipentry(zip, "AndroidManifest.xml");
  288. release_zipfile(zip);
  289. if (entry == NULL) {
  290. fprintf(stderr, "file '%s' does not contain AndroidManifest.xml\n",
  291. lpath);
  292. free(file_buffer);
  293. return 1;
  294. }
  295. }
  296. msg.req.id = ID_SEND;
  297. msg.req.namelen = htoll(len + r);
  298. if(writex(fd, &msg.req, sizeof(msg.req)) ||
  299. writex(fd, rpath, len) || writex(fd, tmp, r)) {
  300. free(file_buffer);
  301. goto fail;
  302. }
  303. if (file_buffer) {
  304. write_data_buffer(fd, file_buffer, size, sbuf);
  305. free(file_buffer);
  306. } else if (S_ISREG(mode))
  307. write_data_file(fd, lpath, sbuf);
  308. #ifdef HAVE_SYMLINKS
  309. else if (S_ISLNK(mode))
  310. write_data_link(fd, lpath, sbuf);
  311. #endif
  312. else
  313. goto fail;
  314. msg.data.id = ID_DONE;
  315. msg.data.size = htoll(mtime);
  316. if(writex(fd, &msg.data, sizeof(msg.data)))
  317. goto fail;
  318. if(readx(fd, &msg.status, sizeof(msg.status)))
  319. return -1;
  320. if(msg.status.id != ID_OKAY) {
  321. if(msg.status.id == ID_FAIL) {
  322. len = ltohl(msg.status.msglen);
  323. if(len > 256) len = 256;
  324. if(readx(fd, sbuf->data, len)) {
  325. return -1;
  326. }
  327. sbuf->data[len] = 0;
  328. } else
  329. strcpy(sbuf->data, "unknown reason");
  330. fprintf(stderr,"failed to copy '%s' to '%s': %s\n", lpath, rpath, sbuf->data);
  331. return -1;
  332. }
  333. return 0;
  334. fail:
  335. fprintf(stderr,"protocol failure\n");
  336. adb_close(fd);
  337. return -1;
  338. }
  339. static int mkdirs(char *name)
  340. {
  341. int ret;
  342. char *x = name + 1;
  343. for(;;) {
  344. x = adb_dirstart(x);
  345. if(x == 0) return 0;
  346. *x = 0;
  347. ret = adb_mkdir(name, 0775);
  348. *x = OS_PATH_SEPARATOR;
  349. if((ret < 0) && (errno != EEXIST)) {
  350. return ret;
  351. }
  352. x++;
  353. }
  354. return 0;
  355. }
  356. int sync_recv(int fd, const char *rpath, const char *lpath)
  357. {
  358. syncmsg msg;
  359. int len;
  360. int lfd = -1;
  361. char *buffer = send_buffer.data;
  362. unsigned id;
  363. len = strlen(rpath);
  364. if(len > 1024) return -1;
  365. msg.req.id = ID_RECV;
  366. msg.req.namelen = htoll(len);
  367. if(writex(fd, &msg.req, sizeof(msg.req)) ||
  368. writex(fd, rpath, len)) {
  369. return -1;
  370. }
  371. if(readx(fd, &msg.data, sizeof(msg.data))) {
  372. return -1;
  373. }
  374. id = msg.data.id;
  375. if((id == ID_DATA) || (id == ID_DONE)) {
  376. adb_unlink(lpath);
  377. mkdirs((char *)lpath);
  378. lfd = adb_creat(lpath, 0644);
  379. if(lfd < 0) {
  380. fprintf(stderr,"cannot create '%s': %s\n", lpath, strerror(errno));
  381. return -1;
  382. }
  383. goto handle_data;
  384. } else {
  385. goto remote_error;
  386. }
  387. for(;;) {
  388. if(readx(fd, &msg.data, sizeof(msg.data))) {
  389. return -1;
  390. }
  391. id = msg.data.id;
  392. handle_data:
  393. len = ltohl(msg.data.size);
  394. if(id == ID_DONE) break;
  395. if(id != ID_DATA) goto remote_error;
  396. if(len > SYNC_DATA_MAX) {
  397. fprintf(stderr,"data overrun\n");
  398. adb_close(lfd);
  399. return -1;
  400. }
  401. if(readx(fd, buffer, len)) {
  402. adb_close(lfd);
  403. return -1;
  404. }
  405. if(writex(lfd, buffer, len)) {
  406. fprintf(stderr,"cannot write '%s': %s\n", rpath, strerror(errno));
  407. adb_close(lfd);
  408. return -1;
  409. }
  410. total_bytes += len;
  411. }
  412. adb_close(lfd);
  413. return 0;
  414. remote_error:
  415. adb_close(lfd);
  416. adb_unlink(lpath);
  417. if(id == ID_FAIL) {
  418. len = ltohl(msg.data.size);
  419. if(len > 256) len = 256;
  420. if(readx(fd, buffer, len)) {
  421. return -1;
  422. }
  423. buffer[len] = 0;
  424. } else {
  425. memcpy(buffer, &id, 4);
  426. buffer[4] = 0;
  427. // strcpy(buffer,"unknown reason");
  428. }
  429. fprintf(stderr,"failed to copy '%s' to '%s': %s\n", rpath, lpath, buffer);
  430. return 0;
  431. }
  432. /* --- */
  433. static void do_sync_ls_cb(unsigned mode, unsigned size, unsigned time,
  434. const char *name, void *cookie)
  435. {
  436. printf("%08x %08x %08x %s\n", mode, size, time, name);
  437. }
  438. int do_sync_ls(const char *path)
  439. {
  440. int fd = adb_connect("sync:");
  441. if(fd < 0) {
  442. fprintf(stderr,"error: %s\n", adb_error());
  443. return 1;
  444. }
  445. if(sync_ls(fd, path, do_sync_ls_cb, 0)) {
  446. return 1;
  447. } else {
  448. sync_quit(fd);
  449. return 0;
  450. }
  451. }
  452. typedef struct copyinfo copyinfo;
  453. struct copyinfo
  454. {
  455. copyinfo *next;
  456. const char *src;
  457. const char *dst;
  458. unsigned int time;
  459. unsigned int mode;
  460. unsigned int size;
  461. int flag;
  462. //char data[0];
  463. };
  464. copyinfo *mkcopyinfo(const char *spath, const char *dpath,
  465. const char *name, int isdir)
  466. {
  467. int slen = strlen(spath);
  468. int dlen = strlen(dpath);
  469. int nlen = strlen(name);
  470. int ssize = slen + nlen + 2;
  471. int dsize = dlen + nlen + 2;
  472. copyinfo *ci = malloc(sizeof(copyinfo) + ssize + dsize);
  473. if(ci == 0) {
  474. fprintf(stderr,"out of memory\n");
  475. abort();
  476. }
  477. ci->next = 0;
  478. ci->time = 0;
  479. ci->mode = 0;
  480. ci->size = 0;
  481. ci->flag = 0;
  482. ci->src = (const char*)(ci + 1);
  483. ci->dst = ci->src + ssize;
  484. snprintf((char*) ci->src, ssize, isdir ? "%s%s/" : "%s%s", spath, name);
  485. snprintf((char*) ci->dst, dsize, isdir ? "%s%s/" : "%s%s", dpath, name);
  486. // fprintf(stderr,"mkcopyinfo('%s','%s')\n", ci->src, ci->dst);
  487. return ci;
  488. }
  489. static int local_build_list(copyinfo **filelist,
  490. const char *lpath, const char *rpath)
  491. {
  492. DIR *d;
  493. struct dirent *de;
  494. struct stat st;
  495. copyinfo *dirlist = 0;
  496. copyinfo *ci, *next;
  497. // fprintf(stderr,"local_build_list('%s','%s')\n", lpath, rpath);
  498. d = opendir(lpath);
  499. if(d == 0) {
  500. fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
  501. return -1;
  502. }
  503. while((de = readdir(d))) {
  504. char stat_path[PATH_MAX];
  505. char *name = de->d_name;
  506. if(name[0] == '.') {
  507. if(name[1] == 0) continue;
  508. if((name[1] == '.') && (name[2] == 0)) continue;
  509. }
  510. /*
  511. * We could use d_type if HAVE_DIRENT_D_TYPE is defined, but reiserfs
  512. * always returns DT_UNKNOWN, so we just use stat() for all cases.
  513. */
  514. if (strlen(lpath) + strlen(de->d_name) + 1 > sizeof(stat_path))
  515. continue;
  516. strcpy(stat_path, lpath);
  517. strcat(stat_path, de->d_name);
  518. stat(stat_path, &st);
  519. if (S_ISDIR(st.st_mode)) {
  520. ci = mkcopyinfo(lpath, rpath, name, 1);
  521. ci->next = dirlist;
  522. dirlist = ci;
  523. } else {
  524. ci = mkcopyinfo(lpath, rpath, name, 0);
  525. if(lstat(ci->src, &st)) {
  526. fprintf(stderr,"cannot stat '%s': %s\n", ci->src, strerror(errno));
  527. free(ci);
  528. closedir(d);
  529. return -1;
  530. }
  531. if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
  532. fprintf(stderr, "skipping special file '%s'\n", ci->src);
  533. free(ci);
  534. } else {
  535. ci->time = st.st_mtime;
  536. ci->mode = st.st_mode;
  537. ci->size = st.st_size;
  538. ci->next = *filelist;
  539. *filelist = ci;
  540. }
  541. }
  542. }
  543. closedir(d);
  544. for(ci = dirlist; ci != 0; ci = next) {
  545. next = ci->next;
  546. local_build_list(filelist, ci->src, ci->dst);
  547. free(ci);
  548. }
  549. return 0;
  550. }
  551. static int copy_local_dir_remote(int fd, const char *lpath, const char *rpath, int checktimestamps, int listonly)
  552. {
  553. copyinfo *filelist = 0;
  554. copyinfo *ci, *next;
  555. int pushed = 0;
  556. int skipped = 0;
  557. if((lpath[0] == 0) || (rpath[0] == 0)) return -1;
  558. if(lpath[strlen(lpath) - 1] != '/') {
  559. int tmplen = strlen(lpath)+2;
  560. char *tmp = malloc(tmplen);
  561. if(tmp == 0) return -1;
  562. snprintf(tmp, tmplen, "%s/",lpath);
  563. lpath = tmp;
  564. }
  565. if(rpath[strlen(rpath) - 1] != '/') {
  566. int tmplen = strlen(rpath)+2;
  567. char *tmp = malloc(tmplen);
  568. if(tmp == 0) return -1;
  569. snprintf(tmp, tmplen, "%s/",rpath);
  570. rpath = tmp;
  571. }
  572. if(local_build_list(&filelist, lpath, rpath)) {
  573. return -1;
  574. }
  575. if(checktimestamps){
  576. for(ci = filelist; ci != 0; ci = ci->next) {
  577. if(sync_start_readtime(fd, ci->dst)) {
  578. return 1;
  579. }
  580. }
  581. for(ci = filelist; ci != 0; ci = ci->next) {
  582. unsigned int timestamp, mode, size;
  583. if(sync_finish_readtime(fd, &timestamp, &mode, &size))
  584. return 1;
  585. if(size == ci->size) {
  586. /* for links, we cannot update the atime/mtime */
  587. if((S_ISREG(ci->mode & mode) && timestamp == ci->time) ||
  588. (S_ISLNK(ci->mode & mode) && timestamp >= ci->time))
  589. ci->flag = 1;
  590. }
  591. }
  592. }
  593. for(ci = filelist; ci != 0; ci = next) {
  594. next = ci->next;
  595. if(ci->flag == 0) {
  596. fprintf(stderr,"%spush: %s -> %s\n", listonly ? "would " : "", ci->src, ci->dst);
  597. if(!listonly &&
  598. sync_send(fd, ci->src, ci->dst, ci->time, ci->mode, 0 /* no verify APK */)){
  599. return 1;
  600. }
  601. pushed++;
  602. } else {
  603. skipped++;
  604. }
  605. free(ci);
  606. }
  607. fprintf(stderr,"%d file%s pushed. %d file%s skipped.\n",
  608. pushed, (pushed == 1) ? "" : "s",
  609. skipped, (skipped == 1) ? "" : "s");
  610. return 0;
  611. }
  612. int do_sync_push(const char *lpath, const char *rpath, int verifyApk)
  613. {
  614. struct stat st;
  615. unsigned mode;
  616. int fd;
  617. fd = adb_connect("sync:");
  618. if(fd < 0) {
  619. fprintf(stderr,"error: %s\n", adb_error());
  620. return 1;
  621. }
  622. if(stat(lpath, &st)) {
  623. fprintf(stderr,"cannot stat '%s': %s\n", lpath, strerror(errno));
  624. sync_quit(fd);
  625. return 1;
  626. }
  627. if(S_ISDIR(st.st_mode)) {
  628. BEGIN();
  629. if(copy_local_dir_remote(fd, lpath, rpath, 0, 0)) {
  630. return 1;
  631. } else {
  632. END();
  633. sync_quit(fd);
  634. }
  635. } else {
  636. if(sync_readmode(fd, rpath, &mode)) {
  637. return 1;
  638. }
  639. if((mode != 0) && S_ISDIR(mode)) {
  640. /* if we're copying a local file to a remote directory,
  641. ** we *really* want to copy to remotedir + "/" + localfilename
  642. */
  643. const char *name = adb_dirstop(lpath);
  644. if(name == 0) {
  645. name = lpath;
  646. } else {
  647. name++;
  648. }
  649. int tmplen = strlen(name) + strlen(rpath) + 2;
  650. char *tmp = malloc(strlen(name) + strlen(rpath) + 2);
  651. if(tmp == 0) return 1;
  652. snprintf(tmp, tmplen, "%s/%s", rpath, name);
  653. rpath = tmp;
  654. }
  655. BEGIN();
  656. if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, verifyApk)) {
  657. return 1;
  658. } else {
  659. END();
  660. sync_quit(fd);
  661. return 0;
  662. }
  663. }
  664. return 0;
  665. }
  666. typedef struct {
  667. copyinfo **filelist;
  668. copyinfo **dirlist;
  669. const char *rpath;
  670. const char *lpath;
  671. } sync_ls_build_list_cb_args;
  672. void
  673. sync_ls_build_list_cb(unsigned mode, unsigned size, unsigned time,
  674. const char *name, void *cookie)
  675. {
  676. sync_ls_build_list_cb_args *args = (sync_ls_build_list_cb_args *)cookie;
  677. copyinfo *ci;
  678. if (S_ISDIR(mode)) {
  679. copyinfo **dirlist = args->dirlist;
  680. /* Don't try recursing down "." or ".." */
  681. if (name[0] == '.') {
  682. if (name[1] == '\0') return;
  683. if ((name[1] == '.') && (name[2] == '\0')) return;
  684. }
  685. ci = mkcopyinfo(args->rpath, args->lpath, name, 1);
  686. ci->next = *dirlist;
  687. *dirlist = ci;
  688. } else if (S_ISREG(mode) || S_ISLNK(mode)) {
  689. copyinfo **filelist = args->filelist;
  690. ci = mkcopyinfo(args->rpath, args->lpath, name, 0);
  691. ci->time = time;
  692. ci->mode = mode;
  693. ci->size = size;
  694. ci->next = *filelist;
  695. *filelist = ci;
  696. } else {
  697. fprintf(stderr, "skipping special file '%s'\n", name);
  698. }
  699. }
  700. static int remote_build_list(int syncfd, copyinfo **filelist,
  701. const char *rpath, const char *lpath)
  702. {
  703. copyinfo *dirlist = NULL;
  704. sync_ls_build_list_cb_args args;
  705. args.filelist = filelist;
  706. args.dirlist = &dirlist;
  707. args.rpath = rpath;
  708. args.lpath = lpath;
  709. /* Put the files/dirs in rpath on the lists. */
  710. if (sync_ls(syncfd, rpath, sync_ls_build_list_cb, (void *)&args)) {
  711. return 1;
  712. }
  713. /* Recurse into each directory we found. */
  714. while (dirlist != NULL) {
  715. copyinfo *next = dirlist->next;
  716. if (remote_build_list(syncfd, filelist, dirlist->src, dirlist->dst)) {
  717. return 1;
  718. }
  719. free(dirlist);
  720. dirlist = next;
  721. }
  722. return 0;
  723. }
  724. static int copy_remote_dir_local(int fd, const char *rpath, const char *lpath,
  725. int checktimestamps)
  726. {
  727. copyinfo *filelist = 0;
  728. copyinfo *ci, *next;
  729. int pulled = 0;
  730. int skipped = 0;
  731. /* Make sure that both directory paths end in a slash. */
  732. if (rpath[0] == 0 || lpath[0] == 0) return -1;
  733. if (rpath[strlen(rpath) - 1] != '/') {
  734. int tmplen = strlen(rpath) + 2;
  735. char *tmp = malloc(tmplen);
  736. if (tmp == 0) return -1;
  737. snprintf(tmp, tmplen, "%s/", rpath);
  738. rpath = tmp;
  739. }
  740. if (lpath[strlen(lpath) - 1] != '/') {
  741. int tmplen = strlen(lpath) + 2;
  742. char *tmp = malloc(tmplen);
  743. if (tmp == 0) return -1;
  744. snprintf(tmp, tmplen, "%s/", lpath);
  745. lpath = tmp;
  746. }
  747. fprintf(stderr, "pull: building file list...\n");
  748. /* Recursively build the list of files to copy. */
  749. if (remote_build_list(fd, &filelist, rpath, lpath)) {
  750. return -1;
  751. }
  752. #if 0
  753. if (checktimestamps) {
  754. for (ci = filelist; ci != 0; ci = ci->next) {
  755. if (sync_start_readtime(fd, ci->dst)) {
  756. return 1;
  757. }
  758. }
  759. for (ci = filelist; ci != 0; ci = ci->next) {
  760. unsigned int timestamp, mode, size;
  761. if (sync_finish_readtime(fd, &timestamp, &mode, &size))
  762. return 1;
  763. if (size == ci->size) {
  764. /* for links, we cannot update the atime/mtime */
  765. if ((S_ISREG(ci->mode & mode) && timestamp == ci->time) ||
  766. (S_ISLNK(ci->mode & mode) && timestamp >= ci->time))
  767. ci->flag = 1;
  768. }
  769. }
  770. }
  771. #endif
  772. for (ci = filelist; ci != 0; ci = next) {
  773. next = ci->next;
  774. if (ci->flag == 0) {
  775. fprintf(stderr, "pull: %s -> %s\n", ci->src, ci->dst);
  776. if (sync_recv(fd, ci->src, ci->dst)) {
  777. return 1;
  778. }
  779. pulled++;
  780. } else {
  781. skipped++;
  782. }
  783. free(ci);
  784. }
  785. fprintf(stderr, "%d file%s pulled. %d file%s skipped.\n",
  786. pulled, (pulled == 1) ? "" : "s",
  787. skipped, (skipped == 1) ? "" : "s");
  788. return 0;
  789. }
  790. int do_sync_pull(const char *rpath, const char *lpath)
  791. {
  792. unsigned mode;
  793. struct stat st;
  794. int fd;
  795. fd = adb_connect("sync:");
  796. if(fd < 0) {
  797. fprintf(stderr,"error: %s\n", adb_error());
  798. return 1;
  799. }
  800. if(sync_readmode(fd, rpath, &mode)) {
  801. return 1;
  802. }
  803. if(mode == 0) {
  804. fprintf(stderr,"remote object '%s' does not exist\n", rpath);
  805. return 1;
  806. }
  807. if(S_ISREG(mode) || S_ISLNK(mode) || S_ISCHR(mode) || S_ISBLK(mode)) {
  808. if(stat(lpath, &st) == 0) {
  809. if(S_ISDIR(st.st_mode)) {
  810. /* if we're copying a remote file to a local directory,
  811. ** we *really* want to copy to localdir + "/" + remotefilename
  812. */
  813. const char *name = adb_dirstop(rpath);
  814. if(name == 0) {
  815. name = rpath;
  816. } else {
  817. name++;
  818. }
  819. int tmplen = strlen(name) + strlen(lpath) + 2;
  820. char *tmp = malloc(tmplen);
  821. if(tmp == 0) return 1;
  822. snprintf(tmp, tmplen, "%s/%s", lpath, name);
  823. lpath = tmp;
  824. }
  825. }
  826. BEGIN();
  827. if(sync_recv(fd, rpath, lpath)) {
  828. return 1;
  829. } else {
  830. END();
  831. sync_quit(fd);
  832. return 0;
  833. }
  834. } else if(S_ISDIR(mode)) {
  835. BEGIN();
  836. if (copy_remote_dir_local(fd, rpath, lpath, 0)) {
  837. return 1;
  838. } else {
  839. END();
  840. sync_quit(fd);
  841. return 0;
  842. }
  843. } else {
  844. fprintf(stderr,"remote object '%s' not a file or directory\n", rpath);
  845. return 1;
  846. }
  847. }
  848. int do_sync_sync(const char *lpath, const char *rpath, int listonly)
  849. {
  850. fprintf(stderr,"syncing %s...\n",rpath);
  851. int fd = adb_connect("sync:");
  852. if(fd < 0) {
  853. fprintf(stderr,"error: %s\n", adb_error());
  854. return 1;
  855. }
  856. BEGIN();
  857. if(copy_local_dir_remote(fd, lpath, rpath, 1, listonly)){
  858. return 1;
  859. } else {
  860. END();
  861. sync_quit(fd);
  862. return 0;
  863. }
  864. }