* initial contribution for 2.0 beta (0.0.2)
[framework/system/sdbd.git] / src / 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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <time.h>
24 #include <dirent.h>
25 #include <limits.h>
26 #include <sys/types.h>
27 #if 0 //eric
28 #include <zipfile/zipfile.h>
29 #endif
30 #include "sysdeps.h"
31 #include "sdb.h"
32 #include "sdb_client.h"
33 #include "file_sync_service.h"
34
35
36 static unsigned total_bytes;
37 static long long start_time;
38
39 static long long NOW()
40 {
41     struct timeval tv;
42     gettimeofday(&tv, 0);
43     return ((long long) tv.tv_usec) +
44         1000000LL * ((long long) tv.tv_sec);
45 }
46
47 static void BEGIN()
48 {
49     total_bytes = 0;
50     start_time = NOW();
51 }
52
53 static void END()
54 {
55     long long t = NOW() - start_time;
56     if(total_bytes == 0) return;
57
58     if (t == 0)  /* prevent division by 0 :-) */
59         t = 1000000;
60
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);
64 }
65
66 void sync_quit(int fd)
67 {
68     syncmsg msg;
69
70     msg.req.id = ID_QUIT;
71     msg.req.namelen = 0;
72
73     writex(fd, &msg.req, sizeof(msg.req));
74 }
75
76 typedef void (*sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char *name, void *cookie);
77
78 int sync_ls(int fd, const char *path, sync_ls_cb func, void *cookie)
79 {
80     syncmsg msg;
81     char buf[257];
82     int len;
83
84     len = strlen(path);
85     if(len > 1024) goto fail;
86
87     msg.req.id = ID_LIST;
88     msg.req.namelen = htoll(len);
89
90     if(writex(fd, &msg.req, sizeof(msg.req)) ||
91        writex(fd, path, len)) {
92         goto fail;
93     }
94
95     for(;;) {
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;
99
100         len = ltohl(msg.dent.namelen);
101         if(len > 256) break;
102
103         if(readx(fd, buf, len)) break;
104         buf[len] = 0;
105
106         func(ltohl(msg.dent.mode),
107              ltohl(msg.dent.size),
108              ltohl(msg.dent.time),
109              buf, cookie);
110     }
111
112 fail:
113     sdb_close(fd);
114     return -1;
115 }
116
117 typedef struct syncsendbuf syncsendbuf;
118
119 struct syncsendbuf {
120     unsigned id;
121     unsigned size;
122     char data[SYNC_DATA_MAX];
123 };
124
125 static syncsendbuf send_buffer;
126
127 int sync_readtime(int fd, const char *path, unsigned *timestamp)
128 {
129     syncmsg msg;
130     int len = strlen(path);
131
132     msg.req.id = ID_STAT;
133     msg.req.namelen = htoll(len);
134
135     if(writex(fd, &msg.req, sizeof(msg.req)) ||
136        writex(fd, path, len)) {
137         return -1;
138     }
139
140     if(readx(fd, &msg.stat, sizeof(msg.stat))) {
141         return -1;
142     }
143
144     if(msg.stat.id != ID_STAT) {
145         return -1;
146     }
147
148     *timestamp = ltohl(msg.stat.time);
149     return 0;
150 }
151
152 static int sync_start_readtime(int fd, const char *path)
153 {
154     syncmsg msg;
155     int len = strlen(path);
156
157     msg.req.id = ID_STAT;
158     msg.req.namelen = htoll(len);
159
160     if(writex(fd, &msg.req, sizeof(msg.req)) ||
161        writex(fd, path, len)) {
162         return -1;
163     }
164
165     return 0;
166 }
167
168 static int sync_finish_readtime(int fd, unsigned int *timestamp,
169                                 unsigned int *mode, unsigned int *size)
170 {
171     syncmsg msg;
172
173     if(readx(fd, &msg.stat, sizeof(msg.stat)))
174         return -1;
175
176     if(msg.stat.id != ID_STAT)
177         return -1;
178
179     *timestamp = ltohl(msg.stat.time);
180     *mode = ltohl(msg.stat.mode);
181     *size = ltohl(msg.stat.size);
182
183     return 0;
184 }
185
186 int sync_readmode(int fd, const char *path, unsigned *mode)
187 {
188     syncmsg msg;
189     int len = strlen(path);
190
191     msg.req.id = ID_STAT;
192     msg.req.namelen = htoll(len);
193
194     if(writex(fd, &msg.req, sizeof(msg.req)) ||
195        writex(fd, path, len)) {
196         return -1;
197     }
198
199     if(readx(fd, &msg.stat, sizeof(msg.stat))) {
200         return -1;
201     }
202
203     if(msg.stat.id != ID_STAT) {
204         return -1;
205     }
206
207     *mode = ltohl(msg.stat.mode);
208     return 0;
209 }
210
211 static int write_data_file(int fd, const char *path, syncsendbuf *sbuf)
212 {
213     int lfd, err = 0;
214
215     lfd = sdb_open(path, O_RDONLY);
216     if(lfd < 0) {
217         fprintf(stderr,"cannot open '%s': %s\n", path, strerror(errno));
218         return -1;
219     }
220
221     sbuf->id = ID_DATA;
222     for(;;) {
223         int ret;
224
225         ret = sdb_read(lfd, sbuf->data, SYNC_DATA_MAX);
226         if(!ret)
227             break;
228
229         if(ret < 0) {
230             if(errno == EINTR)
231                 continue;
232             fprintf(stderr,"cannot read '%s': %s\n", path, strerror(errno));
233             break;
234         }
235
236         sbuf->size = htoll(ret);
237         if(writex(fd, sbuf, sizeof(unsigned) * 2 + ret)){
238             err = -1;
239             break;
240         }
241         total_bytes += ret;
242     }
243
244     sdb_close(lfd);
245     return err;
246 }
247
248 static int write_data_buffer(int fd, char* file_buffer, int size, syncsendbuf *sbuf)
249 {
250     int err = 0;
251     int total = 0;
252
253     sbuf->id = ID_DATA;
254     while (total < size) {
255         int count = size - total;
256         if (count > SYNC_DATA_MAX) {
257             count = SYNC_DATA_MAX;
258         }
259
260         memcpy(sbuf->data, &file_buffer[total], count);
261         sbuf->size = htoll(count);
262         if(writex(fd, sbuf, sizeof(unsigned) * 2 + count)){
263             err = -1;
264             break;
265         }
266         total += count;
267         total_bytes += count;
268     }
269
270     return err;
271 }
272
273 #ifdef HAVE_SYMLINKS
274 static int write_data_link(int fd, const char *path, syncsendbuf *sbuf)
275 {
276     int len, ret;
277
278     len = readlink(path, sbuf->data, SYNC_DATA_MAX-1);
279     if(len < 0) {
280         fprintf(stderr, "error reading link '%s': %s\n", path, strerror(errno));
281         return -1;
282     }
283     sbuf->data[len] = '\0';
284
285     sbuf->size = htoll(len + 1);
286     sbuf->id = ID_DATA;
287
288     ret = writex(fd, sbuf, sizeof(unsigned) * 2 + len + 1);
289     if(ret)
290         return -1;
291
292     total_bytes += len + 1;
293
294     return 0;
295 }
296 #endif
297
298 static int sync_send(int fd, const char *lpath, const char *rpath,
299                      unsigned mtime, mode_t mode, int verifyApk)
300 {
301     syncmsg msg;
302     int len, r;
303     syncsendbuf *sbuf = &send_buffer;
304     char* file_buffer = NULL;
305     int size = 0;
306     char tmp[64];
307
308     len = strlen(rpath);
309     if(len > 1024) goto fail;
310
311     snprintf(tmp, sizeof(tmp), ",%d", mode);
312     r = strlen(tmp);
313
314     if (verifyApk) {
315 #if 0 //eric
316         int lfd;
317         zipfile_t zip;
318         zipentry_t entry;
319         int amt;
320
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);
325         if(lfd < 0) {
326             fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
327             return -1;
328         }
329
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);
333             sdb_close(lfd);
334             return 1;
335         }
336
337         file_buffer = (char *)malloc(size);
338         if (file_buffer == NULL) {
339             fprintf(stderr, "could not allocate buffer for '%s'\n",
340                     lpath);
341             sdb_close(lfd);
342             return 1;
343         }
344         amt = sdb_read(lfd, file_buffer, size);
345         if (amt != size) {
346             fprintf(stderr, "error reading from file: '%s'\n", lpath);
347             sdb_close(lfd);
348             free(file_buffer);
349             return 1;
350         }
351
352         sdb_close(lfd);
353
354         zip = init_zipfile(file_buffer, size);
355         if (zip == NULL) {
356             fprintf(stderr, "file '%s' is not a valid zip file\n",
357                     lpath);
358             free(file_buffer);
359             return 1;
360         }
361
362         entry = lookup_zipentry(zip, "AndroidManifest.xml");
363         release_zipfile(zip);
364         if (entry == NULL) {
365             fprintf(stderr, "file '%s' does not contain AndroidManifest.xml\n",
366                     lpath);
367             free(file_buffer);
368             return 1;
369         }
370 #endif
371     }
372
373     msg.req.id = ID_SEND;
374     msg.req.namelen = htoll(len + r);
375
376     if(writex(fd, &msg.req, sizeof(msg.req)) ||
377        writex(fd, rpath, len) || writex(fd, tmp, r)) {
378         free(file_buffer);
379         goto fail;
380     }
381
382     if (file_buffer) {
383         write_data_buffer(fd, file_buffer, size, sbuf);
384         free(file_buffer);
385     } else if (S_ISREG(mode))
386         write_data_file(fd, lpath, sbuf);
387 #ifdef HAVE_SYMLINKS
388     else if (S_ISLNK(mode))
389         write_data_link(fd, lpath, sbuf);
390 #endif
391     else
392         goto fail;
393
394     msg.data.id = ID_DONE;
395     msg.data.size = htoll(mtime);
396     if(writex(fd, &msg.data, sizeof(msg.data)))
397         goto fail;
398
399     if(readx(fd, &msg.status, sizeof(msg.status)))
400         return -1;
401
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)) {
407                 return -1;
408             }
409             sbuf->data[len] = 0;
410         } else
411             strcpy(sbuf->data, "unknown reason");
412
413         fprintf(stderr,"failed to copy '%s' to '%s': %s\n", lpath, rpath, sbuf->data);
414         return -1;
415     }
416
417     return 0;
418
419 fail:
420     fprintf(stderr,"protocol failure\n");
421     sdb_close(fd);
422     return -1;
423 }
424
425 static int mkdirs(char *name)
426 {
427     int ret;
428     char *x = name + 1;
429
430     for(;;) {
431         x = sdb_dirstart(x);
432         if(x == 0) return 0;
433         *x = 0;
434         ret = sdb_mkdir(name, 0775);
435         *x = OS_PATH_SEPARATOR;
436         if((ret < 0) && (errno != EEXIST)) {
437             return ret;
438         }
439         x++;
440     }
441     return 0;
442 }
443
444 int sync_recv(int fd, const char *rpath, const char *lpath)
445 {
446     syncmsg msg;
447     int len;
448     int lfd = -1;
449     char *buffer = send_buffer.data;
450     unsigned id;
451
452     len = strlen(rpath);
453     if(len > 1024) return -1;
454
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)) {
459         return -1;
460     }
461
462     if(readx(fd, &msg.data, sizeof(msg.data))) {
463         return -1;
464     }
465     id = msg.data.id;
466
467     if((id == ID_DATA) || (id == ID_DONE)) {
468         sdb_unlink(lpath);
469         mkdirs((char *)lpath);
470         lfd = sdb_creat(lpath, 0644);
471         if(lfd < 0) {
472             fprintf(stderr,"cannot create '%s': %s\n", lpath, strerror(errno));
473             return -1;
474         }
475         goto handle_data;
476     } else {
477         goto remote_error;
478     }
479
480     for(;;) {
481         if(readx(fd, &msg.data, sizeof(msg.data))) {
482             return -1;
483         }
484         id = msg.data.id;
485
486     handle_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");
492             sdb_close(lfd);
493             return -1;
494         }
495
496         if(readx(fd, buffer, len)) {
497             sdb_close(lfd);
498             return -1;
499         }
500
501         if(writex(lfd, buffer, len)) {
502             fprintf(stderr,"cannot write '%s': %s\n", rpath, strerror(errno));
503             sdb_close(lfd);
504             return -1;
505         }
506
507         total_bytes += len;
508     }
509
510     sdb_close(lfd);
511     return 0;
512
513 remote_error:
514     sdb_close(lfd);
515     sdb_unlink(lpath);
516
517     if(id == ID_FAIL) {
518         len = ltohl(msg.data.size);
519         if(len > 256) len = 256;
520         if(readx(fd, buffer, len)) {
521             return -1;
522         }
523         buffer[len] = 0;
524     } else {
525         memcpy(buffer, &id, 4);
526         buffer[4] = 0;
527 //        strcpy(buffer,"unknown reason");
528     }
529     fprintf(stderr,"failed to copy '%s' to '%s': %s\n", rpath, lpath, buffer);
530     return 0;
531 }
532
533
534
535 /* --- */
536
537
538 static void do_sync_ls_cb(unsigned mode, unsigned size, unsigned time,
539                           const char *name, void *cookie)
540 {
541     printf("%08x %08x %08x %s\n", mode, size, time, name);
542 }
543
544 int do_sync_ls(const char *path)
545 {
546     int fd = sdb_connect("sync:");
547     if(fd < 0) {
548         fprintf(stderr,"error: %s\n", sdb_error());
549         return 1;
550     }
551
552     if(sync_ls(fd, path, do_sync_ls_cb, 0)) {
553         return 1;
554     } else {
555         sync_quit(fd);
556         return 0;
557     }
558 }
559
560 typedef struct copyinfo copyinfo;
561
562 struct copyinfo
563 {
564     copyinfo *next;
565     const char *src;
566     const char *dst;
567     unsigned int time;
568     unsigned int mode;
569     unsigned int size;
570     int flag;
571     //char data[0];
572 };
573
574 copyinfo *mkcopyinfo(const char *spath, const char *dpath,
575                      const char *name, int isdir)
576 {
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;
582
583     copyinfo *ci = malloc(sizeof(copyinfo) + ssize + dsize);
584     if(ci == 0) {
585         fprintf(stderr,"out of memory\n");
586         abort();
587     }
588
589     ci->next = 0;
590     ci->time = 0;
591     ci->mode = 0;
592     ci->size = 0;
593     ci->flag = 0;
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);
598
599 //    fprintf(stderr,"mkcopyinfo('%s','%s')\n", ci->src, ci->dst);
600     return ci;
601 }
602
603
604 static int local_build_list(copyinfo **filelist,
605                             const char *lpath, const char *rpath)
606 {
607     DIR *d;
608     struct dirent *de;
609     struct stat st;
610     copyinfo *dirlist = 0;
611     copyinfo *ci, *next;
612
613 //    fprintf(stderr,"local_build_list('%s','%s')\n", lpath, rpath);
614
615     d = opendir(lpath);
616     if(d == 0) {
617         fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
618         return -1;
619     }
620
621     while((de = readdir(d))) {
622         char stat_path[PATH_MAX];
623         char *name = de->d_name;
624
625         if(name[0] == '.') {
626             if(name[1] == 0) continue;
627             if((name[1] == '.') && (name[2] == 0)) continue;
628         }
629
630         /*
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.
633          */
634         if (strlen(lpath) + strlen(de->d_name) + 1 > sizeof(stat_path))
635             continue;
636         strcpy(stat_path, lpath);
637         strcat(stat_path, de->d_name);
638         stat(stat_path, &st);
639
640         if (S_ISDIR(st.st_mode)) {
641             ci = mkcopyinfo(lpath, rpath, name, 1);
642             ci->next = dirlist;
643             dirlist = ci;
644         } else {
645             ci = mkcopyinfo(lpath, rpath, name, 0);
646             if(lstat(ci->src, &st)) {
647                 closedir(d);
648                 fprintf(stderr,"cannot stat '%s': %s\n", ci->src, strerror(errno));
649                 return -1;
650             }
651             if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
652                 fprintf(stderr, "skipping special file '%s'\n", ci->src);
653                 free(ci);
654             } else {
655                 ci->time = st.st_mtime;
656                 ci->mode = st.st_mode;
657                 ci->size = st.st_size;
658                 ci->next = *filelist;
659                 *filelist = ci;
660             }
661         }
662     }
663
664     closedir(d);
665
666     for(ci = dirlist; ci != 0; ci = next) {
667         next = ci->next;
668         local_build_list(filelist, ci->src, ci->dst);
669         free(ci);
670     }
671
672     return 0;
673 }
674
675
676 static int copy_local_dir_remote(int fd, const char *lpath, const char *rpath, int checktimestamps, int listonly)
677 {
678     copyinfo *filelist = 0;
679     copyinfo *ci, *next;
680     int pushed = 0;
681     int skipped = 0;
682
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);
689         lpath = tmp;
690     }
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);
696         rpath = tmp;
697     }
698
699     if(local_build_list(&filelist, lpath, rpath)) {
700         return -1;
701     }
702
703     if(checktimestamps){
704         for(ci = filelist; ci != 0; ci = ci->next) {
705             if(sync_start_readtime(fd, ci->dst)) {
706                 return 1;
707             }
708         }
709         for(ci = filelist; ci != 0; ci = ci->next) {
710             unsigned int timestamp, mode, size;
711             if(sync_finish_readtime(fd, &timestamp, &mode, &size))
712                 return 1;
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))
717                     ci->flag = 1;
718             }
719         }
720     }
721     for(ci = filelist; ci != 0; ci = next) {
722         next = ci->next;
723         if(ci->flag == 0) {
724             fprintf(stderr,"%spush: %s -> %s\n", listonly ? "would " : "", ci->src, ci->dst);
725             if(!listonly &&
726                sync_send(fd, ci->src, ci->dst, ci->time, ci->mode, 0 /* no verify APK */)){
727                 return 1;
728             }
729             pushed++;
730         } else {
731             skipped++;
732         }
733         free(ci);
734     }
735
736     fprintf(stderr,"%d file%s pushed. %d file%s skipped.\n",
737             pushed, (pushed == 1) ? "" : "s",
738             skipped, (skipped == 1) ? "" : "s");
739
740     return 0;
741 }
742
743
744 int do_sync_push(const char *lpath, const char *rpath, int verifyApk)
745 {
746     struct stat st;
747     unsigned mode;
748     int fd;
749
750     fd = sdb_connect("sync:");
751     if(fd < 0) {
752         fprintf(stderr,"error: %s\n", sdb_error());
753         return 1;
754     }
755
756     if(stat(lpath, &st)) {
757         fprintf(stderr,"cannot stat '%s': %s\n", lpath, strerror(errno));
758         sync_quit(fd);
759         return 1;
760     }
761
762     if(S_ISDIR(st.st_mode)) {
763         BEGIN();
764         if(copy_local_dir_remote(fd, lpath, rpath, 0, 0)) {
765             return 1;
766         } else {
767             END();
768             sync_quit(fd);
769         }
770     } else {
771         if(sync_readmode(fd, rpath, &mode)) {
772             return 1;
773         }
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
777                 */
778             const char *name = sdb_dirstop(lpath);
779             if(name == 0) {
780                 name = lpath;
781             } else {
782                 name++;
783             }
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);
788             rpath = tmp;
789         }
790         BEGIN();
791         if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, verifyApk)) {
792             return 1;
793         } else {
794             END();
795             sync_quit(fd);
796             return 0;
797         }
798     }
799
800     return 0;
801 }
802
803
804 typedef struct {
805     copyinfo **filelist;
806     copyinfo **dirlist;
807     const char *rpath;
808     const char *lpath;
809 } sync_ls_build_list_cb_args;
810
811 void
812 sync_ls_build_list_cb(unsigned mode, unsigned size, unsigned time,
813                       const char *name, void *cookie)
814 {
815     sync_ls_build_list_cb_args *args = (sync_ls_build_list_cb_args *)cookie;
816     copyinfo *ci;
817
818     if (S_ISDIR(mode)) {
819         copyinfo **dirlist = args->dirlist;
820
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;
825         }
826
827         ci = mkcopyinfo(args->rpath, args->lpath, name, 1);
828         ci->next = *dirlist;
829         *dirlist = ci;
830     } else if (S_ISREG(mode) || S_ISLNK(mode)) {
831         copyinfo **filelist = args->filelist;
832
833         ci = mkcopyinfo(args->rpath, args->lpath, name, 0);
834         ci->time = time;
835         ci->mode = mode;
836         ci->size = size;
837         ci->next = *filelist;
838         *filelist = ci;
839     } else {
840         fprintf(stderr, "skipping special file '%s'\n", name);
841     }
842 }
843
844 static int remote_build_list(int syncfd, copyinfo **filelist,
845                              const char *rpath, const char *lpath)
846 {
847     copyinfo *dirlist = NULL;
848     sync_ls_build_list_cb_args args;
849
850     args.filelist = filelist;
851     args.dirlist = &dirlist;
852     args.rpath = rpath;
853     args.lpath = lpath;
854
855     /* Put the files/dirs in rpath on the lists. */
856     if (sync_ls(syncfd, rpath, sync_ls_build_list_cb, (void *)&args)) {
857         return 1;
858     }
859
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)) {
864             return 1;
865         }
866         free(dirlist);
867         dirlist = next;
868     }
869
870     return 0;
871 }
872
873 static int copy_remote_dir_local(int fd, const char *rpath, const char *lpath,
874                                  int checktimestamps)
875 {
876     copyinfo *filelist = 0;
877     copyinfo *ci, *next;
878     int pulled = 0;
879     int skipped = 0;
880
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);
888         rpath = tmp;
889     }
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);
895         lpath = tmp;
896     }
897
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)) {
901         return -1;
902     }
903
904 #if 0
905     if (checktimestamps) {
906         for (ci = filelist; ci != 0; ci = ci->next) {
907             if (sync_start_readtime(fd, ci->dst)) {
908                 return 1;
909             }
910         }
911         for (ci = filelist; ci != 0; ci = ci->next) {
912             unsigned int timestamp, mode, size;
913             if (sync_finish_readtime(fd, &timestamp, &mode, &size))
914                 return 1;
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))
919                     ci->flag = 1;
920             }
921         }
922     }
923 #endif
924     for (ci = filelist; ci != 0; ci = next) {
925         next = ci->next;
926         if (ci->flag == 0) {
927             fprintf(stderr, "pull: %s -> %s\n", ci->src, ci->dst);
928             if (sync_recv(fd, ci->src, ci->dst)) {
929                 return 1;
930             }
931             pulled++;
932         } else {
933             skipped++;
934         }
935         free(ci);
936     }
937
938     fprintf(stderr, "%d file%s pulled. %d file%s skipped.\n",
939             pulled, (pulled == 1) ? "" : "s",
940             skipped, (skipped == 1) ? "" : "s");
941
942     return 0;
943 }
944
945 int do_sync_pull(const char *rpath, const char *lpath)
946 {
947     unsigned mode;
948     struct stat st;
949
950     int fd;
951
952     fd = sdb_connect("sync:");
953     if(fd < 0) {
954         fprintf(stderr,"error: %s\n", sdb_error());
955         return 1;
956     }
957
958     if(sync_readmode(fd, rpath, &mode)) {
959         return 1;
960     }
961     if(mode == 0) {
962         fprintf(stderr,"remote object '%s' does not exist\n", rpath);
963         return 1;
964     }
965
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
971                     */
972                 const char *name = sdb_dirstop(rpath);
973                 if(name == 0) {
974                     name = rpath;
975                 } else {
976                     name++;
977                 }
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);
982                 lpath = tmp;
983             }
984         }
985         BEGIN();
986         if(sync_recv(fd, rpath, lpath)) {
987             return 1;
988         } else {
989             END();
990             sync_quit(fd);
991             return 0;
992         }
993     } else if(S_ISDIR(mode)) {
994         BEGIN();
995         if (copy_remote_dir_local(fd, rpath, lpath, 0)) {
996             return 1;
997         } else {
998             END();
999             sync_quit(fd);
1000             return 0;
1001         }
1002     } else {
1003         fprintf(stderr,"remote object '%s' not a file or directory\n", rpath);
1004         return 1;
1005     }
1006 }
1007
1008 int do_sync_sync(const char *lpath, const char *rpath, int listonly)
1009 {
1010     fprintf(stderr,"syncing %s...\n",rpath);
1011
1012     int fd = sdb_connect("sync:");
1013     if(fd < 0) {
1014         fprintf(stderr,"error: %s\n", sdb_error());
1015         return 1;
1016     }
1017
1018     BEGIN();
1019     if(copy_local_dir_remote(fd, lpath, rpath, 1, listonly)){
1020         return 1;
1021     } else {
1022         END();
1023         sync_quit(fd);
1024         return 0;
1025     }
1026 }