From 806eecd7a2fe12daccf2c7c7171ce52e3fd93799 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Fri, 16 Aug 2024 19:48:50 +0200 Subject: tmpfs: minor cleanup --- src/cmd/tmpfs.c | 57 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/src/cmd/tmpfs.c b/src/cmd/tmpfs.c index a031a44..5e4b4e8 100644 --- a/src/cmd/tmpfs.c +++ b/src/cmd/tmpfs.c @@ -11,7 +11,8 @@ #include #include -struct node { +typedef struct Node Node; +struct Node { char *name; size_t namelen; bool directory; @@ -20,43 +21,46 @@ struct node { size_t open; /* amount of open handles */ - struct node *sibling, *child; + Node *sibling, *child; /* Pointer to the sibling/child field referencing the node. NULL for removed * files. */ - struct node **ref; + Node **ref; /* allocated by tmpfs_open * freed by node_close when (open == 0 && ref == NULL && self != node_root). */ }; -static struct node node_root = { +static Node node_root = { .directory = true, }; /** Responds to open(), finding an existing node, or creating one when applicable. */ -static struct node *tmpfs_open(const char *path, struct ufs_request *req); +static Node *tmpfs_open(const char *path, struct ufs_request *req); /** Finds a direct child with the given name. */ -static struct node *node_getchild(struct node *parent, const char *name, size_t len); +static Node *node_getchild(Node *parent, const char *name, size_t len); /** Corresponds to close(); drops a reference. */ -static void node_close(struct node *node); -static long node_move(struct node *from, struct node *to); +static void node_close(Node *node); +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(struct node *node); -static void node_sanity(struct node *node, struct node **from); +static long node_remove(Node *node); +static void node_sanity(Node *node, Node **from); -static struct node *node_getchild(struct node *parent, const char *name, size_t len) { - for (struct node *iter = parent->child; iter; iter = iter->sibling) { - if (iter->namelen == len && !memcmp(name, iter->name, len)) { +static Node * +node_getchild(Node *parent, const char *name, size_t len) { + for (Node *iter = parent->child; iter; iter = iter->sibling) { + if (iter->namelen == len && memcmp(name, iter->name, len) == 0) { return iter; } } return NULL; } -static struct node *tmpfs_open(const char *path, struct ufs_request *req) { +static Node * +tmpfs_open(const char *path, struct ufs_request *req) +{ /* *path is not null terminated! */ - struct node *node = &node_root; + Node *node = &node_root; if (req->len == 0) return NULL; if (req->len == 1) return node; /* "/" */ path++; @@ -65,7 +69,7 @@ static struct node *tmpfs_open(const char *path, struct ufs_request *req) { bool more = true; size_t segpos = 0, seglen; /* segments end with a slash, inclusive */ while (more) { - struct node *parent = node; + Node *parent = node; char *slash = memchr(path + segpos, '/', req->len - segpos); seglen = (slash ? (size_t)(slash - path + 1) : req->len) - segpos; more = segpos + seglen < req->len; @@ -101,7 +105,8 @@ static struct node *tmpfs_open(const char *path, struct ufs_request *req) { return node; } -static void node_close(struct node *node) { +static void +node_close(Node *node) { node->open--; if (node->ref == NULL && node != &node_root && node->open == 0) { if (node->name) { @@ -112,7 +117,8 @@ static void node_close(struct node *node) { } } -static long node_move(struct node *from, struct node *to) { +static long +node_move(Node *from, Node *to) { if (from == &node_root || to == &node_root) { return -1; } @@ -154,7 +160,8 @@ static long node_move(struct node *from, struct node *to) { return 0; } -static long node_remove(struct node *node) { +static long +node_remove(Node *node) { if (node == &node_root) return -1; if (!node->ref) return -1; if (node->child) return -ENOTEMPTY; @@ -165,7 +172,8 @@ static long node_remove(struct node *node) { return 0; } -static void node_sanity(struct node *node, struct node **from) { +static void +node_sanity(Node *node, Node **from) { if (node == NULL) return; if (node != &node_root && node->ref != from) { assert(false); @@ -174,7 +182,8 @@ static void node_sanity(struct node *node, struct node **from) { node_sanity(node->child, &node->child); } -int main(void) { +int +main(void) { const size_t buflen = 4096; char *buf = malloc(buflen); if (!buf) return -1; @@ -184,7 +193,7 @@ int main(void) { for (;;) { struct ufs_request req; hid_t reqh = ufs_wait(buf, buflen, &req); - struct node *ptr = req.id; + Node *ptr = req.id; if (reqh < 0) break; switch (req.op) { @@ -197,7 +206,7 @@ int main(void) { if (ptr->directory) { struct dirbuild db; dir_start(&db, req.offset, buf, buflen); - for (struct node *iter = ptr->child; iter; iter = iter->sibling) { + for (Node *iter = ptr->child; iter; iter = iter->sibling) { dir_appendl(&db, iter->name, iter->namelen); } _sys_fs_respond(reqh, buf, dir_finish(&db), 0); @@ -231,7 +240,7 @@ int main(void) { // TODO could be cached in ptr->size struct dirbuild db; dir_start(&db, req.offset, NULL, buflen); - for (struct node *iter = ptr->child; iter; iter = iter->sibling) { + for (Node *iter = ptr->child; iter; iter = iter->sibling) { dir_append(&db, iter->name); } _sys_fs_respond(reqh, NULL, dir_finish(&db), 0); -- cgit v1.2.3