seahag/linkmap.c
2023-08-17 23:28:25 -04:00

117 lines
4.1 KiB
C

/* _,.---._ .-._ .--.-. ,--.--------.
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
* |==|,| | -|==| , '=' |==| - _ | |==|- |
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
* _ __ ,---. .-._ .=-.-. _,.----.
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
* `--`---' `--` `--`./ `--``--`-`
*
* @(#)Copyright (c) 2023, Nathan D. Fisher.
*
* This is free software. It comes with NO WARRANTY.
* Permission to use, modify and distribute this source code
* is granted subject to the following conditions.
* 1/ that the above copyright notice and this notice
* are preserved in all copies and that due credit be given
* to the author.
* 2/ that any changes to this code are clearly commented
* as such so that the author does not get blamed for bugs
* other than his own.
*/
#include "linkmap.h"
#include "haggis_private.h"
#include <limits.h>
#include <pthread.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
haggis_bucket* haggis_bucket_init(ino_t inode, uint64_t hash, char * path) {
haggis_bucket *bucket;
bucket = calloc(1, sizeof(haggis_bucket));
if (bucket == NULL) return NULL;
bucket->key.val = inode;
bucket->hash = hash;
bucket->path = path;
return bucket;
}
void haggis_bucket_deinit(haggis_bucket *nod) {
if (nod->path != NULL)
free(nod->path);
free(nod);
}
void haggis_bucket_append(haggis_bucket *head, haggis_bucket *tail) {
while (head->next != NULL) {
head = head->next;
}
head->next = tail;
}
int haggis_linkmap_expand(haggis_linkmap *map) {
haggis_bucket *buckets_new;
haggis_bucket *buckets_old;
size_t i, hash, idx;
buckets_new = calloc(map->capacity + HAGGIS_BUCKETS_BASE, sizeof(haggis_bucket));
if (buckets_new == NULL)
return 2;
for (i = 0; i < map->capacity; i++) {
if (map->buckets[i].key.val != 0) {
hash = hash_fnv1a_64(&map->buckets[i].key.bytes[0], sizeof(ino_t));
map ->capacity += HAGGIS_BUCKETS_BASE;
idx = map->capacity % hash;
buckets_new[idx] = map->buckets[i];
}
}
buckets_old = map->buckets;
map->buckets = buckets_new;
free(buckets_old);
map->capacity += HAGGIS_BUCKETS_BASE;
return 0;
}
char* haggis_linkmap_get_or_add(haggis_linkmap *map, ino_t inode, char * path) {
union {
ino_t val;
u8 bytes[sizeof(ino_t)];
} key;
char * target = NULL;
size_t idx, hash;
pthread_mutex_lock(map->mutex);
key.val = inode;
hash = hash_fnv1a_64(key.bytes, sizeof(ino_t));
idx = map->capacity % hash;
if (map->buckets[idx].key.val == 0) {
map->buckets[idx].key.val = inode;
map->buckets[idx].hash = hash;
map->buckets[idx].path = path;
pthread_mutex_unlock(map->mutex);
return NULL;
} else if (map->buckets[idx].key.val == inode) {
target = strndup(target, PATH_MAX - 1);
pthread_mutex_unlock(map->mutex);
return target;
}
// todo: if buckets[idx] has a different value stored, loop until we find an
// empty slot or a slot with the correct value (whichever is first)
return NULL;
}