int do_sync_push(const char *lpath, const char *rpath, int show_progress)
{
    struct stat st;
    unsigned mode;
    int fd;

    fd = adb_connect("sync:");
    if(fd < 0) {
        fprintf(stderr,"error: %s\n", adb_error());
        return 1;
    }

    if(stat(lpath, &st)) {
        fprintf(stderr,"cannot stat '%s': %s\n", lpath, strerror(errno));
        sync_quit(fd);
        return 1;
    }

    if(S_ISDIR(st.st_mode)) {
        BEGIN();
        if(copy_local_dir_remote(fd, lpath, rpath, 0, 0)) {
            return 1;
        } else {
            END();
            sync_quit(fd);
        }
    } else {
        if(sync_readmode(fd, rpath, &mode)) {
            return 1;
        }
        if((mode != 0) && S_ISDIR(mode)) {
                /* if we're copying a local file to a remote directory,
                ** we *really* want to copy to remotedir + "/" + localfilename
                */
            const char *name = adb_dirstop(lpath);
            if(name == 0) {
                name = lpath;
            } else {
                name++;
            }
            int  tmplen = strlen(name) + strlen(rpath) + 2;
            char *tmp = reinterpret_cast<char*>(
                malloc(strlen(name) + strlen(rpath) + 2));
            if(tmp == 0) return 1;
            snprintf(tmp, tmplen, "%s/%s", rpath, name);
            rpath = tmp;
        }
        BEGIN();
        if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, show_progress)) {
            return 1;
        } else {
            END();
            sync_quit(fd);
            return 0;
        }
    }

    return 0;
}
Esempio n. 2
0
static int _do_sync_push(const char *lpath, const char *rpath, int show_progress) {
    struct stat st;
    unsigned mode;
    int fd;

    if((fd = _adb_connect("sync:")) < 0) {
        return 1;
    }

    if(stat(lpath, &st)) {
        sync_quit(fd);
        return 2;
    }

    if(S_ISDIR(st.st_mode)) {
        if(copy_local_dir_remote(fd, lpath, rpath, 0, 0)) {
            return 3;
        } else {
            sync_quit(fd);
        }
    } else {
        if(sync_readmode(fd, rpath, &mode)) {
            return 4;
        }
        if((mode != 0) && S_ISDIR(mode)) {
            /* if we're copying a local file to a remote directory,
            ** we *really* want to copy to remotedir + "/" + localfilename
            */
            const char *name = strrchr(lpath, '/');
            if(name == 0) {
                name = lpath;
            } else {
                name++;
            }
            int  tmplen = strlen(name) + strlen(rpath) + 2;
            char *tmp = malloc(strlen(name) + strlen(rpath) + 2);
            if(tmp == 0) return 1;
            snprintf(tmp, tmplen, "%s/%s", rpath, name);
            rpath = tmp;
        }
        if(sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, show_progress)) {
            return 5;
        } else {
            sync_quit(fd);
            return 0;
        }
    }

    return 0;
}
int do_sync_push(const char* lpath, const char* rpath, bool show_progress) {
    std::string error;
    int fd = adb_connect("sync:", &error);
    if (fd < 0) {
        fprintf(stderr,"error: %s\n", error.c_str());
        return 1;
    }

    struct stat st;
    if (stat(lpath, &st)) {
        fprintf(stderr,"cannot stat '%s': %s\n", lpath, strerror(errno));
        sync_quit(fd);
        return 1;
    }

    if (S_ISDIR(st.st_mode)) {
        BEGIN();
        if (copy_local_dir_remote(fd, lpath, rpath, 0, 0)) {
            return 1;
        }
    } else {
        unsigned mode;
        if (sync_readmode(fd, rpath, &mode)) {
            return 1;
        }
        std::string path_holder;
        if ((mode != 0) && S_ISDIR(mode)) {
            // If we're copying a local file to a remote directory,
            // we really want to copy to remote_dir + "/" + local_filename.
            path_holder = android::base::StringPrintf("%s/%s", rpath, adb_basename(lpath).c_str());
            rpath = path_holder.c_str();
        }
        BEGIN();
        if (sync_send(fd, lpath, rpath, st.st_mtime, st.st_mode, show_progress)) {
            return 1;
        }
    }

    END();
    sync_quit(fd);
    return 0;
}
Esempio n. 4
0
int wthreads_init() {
	// create queues for working threads
	int err;
	if(err = rqueue_init(&unprot_queue_g, unprot_queue_data_g, MAX_QUEUE_SIZE)) 
		return err;
	if(err = rqueue_init(&sync_queue_g, sync_queue_data_g, MAX_QUEUE_SIZE)) 
		return err;

	// start working threads
	if(semaph_init(&init_sem_g, 0))
		return -1;
	pthread_t dummy_pthread;
	if(pthread_create(&dummy_pthread, 0, unprot_thread, 0)) {
		fprintf(stderr, "wthread_init: can\'t start unprot thread\n");
		return -1;
	}
	if(pthread_create(&dummy_pthread, 0, sync_thread, 0)) {
		fprintf(stderr, "wthread_init: can\'t start sync thread\n");
		unprot_quit();
		return -1;
	}
	// set exit handlers
	if(semaph_wait(&init_sem_g) || semaph_wait(&init_sem_g) ||
		 atexit(unprot_quit) || atexit(sync_quit)) {
		fprintf(stderr, "wthread_init: can\'t finish initialization\n");
		unprot_quit();
		sync_quit();
	}
	// add to immute threads
	if(immune_nthreads_g + 2 > MAX_NTHREADS) {
		fprintf(stderr, "wthread_init: too many immune threads\n");
		unprot_quit();
		sync_quit();
	}
	immune_threads_g[immune_nthreads_g++] = unprot_thread_g;
	immune_threads_g[immune_nthreads_g++] = sync_thread_g;

	// destroy initialization semaphore
	semaph_destroy(&init_sem_g);

	return 0;
}  // wthread_init
int do_sync_pull(const char* rpath, const char* lpath, bool show_progress, int copy_attrs) {
    std::string error;
    int fd = adb_connect("sync:", &error);
    if (fd < 0) {
        fprintf(stderr,"error: %s\n", error.c_str());
        return 1;
    }

    unsigned mode, time;
    if (sync_readtime(fd, rpath, &time, &mode)) {
        return 1;
    }
    if (mode == 0) {
        fprintf(stderr,"remote object '%s' does not exist\n", rpath);
        return 1;
    }

    if (S_ISREG(mode) || S_ISLNK(mode) || S_ISCHR(mode) || S_ISBLK(mode)) {
        std::string path_holder;
        struct stat st;
        if (stat(lpath, &st) == 0) {
            if (S_ISDIR(st.st_mode)) {
                // If we're copying a remote file to a local directory,
                // we really want to copy to local_dir + "/" + basename(remote).
                path_holder = android::base::StringPrintf("%s/%s", lpath, adb_basename(rpath).c_str());
                lpath = path_holder.c_str();
            }
        }
        BEGIN();
        if (sync_recv(fd, rpath, lpath, show_progress)) {
            return 1;
        } else {
            if (copy_attrs && set_time_and_mode(lpath, time, mode)) {
                return 1;
            }
        }
    } else if(S_ISDIR(mode)) {
        BEGIN();
        if (copy_remote_dir_local(fd, rpath, lpath, copy_attrs)) {
            return 1;
        }
    } else {
        fprintf(stderr,"remote object '%s' not a file or directory\n", rpath);
        return 1;
    }
    END();
    sync_quit(fd);
    return 0;
}
Esempio n. 6
0
int do_sync_ls(const char *path)
{
    int fd = adb_connect("sync:");
    if(fd < 0) {
        fprintf(stderr,"error: %s\n", adb_error());
        return 1;
    }

    if(sync_ls(fd, path, do_sync_ls_cb, 0)) {
        return 1;
    } else {
        sync_quit(fd);
        return 0;
    }
}
int do_sync_ls(const char* path) {
    std::string error;
    int fd = adb_connect("sync:", &error);
    if (fd < 0) {
        fprintf(stderr,"error: %s\n", error.c_str());
        return 1;
    }

    if (sync_ls(fd, path, do_sync_ls_cb, 0)) {
        return 1;
    }

    sync_quit(fd);
    return 0;
}
Esempio n. 8
0
int do_sync_sync(const char *lpath, const char *rpath, int listonly)
{
    fprintf(stderr,"syncing %s...\n",rpath);

    int fd = adb_connect("sync:");
    if(fd < 0) {
        fprintf(stderr,"error: %s\n", adb_error());
        return 1;
    }

    BEGIN();
    if(copy_local_dir_remote(fd, lpath, rpath, 1, listonly)){
        return 1;
    } else {
        END();
        sync_quit(fd);
        return 0;
    }
}
int do_sync_sync(const std::string& lpath, const std::string& rpath, bool list_only)
{
    fprintf(stderr, "syncing %s...\n", rpath.c_str());

    std::string error;
    int fd = adb_connect("sync:", &error);
    if (fd < 0) {
        fprintf(stderr, "error: %s\n", error.c_str());
        return 1;
    }

    BEGIN();
    if (copy_local_dir_remote(fd, lpath.c_str(), rpath.c_str(), 1, list_only)) {
        return 1;
    }
    END();
    sync_quit(fd);
    return 0;
}
Esempio n. 10
0
int do_sync_pull(const char *rpath, const char *lpath, int show_progress, int copy_attrs)
{
    unsigned mode, time;
    struct stat st;

    int fd;

    fd = adb_connect("sync:");
    if(fd < 0) {
        fprintf(stderr,"error: %s\n", adb_error());
        return 1;
    }

    if(sync_readtime(fd, rpath, &time, &mode)) {
        return 1;
    }
    if(mode == 0) {
        fprintf(stderr,"remote object '%s' does not exist\n", rpath);
        return 1;
    }

    if(S_ISREG(mode) || S_ISLNK(mode) || S_ISCHR(mode) || S_ISBLK(mode)) {
        if(stat(lpath, &st) == 0) {
            if(S_ISDIR(st.st_mode)) {
                    /* if we're copying a remote file to a local directory,
                    ** we *really* want to copy to localdir + "/" + remotefilename
                    */
                const char *name = adb_dirstop(rpath);
                if(name == 0) {
                    name = rpath;
                } else {
                    name++;
                }
                int  tmplen = strlen(name) + strlen(lpath) + 2;
                char *tmp = malloc(tmplen);
                if(tmp == 0) return 1;
                snprintf(tmp, tmplen, "%s/%s", lpath, name);
                lpath = tmp;
            }
        }
        BEGIN();
        if (sync_recv(fd, rpath, lpath, show_progress)) {
            return 1;
        } else {
            if (copy_attrs && set_time_and_mode(lpath, time, mode))
                return 1;
            END();
            sync_quit(fd);
            return 0;
        }
    } else if(S_ISDIR(mode)) {
        BEGIN();
        if (copy_remote_dir_local(fd, rpath, lpath, copy_attrs)) {
            return 1;
        } else {
            END();
            sync_quit(fd);
            return 0;
        }
    } else {
        fprintf(stderr,"remote object '%s' not a file or directory\n", rpath);
        return 1;
    }
}