2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <sys/types.h>
28 #include <zipfile/zipfile.h>
32 #include "sdb_client.h"
33 #include "file_sync_service.h"
36 static unsigned total_bytes;
37 static long long start_time;
39 static long long NOW()
43 return ((long long) tv.tv_usec) +
44 1000000LL * ((long long) tv.tv_sec);
55 long long t = NOW() - start_time;
56 if(total_bytes == 0) return;
58 if (t == 0) /* prevent division by 0 :-) */
61 fprintf(stderr,"%lld KB/s (%d bytes in %lld.%03llds)\n",
62 ((((long long) total_bytes) * 1000000LL) / t) / 1024LL,
63 total_bytes, (t / 1000000LL), (t % 1000000LL) / 1000LL);
66 void sync_quit(int fd)
73 writex(fd, &msg.req, sizeof(msg.req));
76 typedef void (*sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char *name, void *cookie);
78 int sync_ls(int fd, const char *path, sync_ls_cb func, void *cookie)
85 if(len > 1024) goto fail;
88 msg.req.namelen = htoll(len);
90 if(writex(fd, &msg.req, sizeof(msg.req)) ||
91 writex(fd, path, len)) {
96 if(readx(fd, &msg.dent, sizeof(msg.dent))) break;
97 if(msg.dent.id == ID_DONE) return 0;
98 if(msg.dent.id != ID_DENT) break;
100 len = ltohl(msg.dent.namelen);
103 if(readx(fd, buf, len)) break;
106 func(ltohl(msg.dent.mode),
107 ltohl(msg.dent.size),
108 ltohl(msg.dent.time),
117 typedef struct syncsendbuf syncsendbuf;
122 char data[SYNC_DATA_MAX];
125 static syncsendbuf send_buffer;
127 int sync_readtime(int fd, const char *path, unsigned *timestamp)
130 int len = strlen(path);
132 msg.req.id = ID_STAT;
133 msg.req.namelen = htoll(len);
135 if(writex(fd, &msg.req, sizeof(msg.req)) ||
136 writex(fd, path, len)) {
140 if(readx(fd, &msg.stat, sizeof(msg.stat))) {
144 if(msg.stat.id != ID_STAT) {
148 *timestamp = ltohl(msg.stat.time);
152 static int sync_start_readtime(int fd, const char *path)
155 int len = strlen(path);
157 msg.req.id = ID_STAT;
158 msg.req.namelen = htoll(len);
160 if(writex(fd, &msg.req, sizeof(msg.req)) ||
161 writex(fd, path, len)) {
168 static int sync_finish_readtime(int fd, unsigned int *timestamp,
169 unsigned int *mode, unsigned int *size)
173 if(readx(fd, &msg.stat, sizeof(msg.stat)))
176 if(msg.stat.id != ID_STAT)
179 *timestamp = ltohl(msg.stat.time);
180 *mode = ltohl(msg.stat.mode);
181 *size = ltohl(msg.stat.size);
186 int sync_readmode(int fd, const char *path, unsigned *mode)
189 int len = strlen(path);
191 msg.req.id = ID_STAT;
192 msg.req.namelen = htoll(len);
194 if(writex(fd, &msg.req, sizeof(msg.req)) ||
195 writex(fd, path, len)) {
199 if(readx(fd, &msg.stat, sizeof(msg.stat))) {
203 if(msg.stat.id != ID_STAT) {
207 *mode = ltohl(msg.stat.mode);
211 static int write_data_file(int fd, const char *path, syncsendbuf *sbuf)
215 lfd = sdb_open(path, O_RDONLY);
217 fprintf(stderr,"cannot open '%s': %s\n", path, strerror(errno));
225 ret = sdb_read(lfd, sbuf->data, SYNC_DATA_MAX);
232 fprintf(stderr,"cannot read '%s': %s\n", path, strerror(errno));
236 sbuf->size = htoll(ret);
237 if(writex(fd, sbuf, sizeof(unsigned) * 2 + ret)){
248 static int write_data_buffer(int fd, char* file_buffer, int size, syncsendbuf *sbuf)
254 while (total < size) {
255 int count = size - total;
256 if (count > SYNC_DATA_MAX) {
257 count = SYNC_DATA_MAX;
260 memcpy(sbuf->data, &file_buffer[total], count);
261 sbuf->size = htoll(count);
262 if(writex(fd, sbuf, sizeof(unsigned) * 2 + count)){
267 total_bytes += count;
274 static int write_data_link(int fd, const char *path, syncsendbuf *sbuf)
278 len = readlink(path, sbuf->data, SYNC_DATA_MAX-1);
280 fprintf(stderr, "error reading link '%s': %s\n", path, strerror(errno));
283 sbuf->data[len] = '\0';
285 sbuf->size = htoll(len + 1);
288 ret = writex(fd, sbuf, sizeof(unsigned) * 2 + len + 1);
292 total_bytes += len + 1;
298 static int sync_send(int fd, const char *lpath, const char *rpath,
299 unsigned mtime, mode_t mode, int verifyApk)
303 syncsendbuf *sbuf = &send_buffer;
304 char* file_buffer = NULL;
309 if(len > 1024) goto fail;
311 snprintf(tmp, sizeof(tmp), ",%d", mode);
321 // if we are transferring an APK file, then sanity check to make sure
322 // we have a real zip file that contains an AndroidManifest.xml
323 // this requires that we read the entire file into memory.
324 lfd = sdb_open(lpath, O_RDONLY);
326 fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
330 size = sdb_lseek(lfd, 0, SEEK_END);
331 if (size == -1 || -1 == sdb_lseek(lfd, 0, SEEK_SET)) {
332 fprintf(stderr, "error seeking in file '%s'\n", lpath);
337 file_buffer = (char *)malloc(size);
338 if (file_buffer == NULL) {
339 fprintf(stderr, "could not allocate buffer for '%s'\n",
344 amt = sdb_read(lfd, file_buffer, size);
346 fprintf(stderr, "error reading from file: '%s'\n", lpath);
354 zip = init_zipfile(file_buffer, size);
356 fprintf(stderr, "file '%s' is not a valid zip file\n",
362 entry = lookup_zipentry(zip, "AndroidManifest.xml");
363 release_zipfile(zip);
365 fprintf(stderr, "file '%s' does not contain AndroidManifest.xml\n",
373 msg.req.id = ID_SEND;
374 msg.req.namelen = htoll(len + r);
376 if(writex(fd, &msg.req, sizeof(msg.req)) ||
377 writex(fd, rpath, len) || writex(fd, tmp, r)) {
383 write_data_buffer(fd, file_buffer, size, sbuf);
385 } else if (S_ISREG(mode))
386 write_data_file(fd, lpath, sbuf);
388 else if (S_ISLNK(mode))
389 write_data_link(fd, lpath, sbuf);
394 msg.data.id = ID_DONE;
395 msg.data.size = htoll(mtime);
396 if(writex(fd, &msg.data, sizeof(msg.data)))
399 if(readx(fd, &msg.status, sizeof(msg.status)))
402 if(msg.status.id != ID_OKAY) {
403 if(msg.status.id == ID_FAIL) {
404 len = ltohl(msg.status.msglen);
405 if(len > 256) len = 256;
406 if(readx(fd, sbuf->data, len)) {
411 strcpy(sbuf->data, "unknown reason");
413 fprintf(stderr,"failed to copy '%s' to '%s': %s\n", lpath, rpath, sbuf->data);
420 fprintf(stderr,"protocol failure\n");
425 static int mkdirs(char *name)
434 ret = sdb_mkdir(name, 0775);
435 *x = OS_PATH_SEPARATOR;
436 if((ret < 0) && (errno != EEXIST)) {
444 int sync_recv(int fd, const char *rpath, const char *lpath)
449 char *buffer = send_buffer.data;
453 if(len > 1024) return -1;
455 msg.req.id = ID_RECV;
456 msg.req.namelen = htoll(len);
457 if(writex(fd, &msg.req, sizeof(msg.req)) ||
458 writex(fd, rpath, len)) {
462 if(readx(fd, &msg.data, sizeof(msg.data))) {
467 if((id == ID_DATA) || (id == ID_DONE)) {
469 mkdirs((char *)lpath);
470 lfd = sdb_creat(lpath, 0644);
472 fprintf(stderr,"cannot create '%s': %s\n", lpath, strerror(errno));
481 if(readx(fd, &msg.data, sizeof(msg.data))) {
487 len = ltohl(msg.data.size);
488 if(id == ID_DONE) break;
489 if(id != ID_DATA) goto remote_error;
490 if(len > SYNC_DATA_MAX) {
491 fprintf(stderr,"data overrun\n");
496 if(readx(fd, buffer, len)) {
501 if(writex(lfd, buffer, len)) {
502 fprintf(stderr,"cannot write '%s': %s\n", rpath, strerror(errno));
518 len = ltohl(msg.data.size);
519 if(len > 256) len = 256;
520 if(readx(fd, buffer, len)) {
525 memcpy(buffer, &id, 4);
527 // strcpy(buffer,"unknown reason");
529 fprintf(stderr,"failed to copy '%s' to '%s': %s\n", rpath, lpath, buffer);
538 static void do_sync_ls_cb(unsigned mode, unsigned size, unsigned time,
539 const char *name, void *cookie)
541 printf("%08x %08x %08x %s\n", mode, size, time, name);
544 int do_sync_ls(const char *path)
546 int fd = sdb_connect("sync:");
548 fprintf(stderr,"error: %s\n", sdb_error());
552 if(sync_ls(fd, path, do_sync_ls_cb, 0)) {
560 typedef struct copyinfo copyinfo;
574 copyinfo *mkcopyinfo(const char *spath, const char *dpath,
575 const char *name, int isdir)
577 int slen = strlen(spath);
578 int dlen = strlen(dpath);
579 int nlen = strlen(name);
580 int ssize = slen + nlen + 2;
581 int dsize = dlen + nlen + 2;
583 copyinfo *ci = malloc(sizeof(copyinfo) + ssize + dsize);
585 fprintf(stderr,"out of memory\n");
594 ci->src = (const char*)(ci + 1);
595 ci->dst = ci->src + ssize;
596 snprintf((char*) ci->src, ssize, isdir ? "%s%s/" : "%s%s", spath, name);
597 snprintf((char*) ci->dst, dsize, isdir ? "%s%s/" : "%s%s", dpath, name);
599 // fprintf(stderr,"mkcopyinfo('%s','%s')\n", ci->src, ci->dst);
604 static int local_build_list(copyinfo **filelist,
605 const char *lpath, const char *rpath)
610 copyinfo *dirlist = 0;
613 // fprintf(stderr,"local_build_list('%s','%s')\n", lpath, rpath);
617 fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
621 while((de = readdir(d))) {
622 char stat_path[PATH_MAX];
623 char *name = de->d_name;
626 if(name[1] == 0) continue;
627 if((name[1] == '.') && (name[2] == 0)) continue;
631 * We could use d_type if HAVE_DIRENT_D_TYPE is defined, but reiserfs
632 * always returns DT_UNKNOWN, so we just use stat() for all cases.
634 if (strlen(lpath) + strlen(de->d_name) + 1 > sizeof(stat_path))
636 strcpy(stat_path, lpath);
637 strcat(stat_path, de->d_name);
638 stat(stat_path, &st);
640 if (S_ISDIR(st.st_mode)) {
641 ci = mkcopyinfo(lpath, rpath, name, 1);
645 ci = mkcopyinfo(lpath, rpath, name, 0);
646 if(lstat(ci->src, &st)) {
648 fprintf(stderr,"cannot stat '%s': %s\n", ci->src, strerror(errno));
651 if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
652 fprintf(stderr, "skipping special file '%s'\n", ci->src);
655 ci->time = st.st_mtime;
656 ci->mode = st.st_mode;
657 ci->size = st.st_size;
658 ci->next = *filelist;
666 for(ci = dirlist; ci != 0; ci = next) {
668 local_build_list(filelist, ci->src, ci->dst);
676 static int copy_local_dir_remote(int fd, const char *lpath, const char *rpath, int checktimestamps, int listonly)
678 copyinfo *filelist = 0;
683 if((lpath[0] == 0) || (rpath[0] == 0)) return -1;
684 if(lpath[strlen(lpath) - 1] != '/') {
685 int tmplen = strlen(lpath)+2;
686 char *tmp = malloc(tmplen);
687 if(tmp == 0) return -1;
688 snprintf(tmp, tmplen, "%s/",lpath);
691 if(rpath[strlen(rpath) - 1] != '/') {
692 int tmplen = strlen(rpath)+2;
693 char *tmp = malloc(tmplen);
694 if(tmp == 0) return -1;
695 snprintf(tmp, tmplen, "%s/",rpath);
699 if(local_build_list(&filelist, lpath, rpath)) {
704 for(ci = filelist; ci != 0; ci = ci->next) {
705 if(sync_start_readtime(fd, ci->dst)) {
709 for(ci = filelist; ci != 0; ci = ci->next) {
710 unsigned int timestamp, mode, size;
711 if(sync_finish_readtime(fd, ×tamp, &mode, &size))
713 if(size == ci->size) {
714 /* for links, we cannot update the atime/mtime */
715 if((S_ISREG(ci->mode & mode) && timestamp == ci->time) ||
716 (S_ISLNK(ci->mode & mode) && timestamp >= ci->time))
721 for(ci = filelist; ci != 0; ci = next) {
724 fprintf(stderr,"%spush: %s -> %s\n", listonly ? "would " : "", ci->src, ci->dst);
726 sync_send(fd, ci->src, ci->dst, ci->time, ci->mode, 0 /* no verify APK */)){
736 fprintf(stderr,"%d file%s pushed. %d file%s skipped.\n",
737 pushed, (pushed == 1) ? "" : "s",
738 skipped, (skipped == 1) ? "" : "s");
744 int do_sync_push(const char *lpath, const char *rpath, int verifyApk)
750 fd = sdb_connect("sync:");
752 fprintf(stderr,"error: %s\n", sdb_error());
756 if(stat(lpath, &st)) {
757 fprintf(stderr,"cannot stat '%s': %s\n", lpath, strerror(errno));
762 if(S_ISDIR(st.st_mode)) {
764 if(copy_local_dir_remote(fd, lpath, rpath, 0, 0)) {
771 if(sync_readmode(fd, rpath, &mode)) {
774 if((mode != 0) && S_ISDIR(mode)) {
775 /* if we're copying a local file to a remote directory,
776 ** we *really* want to copy to remotedir + "/" + localfilename
778 const char *name = sdb_dirstop(lpath);
784 int tmplen = strlen(name) + strlen(rpath) + 2;
785 char *tmp = malloc(strlen(name) + strlen(rpath) + 2);
786 if(tmp == 0) return 1;
787 snprintf(tmp, tmplen, "%s/%s", rpath, name);
791 if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, verifyApk)) {
809 } sync_ls_build_list_cb_args;
812 sync_ls_build_list_cb(unsigned mode, unsigned size, unsigned time,
813 const char *name, void *cookie)
815 sync_ls_build_list_cb_args *args = (sync_ls_build_list_cb_args *)cookie;
819 copyinfo **dirlist = args->dirlist;
821 /* Don't try recursing down "." or ".." */
822 if (name[0] == '.') {
823 if (name[1] == '\0') return;
824 if ((name[1] == '.') && (name[2] == '\0')) return;
827 ci = mkcopyinfo(args->rpath, args->lpath, name, 1);
830 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
831 copyinfo **filelist = args->filelist;
833 ci = mkcopyinfo(args->rpath, args->lpath, name, 0);
837 ci->next = *filelist;
840 fprintf(stderr, "skipping special file '%s'\n", name);
844 static int remote_build_list(int syncfd, copyinfo **filelist,
845 const char *rpath, const char *lpath)
847 copyinfo *dirlist = NULL;
848 sync_ls_build_list_cb_args args;
850 args.filelist = filelist;
851 args.dirlist = &dirlist;
855 /* Put the files/dirs in rpath on the lists. */
856 if (sync_ls(syncfd, rpath, sync_ls_build_list_cb, (void *)&args)) {
860 /* Recurse into each directory we found. */
861 while (dirlist != NULL) {
862 copyinfo *next = dirlist->next;
863 if (remote_build_list(syncfd, filelist, dirlist->src, dirlist->dst)) {
873 static int copy_remote_dir_local(int fd, const char *rpath, const char *lpath,
876 copyinfo *filelist = 0;
881 /* Make sure that both directory paths end in a slash. */
882 if (rpath[0] == 0 || lpath[0] == 0) return -1;
883 if (rpath[strlen(rpath) - 1] != '/') {
884 int tmplen = strlen(rpath) + 2;
885 char *tmp = malloc(tmplen);
886 if (tmp == 0) return -1;
887 snprintf(tmp, tmplen, "%s/", rpath);
890 if (lpath[strlen(lpath) - 1] != '/') {
891 int tmplen = strlen(lpath) + 2;
892 char *tmp = malloc(tmplen);
893 if (tmp == 0) return -1;
894 snprintf(tmp, tmplen, "%s/", lpath);
898 fprintf(stderr, "pull: building file list...\n");
899 /* Recursively build the list of files to copy. */
900 if (remote_build_list(fd, &filelist, rpath, lpath)) {
905 if (checktimestamps) {
906 for (ci = filelist; ci != 0; ci = ci->next) {
907 if (sync_start_readtime(fd, ci->dst)) {
911 for (ci = filelist; ci != 0; ci = ci->next) {
912 unsigned int timestamp, mode, size;
913 if (sync_finish_readtime(fd, ×tamp, &mode, &size))
915 if (size == ci->size) {
916 /* for links, we cannot update the atime/mtime */
917 if ((S_ISREG(ci->mode & mode) && timestamp == ci->time) ||
918 (S_ISLNK(ci->mode & mode) && timestamp >= ci->time))
924 for (ci = filelist; ci != 0; ci = next) {
927 fprintf(stderr, "pull: %s -> %s\n", ci->src, ci->dst);
928 if (sync_recv(fd, ci->src, ci->dst)) {
938 fprintf(stderr, "%d file%s pulled. %d file%s skipped.\n",
939 pulled, (pulled == 1) ? "" : "s",
940 skipped, (skipped == 1) ? "" : "s");
945 int do_sync_pull(const char *rpath, const char *lpath)
952 fd = sdb_connect("sync:");
954 fprintf(stderr,"error: %s\n", sdb_error());
958 if(sync_readmode(fd, rpath, &mode)) {
962 fprintf(stderr,"remote object '%s' does not exist\n", rpath);
966 if(S_ISREG(mode) || S_ISLNK(mode) || S_ISCHR(mode) || S_ISBLK(mode)) {
967 if(stat(lpath, &st) == 0) {
968 if(S_ISDIR(st.st_mode)) {
969 /* if we're copying a remote file to a local directory,
970 ** we *really* want to copy to localdir + "/" + remotefilename
972 const char *name = sdb_dirstop(rpath);
978 int tmplen = strlen(name) + strlen(lpath) + 2;
979 char *tmp = malloc(tmplen);
980 if(tmp == 0) return 1;
981 snprintf(tmp, tmplen, "%s/%s", lpath, name);
986 if(sync_recv(fd, rpath, lpath)) {
993 } else if(S_ISDIR(mode)) {
995 if (copy_remote_dir_local(fd, rpath, lpath, 0)) {
1003 fprintf(stderr,"remote object '%s' not a file or directory\n", rpath);
1008 int do_sync_sync(const char *lpath, const char *rpath, int listonly)
1010 fprintf(stderr,"syncing %s...\n",rpath);
1012 int fd = sdb_connect("sync:");
1014 fprintf(stderr,"error: %s\n", sdb_error());
1019 if(copy_local_dir_remote(fd, lpath, rpath, 1, listonly)){