int mzcp_start_scan(CPSettings *settings) {
- struct stat s;
- if(stat(settings->from, &s)) {
+ if(stat(settings->from, &settings->root_stat)) {
// TODO: error
return 1;
}
- if(!S_ISDIR(s.st_mode)) {
+ if(!S_ISDIR(settings->root_stat.st_mode)) {
// queue single file
queue_begin = queue_root_elm_new();
if(!queue_begin) {
}
file->path = root;
file->isdir = 1;
+ file->mode = settings->root_stat.st_mode;
if(enqueue_file(file)) {
scan_complete = 1;
// TODO: error
SrcFile *f = calloc(1, sizeof(SrcFile));
f->path = util_concat_path(elm->path, name);
f->isdir = S_ISDIR(s.st_mode);
+ f->mode = s.st_mode;
f->depends_on = elm;
if(enqueue_file(f)) {
void* copy_run(void *data) {
CPSettings *settings = data;
+
+ char *buffer = malloc(MZ_COPY_BUFSIZE);
+ size_t bufsize = MZ_COPY_BUFSIZE;
+
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 *from = file->path ? util_concat_path(settings->from, file->path) : settings->from;
char *to = util_concat_path(settings->to, file->path ? file->path : util_resource_name(settings->from));
- printf("dst: %s\n", to);
+
+ size_t from_len = strlen(from);
+ size_t to_len = strlen(to);
+
+ if(from[from_len-1] == '/') {
+ from[from_len-1] = 0;
+ }
+ if(to[to_len-1] == '/') {
+ to[to_len-1] = 0;
+ }
+
+ int ret;
+ if(file->isdir) {
+ ret = mz_copy_dir(settings, file, from, to);
+ } else {
+ ret = mz_copy_file(settings, file, from, to, buffer, bufsize);
+ }
free(to);
if(from != settings->from) {
return NULL;
}
+int mz_copy_dir(CPSettings *settings, SrcFile *file, const char *from, const char *to) {
+ printf("mkdir %s\n", to);
+ return mkdir(to, file->mode);
+}
+
+int mz_copy_file(CPSettings *settings, SrcFile *file, const char *from, const char *to, char *buffer, size_t bufsize) {
+ printf("cp %s %s\n", from, to);
+ int fin = open(from, O_RDONLY);
+ if(fin < 0) {
+ return 1;
+ }
+
+ int ret = 0;
+
+ int fout = open(to, O_WRONLY|O_CREAT, file->mode);
+ if(fout < 0) {
+ perror("open");
+ close(fin);
+ return 1;
+ }
+
+ ssize_t r;
+ while((r = read(fin, buffer, bufsize)) > 0) {
+ if(write(fout, buffer, r) != r) {
+ ret = 1;
+ break;
+ }
+ }
+
+ close(fin);
+ close(fout);
+
+ return ret;
+}
#define MAIN_H
#include <stdlib.h>
+#include <unistd.h>
+#include <sys/stat.h>
#include <ucx/string.h>
#define CLIENT_MSG_BUFSIZE 512
#define MAX_COPY_THREADS 32
+#define MZ_COPY_BUFSIZE 4 * 1024 * 1024
typedef char CPBool;
CPBool url;
CPBool pause;
CPBool printsocket;
+ struct stat root_stat;
} CPSettings;
struct MZLock {
*/
CPBool finished;
+ /*
+ * file mode
+ */
+ mode_t mode;
+
/*
* processing this file depends on another file (directory)
*/
int mzcp_start_copy_threads(CPSettings *settings);
void* copy_run(void *data);
+int mz_copy_dir(CPSettings *settings, SrcFile *file, const char *from, const char *to);
+int mz_copy_file(CPSettings *settings, SrcFile *file, const char *from, const char *to, char *buffer, size_t bufsize);
#ifdef __cplusplus