2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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>
27 // tizen specific #include <zipfile/zipfile.h>
31 #include "sdb_client.h"
32 #include "file_sync_service.h"
35 static unsigned total_bytes;
36 static long long start_time;
37 extern const char* get_basename(const char* filename);
39 static long long NOW()
43 return ((long long) tv.tv_usec) +
44 1000000LL * ((long long) tv.tv_sec);
53 static void END(const char* filename)
55 long long t = NOW() - start_time;
56 if(total_bytes == 0) return;
58 if (t == 0) /* prevent division by 0 :-) */
61 fprintf(stderr,"%-30s %lld KB/s (%lld bytes in %lld.%03llds)\n",
63 ((((long long) total_bytes) * 1000000LL) / t) / 1024LL,
64 (long long) total_bytes, (t / 1000000LL), (t % 1000000LL) / 1000LL);
68 void sync_quit(int fd)
75 writex(fd, &msg.req, sizeof(msg.req));
78 typedef void (*sync_ls_cb)(unsigned mode, unsigned size, unsigned time, const char *name, void *cookie);
80 int sync_ls(int fd, const char *path, sync_ls_cb func, void *cookie)
87 if(len > 1024) goto fail;
90 msg.req.namelen = htoll(len);
92 if(writex(fd, &msg.req, sizeof(msg.req)) ||
93 writex(fd, path, len)) {
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;
102 len = ltohl(msg.dent.namelen);
105 if(readx(fd, buf, len)) break;
108 func(ltohl(msg.dent.mode),
109 ltohl(msg.dent.size),
110 ltohl(msg.dent.time),
119 typedef struct syncsendbuf syncsendbuf;
124 char data[SYNC_DATA_MAX];
127 static syncsendbuf send_buffer;
129 int sync_readtime(int fd, const char *path, unsigned *timestamp)
132 int len = strlen(path);
134 msg.req.id = ID_STAT;
135 msg.req.namelen = htoll(len);
137 if(writex(fd, &msg.req, sizeof(msg.req)) ||
138 writex(fd, path, len)) {
142 if(readx(fd, &msg.stat, sizeof(msg.stat))) {
146 if(msg.stat.id != ID_STAT) {
150 *timestamp = ltohl(msg.stat.time);
154 static int sync_start_readtime(int fd, const char *path)
157 int len = strlen(path);
159 msg.req.id = ID_STAT;
160 msg.req.namelen = htoll(len);
162 if(writex(fd, &msg.req, sizeof(msg.req)) ||
163 writex(fd, path, len)) {
170 static int sync_finish_readtime(int fd, unsigned int *timestamp,
171 unsigned int *mode, unsigned int *size)
175 if(readx(fd, &msg.stat, sizeof(msg.stat)))
178 if(msg.stat.id != ID_STAT)
181 *timestamp = ltohl(msg.stat.time);
182 *mode = ltohl(msg.stat.mode);
183 *size = ltohl(msg.stat.size);
188 int sync_readmode(int fd, const char *path, unsigned *mode)
191 int len = strlen(path);
193 msg.req.id = ID_STAT;
194 msg.req.namelen = htoll(len);
196 if(writex(fd, &msg.req, sizeof(msg.req)) ||
197 writex(fd, path, len)) {
201 if(readx(fd, &msg.stat, sizeof(msg.stat))) {
205 if(msg.stat.id != ID_STAT) {
209 *mode = ltohl(msg.stat.mode);
213 static int write_data_file(int fd, const char *path, syncsendbuf *sbuf)
217 lfd = sdb_open(path, O_RDONLY);
219 fprintf(stderr,"cannot open '%s': errno:%d\n", path, errno);
227 ret = sdb_read(lfd, sbuf->data, SYNC_DATA_MAX);
234 fprintf(stderr,"cannot read '%s': errno:%d\n", path, errno);
238 sbuf->size = htoll(ret);
239 if(writex(fd, sbuf, sizeof(unsigned) * 2 + ret)){
250 static int write_data_buffer(int fd, char* file_buffer, int size, syncsendbuf *sbuf)
256 while (total < size) {
257 int count = size - total;
258 if (count > SYNC_DATA_MAX) {
259 count = SYNC_DATA_MAX;
262 memcpy(sbuf->data, &file_buffer[total], count);
263 sbuf->size = htoll(count);
264 if(writex(fd, sbuf, sizeof(unsigned) * 2 + count)){
269 total_bytes += count;
276 static int write_data_link(int fd, const char *path, syncsendbuf *sbuf)
280 len = readlink(path, sbuf->data, SYNC_DATA_MAX-1);
282 fprintf(stderr, "error reading link '%s': errno:%d\n", path, errno);
285 sbuf->data[len] = '\0';
287 sbuf->size = htoll(len + 1);
290 ret = writex(fd, sbuf, sizeof(unsigned) * 2 + len + 1);
294 total_bytes += len + 1;
300 static int sync_send(int fd, const char *lpath, const char *rpath,
301 unsigned mtime, mode_t mode, int verifyApk)
305 syncsendbuf *sbuf = &send_buffer;
306 char* file_buffer = NULL;
311 if(len > 1024) goto fail;
313 snprintf(tmp, sizeof(tmp), ",%d", mode);
315 #if 0 /* tizen specific */
322 // if we are transferring an APK file, then sanity check to make sure
323 // we have a real zip file that contains an AndroidManifest.xml
324 // this requires that we read the entire file into memory.
325 lfd = sdb_open(lpath, O_RDONLY);
327 fprintf(stderr,"cannot open '%s': errno:%d\n", lpath, errno);
331 size = sdb_lseek(lfd, 0, SEEK_END);
332 if (size == -1 || -1 == sdb_lseek(lfd, 0, SEEK_SET)) {
333 fprintf(stderr, "error seeking in file '%s'\n", lpath);
338 file_buffer = (char *)malloc(size);
339 if (file_buffer == NULL) {
340 fprintf(stderr, "could not allocate buffer for '%s'\n",
345 amt = sdb_read(lfd, file_buffer, size);
347 fprintf(stderr, "error reading from file: '%s'\n", lpath);
355 zip = init_zipfile(file_buffer, size);
357 fprintf(stderr, "file '%s' is not a valid zip file\n",
363 entry = lookup_zipentry(zip, "AndroidManifest.xml");
364 release_zipfile(zip);
366 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': errno:%d\n", lpath, 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': errno:%d\n", rpath, 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': errno:%d\n", lpath, 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)) {
647 fprintf(stderr,"cannot stat '%s': errno:%d\n", ci->src, errno);
652 if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) {
653 fprintf(stderr, "skipping special file\n");
656 ci->time = st.st_mtime;
657 ci->mode = st.st_mode;
658 ci->size = st.st_size;
659 ci->next = *filelist;
667 for(ci = dirlist; ci != 0; ci = next) {
669 local_build_list(filelist, ci->src, ci->dst);
677 static int copy_local_dir_remote(int fd, const char *lpath, const char *rpath, int checktimestamps, int listonly)
679 copyinfo *filelist = 0;
684 if((lpath[0] == 0) || (rpath[0] == 0)) return -1;
685 if(lpath[strlen(lpath) - 1] != '/') {
686 int tmplen = strlen(lpath)+2;
687 char *tmp = malloc(tmplen);
688 if(tmp == 0) return -1;
689 snprintf(tmp, tmplen, "%s/",lpath);
692 if(rpath[strlen(rpath) - 1] != '/') {
693 int tmplen = strlen(rpath)+2;
694 char *tmp = malloc(tmplen);
695 if(tmp == 0) return -1;
696 snprintf(tmp, tmplen, "%s/",rpath);
700 if(local_build_list(&filelist, lpath, rpath)) {
705 for(ci = filelist; ci != 0; ci = ci->next) {
706 if(sync_start_readtime(fd, ci->dst)) {
710 for(ci = filelist; ci != 0; ci = ci->next) {
711 unsigned int timestamp, mode, size;
712 if(sync_finish_readtime(fd, ×tamp, &mode, &size))
714 if(size == ci->size) {
715 /* for links, we cannot update the atime/mtime */
716 if((S_ISREG(ci->mode & mode) && timestamp == ci->time) ||
717 (S_ISLNK(ci->mode & mode) && timestamp >= ci->time))
722 for(ci = filelist; ci != 0; ci = next) {
725 fprintf(stderr,"%spush: %s -> %s\n", listonly ? "would " : "", ci->src, ci->dst);
727 sync_send(fd, ci->src, ci->dst, ci->time, ci->mode, 0 /* no verify APK */)){
737 fprintf(stderr,"%d file%s pushed. %d file%s skipped.\n",
738 pushed, (pushed == 1) ? "" : "s",
739 skipped, (skipped == 1) ? "" : "s");
745 int do_sync_push(const char *lpath, const char *rpath, int verifyApk, int isUtf8)
754 fd = sdb_connect("sync:");
756 fprintf(stderr,"error: %s\n", sdb_error());
760 if(stat(lpath, &st)) {
761 fprintf(stderr,"cannot stat '%s': errno:%d\n", lpath, errno);
766 if(S_ISDIR(st.st_mode)) {
768 if(copy_local_dir_remote(fd, lpath, rpath, 0, 0)) {
771 END(get_basename(lpath));
775 if(sync_readmode(fd, rpath, &mode)) {
779 if((mode != 0) && S_ISDIR(mode)) {
780 /* if we're copying a local file to a remote directory,
781 ** we *really* want to copy to remotedir + "/" + localfilename
783 const char *name = sdb_dirstop(lpath);
789 int tmplen = strlen(name) + strlen(rpath) + 2;
790 tmp = malloc(strlen(name) + strlen(rpath) + 2);
791 if(tmp == 0) return 1;
792 snprintf(tmp, tmplen, "%s/%s", rpath, name);
793 if (isUtf8 != 0) { //ansi to utf8
794 utf8 = ansi_to_utf8(tmp);
801 if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, verifyApk)) {
805 END(get_basename(lpath));
828 } sync_ls_build_list_cb_args;
831 sync_ls_build_list_cb(unsigned mode, unsigned size, unsigned time,
832 const char *name, void *cookie)
834 sync_ls_build_list_cb_args *args = (sync_ls_build_list_cb_args *)cookie;
838 copyinfo **dirlist = args->dirlist;
840 /* Don't try recursing down "." or ".." */
841 if (name[0] == '.') {
842 if (name[1] == '\0') return;
843 if ((name[1] == '.') && (name[2] == '\0')) return;
846 ci = mkcopyinfo(args->rpath, args->lpath, name, 1);
849 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
850 copyinfo **filelist = args->filelist;
852 ci = mkcopyinfo(args->rpath, args->lpath, name, 0);
856 ci->next = *filelist;
859 fprintf(stderr, "skipping special file '%s'\n", name);
863 static int remote_build_list(int syncfd, copyinfo **filelist,
864 const char *rpath, const char *lpath)
866 copyinfo *dirlist = NULL;
867 sync_ls_build_list_cb_args args;
869 args.filelist = filelist;
870 args.dirlist = &dirlist;
874 /* Put the files/dirs in rpath on the lists. */
875 if (sync_ls(syncfd, rpath, sync_ls_build_list_cb, (void *)&args)) {
879 /* Recurse into each directory we found. */
880 while (dirlist != NULL) {
881 copyinfo *next = dirlist->next;
882 if (remote_build_list(syncfd, filelist, dirlist->src, dirlist->dst)) {
892 static int copy_remote_dir_local(int fd, const char *rpath, const char *lpath,
895 copyinfo *filelist = 0;
900 /* Make sure that both directory paths end in a slash. */
901 if (rpath[0] == 0 || lpath[0] == 0) return -1;
902 if (rpath[strlen(rpath) - 1] != '/') {
903 int tmplen = strlen(rpath) + 2;
904 char *tmp = malloc(tmplen);
905 if (tmp == 0) return -1;
906 snprintf(tmp, tmplen, "%s/", rpath);
909 if (lpath[strlen(lpath) - 1] != '/') {
910 int tmplen = strlen(lpath) + 2;
911 char *tmp = malloc(tmplen);
912 if (tmp == 0) return -1;
913 snprintf(tmp, tmplen, "%s/", lpath);
917 fprintf(stderr, "pull: building file list...\n");
918 /* Recursively build the list of files to copy. */
919 if (remote_build_list(fd, &filelist, rpath, lpath)) {
924 if (checktimestamps) {
925 for (ci = filelist; ci != 0; ci = ci->next) {
926 if (sync_start_readtime(fd, ci->dst)) {
930 for (ci = filelist; ci != 0; ci = ci->next) {
931 unsigned int timestamp, mode, size;
932 if (sync_finish_readtime(fd, ×tamp, &mode, &size))
934 if (size == ci->size) {
935 /* for links, we cannot update the atime/mtime */
936 if ((S_ISREG(ci->mode & mode) && timestamp == ci->time) ||
937 (S_ISLNK(ci->mode & mode) && timestamp >= ci->time))
943 for (ci = filelist; ci != 0; ci = next) {
946 fprintf(stderr, "pull: %s -> %s\n", ci->src, ci->dst);
947 if (sync_recv(fd, ci->src, ci->dst)) {
957 fprintf(stderr, "%d file%s pulled. %d file%s skipped.\n",
958 pulled, (pulled == 1) ? "" : "s",
959 skipped, (skipped == 1) ? "" : "s");
964 int do_sync_pull(const char *rpath, const char *lpath)
971 fd = sdb_connect("sync:");
973 fprintf(stderr,"error: %s\n", sdb_error());
977 if(sync_readmode(fd, rpath, &mode)) {
981 fprintf(stderr,"'%s': No such file or directory\n", rpath);
986 if(S_ISREG(mode) || S_ISLNK(mode) || S_ISCHR(mode) || S_ISBLK(mode)) {
987 if(stat(lpath, &st) == 0) {
988 if(S_ISDIR(st.st_mode)) {
989 /* if we're copying a remote file to a local directory,
990 ** we *really* want to copy to localdir + "/" + remotefilename
992 const char *name = sdb_dirstop(rpath);
998 int tmplen = strlen(name) + strlen(lpath) + 2;
999 char *tmp = malloc(tmplen);
1000 if(tmp == 0) return 1;
1001 snprintf(tmp, tmplen, "%s/%s", lpath, name);
1006 if(sync_recv(fd, rpath, lpath)) {
1009 END(get_basename(rpath));
1013 } else if(S_ISDIR(mode)) {
1015 if (copy_remote_dir_local(fd, rpath, lpath, 0)) {
1018 END(get_basename(rpath));
1023 fprintf(stderr,"'%s': No such file or directory\n", rpath);
1028 int do_sync_sync(const char *lpath, const char *rpath, int listonly)
1030 fprintf(stderr,"syncing %s...\n",rpath);
1032 int fd = sdb_connect("sync:");
1034 fprintf(stderr,"error: %s\n", sdb_error());
1039 if(copy_local_dir_remote(fd, lpath, rpath, 1, listonly)){
1042 END(get_basename(lpath));