2023-08-17 11:45:59 -04:00
|
|
|
/* _,.---._ .-._ .--.-. ,--.--------.
|
|
|
|
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
|
|
|
|
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
|
|
|
|
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
|
|
|
|
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
|
|
|
|
* |==|,| | -|==| , '=' |==| - _ | |==|- |
|
|
|
|
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
|
|
|
|
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
|
|
|
|
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
|
|
|
|
* _ __ ,---. .-._ .=-.-. _,.----.
|
|
|
|
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
|
|
|
|
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
|
|
|
|
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
|
|
|
|
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
|
|
|
|
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
|
|
|
|
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
|
|
|
|
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
|
|
|
|
* `--`---' `--` `--`./ `--``--`-`
|
|
|
|
*
|
|
|
|
* @(#)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"
|
2023-08-17 23:26:53 -04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <pthread.h>
|
2023-08-17 11:45:59 -04:00
|
|
|
#include <stddef.h>
|
2023-08-17 23:26:53 -04:00
|
|
|
#include <stdint.h>
|
2023-08-17 11:45:59 -04:00
|
|
|
#include <stdlib.h>
|
2023-08-17 23:26:53 -04:00
|
|
|
#include <string.h>
|
2023-08-17 11:45:59 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2023-08-17 23:26:53 -04:00
|
|
|
haggis_bucket* haggis_bucket_init(ino_t inode, uint64_t hash, char * path) {
|
|
|
|
haggis_bucket *bucket;
|
2023-08-17 11:45:59 -04:00
|
|
|
|
2023-08-17 23:26:53 -04:00
|
|
|
bucket = calloc(1, sizeof(haggis_bucket));
|
|
|
|
if (bucket == NULL) return NULL;
|
|
|
|
bucket->key.val = inode;
|
|
|
|
bucket->hash = hash;
|
|
|
|
bucket->path = path;
|
|
|
|
return bucket;
|
2023-08-17 11:45:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-17 23:26:53 -04:00
|
|
|
void haggis_bucket_deinit(haggis_bucket *nod) {
|
|
|
|
if (nod->path != NULL)
|
|
|
|
free(nod->path);
|
|
|
|
free(nod);
|
2023-08-17 11:45:59 -04:00
|
|
|
}
|
|
|
|
|
2023-08-17 23:26:53 -04:00
|
|
|
void haggis_bucket_append(haggis_bucket *head, haggis_bucket *tail) {
|
2023-08-17 11:45:59 -04:00
|
|
|
|
|
|
|
while (head->next != NULL) {
|
2023-08-17 23:26:53 -04:00
|
|
|
head = head->next;
|
2023-08-17 11:45:59 -04:00
|
|
|
}
|
|
|
|
head->next = tail;
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:23:02 -04:00
|
|
|
haggis_linkmap* haggis_linkmap_init() {
|
|
|
|
haggis_linkmap *map;
|
|
|
|
|
|
|
|
map = calloc(1, sizeof(haggis_linkmap));
|
|
|
|
if (map == NULL)
|
|
|
|
return NULL;
|
|
|
|
map->buckets = calloc(HAGGIS_BUCKETS_BASE, sizeof(size_t));
|
|
|
|
if (map->buckets == NULL) {
|
|
|
|
free(map);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
map->capacity = HAGGIS_BUCKETS_BASE;
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2023-08-17 11:45:59 -04:00
|
|
|
int haggis_linkmap_expand(haggis_linkmap *map) {
|
2023-08-17 23:26:53 -04:00
|
|
|
haggis_bucket *buckets_new;
|
|
|
|
haggis_bucket *buckets_old;
|
2023-08-17 11:45:59 -04:00
|
|
|
size_t i, hash, idx;
|
|
|
|
|
2023-08-17 23:26:53 -04:00
|
|
|
buckets_new = calloc(map->capacity + HAGGIS_BUCKETS_BASE, sizeof(haggis_bucket));
|
2023-08-17 11:45:59 -04:00
|
|
|
if (buckets_new == NULL)
|
|
|
|
return 2;
|
|
|
|
for (i = 0; i < map->capacity; i++) {
|
2023-08-17 23:26:53 -04:00
|
|
|
if (map->buckets[i].key.val != 0) {
|
|
|
|
hash = hash_fnv1a_64(&map->buckets[i].key.bytes[0], sizeof(ino_t));
|
2023-08-17 11:45:59 -04:00
|
|
|
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;
|
|
|
|
}
|
2023-08-17 23:26:53 -04:00
|
|
|
|
|
|
|
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;
|
2023-08-18 00:23:02 -04:00
|
|
|
int i;
|
2023-08-17 23:26:53 -04:00
|
|
|
|
2023-08-18 00:23:02 -04:00
|
|
|
pthread_mutex_lock(&map->mutex);
|
|
|
|
if (map->len >= map->capacity)
|
|
|
|
haggis_linkmap_expand(map);
|
2023-08-17 23:26:53 -04:00
|
|
|
key.val = inode;
|
|
|
|
hash = hash_fnv1a_64(key.bytes, sizeof(ino_t));
|
|
|
|
idx = map->capacity % hash;
|
2023-08-18 00:23:02 -04:00
|
|
|
for (i = 0; i < map->capacity; i++) {
|
|
|
|
if (map->buckets[idx].key.val == 0) {
|
|
|
|
map->buckets[idx].key.val = inode;
|
|
|
|
map->buckets[idx].hash = hash;
|
|
|
|
map->buckets[idx].path = path;
|
|
|
|
map->len++;
|
|
|
|
pthread_mutex_unlock(&map->mutex);
|
|
|
|
break;
|
|
|
|
} else if (map->buckets[idx].key.val == inode) {
|
|
|
|
target = strndup(target, PATH_MAX - 1);
|
|
|
|
pthread_mutex_unlock(&map->mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (idx == map->capacity - 1)
|
|
|
|
idx = 0;
|
|
|
|
else
|
|
|
|
idx++;
|
2023-08-17 23:26:53 -04:00
|
|
|
}
|
2023-08-18 00:23:02 -04:00
|
|
|
return target;
|
2023-08-17 23:26:53 -04:00
|
|
|
}
|