From 7f4b7142b09205dc773a915e3fe3cb7954f6d041 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Tue, 26 Dec 2023 22:33:51 -0600 Subject: refactor instances --- src/instance.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'src/instance.c') diff --git a/src/instance.c b/src/instance.c index ca80a45..8d62e1e 100644 --- a/src/instance.c +++ b/src/instance.c @@ -11,6 +11,14 @@ #include +const char *const l2_instance_errormsg[] = { + "Success", + "OS error", + "JSON error", + "Malformed instance file", + NULL +}; + int l2_instance__load_all_from_object(json_t *obj); json_t *l2_instance__save_all_to_object(void); @@ -116,8 +124,8 @@ int l2_instance_del_instance(struct l2_instance *inst) l2_state.instance_head = inst->next; } - free((char *)inst->name); /* okay to cast away const because any instance in the list will be allocated with strdup */ - free((char *)inst->path); + free(inst->name); /* okay to cast away const because any instance in the list will be allocated with strdup */ + free(inst->path); free(inst); return INSTANCE_SUCCESS; @@ -128,11 +136,33 @@ int l2_instance_rename_instance(struct l2_instance *inst, const char *newname) char *nn = strdup(newname); if (!nn) return INSTANCE_ERRNO; - free((char *)inst->name); + free(inst->name); inst->name = nn; return INSTANCE_SUCCESS; } +struct l2_instance *l2_instance_find_by_uuid(const uuid_t *id) +{ + for (struct l2_instance *inst = l2_state.instance_head; inst; inst = inst->next) { + if (!memcmp(id, &inst->uuid, sizeof(uuid_t))) { + return inst; + } + } + + return NULL; +} + +struct l2_instance *l2_instance_find_by_name(const char *name) +{ + for (struct l2_instance *inst = l2_state.instance_head; inst; inst = inst->next) { + if (!strcmp(inst->name, name)) { + return inst; + } + } + + return NULL; +} + /* internal utilities */ int l2_instance__load_all_from_object(json_t *obj) -- cgit v1.2.3-70-g09d2