obj: implement link objects
This commit is contained in:
132
obj/namespace.c
132
obj/namespace.c
@@ -128,27 +128,143 @@ kern_status_t object_namespace_get_object(struct object_namespace *ns, const cha
|
||||
|
||||
unsigned long flags;
|
||||
while (tok) {
|
||||
object_lock(cur, &flags);
|
||||
object_lock_irqsave(cur, &flags);
|
||||
|
||||
struct object *next;
|
||||
kern_status_t status = object_get_child_named(cur, tok, &next);
|
||||
if (status != KERN_OK) {
|
||||
object_unlock(cur, flags);
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
kfree(rpath);
|
||||
return status;
|
||||
}
|
||||
|
||||
object_unlock(cur, flags);
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
cur = next;
|
||||
tok = strtok_r(NULL, "/", &sp);
|
||||
|
||||
object_lock_irqsave(cur, &flags);
|
||||
if (!object_is_link(cur)) {
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
continue;
|
||||
}
|
||||
|
||||
struct object *dest_obj;
|
||||
dest_obj = link_read_ptr(cur);
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
|
||||
if (!dest_obj) {
|
||||
kfree(rpath);
|
||||
/* TODO better error code for broken links */
|
||||
return KERN_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
object_deref(cur);
|
||||
cur = dest_obj;
|
||||
}
|
||||
|
||||
kfree(rpath);
|
||||
|
||||
*out = object_ref(cur);
|
||||
if (object_is_link(cur)) {
|
||||
struct object *link_dest = link_read_ptr(cur);
|
||||
object_deref(cur);
|
||||
cur = object_ref(link_dest);
|
||||
}
|
||||
|
||||
*out = cur;
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t object_namespace_create_link(struct object_namespace *ns, const char *linkpath, struct object *dest)
|
||||
{
|
||||
if (*linkpath != '/') {
|
||||
return KERN_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
while (*linkpath == '/') {
|
||||
linkpath++;
|
||||
}
|
||||
|
||||
size_t path_len = strlen(linkpath);
|
||||
if (path_len == 0) {
|
||||
return KERN_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
size_t parts = 0;
|
||||
char *rpath = kmalloc(path_len, 0);
|
||||
if (!rpath) {
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
|
||||
memcpy(rpath, linkpath, path_len);
|
||||
cleanup_object_path(rpath, path_len, &parts);
|
||||
|
||||
char *p = rpath + strlen(rpath) - 1;
|
||||
while (*p != '/' && p >= rpath) {
|
||||
p--;
|
||||
}
|
||||
|
||||
if (p == rpath) {
|
||||
kfree(rpath);
|
||||
return KERN_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
*p = '\0';
|
||||
const char *linkname = p + 1;
|
||||
|
||||
char *sp;
|
||||
char *tok = strtok_r(rpath, "/", &sp);
|
||||
struct object *cur = object_ref(ns->ns_root);
|
||||
|
||||
unsigned long flags;
|
||||
while (tok) {
|
||||
object_lock_irqsave(cur, &flags);
|
||||
|
||||
struct object *next;
|
||||
kern_status_t status = object_get_child_named(cur, tok, &next);
|
||||
if (status == KERN_NO_ENTRY) {
|
||||
next = set_create(tok);
|
||||
if (!next) {
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
object_deref(cur);
|
||||
kfree(rpath);
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
|
||||
status = set_add_object(cur, next);
|
||||
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
object_deref(cur);
|
||||
} else {
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
}
|
||||
|
||||
if (status != KERN_OK) {
|
||||
kfree(rpath);
|
||||
return status;
|
||||
}
|
||||
|
||||
cur = next;
|
||||
tok = strtok_r(NULL, "/", &sp);
|
||||
}
|
||||
|
||||
struct object *link = link_create(linkname, dest);
|
||||
kfree(rpath);
|
||||
|
||||
if (!link) {
|
||||
object_deref(cur);
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
|
||||
object_lock_irqsave(cur, &flags);
|
||||
kern_status_t status = set_add_object(cur, link);
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
|
||||
object_deref(cur);
|
||||
object_deref(link);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
kern_status_t object_publish(struct object_namespace *ns, const char *path, struct object *obj)
|
||||
{
|
||||
if (*path != '/') {
|
||||
@@ -179,14 +295,14 @@ kern_status_t object_publish(struct object_namespace *ns, const char *path, stru
|
||||
|
||||
unsigned long flags;
|
||||
while (tok) {
|
||||
object_lock(cur, &flags);
|
||||
object_lock_irqsave(cur, &flags);
|
||||
|
||||
struct object *next;
|
||||
kern_status_t status = object_get_child_named(cur, tok, &next);
|
||||
if (status == KERN_NO_ENTRY) {
|
||||
next = set_create(tok);
|
||||
if (!next) {
|
||||
object_unlock(cur, flags);
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
kfree(rpath);
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
@@ -195,12 +311,12 @@ kern_status_t object_publish(struct object_namespace *ns, const char *path, stru
|
||||
}
|
||||
|
||||
if (status != KERN_OK) {
|
||||
object_unlock(cur, flags);
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
kfree(rpath);
|
||||
return status;
|
||||
}
|
||||
|
||||
object_unlock(cur, flags);
|
||||
object_unlock_irqrestore(cur, flags);
|
||||
cur = next;
|
||||
tok = strtok_r(NULL, "/", &sp);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user