+
+
+int mzcp_start_scan(CPSettings *settings) {
+ struct stat s;
+ if(stat(settings->from, &s)) {
+ // TODO: error
+ return 1;
+ }
+
+ if(!S_ISDIR(s.st_mode)) {
+ // queue single file
+ queue_begin = queue_root_elm_new();
+ if(!queue_begin) {
+ return 1;
+ }
+ queue_end = queue_begin;
+ scan_complete = 1;
+ settings->num_threads = 1;
+ } else {
+ // scan src directory in a separate thread
+ if(pthread_create(&scan_thread, NULL, scan_run, settings)) {
+ // TODO: we need some clever error handling
+ // we are already in the forked procress and stdout/stderr are closed
+ // maybe wait for someone to connect to the unix domain socket
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+
+void* scan_run(void *data) {
+ CPSettings *settings = data;
+
+ UcxList *stack = NULL;
+
+ char *root = strdup("");
+
+ SrcFile *file = calloc(1, sizeof(SrcFile));
+ if(!file) {
+ scan_complete = 1;
+ // TODO: error
+ return NULL;
+ }
+ file->path = root;
+ file->isdir = 1;
+ if(enqueue_file(file)) {
+ scan_complete = 1;
+ // TODO: error
+ return NULL;
+ }
+
+ stack = ucx_list_prepend(NULL, file);
+ while(stack) {
+ SrcFile *elm = stack->data;
+ UcxList *next = stack->next;
+ free(stack);
+ stack = next;
+
+ char *path = util_concat_path(settings->from, elm->path);
+
+ int dir_fd = open(path, O_RDONLY);
+ if(dir_fd < 0) {
+ // TODO: error?
+ continue;
+ }
+
+ // read directory and enqueue all children
+ DIR *dir = fdopendir(dir_fd);
+ struct dirent *ent;
+ while((ent = readdir(dir)) != NULL) {
+ char *name = ent->d_name;
+ if(!strcmp(name, ".") || !strcmp(name, "..")) {
+ continue;
+ }
+
+ struct stat s;
+ if(fstatat(dir_fd, name, &s, 0)) {
+ // TODO: error?
+ continue;
+ }
+
+ SrcFile *f = calloc(1, sizeof(SrcFile));
+ f->path = util_concat_path(elm->path, name);
+ f->isdir = S_ISDIR(s.st_mode);
+ f->depends_on = elm;
+
+ if(enqueue_file(f)) {
+ // TODO: error?
+ fprintf(stderr, "enqueue failed\n");
+ break;
+ }
+
+ // put dir on stack
+ if(f->isdir) {
+ stack = ucx_list_prepend(stack, f);
+ }
+ }
+
+ closedir(dir);
+ }
+
+ scan_complete = 1;
+
+ return NULL;
+}
+
+int mzcp_start_copy_threads(CPSettings *settings) {
+ if(settings->num_threads == 0) {
+ num_copy_threads = 1;
+ } else if(settings->num_threads > MAX_COPY_THREADS) {
+ num_copy_threads = MAX_COPY_THREADS;
+ } else {
+ num_copy_threads = settings->num_threads;
+ }
+
+ copy_threads = calloc(num_copy_threads, sizeof(pthread_t));
+
+ int f = 0;
+ for(int i=0;i<num_copy_threads;i++) {
+ if(pthread_create(©_threads[i], NULL, copy_run, settings)) {
+ f++;
+ }
+ }
+
+ return f < num_copy_threads ? 0 : 1;
+}
+
+int enqueue_file(SrcFile *file) {
+ MZQueue *q = malloc(sizeof(MZQueue));
+ if(!q) {
+ return 1;
+ }
+
+ q->file = file;
+ q->next = NULL;
+
+ pthread_mutex_lock(&queue_lock);
+
+ if(queue_end) {
+ queue_end->next = q;
+ queue_end = q;
+ } else {
+ queue_begin = q;
+ queue_end = q;
+ }
+
+ pthread_cond_signal(&queue_available);
+ pthread_mutex_unlock(&queue_lock);
+
+ return 0;
+}
+
+static SrcFile* queue_get_file(void) {
+ SrcFile *file = NULL;
+ pthread_mutex_lock(&queue_lock);
+
+ MZQueue *q = NULL;
+ while(!q) {
+ if(!queue_begin) {
+ if(scan_complete) {
+ break;
+ }
+ pthread_cond_wait(&queue_available, &queue_lock);
+ continue;
+ } else {
+ q = queue_begin;
+ queue_begin = queue_begin->next;
+ if(!queue_begin) {
+ queue_end = NULL;
+ }
+ }
+ }
+
+ if(q) {
+ file = q->file;
+ free(q);
+ }
+
+ pthread_mutex_unlock(&queue_lock);
+
+ return file;
+}
+
+void* copy_run(void *data) {
+ CPSettings *settings = data;
+ for(;;) {
+ SrcFile *file = queue_get_file();
+ if(!file) {
+ break;
+ }
+
+ char *from = file->path ? util_concat_path(settings->from, file->path) : settings->from;
+ printf("src: %s\n", from);
+
+ char *to = util_concat_path(settings->to, file->path ? file->path : util_resource_name(settings->from));
+ printf("dst: %s\n", to);
+
+ free(to);
+ if(from != settings->from) {
+ free(from);
+ }
+ }
+
+ return NULL;
+}
+