Replace linklist with linkmap and remove former completely. Also
remove generic hashmap implementation in favor of linkmap.
This commit is contained in:
parent
e372af0c42
commit
ed9648ff78
13 changed files with 73 additions and 577 deletions
2
Makefile
2
Makefile
|
@ -45,10 +45,8 @@ hdrs += include/jobq.h
|
||||||
hdrs += include/linklist.h
|
hdrs += include/linklist.h
|
||||||
|
|
||||||
srcs += bytes.c
|
srcs += bytes.c
|
||||||
srcs += hmap.c
|
|
||||||
srcs += haggis.c
|
srcs += haggis.c
|
||||||
srcs += jobq.c
|
srcs += jobq.c
|
||||||
srcs += linklist.c
|
|
||||||
srcs += linkmap.c
|
srcs += linkmap.c
|
||||||
|
|
||||||
objs = $(srcs:.c=.o)
|
objs = $(srcs:.c=.o)
|
||||||
|
|
11
haggis.c
11
haggis.c
|
@ -57,7 +57,6 @@
|
||||||
|
|
||||||
#include "bytes.h"
|
#include "bytes.h"
|
||||||
#include "haggis.h"
|
#include "haggis.h"
|
||||||
#include "linklist.h"
|
|
||||||
|
|
||||||
static unsigned char header[7] = {0x89, 'h', 'a', 'g', 'g', 'i', 's'};
|
static unsigned char header[7] = {0x89, 'h', 'a', 'g', 'g', 'i', 's'};
|
||||||
|
|
||||||
|
@ -520,7 +519,7 @@ void haggis_node_deinit(haggis_node *node) {
|
||||||
free(node);
|
free(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_hardlink_list *list) {
|
haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_linkmap *map) {
|
||||||
struct stat *st = NULL;
|
struct stat *st = NULL;
|
||||||
u16 mode;
|
u16 mode;
|
||||||
char *target;
|
char *target;
|
||||||
|
@ -558,7 +557,7 @@ haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_hardlink_
|
||||||
switch (node->filetype.tag) {
|
switch (node->filetype.tag) {
|
||||||
case normal:
|
case normal:
|
||||||
if (st->st_nlink > 1) {
|
if (st->st_nlink > 1) {
|
||||||
target = haggis_linklist_get_or_put(list, st->st_ino, file);
|
target = haggis_linkmap_get_or_add(map, st->st_ino, file);
|
||||||
if (target != NULL) {
|
if (target != NULL) {
|
||||||
node->filetype.tag = hardlink;
|
node->filetype.tag = hardlink;
|
||||||
haggis_filename_init(target, &node->filetype.f_type.target);
|
haggis_filename_init(target, &node->filetype.f_type.target);
|
||||||
|
@ -573,7 +572,7 @@ haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_hardlink_
|
||||||
break;
|
break;
|
||||||
case block:
|
case block:
|
||||||
if (st->st_nlink > 1) {
|
if (st->st_nlink > 1) {
|
||||||
target = haggis_linklist_get_or_put(list, st->st_ino, file);
|
target = haggis_linkmap_get_or_add(map, st->st_ino, file);
|
||||||
if (target != NULL) {
|
if (target != NULL) {
|
||||||
node->filetype.tag = hardlink;
|
node->filetype.tag = hardlink;
|
||||||
haggis_filename_init(target, &node->filetype.f_type.target);
|
haggis_filename_init(target, &node->filetype.f_type.target);
|
||||||
|
@ -584,7 +583,7 @@ haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_hardlink_
|
||||||
break;
|
break;
|
||||||
case character:
|
case character:
|
||||||
if (st->st_nlink > 1) {
|
if (st->st_nlink > 1) {
|
||||||
target = haggis_linklist_get_or_put(list, st->st_ino, file);
|
target = haggis_linkmap_get_or_add(map, st->st_ino, file);
|
||||||
if (target != NULL) {
|
if (target != NULL) {
|
||||||
node->filetype.tag = hardlink;
|
node->filetype.tag = hardlink;
|
||||||
haggis_filename_init(target, &node->filetype.f_type.target);
|
haggis_filename_init(target, &node->filetype.f_type.target);
|
||||||
|
@ -595,7 +594,7 @@ haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_hardlink_
|
||||||
break;
|
break;
|
||||||
case fifo:
|
case fifo:
|
||||||
if (st->st_nlink > 1) {
|
if (st->st_nlink > 1) {
|
||||||
target = haggis_linklist_get_or_put(list, st->st_ino, file);
|
target = haggis_linkmap_get_or_add(map, st->st_ino, file);
|
||||||
if (target != NULL) {
|
if (target != NULL) {
|
||||||
node->filetype.tag = hardlink;
|
node->filetype.tag = hardlink;
|
||||||
haggis_filename_init(target, &node->filetype.f_type.target);
|
haggis_filename_init(target, &node->filetype.f_type.target);
|
||||||
|
|
241
hmap.c
241
hmap.c
|
@ -1,241 +0,0 @@
|
||||||
/* _,.---._ .-._ .--.-. ,--.--------.
|
|
||||||
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
|
|
||||||
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
|
|
||||||
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
|
|
||||||
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
|
|
||||||
* |==|,| | -|==| , '=' |==| - _ | |==|- |
|
|
||||||
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
|
|
||||||
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
|
|
||||||
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
|
|
||||||
* _ __ ,---. .-._ .=-.-. _,.----.
|
|
||||||
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
|
|
||||||
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
|
|
||||||
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
|
|
||||||
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
|
|
||||||
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
|
|
||||||
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
|
|
||||||
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
|
|
||||||
* `--`---' `--` `--`./ `--``--`-`
|
|
||||||
*
|
|
||||||
* @(#)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 <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#include "haggis.h"
|
|
||||||
#include "haggis_private.h"
|
|
||||||
|
|
||||||
#define FNV64_OFFSET_BASIS 14695981039346656037u
|
|
||||||
#define FNV64_PRIME 1099511628211u
|
|
||||||
|
|
||||||
uint64_t hash_fnv1a_64(uint8_t *key, size_t len) {
|
|
||||||
int i;
|
|
||||||
uint64_t hash = FNV64_OFFSET_BASIS;
|
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
|
||||||
hash = hash ^ *key;
|
|
||||||
hash = hash * FNV64_PRIME;
|
|
||||||
key++;
|
|
||||||
}
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t hash_str_fnv1a_64(char * s) {
|
|
||||||
uint64_t hash = FNV64_OFFSET_BASIS;
|
|
||||||
|
|
||||||
while (*s != '\0') {
|
|
||||||
hash = hash ^ *s;
|
|
||||||
hash = hash * FNV64_PRIME;
|
|
||||||
s++;
|
|
||||||
}
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
hmap_node* hmap_node_init(void *key, size_t keysize, void *data) {
|
|
||||||
hmap_node *node;
|
|
||||||
uint64_t hash;
|
|
||||||
|
|
||||||
hash = hash_fnv1a_64(key, keysize);
|
|
||||||
node = calloc(1, sizeof(hmap_node));
|
|
||||||
if (node == NULL) return NULL;
|
|
||||||
node->key = key;
|
|
||||||
node->hash = hash;
|
|
||||||
node->data = data;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
void hmap_node_deinit(hmap_node *node) {
|
|
||||||
hmap_node *current = node;
|
|
||||||
while (current != NULL) {
|
|
||||||
current = node->next;
|
|
||||||
free(node);
|
|
||||||
node = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void* hmap_node_attach(hmap_node *root, hmap_node *leaf, size_t keysize) {
|
|
||||||
void *ret = NULL;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
if (memcmp(root->key, leaf->key, keysize) == 0) {
|
|
||||||
ret = root->data;
|
|
||||||
root->data = leaf->data;
|
|
||||||
return ret;
|
|
||||||
} else if (root->next == NULL) {
|
|
||||||
root->next = leaf;
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
root = root->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
// todo
|
|
||||||
}
|
|
||||||
|
|
||||||
void* hmap_node_search(hmap_node *root, void *key, size_t keysize) {
|
|
||||||
void *ret = NULL;
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
if (memcmp(root->key, key, keysize) == 0) {
|
|
||||||
ret = root->data;
|
|
||||||
return ret;
|
|
||||||
} else if (root->next == NULL) {
|
|
||||||
return NULL;
|
|
||||||
} else {
|
|
||||||
root = root->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
hmap* hmap_init(size_t keysize) {
|
|
||||||
hmap* map;
|
|
||||||
|
|
||||||
map = calloc(1, sizeof(hmap));
|
|
||||||
if (map == NULL) return NULL;
|
|
||||||
map->len = 0;
|
|
||||||
map->capacity = 64;
|
|
||||||
map->keysize = keysize;
|
|
||||||
map->buckets = calloc(64, sizeof(hmap_node));
|
|
||||||
if (map->buckets == NULL) {
|
|
||||||
free(map);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
void hmap_deinit(hmap *map) {
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < map->capacity; i++) {
|
|
||||||
if (map->buckets[i].next != NULL) {
|
|
||||||
hmap_node_deinit(map->buckets[i].next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
free(map->buckets);
|
|
||||||
free(map);
|
|
||||||
}
|
|
||||||
|
|
||||||
int hmap_expand(hmap *map) {
|
|
||||||
hmap_node *old;
|
|
||||||
hmap_node *new;
|
|
||||||
hmap_node *current;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
new = calloc(map->capacity + 64, sizeof(hmap_node));
|
|
||||||
if (new == NULL) return 1;
|
|
||||||
old = map->buckets;
|
|
||||||
map->buckets = new;
|
|
||||||
for (i = 0; i < map->capacity; i++) {
|
|
||||||
current = &old[i];
|
|
||||||
while (current->key != NULL) {
|
|
||||||
hmap_insert(map, current->key, current->data);
|
|
||||||
current = current->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
free(old);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* hmap_insert(hmap *map, void *key, void *data) {
|
|
||||||
hmap_node *node;
|
|
||||||
size_t idx;
|
|
||||||
void *ret = NULL;
|
|
||||||
|
|
||||||
if (map->len >= map->capacity / 2) {
|
|
||||||
if (hmap_expand(map)) return NULL;
|
|
||||||
}
|
|
||||||
node = hmap_node_init(key, map->keysize, data);
|
|
||||||
if (node == NULL) return NULL;
|
|
||||||
idx = node->hash % map->capacity;
|
|
||||||
if (map->buckets[idx].key == NULL) {
|
|
||||||
map->buckets[idx] = *node;
|
|
||||||
} else {
|
|
||||||
ret = hmap_node_attach(&map->buckets[idx], node, map->keysize);
|
|
||||||
}
|
|
||||||
if (ret == NULL) {
|
|
||||||
map->len++;
|
|
||||||
return NULL;
|
|
||||||
} else
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* hmap_get(hmap *map, void *key) {
|
|
||||||
uint64_t hash;
|
|
||||||
size_t idx;
|
|
||||||
void *ret;
|
|
||||||
|
|
||||||
hash = hash_fnv1a_64(key, map->keysize);
|
|
||||||
idx = hash % map->capacity;
|
|
||||||
ret = hmap_node_search(&map->buckets[idx], key, map->keysize);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
void* hmap_remove(hmap *map, void *key) {
|
|
||||||
uint64_t hash;
|
|
||||||
size_t idx;
|
|
||||||
void *ret = NULL;
|
|
||||||
hmap_node *previous = NULL;
|
|
||||||
hmap_node *current;
|
|
||||||
|
|
||||||
hash = hash_fnv1a_64(key, map->keysize);
|
|
||||||
idx = hash % map->capacity;
|
|
||||||
current = &map->buckets[idx];
|
|
||||||
if (current->key == NULL) return NULL;
|
|
||||||
if (memcmp(current->key, key, map->keysize) == 0) {
|
|
||||||
map->buckets[idx] = *current->next;
|
|
||||||
}
|
|
||||||
while (current->key != NULL) {
|
|
||||||
if (memcmp(current->key, key, map->keysize) == 0) {
|
|
||||||
ret = current->data;
|
|
||||||
map->len--;
|
|
||||||
if (previous != NULL) {
|
|
||||||
previous->next = current->next;
|
|
||||||
} else {
|
|
||||||
map->buckets[idx] = *current->next;
|
|
||||||
}
|
|
||||||
free(current);
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
current = current->next;
|
|
||||||
previous = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
|
@ -33,9 +33,10 @@
|
||||||
#ifndef HAGGIS_H
|
#ifndef HAGGIS_H
|
||||||
#define HAGGIS_H 1
|
#define HAGGIS_H 1
|
||||||
|
|
||||||
#include "linklist.h"
|
#include <pthread.h> // pthread_mutex_t
|
||||||
#include <stdint.h> // uint<x>_t
|
#include <stdint.h> // uint<x>_t
|
||||||
#include <stdio.h> // FILE
|
#include <stdio.h> // FILE
|
||||||
|
#include <sys/types.h> // ino_t
|
||||||
|
|
||||||
typedef uint8_t u8;
|
typedef uint8_t u8;
|
||||||
|
|
||||||
|
@ -115,8 +116,30 @@ typedef struct {
|
||||||
haggis_filetype filetype;
|
haggis_filetype filetype;
|
||||||
} haggis_node;
|
} haggis_node;
|
||||||
|
|
||||||
|
#define HAGGIS_BUCKETS_BASE 64
|
||||||
|
|
||||||
|
typedef struct __bucket {
|
||||||
|
union {
|
||||||
|
ino_t val;
|
||||||
|
uint8_t bytes[sizeof(ino_t)];
|
||||||
|
} key;
|
||||||
|
uint64_t hash;
|
||||||
|
char * path;
|
||||||
|
struct __bucket * next;
|
||||||
|
} haggis_bucket;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
pthread_mutex_t mutex;
|
||||||
|
size_t len;
|
||||||
|
size_t capacity;
|
||||||
|
haggis_bucket *buckets;
|
||||||
|
} haggis_linkmap;
|
||||||
|
|
||||||
|
haggis_linkmap* haggis_linkmap_init();
|
||||||
|
void haggis_linkmap_deinit(haggis_linkmap *map);
|
||||||
|
char* haggis_linkmap_get_or_add(haggis_linkmap *map, ino_t inode, char *path);
|
||||||
void haggis_node_deinit(haggis_node *node);
|
void haggis_node_deinit(haggis_node *node);
|
||||||
haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_hardlink_list *list);
|
haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_linkmap *map);
|
||||||
int haggis_extract_node(FILE *stram, haggis_node *node);
|
int haggis_extract_node(FILE *stram, haggis_node *node);
|
||||||
int haggis_load_node(FILE *stream, haggis_node *node);
|
int haggis_load_node(FILE *stream, haggis_node *node);
|
||||||
int haggis_store_node(FILE *stream, haggis_node *node);
|
int haggis_store_node(FILE *stream, haggis_node *node);
|
||||||
|
|
|
@ -1,57 +0,0 @@
|
||||||
/* _,.---._ .-._ .--.-. ,--.--------.
|
|
||||||
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
|
|
||||||
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
|
|
||||||
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
|
|
||||||
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
|
|
||||||
* |==|,| | -|==| , '=' |==| - _ | |==|- |
|
|
||||||
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
|
|
||||||
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
|
|
||||||
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
|
|
||||||
* _ __ ,---. .-._ .=-.-. _,.----.
|
|
||||||
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
|
|
||||||
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
|
|
||||||
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
|
|
||||||
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
|
|
||||||
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
|
|
||||||
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
|
|
||||||
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
|
|
||||||
* `--`---' `--` `--`./ `--``--`-`
|
|
||||||
*
|
|
||||||
* @(#)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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef HAGGIS_LINKLIST
|
|
||||||
#define HAGGIS_LINKLIST
|
|
||||||
|
|
||||||
#include <sys/stat.h> // ino_t
|
|
||||||
#include <pthread.h> // pthread_mutex_t
|
|
||||||
|
|
||||||
struct _haggis_hardlink {
|
|
||||||
struct _haggis_hardlink *next;
|
|
||||||
ino_t inode;
|
|
||||||
char *fname;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct _haggis_hardlink haggis_hardlink;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
pthread_mutex_t *mutex;
|
|
||||||
haggis_hardlink *head;
|
|
||||||
} haggis_hardlink_list;
|
|
||||||
|
|
||||||
int haggis_linklist_push(haggis_hardlink_list *list, haggis_hardlink *link);
|
|
||||||
haggis_hardlink* haggis_linklist_get(haggis_hardlink_list *list, ino_t inode);
|
|
||||||
char* haggis_linklist_get_or_put(haggis_hardlink_list *list, ino_t inode, char *fname);
|
|
||||||
void haggis_linklist_drain(haggis_hardlink_list *list);
|
|
||||||
|
|
||||||
#endif // !HAGGIS_LINKLIST
|
|
|
@ -1,62 +0,0 @@
|
||||||
/* _,.---._ .-._ .--.-. ,--.--------.
|
|
||||||
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
|
|
||||||
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
|
|
||||||
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
|
|
||||||
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
|
|
||||||
* |==|,| | -|==| , '=' |==| - _ | |==|- |
|
|
||||||
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
|
|
||||||
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
|
|
||||||
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
|
|
||||||
* _ __ ,---. .-._ .=-.-. _,.----.
|
|
||||||
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
|
|
||||||
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
|
|
||||||
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
|
|
||||||
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
|
|
||||||
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
|
|
||||||
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
|
|
||||||
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
|
|
||||||
* `--`---' `--` `--`./ `--``--`-`
|
|
||||||
*
|
|
||||||
* @(#)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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef HAGGIS_LINKMAP
|
|
||||||
#define HAGGIS_LINKMAP 1
|
|
||||||
|
|
||||||
#include "haggis.h"
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#define HAGGIS_BUCKETS_BASE 64
|
|
||||||
|
|
||||||
typedef struct __bucket {
|
|
||||||
union {
|
|
||||||
ino_t val;
|
|
||||||
u8 bytes[sizeof(ino_t)];
|
|
||||||
} key;
|
|
||||||
uint64_t hash;
|
|
||||||
char * path;
|
|
||||||
struct __bucket * next;
|
|
||||||
} haggis_bucket;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
pthread_mutex_t mutex;
|
|
||||||
size_t len;
|
|
||||||
size_t capacity;
|
|
||||||
haggis_bucket *buckets;
|
|
||||||
} haggis_linkmap;
|
|
||||||
|
|
||||||
#endif // !HAGGIS_LINKMAP
|
|
88
linklist.c
88
linklist.c
|
@ -1,88 +0,0 @@
|
||||||
/* _,.---._ .-._ .--.-. ,--.--------.
|
|
||||||
* _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\
|
|
||||||
* /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./
|
|
||||||
* |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \
|
|
||||||
* |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \
|
|
||||||
* |==|,| | -|==| , '=' |==| - _ | |==|- |
|
|
||||||
* |==| '=' /\==\ - ,_ /|==| /\ , | |==|, |
|
|
||||||
* |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/
|
|
||||||
* `-.`.____.' `--`--'' `--`./ `--` `--`--`
|
|
||||||
* _ __ ,---. .-._ .=-.-. _,.----.
|
|
||||||
* .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \
|
|
||||||
* /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-'
|
|
||||||
* |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | .
|
|
||||||
* |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \
|
|
||||||
* |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , |
|
|
||||||
* |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. /
|
|
||||||
* /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-'
|
|
||||||
* `--`---' `--` `--`./ `--``--`-`
|
|
||||||
*
|
|
||||||
* @(#)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 "linklist.h"
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
|
|
||||||
int haggis_linklist_push(haggis_hardlink_list *list, haggis_hardlink *link) {
|
|
||||||
pthread_mutex_lock(list->mutex);
|
|
||||||
link->next = list->head;
|
|
||||||
list->head = link;
|
|
||||||
return pthread_mutex_unlock(list->mutex);
|
|
||||||
}
|
|
||||||
|
|
||||||
haggis_hardlink* haggis_linklist_get(haggis_hardlink_list *list, ino_t inode) {
|
|
||||||
haggis_hardlink *link;
|
|
||||||
|
|
||||||
pthread_mutex_lock(list->mutex);
|
|
||||||
link = list->head;
|
|
||||||
while(link != NULL) {
|
|
||||||
if (link->inode == inode) {
|
|
||||||
return link;
|
|
||||||
}
|
|
||||||
link = link->next;
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(list->mutex);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
char* haggis_linklist_get_or_put(haggis_hardlink_list *list, ino_t inode, char *fname) {
|
|
||||||
haggis_hardlink *link;
|
|
||||||
|
|
||||||
link = haggis_linklist_get(list, inode);
|
|
||||||
if (link == NULL) {
|
|
||||||
link = malloc(sizeof(haggis_hardlink));
|
|
||||||
if (link == NULL) return NULL;
|
|
||||||
link->inode = inode;
|
|
||||||
link->next = NULL;
|
|
||||||
haggis_linklist_push(list, link);
|
|
||||||
} else {
|
|
||||||
return link->fname;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void haggis_linklist_drain(haggis_hardlink_list *list) {
|
|
||||||
haggis_hardlink *link;
|
|
||||||
haggis_hardlink *next;
|
|
||||||
|
|
||||||
pthread_mutex_lock(list->mutex);
|
|
||||||
link = list->head;
|
|
||||||
while (link != NULL) {
|
|
||||||
next = link->next;
|
|
||||||
free(link);
|
|
||||||
link = next;
|
|
||||||
}
|
|
||||||
pthread_mutex_unlock(list->mutex);
|
|
||||||
}
|
|
54
linkmap.c
54
linkmap.c
|
@ -30,16 +30,37 @@
|
||||||
* other than his own.
|
* other than his own.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "linkmap.h"
|
|
||||||
#include "haggis_private.h"
|
#include "haggis_private.h"
|
||||||
#include <limits.h>
|
|
||||||
#include <pthread.h>
|
#include <limits.h> // PATH_MAX
|
||||||
#include <stddef.h>
|
#include <stdlib.h> // calloc, free
|
||||||
#include <stdint.h>
|
#include <string.h> // strndup
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#define FNV64_OFFSET_BASIS 14695981039346656037u
|
||||||
#include <sys/stat.h>
|
#define FNV64_PRIME 1099511628211u
|
||||||
#include <sys/syslimits.h>
|
|
||||||
|
uint64_t hash_fnv1a_64(uint8_t *key, size_t len) {
|
||||||
|
int i;
|
||||||
|
uint64_t hash = FNV64_OFFSET_BASIS;
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
hash = hash ^ *key;
|
||||||
|
hash = hash * FNV64_PRIME;
|
||||||
|
key++;
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t hash_str_fnv1a_64(char * s) {
|
||||||
|
uint64_t hash = FNV64_OFFSET_BASIS;
|
||||||
|
|
||||||
|
while (*s != '\0') {
|
||||||
|
hash = hash ^ *s;
|
||||||
|
hash = hash * FNV64_PRIME;
|
||||||
|
s++;
|
||||||
|
}
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
@ -88,6 +109,18 @@ haggis_linkmap* haggis_linkmap_init() {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void haggis_linkmap_deinit(haggis_linkmap *map) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < map->capacity; i++) {
|
||||||
|
if (map->buckets[i].next == NULL) {
|
||||||
|
haggis_bucket_deinit(map->buckets[i].next);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(map->buckets);
|
||||||
|
free(map);
|
||||||
|
}
|
||||||
|
|
||||||
int haggis_linkmap_expand(haggis_linkmap *map) {
|
int haggis_linkmap_expand(haggis_linkmap *map) {
|
||||||
haggis_bucket *buckets_new;
|
haggis_bucket *buckets_new;
|
||||||
haggis_bucket *buckets_old;
|
haggis_bucket *buckets_old;
|
||||||
|
@ -128,17 +161,16 @@ char* haggis_linkmap_get_or_add(haggis_linkmap *map, ino_t inode, char * path) {
|
||||||
idx = map->capacity % hash;
|
idx = map->capacity % hash;
|
||||||
if (map->buckets[idx].key.val == inode) {
|
if (map->buckets[idx].key.val == inode) {
|
||||||
target = strndup(target, PATH_MAX - 1);
|
target = strndup(target, PATH_MAX - 1);
|
||||||
pthread_mutex_unlock(&map->mutex);
|
|
||||||
} else if (map->buckets[idx].key.val == 0) {
|
} else if (map->buckets[idx].key.val == 0) {
|
||||||
map->buckets[idx].key.val = inode;
|
map->buckets[idx].key.val = inode;
|
||||||
map->buckets[idx].hash = hash;
|
map->buckets[idx].hash = hash;
|
||||||
map->buckets[idx].path = path;
|
map->buckets[idx].path = path;
|
||||||
map->len++;
|
map->len++;
|
||||||
pthread_mutex_unlock(&map->mutex);
|
|
||||||
} 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;
|
||||||
target = haggis_bucket_search_append(&map->buckets[idx], b);
|
target = haggis_bucket_search_append(&map->buckets[idx], b);
|
||||||
}
|
}
|
||||||
|
pthread_mutex_unlock(&map->mutex);
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,10 +61,6 @@ tests += store_file_sha256
|
||||||
tests += load_file_sha256
|
tests += load_file_sha256
|
||||||
tests += fnv1a_hash_inode
|
tests += fnv1a_hash_inode
|
||||||
tests += fnv1a_hash_str
|
tests += fnv1a_hash_str
|
||||||
tests += init_hmap
|
|
||||||
tests += hmap_insert
|
|
||||||
tests += hmap_get
|
|
||||||
tests += hmap_replace
|
|
||||||
|
|
||||||
total != echo $(tests) | wc -w | awk '{ print $$1 }'
|
total != echo $(tests) | wc -w | awk '{ print $$1 }'
|
||||||
|
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
#include "haggis.h"
|
|
||||||
#include "haggis_private.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
hmap *map;
|
|
||||||
struct stat st;
|
|
||||||
char *data;
|
|
||||||
union {
|
|
||||||
ino_t val;
|
|
||||||
u8 bytes[sizeof(ino_t)];
|
|
||||||
} nod;
|
|
||||||
char *ret;
|
|
||||||
|
|
||||||
map = hmap_init(sizeof(ino_t));
|
|
||||||
assert(map != NULL);
|
|
||||||
data = "Makefile";
|
|
||||||
stat(data, &st);
|
|
||||||
nod.val = st.st_ino;
|
|
||||||
hmap_insert(map, &nod.bytes[0], data);
|
|
||||||
assert(map->len == 1);
|
|
||||||
ret = hmap_get(map, &nod.bytes[0]);
|
|
||||||
assert(memcmp(ret, data, 9) == 0);
|
|
||||||
hmap_deinit(map);
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -1,26 +0,0 @@
|
||||||
#include "haggis.h"
|
|
||||||
#include "haggis_private.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
hmap *map;
|
|
||||||
struct stat st;
|
|
||||||
char *data;
|
|
||||||
union {
|
|
||||||
ino_t val;
|
|
||||||
u8 bytes[sizeof(ino_t)];
|
|
||||||
} nod;
|
|
||||||
|
|
||||||
map = hmap_init(sizeof(ino_t));
|
|
||||||
assert(map != NULL);
|
|
||||||
data = "Makefile";
|
|
||||||
stat(data, &st);
|
|
||||||
nod.val = st.st_ino;
|
|
||||||
hmap_insert(map, &nod.bytes[0], data);
|
|
||||||
assert(map->len == 1);
|
|
||||||
hmap_deinit(map);
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -1,37 +0,0 @@
|
||||||
#include "haggis.h"
|
|
||||||
#include "haggis_private.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
hmap *map;
|
|
||||||
struct stat st;
|
|
||||||
char *data;
|
|
||||||
union {
|
|
||||||
ino_t val;
|
|
||||||
u8 bytes[sizeof(ino_t)];
|
|
||||||
} nod;
|
|
||||||
char * ret = NULL;
|
|
||||||
|
|
||||||
map = hmap_init(sizeof(ino_t));
|
|
||||||
assert(map != NULL);
|
|
||||||
data = "Makefile";
|
|
||||||
stat(data, &st);
|
|
||||||
nod.val = st.st_ino;
|
|
||||||
ret = hmap_insert(map, &nod.bytes[0], data);
|
|
||||||
assert(ret == NULL);
|
|
||||||
assert(map->len == 1);
|
|
||||||
ret = hmap_get(map, &nod.bytes[0]);
|
|
||||||
assert(memcmp(ret, data, 7) == 0);
|
|
||||||
ret = hmap_insert(map, &nod.bytes[0], "testdata");
|
|
||||||
assert(ret != NULL);
|
|
||||||
assert(memcmp(ret, data, 8) == 0);
|
|
||||||
assert(map->len == 1);
|
|
||||||
ret = hmap_get(map, &nod.bytes[0]);
|
|
||||||
assert(memcmp(ret, "testdata", 9) == 0);
|
|
||||||
hmap_deinit(map);
|
|
||||||
return 0;
|
|
||||||
}
|
|
|
@ -1,12 +0,0 @@
|
||||||
#include "haggis_private.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
hmap *map;
|
|
||||||
|
|
||||||
map = hmap_init(sizeof(ino_t));
|
|
||||||
assert(map != NULL);
|
|
||||||
hmap_deinit(map);
|
|
||||||
return 0;
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue