dav/main.c

changeset 14
d1a43035d3a2
parent 13
8a0cc4d90de7
child 15
182af08b4813
equal deleted inserted replaced
13:8a0cc4d90de7 14:d1a43035d3a2
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2012 Olaf Wintermann. All rights reserved. 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 #include <errno.h> 32 #include <errno.h>
33 #include <unistd.h> 33 #include <unistd.h>
34 #include <time.h>
34 #include <libxml/xmlerror.h> 35 #include <libxml/xmlerror.h>
35 #include <sys/types.h> 36 #include <sys/types.h>
36 #include <sys/stat.h> 37 #include <sys/stat.h>
37 #include <ucx/string.h> 38 #include <ucx/string.h>
38 39
39 #include "webdav.h"
40 #include "utils.h" 40 #include "utils.h"
41 #include "config.h" 41 #include "config.h"
42 #include "crypto.h" 42 #include "crypto.h"
43 #include "main.h" 43 #include "main.h"
44 44
81 } 81 }
82 82
83 void print_usage(char *cmd) { 83 void print_usage(char *cmd) {
84 fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd); 84 fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd);
85 fprintf(stderr, "Commands:\n"); 85 fprintf(stderr, "Commands:\n");
86 fprintf(stderr, " list [-al] <url>\n"); 86 fprintf(stderr, " list [-alt] <url>\n");
87 fprintf(stderr, " get [-p] [-k <key>] [-o <file>] <url>\n"); 87 fprintf(stderr, " get [-p] [-k <key>] [-o <file>] <url>\n");
88 fprintf(stderr, " put [-p] [-k <key>] <url> <file>\n"); 88 fprintf(stderr, " put [-p] [-k <key>] <url> <file>\n");
89 fprintf(stderr, "\n"); 89 fprintf(stderr, "\n");
90 fprintf(stderr, "Options:\n"); 90 fprintf(stderr, "Options:\n");
91 fprintf(stderr, 91 fprintf(stderr,
92 " -k <key> Key to use for encryption or decryption\n"); 92 " -k <key> Key to use for encryption or decryption\n");
93 fprintf(stderr, " -p Don't encrypt or decrypt files\n"); 93 fprintf(stderr, " -p Don't encrypt or decrypt files\n");
94 fprintf(stderr, " -o <file> Write output to file\n"); 94 fprintf(stderr, " -o <file> Write output to file\n");
95 fprintf(stderr, " -a show all files\n");
96 fprintf(stderr, " -l print resources in long list format\n");
97 fprintf(stderr, " -t print content type\n");
95 fprintf(stderr, "\n"); 98 fprintf(stderr, "\n");
96 fprintf(stderr, 99 fprintf(stderr,
97 "Instead of an url you can pass a repository name " 100 "Instead of an url you can pass a repository name "
98 "with an optional path:\n"); 101 "with an optional path:\n");
99 fprintf(stderr, " <repository>/path/\n"); 102 fprintf(stderr, " <repository>/path/\n");
174 sn = dav_session_new(ctx, root); 177 sn = dav_session_new(ctx, root);
175 } 178 }
176 179
177 //printf("baseurl: %s\n", sn->base_url); 180 //printf("baseurl: %s\n", sn->base_url);
178 181
179 DavResource *ls = dav_get(sn, path, NULL); 182 DavResource *ls = dav_get(sn, path, "U:crypto-key");
180 if(!ls) { 183 if(!ls) {
181 print_resource_error(sn, ls); 184 print_resource_error(sn, ls);
182 return -1; 185 return -1;
183 } 186 }
187
188 // parameters
189 int show_all = cmd_getoption(a, "all") ? 1 : 0;
190 void (*print_func)(DavResource*, CmdArgs *);
191 if(cmd_getoption(a, "list")) {
192 print_func = ls_print_list_elm;
193 } else {
194 print_func = ls_print_elm;
195 }
184 DavResource *child = ls->children; 196 DavResource *child = ls->children;
185 while(child) { 197 while(child) {
186 printf("%s\n", child->name); 198 if(child->name[0] != '.' || show_all) {
199 print_func(child, a);
200 }
187 child = child->next; 201 child = child->next;
188 } 202 }
189 203
190 return 0; 204 return 0;
205 }
206
207 static char* ls_date_str(time_t tm) {
208 struct tm t;
209 struct tm n;
210 time_t now = time(NULL);
211 localtime_r(&tm, &t);
212 localtime_r(&now, &n);
213 char *str = malloc(16);
214 if(t.tm_year == n.tm_year) {
215 strftime(str, 16, "%b %d %H:%M", &t);
216 } else {
217 strftime(str, 16, "%b %d %Y", &t);
218 }
219 return str;
220 }
221
222 static char* ls_size_str(DavResource *res) {
223 char *str = malloc(16);
224 uint64_t size = res->contentlength;
225
226 if(res->iscollection) {
227 snprintf(str, 16, "%" PRIu64, size);
228 } else if(size < 0x400) {
229 snprintf(str, 16, "%" PRIu64 " Bytes", size);
230 } else if(size < 0x100000) {
231 size /= 0x400;
232 snprintf(str, 16, "%" PRIu64 " KiB", size);
233 } else if(size < 0x40000000) {
234 size /= 0x100000;
235 snprintf(str, 16, "%" PRIu64 "MiB", size);
236 } else if(size < 0x1000000000ULL) {
237 size /= 0x40000000;
238 snprintf(str, 16, "%" PRIu64 "GiB", size);
239 } else {
240 size /= 0x1000000000ULL;
241 snprintf(str, 16, "%" PRIu64, "TiB", size);
242 }
243 return str;
244 }
245
246 void ls_print_list_elm(DavResource *res, CmdArgs *a) {
247 char flags[16];
248 memset(flags, '-', 15);
249 flags[2] = '\0';
250
251 int type_width = 0;
252 char *type = res->contenttype;
253
254 if(res->iscollection) {
255 flags[0] = 'd';
256 type = "";
257 }
258 char *keyprop = dav_get_property_ns(
259 res,
260 "http://www.uap-core.de/",
261 "crypto-key");
262 if(keyprop) {
263 flags[1] = 'c';
264 }
265
266 if(cmd_getoption(a, "type")) {
267 type_width = 20;
268 }
269 if(type == NULL || type_width == 0) {
270 type = "";
271 }
272
273 char *date = ls_date_str(res->lastmodified);
274 char *size = ls_size_str(res);
275 printf(
276 "%s %*s %10s %12s %s\n",
277 flags,
278 type_width, type,
279 size,
280 date,
281 res->name);
282 free(date);
283 free(size);
284 }
285
286 void ls_print_elm(DavResource *res, CmdArgs *a) {
287 printf("%s\n", res->name);
191 } 288 }
192 289
193 int cmd_get(CmdArgs *a) { 290 int cmd_get(CmdArgs *a) {
194 if(a->argc == 0) { 291 if(a->argc == 0) {
195 return -1; 292 return -1;

mercurial