diff --git a/Makefile b/Makefile index 0a98e23..4b45bfb 100644 --- a/Makefile +++ b/Makefile @@ -42,12 +42,14 @@ CFLAGS += -fPIC hdrs += include/bytes.h hdrs += include/haggis.h hdrs += include/jobq.h +hdrs += include/mq.h mans += man/haggis_node.3 srcs += bytes.c srcs += haggis.c srcs += jobq.c +srcs += mq.c srcs += linkmap.c objs = $(srcs:.c=.o) diff --git a/haggis.c b/haggis.c index 01a98c8..c10c063 100644 --- a/haggis.c +++ b/haggis.c @@ -30,6 +30,7 @@ * other than his own. */ +#include #include #include // PATH_MAX #include // uint_t @@ -635,8 +636,61 @@ haggis_node* haggis_create_node(char *file, haggis_algorithm a, haggis_linkmap * return node; } +int haggis_extract_dev(haggis_node *node, char *basedir) { + // todo + assert(node->filetype.tag == block || node->filetype.tag == character); + return 0; +} + +int haggis_extract_fifo(haggis_node *node, char *basedir) { + // todo + assert(node->filetype.tag == fifo); + return 0; +} + +int haggis_extract_symlink(haggis_node *node, char *basedir) { + // todo + assert(node->filetype.tag == softlink); + return 0; +} + +int haggis_extract_hardlink(haggis_node *node, char *basedir) { + // todo + assert(node->filetype.tag == hardlink); + return 0; +} + +int haggis_extract_dir(haggis_node *node, char *basedir) { + // todo + assert(node->filetype.tag == directory); + return 0; +} + +int haggis_extract_file(haggis_node *node, char *basedir) { + // todo + assert(node->filetype.tag == normal); + return 0; +} + int haggis_extract_node(FILE *stream, char *basedir, haggis_node *node) { // todo + switch (node->filetype.tag) { + case block: + case character: + return haggis_extract_dev(node, basedir); + case fifo: + return haggis_extract_fifo(node, basedir); + case softlink: + return haggis_extract_symlink(node, basedir); + case hardlink: + return haggis_extract_hardlink(node, basedir); + case directory: + return haggis_extract_dir(node, basedir); + case normal: + return haggis_extract_file(node, basedir); + case eof: + return 0; + } return 0; } diff --git a/include/mq.h b/include/mq.h new file mode 100644 index 0000000..5b5f8fe --- /dev/null +++ b/include/mq.h @@ -0,0 +1,72 @@ +/* _,.---._ .-._ .--.-. ,--.--------. + * _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\ + * /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./ + * |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \ + * |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \ + * |==|,| | -|==| , '=' |==| - _ | |==|- | + * |==| '=' /\==\ - ,_ /|==| /\ , | |==|, | + * |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/ + * `-.`.____.' `--`--'' `--`./ `--` `--`--` + * _ __ ,---. .-._ .=-.-. _,.----. + * .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \ + * /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-' + * |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | . + * |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \ + * |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , | + * |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. / + * /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-' + * `--`---' `--` `--`./ `--``--`-` + * + * @(#)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 MQ_H +#define MQ_H 1 + +#include + +#include "haggis.h" + +typedef enum { + NodeCreated, + NodeExtracted, + EndOfArchive, + ArchiveError, +} haggis_message_type; + +typedef union { + char *f_name; + int errno; +} haggis_message_body; + +struct _mq_node { + struct _mq_node *prev; + struct _mq_node *next; + haggis_message_type tag; + haggis_message_body body; +}; + +typedef struct _mq_node haggis_msg; + +typedef struct { + pthread_cond_t *cond; + size_t count; + pthread_mutex_t *mutex; + haggis_msg *head; + haggis_msg *tail; +} haggis_mq; + +int haggis_mq_push(haggis_mq *queue, haggis_msg *msg); +haggis_msg* haggis_mq_pop(haggis_mq *queue); + +#endif // !JOBQ_H diff --git a/mq.c b/mq.c new file mode 100644 index 0000000..3664e1c --- /dev/null +++ b/mq.c @@ -0,0 +1,80 @@ +/* _,.---._ .-._ .--.-. ,--.--------. + * _,..---._ ,-.' , - `. /==/ \ .-._/==/ //==/, - , -\ + * /==/, - \ /==/_, , - \|==|, \/ /, |==\ -\\==\.-. - ,-./ + * |==| _ _\==| .=. |==|- \| | \==\- \`--`\==\- \ + * |==| .=. |==|_ : ;=: - |==| , | -| `--`-' \==\_ \ + * |==|,| | -|==| , '=' |==| - _ | |==|- | + * |==| '=' /\==\ - ,_ /|==| /\ , | |==|, | + * |==|-, _`/ '.='. - .' /==/, | |- | /==/ -/ + * `-.`.____.' `--`--'' `--`./ `--` `--`--` + * _ __ ,---. .-._ .=-.-. _,.----. + * .-`.' ,`..--.' \ /==/ \ .-._ /==/_ /.' .' - \ + * /==/, - \==\-/\ \ |==|, \/ /, /==|, |/==/ , ,-' + * |==| _ .=. /==/-|_\ | |==|- \| ||==| ||==|- | . + * |==| , '=',\==\, - \ |==| , | -||==|- ||==|_ `-' \ + * |==|- '..'/==/ - ,| |==| - _ ||==| ,||==| _ , | + * |==|, | /==/- /\ - \|==| /\ , ||==|- |\==\. / + * /==/ - | \==\ _.\=\.-'/==/, | |- |/==/. / `-.`.___.-' + * `--`---' `--` `--`./ `--``--`-` + * + * @(#)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 // free, malloc + +#include "mq.h" + +haggis_msg* haggis_msg_init(haggis_message_type tag, haggis_message_body body) { + haggis_msg *msg = calloc(1, sizeof(haggis_msg)); + if (msg == NULL) + return NULL; + msg->next = msg->prev = NULL; + msg->tag = tag; + msg->body = body; + return msg; +} + +int haggis_mq_push(haggis_mq *queue, haggis_msg *msg) { + msg->next = msg->prev = NULL; + + pthread_mutex_lock(queue->mutex); + if (queue->tail == NULL) { + queue->tail = queue->head = msg; + } else { + msg->next = queue->tail; + queue->tail->prev = msg; + queue->tail = msg; + } + queue->count++; + pthread_mutex_unlock(queue->mutex); + return 0; +} + +haggis_msg* haggis_mq_pop(haggis_mq *queue) { + haggis_msg *msg; + + while (queue->count == 0) + pthread_cond_wait(queue->cond, queue->mutex); + pthread_mutex_lock(queue->mutex); + queue->count--; + msg = queue->head; + if (msg->tag == EndOfArchive) + return NULL; + if (queue->tail == queue->head) { + queue->tail = queue->head = NULL; + } else { + queue->head = queue->head->prev; + } + pthread_mutex_unlock(queue->mutex); + msg->prev = msg->next = NULL; + return msg; +}