fixing more bugs

This commit is contained in:
elvis
2022-04-08 22:09:01 +02:00
parent 9f4d092af5
commit a87fd9c6fd

View File

@ -239,7 +239,7 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s) {
int purged = 0; int purged = 0;
while(q->size + s > q->maxSize) { while(q->size + s > q->maxSize) {
purged++; purged++;
returnList = realloc(purged, sizeof(fileT*)); returnList = realloc(returnList, purged * sizeof(fileT*));
if(!returnList) { if(!returnList) {
perror("realloc"); perror("realloc");
goto _end_dequeueN; 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 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 returnList[purged] = NULL; // null terminated
tmp->data->size += size; tmp->data->size += s;
q->size += size; q->size += s;
UNLOCK_RETURN(&q->m, NULL); // end me UNLOCK_RETURN(&q->m, NULL); // end me
return data; return returnList;
_end_dequeueN: _end_dequeueN:
UNLOCK_RETURN(&q->m, NULL); UNLOCK_RETURN(&q->m, NULL);
@ -293,7 +293,7 @@ void voidDequeue(queueT *q) {
} }
--q->len; --q->len;
q->size -= data->size; q->size -= (tmp->data)->size;
destroyFile(tmp->data); // free fileT destroyFile(tmp->data); // free fileT
free(tmp); // free nodo free(tmp); // free nodo
@ -322,19 +322,15 @@ int printQueue(FILE *stream, queueT *q) {
fprintf(stream, "Lista file:"); fprintf(stream, "Lista file:");
fprintf(stream, "[Nome File] -> Dimensione in MB"); 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)->size)/1000000; // in MB
// float res = ((float)(tmp->data)->valid)/1000000; // in MB // float res = ((float)(tmp->data)->valid)/1000000; // in MB
fprintf(stream, "[%s]\t-> %f MB\n", (tmp->data)->filepath, res); fprintf(stream, "[%s]\t-> %f MB\n", (tmp->data)->filepath, res);
temp = temp->next; tmp = tmp->next;
} }
UNLOCK_RETURN(&q->m, -1); // end me UNLOCK_RETURN(&q->m, -1); // end me
return 0; return 0;
_end_print_queue:
UNLOCK_RETURN(&q->m, -1);
return -1;
} }
// acquisizione lock // acquisizione lock
@ -372,8 +368,8 @@ int lockFileInQueue(queueT *q, char *filepath, int owner) {
} }
// acquisisco lock sul file // acquisisco lock sul file
(temp->data)->O_LOCK = 1; (tmp->data)->O_LOCK = 1;
(temp->data)->owner = owner; (tmp->data)->owner = owner;
UNLOCK_RETURN(&q->m, -1); // end me UNLOCK_RETURN(&q->m, -1); // end me
return 0; return 0;
@ -409,7 +405,7 @@ int unlockFileInQueue(queueT *q, char *filepath, int owner) {
if(!tmp) { // non trovato if(!tmp) { // non trovato
errno = ENOENT; errno = ENOENT;
goto _end_lock_file_queue; goto _end_unlock_file_queue;
} }
if((tmp->data)->O_LOCK == 0) { // nessuno ha il lock 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; (tmp->data)->O_LOCK = (O_LOCK==0)?0:1;
if(O_LOCK!=0) { if(O_LOCK!=0) {
(tmp->data)->owner = client; (tmp->data)->owner = owner;
} }
UNLOCK_RETURN(&q->m, -1); // end me 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 UNLOCK_RETURN(&q->m, -1); // end me
return 0; return 0;
_end_open_file_queue: _end_close_file_queue:
UNLOCK_RETURN(&q->m, -1); UNLOCK_RETURN(&q->m, -1);
return -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; 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 // memmove sarebbe un'alternativa
(tmp->data)->valid += size; (tmp->data)->valid += size;
q->size -= (tmp->data)->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 if (strcmp(filepath, (tmp->data)->filepath) == 0) { // trovato
break; break;
} }
if(prc!=tmp) if(pre!=tmp)
prc = prc->next; pre = pre->next;
tmp = tmp->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 ((tmp->data)->open != 0 || ((tmp->data)->O_LOCK && (tmp->data)->owner != owner)) {
// if file è aperto o la lock non è del owner // if file è aperto o la lock non è del owner
errno = EPERM; 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; q->head = tmp->next;
if (tmp->next == NULL) { if (tmp->next == NULL) {
q->tail = tmp; q->tail = tmp;
} }
} else { // file non è il primo } else { // file non è il primo
prc->next = tmp->next; pre->next = tmp->next;
if (prc->next == NULL) { if (pre->next == NULL) {
q->tail = prc; q->tail = pre;
} }
} }
@ -766,10 +762,10 @@ _end_find_in_queue:
int searchFile(queueT *q, char *filepath) { int searchFile(queueT *q, char *filepath) {
if(!q || !filepath) { if(!q || !filepath) {
errno = EINVAL; errno = EINVAL;
return NULL; return 0;
} }
LOCK_RETURN(&q->m, NULL); // begin me LOCK_RETURN(&q->m, 0); // begin me
if (q->len == 0) if (q->len == 0)
goto _end_search_file_in_queue; goto _end_search_file_in_queue;
@ -786,11 +782,11 @@ int searchFile(queueT *q, char *filepath) {
if(!tmp) if(!tmp)
goto _end_search_file_in_queue; goto _end_search_file_in_queue;
UNLOCK_RETURN(&q->m, NULL); // end me UNLOCK_RETURN(&q->m, 0); // end me
return 1; return 1;
_end_search_file_in_queue: _end_search_file_in_queue:
UNLOCK_RETURN(&q->m, NULL); UNLOCK_RETURN(&q->m, 0);
return 0; return 0;
} }
@ -804,7 +800,7 @@ size_t getLen(queueT *q) {
LOCK_RETURN(&q->m, -1); // begin me LOCK_RETURN(&q->m, -1); // begin me
len = queue->len; len = q->len;
UNLOCK_RETURN(&q->m, -1); // end me UNLOCK_RETURN(&q->m, -1); // end me
return len; return len;
@ -833,15 +829,13 @@ void destroyQueue(queueT *q) {
// no need for lock over queue // no need for lock over queue
while (q->len > 0) { while (q->len > 0) {
errno = 0; errno = 0;
voiDequeue(q); voidDequeue(q);
if (errno) { if (errno) {
perror("voiDequeue"); perror("voiDequeue");
} }
} }
if (&queue->m) {
pthread_mutex_destroy(&q->m); pthread_mutex_destroy(&q->m);
}
free(queue); free(q);
} }