Return a valid target when adding to linkmap if it exists

This commit is contained in:
Nathan Fisher 2023-08-18 11:00:30 -04:00
parent e266346bf2
commit e372af0c42

View file

@ -39,6 +39,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/syslimits.h>
haggis_bucket* haggis_bucket_init(ino_t inode, uint64_t hash, char * path) { haggis_bucket* haggis_bucket_init(ino_t inode, uint64_t hash, char * path) {
haggis_bucket *bucket; haggis_bucket *bucket;
@ -58,12 +59,18 @@ void haggis_bucket_deinit(haggis_bucket *nod) {
free(nod); free(nod);
} }
void haggis_bucket_append(haggis_bucket *head, haggis_bucket *tail) { char* haggis_bucket_search_append(haggis_bucket *head, haggis_bucket *tail) {
char* target;
while (head->next != NULL) { while (head->next != NULL) {
if (head->key.val == tail->key.val) {
target = strndup(head->path, PATH_MAX - 1);
return target;
}
head = head->next; head = head->next;
} }
head->next = tail; head->next = tail;
return NULL;
} }
haggis_linkmap* haggis_linkmap_init() { haggis_linkmap* haggis_linkmap_init() {
@ -131,7 +138,7 @@ char* haggis_linkmap_get_or_add(haggis_linkmap *map, ino_t inode, char * path) {
} else { } else {
b = haggis_bucket_init(key.val, hash, path); b = haggis_bucket_init(key.val, hash, path);
if (b == NULL) return NULL; if (b == NULL) return NULL;
haggis_bucket_append(&map->buckets[idx], b); target = haggis_bucket_search_append(&map->buckets[idx], b);
} }
return target; return target;
} }