Files
progettoso/lib/threadpool/fileQueue.c

851 lines
16 KiB
C
Raw Normal View History

2022-04-27 21:18:25 +02:00
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <stdio.h>
2022-04-27 21:18:25 +02:00
#include <string.h>
2022-03-15 00:30:04 +01:00
#include <fileQueue.h>
#include <util.h>
2022-04-27 21:18:25 +02:00
#define MAXNAMELEN 512
2022-03-15 00:30:04 +01:00
// creazione di un fileT
fileT* createFileT(char *f, int O_LOCK, int client, int open){
if(!f){
errno = EINVAL;
return NULL;
}
fileT *file = malloc(sizeof(fileT));
2022-04-08 21:32:52 +02:00
if (file == NULL) {
2022-03-15 00:30:04 +01:00
perror("Malloc createFileT");
return NULL;
}
2022-04-08 21:32:52 +02:00
file->O_LOCK = (O_LOCK == 0)? 0 : 1;
file->owner = client;
file->open = (open == 0)? 0 : 1;
file->size = 0;
file->valid = 0;
2022-03-15 00:30:04 +01:00
2022-04-27 21:18:25 +02:00
if ((file->filepath = calloc(strnlen(f, MAXNAMELEN)+1, sizeof(char))) == NULL) {
2022-03-15 00:30:04 +01:00
perror("Malloc filepath");
2022-04-08 21:32:52 +02:00
destroyFile(file);
2022-03-15 00:30:04 +01:00
return NULL;
}
2022-04-27 21:18:25 +02:00
strncpy(file->filepath, f, strnlen(f, MAXNAMELEN));
2022-03-15 00:30:04 +01:00
2022-04-08 21:32:52 +02:00
// in seguito semplicemente facciamo realloc
2022-04-29 15:47:27 +02:00
if ((file->data = calloc(1, sizeof(char))) == NULL) {
perror("Calloc content");
2022-03-15 00:30:04 +01:00
return NULL;
}
2022-04-08 21:32:52 +02:00
return file;
2022-03-15 00:30:04 +01:00
}
// append su fileT
int writeFileT(fileT *f, void *data, size_t size) {
if(!f || !data || size < 0){
errno = EINVAL;
return -1;
}
2022-04-29 15:47:27 +02:00
if(size == 0) {
return 0;
}
2022-04-27 21:18:25 +02:00
2022-04-04 18:58:40 +02:00
if ((f->data = realloc(f->data, f->valid + size)) == NULL) {
2022-03-15 00:30:04 +01:00
perror("Realloc content");
return -1;
}
2022-04-04 18:58:40 +02:00
memcpy((char*) f->data + f->valid, data, size);
f->valid = f->valid + size;
f->size = (f->valid>f->size)?f->valid:f->size;
2022-04-08 21:32:52 +02:00
return 0;
2022-03-15 00:30:04 +01:00
}
// destroy fileT
void destroyFile(fileT *f) {
if(!f)
return;
if(f->filepath)
free(f->filepath);
if(f->data)
free(f->data);
free(f);
}
// --------------
// creazione queue
queueT* createQueue(size_t maxLen, size_t maxSize) {
if(maxLen<0 || maxSize<0){
errno = EINVAL;
return NULL;
}
queueT *q = malloc(sizeof(queueT));
if(q==NULL){
perror("Malloc createQueue");
return NULL;
}
// creazione lock
if (pthread_mutex_init(&q->m, NULL) != 0) {
perror("pthread_mutex_init createQueue");
destroyQueue(q);
return NULL;
}
q->head = NULL;
q->tail = NULL;
q->maxLen = maxLen;
q->len = 0;
q->maxSize = maxSize;
q->size = 0;
return q;
}
// insert
int enqueue(queueT *q, fileT* data) {
if(!q || !data){
errno = EINVAL;
return -1;
}
LOCK_RETURN(&q->m, -1); // begin me
if (q->len == q->maxLen) { // too many files
errno = ENFILE;
goto _end_enqueue;
}
if (q->size + data->size > q->maxSize) {
errno = EFBIG;
goto _end_enqueue;
}
// inserisco l'elemento
nodeT *newNode = malloc(sizeof(nodeT));
if (newNode == NULL) {
perror("malloc newNode");
goto _end_enqueue;
}
newNode->data = data;
newNode->next = NULL;
2022-04-08 21:32:52 +02:00
nodeT *tmp = q->head;
2022-03-15 00:30:04 +01:00
if (q->head == NULL)
q->head = newNode;
else {
while (tmp->next)
2022-04-08 21:32:52 +02:00
tmp = tmp->next;
2022-03-15 00:30:04 +01:00
2022-04-08 21:32:52 +02:00
tmp->next = newNode;
2022-03-15 00:30:04 +01:00
}
q->tail = newNode;
++q->len;
q->size += data->size;
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
_end_enqueue:
UNLOCK_RETURN(&q->m, -1);
return -1;
}
// remove
fileT* dequeue(queueT *q) {
if(!q) {
errno = EINVAL;
return NULL;
}
LOCK_RETURN(&q->m, NULL); // begin me
2022-04-04 18:58:40 +02:00
// TODO: altri oltre fifo
2022-03-15 00:30:04 +01:00
if (q->head == NULL || q->len == 0) { // coda vuota
errno = ENOENT;
goto _end_dequeue;
}
fileT *data = (q->head)->data;
nodeT *tmp = NULL;
tmp = q->head;
q->head = (q->head)->next;
if (q->head == NULL) { // coda diventa vuota
q->tail = NULL;
}
--q->len;
q->size -= data->size;
free(tmp); // free del nodo
UNLOCK_RETURN(&q->m, NULL); // end me
return data;
_end_dequeue:
UNLOCK_RETURN(&q->m, NULL);
return NULL;
}
2022-04-04 18:58:40 +02:00
// dequeue until we have s free space and expand the file by s
fileT ** dequeueN(queueT *q, char *filepath, size_t s) {
if(!q) {
errno = EINVAL;
return NULL;
}
if(s<=0) {
return NULL;
}
LOCK_RETURN(&q->m, NULL); // begin me
/* TODO: altri oltre a fifo */
if (q->head == NULL || q->len == 0) { // coda vuota
errno = ENOENT;
goto _end_dequeueN;
}
if(q->maxSize > s) {
errno = EINVAL;
goto _end_dequeueN;
}
// scorro la queue per trovare l'elemento
nodeT *tmp = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
tmp = tmp->next;
}
if(!tmp) { // non trovato
errno = ENOENT;
goto _end_dequeueN;
}
fileT **returnList = NULL; // lista dei file rimossi
2022-04-08 21:32:52 +02:00
tmp = NULL;
2022-04-04 18:58:40 +02:00
returnList = calloc(1, sizeof(fileT*));
returnList[0] = NULL;
int purged = 0;
while(q->size + s > q->maxSize) {
purged++;
2022-04-08 22:09:01 +02:00
returnList = realloc(returnList, purged * sizeof(fileT*));
2022-04-04 18:58:40 +02:00
if(!returnList) {
perror("realloc");
goto _end_dequeueN;
}
tmp = q->head;
q->head = (q->head)->next;
returnList[purged-1] = tmp->data;
--q->len;
q->size -= tmp->data->size;
if (q->head == NULL) { // coda diventa vuota
q->tail = NULL;
break; // we eliminated everything so we must have enought space
}
}
2022-04-08 22:09:01 +02:00
returnList = realloc(returnList, purged+1 * sizeof(fileT*));
2022-04-04 18:58:40 +02:00
returnList[purged] = NULL; // null terminated
2022-04-08 22:09:01 +02:00
tmp->data->size += s;
q->size += s;
2022-04-04 18:58:40 +02:00
UNLOCK_RETURN(&q->m, NULL); // end me
2022-04-08 22:09:01 +02:00
return returnList;
2022-04-04 18:58:40 +02:00
_end_dequeueN:
UNLOCK_RETURN(&q->m, NULL);
return NULL;
}
2022-03-15 00:30:04 +01:00
void voidDequeue(queueT *q) {
if(!q) {
errno = EINVAL;
return;
}
LOCK(&q->m); // begin me
if (q->head == NULL || q->len == 0) { // coda vuota
errno = ENOENT;
goto _end_void_dequeue;
}
nodeT *tmp = NULL;
tmp = q->head;
q->head = (q->head)->next;
if (q->head == NULL) { // coda diventa vuota
q->tail = NULL;
}
--q->len;
2022-04-08 22:09:01 +02:00
q->size -= (tmp->data)->size;
2022-03-15 00:30:04 +01:00
destroyFile(tmp->data); // free fileT
free(tmp); // free nodo
UNLOCK(&q->m); // end me
return;
_end_void_dequeue:
2022-04-04 18:58:40 +02:00
UNLOCK(&q->m);
return;
2022-03-15 00:30:04 +01:00
}
// print queue
int printQueue(FILE *stream, queueT *q) {
if(!q || !stream) {
errno = EINVAL;
return -1;
}
LOCK_RETURN(&q->m, -1); // begin me
nodeT *tmp = q->head;
2022-04-27 21:18:25 +02:00
fprintf(stream, "Lista file:\n");
fprintf(stream, "[Nome File] -> Dimensione in MB\n");
2022-04-08 22:09:01 +02:00
while (tmp!=NULL) {
2022-03-15 00:30:04 +01:00
float res = ((float)(tmp->data)->size)/1000000; // in MB
2022-04-04 18:58:40 +02:00
// float res = ((float)(tmp->data)->valid)/1000000; // in MB
2022-04-27 21:18:25 +02:00
fprintf(stream, "[%s] -> %f MB\n", (tmp->data)->filepath, res);
2022-04-08 22:09:01 +02:00
tmp = tmp->next;
2022-03-15 00:30:04 +01:00
}
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
}
// acquisizione lock
int lockFileInQueue(queueT *q, char *filepath, int owner) {
if(!q || !filepath) {
errno = EINVAL;
return -1;
}
LOCK_RETURN(&q->m, -1); // begin me
if (q->len == 0) {
errno = ENOENT;
goto _end_lock_file_queue;
}
// scorro la queue per trovare l'elemento
nodeT *tmp = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
tmp = tmp->next;
}
if(!tmp) { // non trovato
errno = ENOENT;
goto _end_lock_file_queue;
}
if ((tmp->data)->O_LOCK && (tmp->data)->owner != owner) { // lock non del owner
errno = EPERM;
goto _end_lock_file_queue;
}
// acquisisco lock sul file
2022-04-08 22:09:01 +02:00
(tmp->data)->O_LOCK = 1;
(tmp->data)->owner = owner;
2022-03-15 00:30:04 +01:00
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
_end_lock_file_queue:
UNLOCK_RETURN(&q->m, -1);
return -1;
}
int unlockFileInQueue(queueT *q, char *filepath, int owner) {
if(!q || !filepath) {
errno = EINVAL;
return -1;
}
LOCK_RETURN(&q->m, -1); // begin me
if (q->len == 0) {
errno = ENOENT;
goto _end_unlock_file_queue;
}
// scorro la queue per trovare l'elemento
nodeT *tmp = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
tmp = tmp->next;
}
if(!tmp) { // non trovato
errno = ENOENT;
2022-04-08 22:09:01 +02:00
goto _end_unlock_file_queue;
2022-03-15 00:30:04 +01:00
}
if((tmp->data)->O_LOCK == 0) { // nessuno ha il lock
UNLOCK_RETURN(&q->m, -1);
return 0;
}
if((tmp->data)->O_LOCK == 1 && (tmp->data)->owner != owner) { // lock non del owner
errno = EPERM;
goto _end_unlock_file_queue;
}
(tmp->data)->O_LOCK = 0;
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
_end_unlock_file_queue:
UNLOCK_RETURN(&q->m, -1);
return -1;
}
// open file
int openFileInQueue(queueT *q, char *filepath, int O_LOCK, int owner) {
if(!q || !filepath) {
errno = EINVAL;
return -1;
}
LOCK_RETURN(&q->m, -1); // begin me
if (q->len == 0) {
errno = ENOENT;
goto _end_open_file_queue;
}
// scorro la queue per trovare l'elemento
nodeT *tmp = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
tmp = tmp->next;
}
if(!tmp) { // non trovato
errno = ENOENT;
goto _end_open_file_queue;
}
2022-04-09 01:11:46 +02:00
// lock non del owner
if((tmp->data)->O_LOCK == 1 && (tmp->data)->owner != owner) {
2022-03-15 00:30:04 +01:00
errno = EPERM;
goto _end_open_file_queue;
}
(tmp->data)->open = 1;
(tmp->data)->O_LOCK = (O_LOCK==0)?0:1;
if(O_LOCK!=0) {
2022-04-08 22:09:01 +02:00
(tmp->data)->owner = owner;
2022-03-15 00:30:04 +01:00
}
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
_end_open_file_queue:
UNLOCK_RETURN(&q->m, -1);
return -1;
}
// close and relese lock
int closeFileInQueue(queueT *q, char *filepath, int owner) {
if(!q || !filepath) {
errno = EINVAL;
return -1;
}
LOCK_RETURN(&q->m, -1); // begin me
if (q->len == 0) {
errno = ENOENT;
goto _end_close_file_queue;
}
// scorro la queue per trovare l'elemento
nodeT *tmp = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
tmp = tmp->next;
}
if(!tmp) { // non trovato
errno = ENOENT;
goto _end_close_file_queue;
}
if((tmp->data)->O_LOCK == 1 && (tmp->data)->owner != owner) { // lock non del owner
errno = EPERM;
goto _end_close_file_queue;
}
(tmp->data)->open = 0;
(tmp->data)->O_LOCK = 0;
(tmp->data)->owner = owner;
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
2022-04-08 22:09:01 +02:00
_end_close_file_queue:
2022-03-15 00:30:04 +01:00
UNLOCK_RETURN(&q->m, -1);
return -1;
}
int writeFileInQueue(queueT *q, char *filepath, void *data, size_t size, int owner) {
if(!q || !filepath || !data) {
errno = EINVAL;
return -1;
}
if(size == 0)
return 0;
LOCK_RETURN(&q->m, -1); // begin me
if (q->len == 0) { // coda vuota
errno = ENOENT;
goto _end_write_file_queue;
}
if (q->size + size > q->maxSize) { // non c'e' abbastanza spazio
errno = EFBIG;
goto _end_write_file_queue;
}
// scorro la queue per trovare l'elemento
nodeT *tmp = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
tmp = tmp->next;
}
if(!tmp) { // non trovato
errno = ENOENT;
goto _end_write_file_queue;
}
if ((tmp->data)->open == 0 || ((tmp->data)->O_LOCK && (tmp->data)->owner != owner)) {
// if file non è aperto o la lock non è del owner
errno = EPERM;
goto _end_write_file_queue;
}
// scrivo
2022-04-04 18:58:40 +02:00
if (((tmp->data)->data = realloc((tmp->data)->data, size)) == NULL) {
2022-03-15 00:30:04 +01:00
perror("Realloc content");
goto _end_write_file_queue;
}
memcpy((tmp->data)->data, data, size);
q->size = (q->size) - ((tmp->data)->size) + size;
2022-04-04 18:58:40 +02:00
(tmp->data)->valid = size;
2022-03-15 00:30:04 +01:00
(tmp->data)->size = size;
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
_end_write_file_queue:
UNLOCK_RETURN(&q->m, -1);
return -1;
}
int appendFileInQueue(queueT *q, char *filepath, void *data, size_t size, int owner) {
if (!q || !filepath || !data) {
errno = EINVAL;
return -1;
}
if (size == 0)
return 0;
LOCK_RETURN(&q->m, -1); // begin me
if (q->len == 0) { // coda vuota
errno = ENOENT;
goto _end_append_file_queue;
}
if (q->size + size > q->maxSize) { // non c'e' abbastanza spazio
errno = EFBIG;
goto _end_append_file_queue;
}
// scorro la queue per trovare l'elemento
nodeT *tmp = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
tmp = tmp->next;
}
if(!tmp) { // non trovato
errno = ENOENT;
goto _end_append_file_queue;
}
if ((tmp->data)->open == 0 || ((tmp->data)->O_LOCK && (tmp->data)->owner != owner)) {
// if file non è aperto o la lock non è del owner
errno = EPERM;
goto _end_append_file_queue;
}
// scrivo
2022-04-04 18:58:40 +02:00
if (((tmp->data)->data = realloc((tmp->data)->data, (tmp->data)->valid + size)) == NULL) {
2022-03-15 00:30:04 +01:00
perror("Realloc content");
goto _end_append_file_queue;
}
2022-04-08 22:09:01 +02:00
memcpy(((char *)(tmp->data)->data) + (tmp->data)->valid, data, size);
2022-03-15 00:30:04 +01:00
// memmove sarebbe un'alternativa
2022-04-04 18:58:40 +02:00
(tmp->data)->valid += size;
q->size -= (tmp->data)->size;
(tmp->data)->size = ((tmp->data)->size > (tmp->data)->valid) ? (tmp->data)->size : (tmp->data)->valid;
q->size += (tmp->data)->size;
2022-03-15 00:30:04 +01:00
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
_end_append_file_queue:
UNLOCK_RETURN(&q->m, -1);
return -1;
}
int removeFileFromQueue(queueT *q, char *filepath, int owner) {
if(!q || !filepath) {
errno = EINVAL;
return -1;
}
LOCK_RETURN(&q->m, -1); // begin me
if (q->len == 0) { // coda vuota
errno = ENOENT;
goto _end_remove_file_queue;
}
// scorro la queue per trovare l'elemento
nodeT *tmp = q->head;
nodeT *pre = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
2022-04-08 22:09:01 +02:00
if(pre!=tmp)
pre = pre->next;
2022-03-15 00:30:04 +01:00
tmp = tmp->next;
}
if (!tmp) { // non trovato
errno = ENOENT;
goto _end_remove_file_queue;
}
2022-04-30 00:37:59 +02:00
if (((tmp->data)->O_LOCK) && ((tmp->data)->owner != owner)) {
// if lock non è del owner
2022-03-15 00:30:04 +01:00
errno = EPERM;
2022-04-08 22:09:01 +02:00
goto _end_remove_file_queue;
2022-03-15 00:30:04 +01:00
}
2022-04-08 22:09:01 +02:00
if (tmp == pre) { // file è il primo
2022-03-15 00:30:04 +01:00
q->head = tmp->next;
if (tmp->next == NULL) {
q->tail = tmp;
}
} else { // file non è il primo
2022-04-08 22:09:01 +02:00
pre->next = tmp->next;
2022-03-15 00:30:04 +01:00
2022-04-08 22:09:01 +02:00
if (pre->next == NULL) {
q->tail = pre;
2022-03-15 00:30:04 +01:00
}
}
--q->len;
q->size -= (tmp->data)->size;
destroyFile(tmp->data); // free file
free(tmp); // free node
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
_end_remove_file_queue:
UNLOCK_RETURN(&q->m, -1);
return -1;
}
// ----------------
// cerco e ritorno una copia
fileT* find(queueT *q, char *filepath) {
if(!q || !filepath) {
errno = EINVAL;
return NULL;
}
LOCK_RETURN(&q->m, NULL); // begin me
if (q->len == 0)
goto _end_find_in_queue;
fileT *res = NULL;
nodeT *tmp = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
tmp = tmp->next;
}
if(!tmp)
goto _end_find_in_queue;
// creo una nuova istanza
res = createFileT((tmp->data)->filepath, (tmp->data)->O_LOCK, (tmp->data)->owner, (tmp->data)->open);
if (!res) {
perror("createFileT res");
goto _end_find_in_queue;
}
if (writeFileT(res, (tmp->data)->data, (tmp->data)->size) == -1) {
perror("writeFileT res");
2022-04-25 20:24:55 +02:00
destroyFile(res);
2022-03-15 00:30:04 +01:00
goto _end_find_in_queue;
}
2022-04-27 21:18:25 +02:00
2022-03-15 00:30:04 +01:00
UNLOCK_RETURN(&q->m, NULL); // end me
return res;
_end_find_in_queue:
UNLOCK_RETURN(&q->m, NULL);
return NULL;
}
2022-03-31 22:26:44 +02:00
// cerco
int searchFile(queueT *q, char *filepath) {
if(!q || !filepath) {
errno = EINVAL;
2022-04-08 22:09:01 +02:00
return 0;
2022-03-31 22:26:44 +02:00
}
2022-04-08 22:09:01 +02:00
LOCK_RETURN(&q->m, 0); // begin me
2022-03-31 22:26:44 +02:00
if (q->len == 0)
goto _end_search_file_in_queue;
nodeT *tmp = q->head;
while (tmp) {
if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break;
}
tmp = tmp->next;
}
if(!tmp)
goto _end_search_file_in_queue;
2022-04-08 22:09:01 +02:00
UNLOCK_RETURN(&q->m, 0); // end me
2022-03-31 22:26:44 +02:00
return 1;
_end_search_file_in_queue:
2022-04-08 22:09:01 +02:00
UNLOCK_RETURN(&q->m, 0);
2022-03-31 22:26:44 +02:00
return 0;
}
2022-03-15 00:30:04 +01:00
// numero di elementi della queue
size_t getLen(queueT *q) {
if(!q) {
errno = EINVAL;
return -1;
}
size_t len = -1;
LOCK_RETURN(&q->m, -1); // begin me
2022-04-08 22:09:01 +02:00
len = q->len;
2022-03-15 00:30:04 +01:00
UNLOCK_RETURN(&q->m, -1); // end me
return len;
}
// dimensione in byte
size_t getSize(queueT *q) {
if(!q) {
errno = EINVAL;
return -1;
}
size_t size = -1;
LOCK_RETURN(&q->m, -1); // begin me
size = q->size;
UNLOCK_RETURN(&q->m, -1); // end me
return size;
}
void destroyQueue(queueT *q) {
if(!q)
return;
// no need for lock over queue
while (q->len > 0) {
errno = 0;
2022-04-08 22:09:01 +02:00
voidDequeue(q);
2022-03-15 00:30:04 +01:00
if (errno) {
perror("voiDequeue");
}
}
2022-04-08 22:09:01 +02:00
pthread_mutex_destroy(&q->m);
2022-03-15 00:30:04 +01:00
2022-04-08 22:09:01 +02:00
free(q);
2022-03-15 00:30:04 +01:00
}