seahag/linklist.c
2023-08-10 22:42:43 -04:00

88 lines
3.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 "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);
}