diff options
author | dzwdz | 2024-08-18 23:06:26 +0200 |
---|---|---|
committer | dzwdz | 2024-08-18 23:06:26 +0200 |
commit | 8b4a2d50de67b746bcc472bd46c55d83eff668fc (patch) | |
tree | 21bcf9814b07bc54e7705f4e9ae93528d627e838 /src/cmd | |
parent | 446acde84c7f244792bf412996678254ee296356 (diff) |
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/setxattr.c | 23 | ||||
-rw-r--r-- | src/cmd/tmpfs.c | 107 |
2 files changed, 124 insertions, 6 deletions
diff --git a/src/cmd/setxattr.c b/src/cmd/setxattr.c new file mode 100644 index 0000000..c2e422e --- /dev/null +++ b/src/cmd/setxattr.c @@ -0,0 +1,23 @@ +#include <camellia.h> +#include <camellia/syscalls.h> +#include <err.h> +#include <errno.h> +#include <stdio.h> +#include <string.h> + +int +main(int argc, char **argv) +{ + if (argc != 4) { + fprintf(stderr, "usage: setxattr file key value\n"); + return 1; + } + hid_t h = camellia_open(argv[1], OPEN_WRITE); + if (h < 0) err(1, "open"); + + ssize_t ret = _sys_setxattr(h, argv[2], argv[3], strlen(argv[3]), 0); + if (ret < 0) { + errno = -ret; + err(1, "setxattr"); + } +} diff --git a/src/cmd/tmpfs.c b/src/cmd/tmpfs.c index 5e4b4e8..352b2f1 100644 --- a/src/cmd/tmpfs.c +++ b/src/cmd/tmpfs.c @@ -12,6 +12,7 @@ #include <unistd.h> typedef struct Node Node; +typedef struct Xattr Xattr; struct Node { char *name; size_t namelen; @@ -26,9 +27,16 @@ struct Node { * files. */ Node **ref; + Xattr *xattr; + /* allocated by tmpfs_open * freed by node_close when (open == 0 && ref == NULL && self != node_root). */ }; +struct Xattr { + Xattr *next; + char *name, *buf; /* owned */ + size_t len; /* 0 means deleted. yeah, this isn't efficient, idc */ +}; static Node node_root = { .directory = true, @@ -44,6 +52,8 @@ static long node_move(Node *from, Node *to); /** Removes a file. It's kept in memory until all the open handles are closed. */ static long node_remove(Node *node); static void node_sanity(Node *node, Node **from); +static Xattr *node_getxattr(Node *node, const char *name); +static char *node_xattrindex(Node *node, int *plen); static Node * @@ -106,19 +116,28 @@ tmpfs_open(const char *path, struct ufs_request *req) } static void -node_close(Node *node) { +node_close(Node *node) +{ node->open--; if (node->ref == NULL && node != &node_root && node->open == 0) { if (node->name) { free(node->name); } + while (node->xattr) { + Xattr *x = node->xattr; + node->xattr = x->next; + free(x->name); + free(x->buf); + free(x); + } free(node->buf); free(node); } } static long -node_move(Node *from, Node *to) { +node_move(Node *from, Node *to) +{ if (from == &node_root || to == &node_root) { return -1; } @@ -161,7 +180,8 @@ node_move(Node *from, Node *to) { } static long -node_remove(Node *node) { +node_remove(Node *node) +{ if (node == &node_root) return -1; if (!node->ref) return -1; if (node->child) return -ENOTEMPTY; @@ -173,7 +193,8 @@ node_remove(Node *node) { } static void -node_sanity(Node *node, Node **from) { +node_sanity(Node *node, Node **from) +{ if (node == NULL) return; if (node != &node_root && node->ref != from) { assert(false); @@ -182,8 +203,41 @@ node_sanity(Node *node, Node **from) { node_sanity(node->child, &node->child); } +static Xattr * +node_getxattr(Node *node, const char *name) +{ + for (Xattr *x = node->xattr; x; x = x->next) { + if (strcmp(name, x->name) == 0) { + return x; + } + } + return NULL; +} + +static char * +node_xattrindex(Node *node, int *plen) +{ + char *buf, *it; + int len = 0; + for (Xattr *x = node->xattr; x; x = x->next) { + if (x->len == 0) continue; + len += strlen(x->name) + 1; + } + buf = it = malloc(len); + *plen = len; + for (Xattr *x = node->xattr; x; x = x->next) { + if (x->len == 0) continue; + int slen = strlen(x->name) + 1; + memcpy(it, x->name, slen); + it += slen; + } + assert(buf + len == it); + return buf; +} + int -main(void) { +main(void) +{ const size_t buflen = 4096; char *buf = malloc(buflen); if (!buf) return -1; @@ -266,8 +320,49 @@ main(void) { } break; + case VFSOP_GETXATTR: + if (strcmp(buf, "virt.index") == 0) { + int len; + char *res = node_xattrindex(req.id, &len); + _sys_fs_respond(reqh, res, len, 0); + free(res); + } else { + Xattr *x = node_getxattr(req.id, buf); + if (x == NULL || x->len == 0) { + _sys_fs_respond(reqh, NULL, -ENOENT, 0); + } else { + _sys_fs_respond(reqh, x->buf, x->len, 0); + } + } + break; + + case VFSOP_SETXATTR: + /* dirty but safe in this context */ + if (memcmp("user.", buf, 5) != 0) { + _sys_fs_respond(reqh, NULL, -EACCES, 0); + } else { + Xattr *x = node_getxattr(req.id, buf); + if (x == NULL) { + x = calloc(1, sizeof(Xattr)); + x->name = strdup(buf); + x->next = ptr->xattr; + ptr->xattr = x; + } + free(x->buf); + x->buf = malloc(req.len); + if (x->buf == NULL) { + x->len = 0; + _sys_fs_respond(reqh, NULL, -ENOSPC, 0); + break; + } + x->len = req.len; + memcpy(x->buf, req.id2, req.len); + _sys_fs_respond(reqh, 0, req.len, 0); + } + break; + default: - _sys_fs_respond(reqh, NULL, -1, 0); + _sys_fs_respond(reqh, NULL, -ENOSYS, 0); break; } node_sanity(&node_root, NULL); |