From a87fd9c6fd6e5cf0a33299a5c626b0348ab4c579 Mon Sep 17 00:00:00 2001 From: elvis Date: Fri, 8 Apr 2022 22:09:01 +0200 Subject: [PATCH] fixing more bugs --- lib/threadpool/fileQueue.c | 64 +++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/lib/threadpool/fileQueue.c b/lib/threadpool/fileQueue.c index 1b17630..17f454f 100644 --- a/lib/threadpool/fileQueue.c +++ b/lib/threadpool/fileQueue.c @@ -239,7 +239,7 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s) { int purged = 0; while(q->size + s > q->maxSize) { purged++; - returnList = realloc(purged, sizeof(fileT*)); + returnList = realloc(returnList, purged * sizeof(fileT*)); if(!returnList) { perror("realloc"); goto _end_dequeueN; @@ -256,14 +256,14 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s) { break; // we eliminated everything so we must have enought space } } - returnList = realloc(purged+1, sizeof(fileT*)); + returnList = realloc(returnList, purged+1 * sizeof(fileT*)); returnList[purged] = NULL; // null terminated - tmp->data->size += size; - q->size += size; + tmp->data->size += s; + q->size += s; UNLOCK_RETURN(&q->m, NULL); // end me - return data; + return returnList; _end_dequeueN: UNLOCK_RETURN(&q->m, NULL); @@ -293,7 +293,7 @@ void voidDequeue(queueT *q) { } --q->len; - q->size -= data->size; + q->size -= (tmp->data)->size; destroyFile(tmp->data); // free fileT free(tmp); // free nodo @@ -322,19 +322,15 @@ int printQueue(FILE *stream, queueT *q) { fprintf(stream, "Lista file:"); fprintf(stream, "[Nome File] -> Dimensione in MB"); - while (temp!=NULL) { + while (tmp!=NULL) { float res = ((float)(tmp->data)->size)/1000000; // in MB // float res = ((float)(tmp->data)->valid)/1000000; // in MB fprintf(stream, "[%s]\t-> %f MB\n", (tmp->data)->filepath, res); - temp = temp->next; + tmp = tmp->next; } UNLOCK_RETURN(&q->m, -1); // end me return 0; - -_end_print_queue: - UNLOCK_RETURN(&q->m, -1); - return -1; } // acquisizione lock @@ -372,8 +368,8 @@ int lockFileInQueue(queueT *q, char *filepath, int owner) { } // acquisisco lock sul file - (temp->data)->O_LOCK = 1; - (temp->data)->owner = owner; + (tmp->data)->O_LOCK = 1; + (tmp->data)->owner = owner; UNLOCK_RETURN(&q->m, -1); // end me return 0; @@ -409,7 +405,7 @@ int unlockFileInQueue(queueT *q, char *filepath, int owner) { if(!tmp) { // non trovato errno = ENOENT; - goto _end_lock_file_queue; + goto _end_unlock_file_queue; } if((tmp->data)->O_LOCK == 0) { // nessuno ha il lock @@ -470,7 +466,7 @@ int openFileInQueue(queueT *q, char *filepath, int O_LOCK, int owner) { (tmp->data)->O_LOCK = (O_LOCK==0)?0:1; if(O_LOCK!=0) { - (tmp->data)->owner = client; + (tmp->data)->owner = owner; } UNLOCK_RETURN(&q->m, -1); // end me @@ -521,7 +517,7 @@ int closeFileInQueue(queueT *q, char *filepath, int owner) { UNLOCK_RETURN(&q->m, -1); // end me return 0; -_end_open_file_queue: +_end_close_file_queue: UNLOCK_RETURN(&q->m, -1); return -1; } @@ -634,7 +630,7 @@ int appendFileInQueue(queueT *q, char *filepath, void *data, size_t size, int ow goto _end_append_file_queue; } - memcpy(((tmp->data)->data) + (tmp->data)->valid, data, size); + memcpy(((char *)(tmp->data)->data) + (tmp->data)->valid, data, size); // memmove sarebbe un'alternativa (tmp->data)->valid += size; q->size -= (tmp->data)->size; @@ -670,8 +666,8 @@ int removeFileFromQueue(queueT *q, char *filepath, int owner) { if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato break; } - if(prc!=tmp) - prc = prc->next; + if(pre!=tmp) + pre = pre->next; tmp = tmp->next; } @@ -683,20 +679,20 @@ int removeFileFromQueue(queueT *q, char *filepath, int owner) { if ((tmp->data)->open != 0 || ((tmp->data)->O_LOCK && (tmp->data)->owner != owner)) { // if file è aperto o la lock non è del owner errno = EPERM; - goto _end_append_file_queue; + goto _end_remove_file_queue; } - if (tmp == prc) { // file è il primo + if (tmp == pre) { // file è il primo q->head = tmp->next; if (tmp->next == NULL) { q->tail = tmp; } } else { // file non è il primo - prc->next = tmp->next; + pre->next = tmp->next; - if (prc->next == NULL) { - q->tail = prc; + if (pre->next == NULL) { + q->tail = pre; } } @@ -766,10 +762,10 @@ _end_find_in_queue: int searchFile(queueT *q, char *filepath) { if(!q || !filepath) { errno = EINVAL; - return NULL; + return 0; } - LOCK_RETURN(&q->m, NULL); // begin me + LOCK_RETURN(&q->m, 0); // begin me if (q->len == 0) goto _end_search_file_in_queue; @@ -786,11 +782,11 @@ int searchFile(queueT *q, char *filepath) { if(!tmp) goto _end_search_file_in_queue; - UNLOCK_RETURN(&q->m, NULL); // end me + UNLOCK_RETURN(&q->m, 0); // end me return 1; _end_search_file_in_queue: - UNLOCK_RETURN(&q->m, NULL); + UNLOCK_RETURN(&q->m, 0); return 0; } @@ -804,7 +800,7 @@ size_t getLen(queueT *q) { LOCK_RETURN(&q->m, -1); // begin me - len = queue->len; + len = q->len; UNLOCK_RETURN(&q->m, -1); // end me return len; @@ -833,15 +829,13 @@ void destroyQueue(queueT *q) { // no need for lock over queue while (q->len > 0) { errno = 0; - voiDequeue(q); + voidDequeue(q); if (errno) { perror("voiDequeue"); } } - if (&queue->m) { - pthread_mutex_destroy(&q->m); - } + pthread_mutex_destroy(&q->m); - free(queue); + free(q); }