add tool for accessing extended attributes

Sat, 19 Oct 2019 09:47:31 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 19 Oct 2019 09:47:31 +0200
changeset 660
e835ec0b7f17
parent 659
51206020cfbe
child 661
1baec7ff8931

add tool for accessing extended attributes
this tool is needed for testing

dav/Makefile file | annotate | diff | comparison | revisions
dav/xattrtool.c file | annotate | diff | comparison | revisions
--- a/dav/Makefile	Tue Oct 15 09:22:57 2019 +0200
+++ b/dav/Makefile	Sat Oct 19 09:47:31 2019 +0200
@@ -54,11 +54,15 @@
 SYNC_SRC += system.c
 SYNC_SRC += pwd.c
 
+XATTR_SRC  = xattrtool.c
+XATTR_SRC += libxattr.c
+
 DAV_OBJ = $(DAV_SRC:%.c=../build/tool/%$(OBJ_EXT))
 SYNC_OBJ = $(SYNC_SRC:%.c=../build/tool/%$(OBJ_EXT))
+XATTR_OBJ = $(XATTR_SRC:%.c=../build/tool/%$(OBJ_EXT))
 
 
-all: ../build/dav ../build/dav-sync
+all: ../build/dav ../build/dav-sync ../build/xattrtool
 
 ../build/dav: $(DAV_OBJ) ../build/libidav$(LIB_EXT)
 	$(LD) -o ../build/dav$(APP_EXT) $(DAV_OBJ) \
@@ -70,6 +74,9 @@
 		../build/libidav$(LIB_EXT) ../build/libucx$(LIB_EXT)  \
 		$(LDFLAGS) $(DAV_LDFLAGS)
 
+../build/xattrtool: $(XATTR_OBJ)
+	$(LD) -o ../build/xattrtool$(APP_EXT) $(XATTR_OBJ) $(LDFLAGS)
+
 ../build/tool/%$(OBJ_EXT): %.c 
 	$(CC) $(CFLAGS) $(DAV_CFLAGS) -I../ -I../ucx -o $@ $<
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dav/xattrtool.c	Sat Oct 19 09:47:31 2019 +0200
@@ -0,0 +1,160 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2019 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:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * small tool for accessing extended attributes
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "libxattr.h"
+
+int attrtool_list(int argc, char **argv, int values);
+int attrtool_get(int argc, char **argv, int raw);
+int attrtool_set(int argc, char **argv);
+
+void print_usage(char *cmd) {
+    fprintf(stderr, "usage %s:\n", cmd);
+    fprintf(stderr, "   list <file>\n");
+    fprintf(stderr, "   listvalues <file>\n");
+    fprintf(stderr, "   get <file> <name>\n");
+    fprintf(stderr, "   set <file> <name> <value>\n");
+    fprintf(stderr, "   getraw <file> <name>\n");
+}
+
+int main(int argc, char **argv) {
+    if(argc < 3) {
+        print_usage(argv[0]);
+        return 1;
+    }
+    
+    if(!strcmp(argv[1], "list") || !strcmp(argv[1], "ls")) {
+        return attrtool_list(argc, argv, 0);
+    } else if(!strcmp(argv[1], "listvalues") || !strcmp(argv[1], "lsv")) {
+        return attrtool_list(argc, argv, 1);
+    } else if(!strcmp(argv[1], "get")) {
+        return attrtool_get(argc, argv, 0);
+    } else if(!strcmp(argv[1], "getraw")) {
+        return attrtool_get(argc, argv, 1);
+    } else if(!strcmp(argv[1], "set")) {
+        return attrtool_set(argc, argv);
+    } else {
+        fprintf(stderr, "Unknown command\n");
+        print_usage(argv[0]);
+    }
+    
+    return 1;
+}
+
+int attrtool_list(int argc, char **argv, int values) {
+    ssize_t nelm = 0;
+    char **list = xattr_list(argv[2], &nelm);
+    if(nelm < 0) {
+        perror("xattr_list");
+        return 3;
+    }
+    
+    for(int i=0;i<nelm;i++) {
+        char *attr = list[i];
+        if(values) {
+            ssize_t valuelen = 0;
+            char *value = xattr_get(argv[2], attr, &valuelen);
+            if(valuelen < 0) {
+                printf("%s: #ERROR: %s\n", attr, strerror(errno));
+            } else {
+                printf("%s: %.*s\n", attr, (int)valuelen, value);
+            }
+            
+            if(value) {
+                free(value);
+            }
+        } else {
+            printf("%s\n", attr);
+        }
+        
+        free(list[i]);
+    }
+    if(list) {
+        free(list);
+    }
+    
+    return 0;
+}
+
+int attrtool_get(int argc, char **argv, int raw) {
+    if(argc < 4) {
+        fprintf(stderr, "Too few arguments\n");
+        print_usage(argv[1]);
+        return 1;
+    }
+    
+    char *file = argv[2];
+    char *attr = argv[3];
+    int ret = 0;
+    
+    size_t valuelen = 0;
+    char *value = xattr_get(file, attr, &valuelen);
+    if(value) {
+        if(raw) {
+            fwrite(value, 1, valuelen, stdout);
+        } else {
+            printf("%.*s\n", (int)valuelen, value);
+        }
+        free(value);
+    } else {
+        perror("xattr_get");
+        ret = 3;
+    }
+    
+    return ret;
+}
+
+int attrtool_set(int argc, char **argv) {
+    if(argc < 4) {
+        fprintf(stderr, "Too few arguments\n");
+        print_usage(argv[1]);
+        return 1;
+    }
+    
+    size_t len = strlen(argv[4]);
+    char *file = argv[2];
+    char *attr = argv[3];
+    char *value = argv[4];
+    int ret = 0;
+    
+    if(xattr_set(file, attr, value, len)) {
+        perror("xattr_set");
+        ret = 3;
+    }
+    
+    return ret;
+}
+

mercurial