From ad8865fe661f3a8aff2723d185e72dcf93798513 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Wed, 10 May 2023 20:33:08 +0100 Subject: [PATCH] obj: implement object_namespace_get_object() --- obj/namespace.c | 57 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/obj/namespace.c b/obj/namespace.c index f89dce8..ce86730 100644 --- a/obj/namespace.c +++ b/obj/namespace.c @@ -60,11 +60,6 @@ struct object_namespace *object_namespace_create(void) return ns; } -kern_status_t object_namespace_get_object(struct object_namespace *ns, const char *path, struct object **out) -{ - return KERN_OK; -} - static void cleanup_object_path(char *path, size_t len, size_t *parts) { while (path[len - 1] == '/') { @@ -102,6 +97,58 @@ struct object *ns_header(struct object_namespace *ns) return &ns->ns_base; } +kern_status_t object_namespace_get_object(struct object_namespace *ns, const char *path, struct object **out) +{ + if (*path != '/') { + return KERN_INVALID_ARGUMENT; + } + + while (*path == '/') { + path++; + } + + size_t path_len = strlen(path); + if (path_len == 0) { + *out = object_ref(ns->ns_root); + return KERN_OK; + } + + size_t parts = 0; + char *rpath = kmalloc(path_len, 0); + if (!rpath) { + return KERN_NO_MEMORY; + } + + memcpy(rpath, path, path_len); + cleanup_object_path(rpath, path_len, &parts); + + char *sp; + char *tok = strtok_r(rpath, "/", &sp); + struct object *cur = ns->ns_root; + + unsigned long flags; + while (tok) { + object_lock(cur, &flags); + + struct object *next; + kern_status_t status = object_get_child_named(cur, tok, &next); + if (status != KERN_OK) { + object_unlock(cur, flags); + kfree(rpath); + return status; + } + + object_unlock(cur, flags); + cur = next; + tok = strtok_r(NULL, "/", &sp); + } + + kfree(rpath); + + *out = object_ref(cur); + return KERN_OK; +} + kern_status_t object_publish(struct object_namespace *ns, const char *path, struct object *obj) { if (*path != '/') {