Sat, 25 Jul 2020 11:16:31 +0200
add dav-sync outgoing command
dav/main.c | file | annotate | diff | comparison | revisions | |
dav/sync.c | file | annotate | diff | comparison | revisions | |
dav/sync.h | file | annotate | diff | comparison | revisions | |
libidav/utils.c | file | annotate | diff | comparison | revisions | |
libidav/utils.h | file | annotate | diff | comparison | revisions |
--- a/dav/main.c Sun Jul 05 11:55:54 2020 +0200 +++ b/dav/main.c Sat Jul 25 11:16:31 2020 +0200 @@ -715,94 +715,6 @@ return ret; } -static char* ls_date_str(time_t tm) { - struct tm t; - struct tm n; - time_t now = time(NULL); -#ifdef _WIN32 - memcpy(&t, localtime(&tm), sizeof(struct tm)); - memcpy(&n, localtime(&now), sizeof(struct tm)); -#else - localtime_r(&tm, &t); - localtime_r(&now, &n); -#endif /* _WIN32 */ - char *str = malloc(16); - if(t.tm_year == n.tm_year) { - strftime(str, 16, "%b %d %H:%M", &t); - } else { - strftime(str, 16, "%b %d %Y", &t); - } - return str; -} - -static char* ls_size_str(DavResource *res) { - char *str = malloc(16); - uint64_t size = res->contentlength; - - if(res->iscollection) { - str[0] = '\0'; // currently no information for collections - } else if(size < 0x400) { - snprintf(str, 16, "%" PRIu64 " bytes", size); - } else if(size < 0x100000) { - float s = (float)size/0x400; - int diff = (s*100 - (int)s*100); - if(diff > 90) { - diff = 0; - s += 0.10f; - } - if(size < 0x2800 && diff != 0) { - // size < 10 KiB - snprintf(str, 16, "%.1f KiB", s); - } else { - snprintf(str, 16, "%.0f KiB", s); - } - } else if(size < 0x40000000) { - float s = (float)size/0x100000; - int diff = (s*100 - (int)s*100); - if(diff > 90) { - diff = 0; - s += 0.10f; - } - if(size < 0xa00000 && diff != 0) { - // size < 10 MiB - snprintf(str, 16, "%.1f MiB", s); - } else { - size /= 0x100000; - snprintf(str, 16, "%.0f MiB", s); - } - } else if(size < 0x1000000000ULL) { - float s = (float)size/0x40000000; - int diff = (s*100 - (int)s*100); - if(diff > 90) { - diff = 0; - s += 0.10f; - } - if(size < 0x280000000 && diff != 0) { - // size < 10 GiB - snprintf(str, 16, "%.1f GiB", s); - } else { - size /= 0x40000000; - snprintf(str, 16, "%.0f GiB", s); - } - } else { - size /= 1024; - float s = (float)size/0x40000000; - int diff = (s*100 - (int)s*100); - if(diff > 90) { - diff = 0; - s += 0.10f; - } - if(size < 0x280000000 && diff != 0) { - // size < 10 TiB - snprintf(str, 16, "%.1f TiB", s); - } else { - size /= 0x40000000; - snprintf(str, 16, "%.0f TiB", s); - } - } - return str; -} - static char* ls_name(char *parent, char *path, int *len) { if(parent) { path += strlen(parent); @@ -866,8 +778,8 @@ type = ""; } - char *date = ls_date_str(res->lastmodified); - char *size = ls_size_str(res); + char *date = util_date_str(res->lastmodified); + char *size = util_size_str(res->iscollection, res->contentlength); int namelen = strlen(res->name); char *name = recursive ? ls_name(parent, res->path, &namelen) : res->name; @@ -2431,7 +2343,7 @@ printf("size: %d\n", count_children(res)); } else { printf("type: resource\n"); - char *len = ls_size_str(res); + char *len = util_size_str(res->iscollection, res->contentlength); printf("size: %s\n", len); free(len); }
--- a/dav/sync.c Sun Jul 05 11:55:54 2020 +0200 +++ b/dav/sync.c Sat Jul 25 11:16:31 2020 +0200 @@ -205,15 +205,17 @@ } else if(!cfgret) { if(!strcmp(cmd, "pull")) { tid = start_sighandler(&mutex); - ret = cmd_pull(args); + ret = cmd_pull(args, FALSE); stop_sighandler(&mutex, tid); } else if(!strcmp(cmd, "push")) { tid = start_sighandler(&mutex); - ret = cmd_push(args, FALSE); + ret = cmd_push(args, FALSE, FALSE); stop_sighandler(&mutex, tid); + } else if(!strcmp(cmd, "outgoing")) { + ret = cmd_push(args, TRUE, FALSE); } else if(!strcmp(cmd, "archive")) { tid = start_sighandler(&mutex); - ret = cmd_push(args, TRUE); + ret = cmd_push(args, FALSE, TRUE); stop_sighandler(&mutex, tid); } else if(!strcmp(cmd, "restore")) { tid = start_sighandler(&mutex); @@ -546,7 +548,7 @@ } } -int cmd_pull(CmdArgs *a) { +int cmd_pull(CmdArgs *a, DavBool incoming) { if(a->argc != 1) { fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few" : "many"); return -1; @@ -1880,7 +1882,7 @@ return ucx_map_cstr_get(db->conflict, res->path) ? 1 : 0; } -int cmd_push(CmdArgs *a, DavBool archive) { +int cmd_push(CmdArgs *a, DavBool outgoing, DavBool archive) { if(a->argc != 1) { fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few" : "many"); return -1; @@ -1965,7 +1967,7 @@ // lock repository DavBool locked = FALSE; char *locktokenfile = NULL; - if((dir->lockpush || cmd_getoption(a, "lock")) && !cmd_getoption(a, "nolock")) { + if((dir->lockpush || cmd_getoption(a, "lock")) && !cmd_getoption(a, "nolock") && !outgoing) { if(dav_lock_t(root, dir->lock_timeout)) { print_resource_error(sn, "/"); dav_session_destroy(sn); @@ -2000,7 +2002,7 @@ UcxList *ls_move = NULL; UcxList *ls_copy = NULL; UcxList *ls_mkcol = NULL; - + // upload all changed files //UcxList *resources = cmd_getoption(a, "read") ? // read_changes(dir, db) : local_scan(dir, db); @@ -2156,6 +2158,19 @@ ucx_map_cstr_remove(db->resources, local->path); } + if(outgoing) { + print_outgoing( + ls_new, + ls_modified, + ls_conflict, + ls_update, + ls_delete, + ls_move, + ls_copy, + ls_mkcol); + return 0; + } + // // BEGIN PUSH // @@ -2698,6 +2713,103 @@ return ret; } +static void print_outgoging_file(LocalResource *res) { + char *lastmodified = util_date_str(res->last_modified); + char *size = util_size_str(FALSE, res->size); + printf(" %-49s %12s %10s\n", res->path+1, lastmodified, size); + free(lastmodified); + free(size); +} + +void print_outgoing( + UcxList *ls_new, + UcxList *ls_modified, + UcxList *ls_conflict, + UcxList *ls_update, + UcxList *ls_delete, + UcxList *ls_move, + UcxList *ls_copy, + UcxList *ls_mkcol) +{ + printf("%s\n", "File Last Modified Size"); + printf("%s\n", "=============================================================================="); + + int num_files = 0; + int64_t total_size = 0; + + if(ls_mkcol) { + printf("Directories:\n"); + UCX_FOREACH(elm, ls_mkcol) { + LocalResource *res = elm->data; + printf(" %-49s\n", res->path+1); + total_size += res->size; + num_files++; + } + } + if(ls_new) { + printf("New:\n"); + UCX_FOREACH(elm, ls_new) { + LocalResource *res = elm->data; + print_outgoging_file(elm->data); + total_size += res->size; + num_files++; + } + } + if(ls_modified) { + printf("Modified:\n"); + UCX_FOREACH(elm, ls_modified) { + LocalResource *res = elm->data; + print_outgoging_file(elm->data); + total_size += res->size; + num_files++; + } + } + if(ls_update) { + printf("Update:\n"); + UCX_FOREACH(elm, ls_update) { + LocalResource *res = elm->data; + char *lastmodified = util_date_str(res->last_modified); + printf(" %-49s %12s\n", res->path+1, lastmodified); + free(lastmodified); + num_files++; + } + } + if(ls_delete) { + printf("Delete:\n"); + UCX_FOREACH(elm, ls_delete) { + LocalResource *res = elm->data; + printf(" %s\n", res->path+1); + } + } + if(ls_copy) { + printf("Copy:\n"); + UCX_FOREACH(elm, ls_copy) { + LocalResource *res = elm->data; + printf("%s -> %s\n", res->origin->path+1, res->path); + num_files++; + } + } + if(ls_move) { + printf("Move:\n"); + UCX_FOREACH(elm, ls_move) { + LocalResource *res = elm->data; + printf("%s -> %s\n", res->origin->path+1, res->path); + num_files++; + } + } + if(ls_conflict) { + printf("Conflict\n"); + UCX_FOREACH(elm, ls_conflict) { + LocalResource *res = elm->data; + printf(" %s\n", res->path+1); + } + } + + char *total_size_str = util_size_str(FALSE, total_size); + printf("\n%d outgoing files, size: %s\n", num_files, total_size_str); + free(total_size_str); +} + UcxList* local_scan(SyncDirectory *dir, SyncDatabase *db) { UcxList *resources = NULL;
--- a/dav/sync.h Sun Jul 05 11:55:54 2020 +0200 +++ b/dav/sync.h Sat Jul 25 11:16:31 2020 +0200 @@ -100,10 +100,20 @@ void res2map(DavResource *root, UcxMap *map); -int cmd_pull(CmdArgs *args); -int cmd_push(CmdArgs *args, DavBool archive); +int cmd_pull(CmdArgs *args, DavBool incoming); +int cmd_push(CmdArgs *args, DavBool outgoing, DavBool archive); int cmd_restore(CmdArgs *args); +void print_outgoing( + UcxList *ls_new, + UcxList *ls_modified, + UcxList *ls_conflict, + UcxList *ls_update, + UcxList *ls_delete, + UcxList *ls_move, + UcxList *ls_copy, + UcxList *ls_mkcol); + RemoteChangeType resource_get_remote_change( CmdArgs *a, DavResource *res,
--- a/libidav/utils.c Sun Jul 05 11:55:54 2020 +0200 +++ b/libidav/utils.c Sat Jul 25 11:16:31 2020 +0200 @@ -702,6 +702,94 @@ return parent; } +char* util_size_str(DavBool iscollection, uint64_t contentlength) { + char *str = malloc(16); + uint64_t size = contentlength; + + if(iscollection) { + str[0] = '\0'; // currently no information for collections + } else if(size < 0x400) { + snprintf(str, 16, "%" PRIu64 " bytes", size); + } else if(size < 0x100000) { + float s = (float)size/0x400; + int diff = (s*100 - (int)s*100); + if(diff > 90) { + diff = 0; + s += 0.10f; + } + if(size < 0x2800 && diff != 0) { + // size < 10 KiB + snprintf(str, 16, "%.1f KiB", s); + } else { + snprintf(str, 16, "%.0f KiB", s); + } + } else if(size < 0x40000000) { + float s = (float)size/0x100000; + int diff = (s*100 - (int)s*100); + if(diff > 90) { + diff = 0; + s += 0.10f; + } + if(size < 0xa00000 && diff != 0) { + // size < 10 MiB + snprintf(str, 16, "%.1f MiB", s); + } else { + size /= 0x100000; + snprintf(str, 16, "%.0f MiB", s); + } + } else if(size < 0x1000000000ULL) { + float s = (float)size/0x40000000; + int diff = (s*100 - (int)s*100); + if(diff > 90) { + diff = 0; + s += 0.10f; + } + if(size < 0x280000000 && diff != 0) { + // size < 10 GiB + snprintf(str, 16, "%.1f GiB", s); + } else { + size /= 0x40000000; + snprintf(str, 16, "%.0f GiB", s); + } + } else { + size /= 1024; + float s = (float)size/0x40000000; + int diff = (s*100 - (int)s*100); + if(diff > 90) { + diff = 0; + s += 0.10f; + } + if(size < 0x280000000 && diff != 0) { + // size < 10 TiB + snprintf(str, 16, "%.1f TiB", s); + } else { + size /= 0x40000000; + snprintf(str, 16, "%.0f TiB", s); + } + } + return str; +} + +char* util_date_str(time_t tm) { + struct tm t; + struct tm n; + time_t now = time(NULL); +#ifdef _WIN32 + memcpy(&t, localtime(&tm), sizeof(struct tm)); + memcpy(&n, localtime(&now), sizeof(struct tm)); +#else + localtime_r(&tm, &t); + localtime_r(&now, &n); +#endif /* _WIN32 */ + char *str = malloc(16); + if(t.tm_year == n.tm_year) { + strftime(str, 16, "%b %d %H:%M", &t); + } else { + strftime(str, 16, "%b %d %Y", &t); + } + return str; +} + char* util_xml_get_text(const xmlNode *elm) { xmlNode *node = elm->children;
--- a/libidav/utils.h Sun Jul 05 11:55:54 2020 +0200 +++ b/libidav/utils.h Sat Jul 25 11:16:31 2020 +0200 @@ -88,6 +88,9 @@ char* util_path_to_url(DavSession *sn, char *path); char* util_parent_path(const char *path); +char* util_size_str(DavBool iscollection, uint64_t contentlength); +char* util_date_str(time_t tm); + int util_getboolean(const char *v); int util_strtouint(const char *str, uint64_t *value); int util_strtoint(const char *str, int64_t *value);