added some options to list command

Wed, 14 Aug 2013 18:01:37 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 14 Aug 2013 18:01:37 +0200
changeset 14
d1a43035d3a2
parent 13
8a0cc4d90de7
child 15
182af08b4813

added some options to list command

dav/main.c file | annotate | diff | comparison | revisions
dav/main.h file | annotate | diff | comparison | revisions
dav/optparser.c file | annotate | diff | comparison | revisions
dav/optparser.h file | annotate | diff | comparison | revisions
dav/utils.c file | annotate | diff | comparison | revisions
dav/utils.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Tue Aug 13 13:51:00 2013 +0200
+++ b/dav/main.c	Wed Aug 14 18:01:37 2013 +0200
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2012 Olaf Wintermann. All rights reserved.
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -31,12 +31,12 @@
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
+#include <time.h>
 #include <libxml/xmlerror.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <ucx/string.h>
 
-#include "webdav.h"
 #include "utils.h"
 #include "config.h"
 #include "crypto.h"
@@ -83,7 +83,7 @@
 void print_usage(char *cmd) {
     fprintf(stderr, "Usage: %s command [options] arguments...\n\n", cmd);
     fprintf(stderr, "Commands:\n");
-    fprintf(stderr, "        list [-al] <url>\n");
+    fprintf(stderr, "        list [-alt] <url>\n");
     fprintf(stderr, "        get [-p] [-k <key>] [-o <file>] <url>\n");
     fprintf(stderr, "        put [-p] [-k <key>] <url> <file>\n");
     fprintf(stderr, "\n");
@@ -92,6 +92,9 @@
             "        -k <key>   Key to use for encryption or decryption\n");
     fprintf(stderr, "        -p         Don't encrypt or decrypt files\n");
     fprintf(stderr, "        -o <file>  Write output to file\n");
+    fprintf(stderr, "        -a         show all files\n");
+    fprintf(stderr, "        -l         print resources in long list format\n");
+    fprintf(stderr, "        -t         print content type\n");
     fprintf(stderr, "\n");
     fprintf(stderr,
             "Instead of an url you can pass a repository name "
@@ -176,20 +179,114 @@
     
     //printf("baseurl: %s\n", sn->base_url);
     
-    DavResource *ls = dav_get(sn, path, NULL);
+    DavResource *ls = dav_get(sn, path, "U:crypto-key");
     if(!ls) {
         print_resource_error(sn, ls);
         return -1;
     }
+    
+    // parameters
+    int show_all = cmd_getoption(a, "all") ? 1 : 0;
+    void (*print_func)(DavResource*, CmdArgs *);
+    if(cmd_getoption(a, "list")) {
+        print_func = ls_print_list_elm;
+    } else {
+        print_func = ls_print_elm;
+    }
     DavResource *child = ls->children;
     while(child) {
-        printf("%s\n", child->name);
+        if(child->name[0] != '.' || show_all) {
+            print_func(child, a);
+        }
         child = child->next;
     }
     
     return 0;
 }
 
+static char* ls_date_str(time_t tm) {
+    struct tm t;
+    struct tm n;
+    time_t now = time(NULL);
+    localtime_r(&tm, &t);
+    localtime_r(&now, &n);
+    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) {
+        snprintf(str, 16, "%" PRIu64, size);
+    } else if(size < 0x400) {
+        snprintf(str, 16, "%" PRIu64 " Bytes", size);
+    } else if(size < 0x100000) {
+        size /= 0x400;
+        snprintf(str, 16, "%" PRIu64 " KiB", size);
+    } else if(size < 0x40000000) {
+        size /= 0x100000;
+        snprintf(str, 16, "%" PRIu64 "MiB", size);
+    } else if(size < 0x1000000000ULL) {
+        size /= 0x40000000;
+        snprintf(str, 16, "%" PRIu64 "GiB", size);
+    } else {
+        size /= 0x1000000000ULL;
+        snprintf(str, 16, "%" PRIu64, "TiB", size);
+    }
+    return str;
+}
+
+void ls_print_list_elm(DavResource *res, CmdArgs *a) {
+    char flags[16];
+    memset(flags, '-', 15);
+    flags[2] = '\0';
+    
+    int type_width = 0;
+    char *type = res->contenttype;
+    
+    if(res->iscollection) {
+        flags[0] = 'd';
+        type = "";
+    }
+    char *keyprop = dav_get_property_ns(
+            res,
+            "http://www.uap-core.de/",
+            "crypto-key");
+    if(keyprop) {
+        flags[1] = 'c';
+    }
+    
+    if(cmd_getoption(a, "type")) {
+        type_width = 20;
+    }
+    if(type == NULL || type_width == 0) {
+        type = "";
+    }
+    
+    char *date = ls_date_str(res->lastmodified);
+    char *size = ls_size_str(res);
+    printf(
+            "%s %*s %10s  %12s  %s\n",
+            flags,
+            type_width, type,
+            size,
+            date,
+            res->name);
+    free(date);
+    free(size);
+}
+
+void ls_print_elm(DavResource *res, CmdArgs *a) {
+    printf("%s\n", res->name);
+}
+
 int cmd_get(CmdArgs *a) {
     if(a->argc == 0) {
         return -1;
--- a/dav/main.h	Tue Aug 13 13:51:00 2013 +0200
+++ b/dav/main.h	Wed Aug 14 18:01:37 2013 +0200
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2012 Olaf Wintermann. All rights reserved.
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -31,6 +31,7 @@
 
 #include <curl/curl.h>
 #include "optparser.h"
+#include "webdav.h"
 
 #ifdef	__cplusplus
 extern "C" {
@@ -39,6 +40,9 @@
 void print_usage(char *cmd);
 
 int cmd_list(CmdArgs *args);
+void ls_print_list_elm(DavResource *res, CmdArgs *args);
+void ls_print_elm(DavResource *res, CmdArgs *args);
+
 int cmd_get(CmdArgs *args);
 int cmd_put(CmdArgs *args);
 
--- a/dav/optparser.c	Tue Aug 13 13:51:00 2013 +0200
+++ b/dav/optparser.c	Wed Aug 14 18:01:37 2013 +0200
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2012 Olaf Wintermann. All rights reserved.
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
@@ -74,6 +74,18 @@
                         ucx_map_cstr_put(a->options, "plain", "");
                         break;
                     }
+                    case 'a': {
+                        ucx_map_cstr_put(a->options, "all", "");
+                        break;
+                    }
+                    case 'l': {
+                        ucx_map_cstr_put(a->options, "list", "");
+                        break;
+                    }
+                    case 't': {
+                        ucx_map_cstr_put(a->options, "type", "");
+                        break;
+                    }
                     case 'o': {
                         if(!option) {
                             option = "output";
--- a/dav/optparser.h	Tue Aug 13 13:51:00 2013 +0200
+++ b/dav/optparser.h	Wed Aug 14 18:01:37 2013 +0200
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2012 Olaf Wintermann. All rights reserved.
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
--- a/dav/utils.c	Tue Aug 13 13:51:00 2013 +0200
+++ b/dav/utils.c	Wed Aug 14 18:01:37 2013 +0200
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2012 Olaf Wintermann. All rights reserved.
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:
--- a/dav/utils.h	Tue Aug 13 13:51:00 2013 +0200
+++ b/dav/utils.h	Wed Aug 14 18:01:37 2013 +0200
@@ -1,7 +1,7 @@
 /*
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  *
- * Copyright 2012 Olaf Wintermann. All rights reserved.
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are met:

mercurial