started fixing bugs

This commit is contained in:
elvis
2022-04-08 21:32:52 +02:00
parent 044b0f527a
commit 9f4d092af5
7 changed files with 170 additions and 116 deletions

View File

@ -1,3 +1,5 @@
#include <strings.h>
#include <apiFile.h>
#include <taglialegna.h>
@ -21,6 +23,30 @@
// -----------------------------------------------------------------------------
/* funzioni ausiliarie */
// invio il messaggio al client e poi l'errno
void serror(char *m, long fd_c, taglia_t *taglia, char *mlog) {
if(!m) {
errno = EINVAL;
m = MESY;
}
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
perror("writen");
goto _serror_cleanup;
}
if(writen(fd_c, &errno, sizeof(errno)) < 0) {
perror("writen");
goto _serror_cleanup;
}
if(taglia_write(taglia, mlog) < 0)
goto _serror_cleanup;
return;
_serror_cleanup:
return;
}
// invio il messaggio al client
void sendMessage(char *m, long fd_c, taglia_t *taglia, char *mlog) {
if(!m) {
@ -45,11 +71,13 @@ _sendM_cleanup:
void sendMessageFile(char *m, fileT *f, long fd_c, taglia_t *taglia, char *mlog) {
if(!f) {
errno = EINVAL;
serror(MESY, fd_c);
char errmlog[2048] = "Errore negli argomenti alla funzione sendMessagefile (fileT == NULL). Messaggio originale:\n\t";
strncat(errmlog, mlog, sizeof(errmlog)-strlen(errmlog)-1);
serror(MESY, fd_c, taglia, errmlog);
goto _sendMF_cleanup;
}
if(!m) {
m = MEPF;
m = MEFP;
}
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
@ -72,30 +100,32 @@ _sendMF_cleanup:
}
// invio il messaggio al client e poi n file
void sendMessageFileN(char *m, fileT **f, int n, long fd_c, taglia *taglia, char *mlog) {
void sendMessageFileN(char *m, fileT **f, int n, long fd_c, taglia_t *taglia, char *mlog) {
if(!f) {
errno = EINVAL;
serror(MESY, fd_c);
char errmlog[2048] = "Errore negli argomenti alla funzione sendMessagefile (fileT == NULL). Messaggio originale:\n\t";
strncat(errmlog, mlog, sizeof(errmlog)-strlen(errmlog)-1);
serror(MESY, fd_c, taglia, errmlog);
goto _sendMFN_cleanup;
}
if(!m) {
m = MEPF;
m = MEFP;
}
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
perror("writen");
goto _sendMF_cleanup;
goto _sendMFN_cleanup;
}
for(int i=0; i<n && (f[i]!=NULL); ++i) {
if(sendFile(f[i], fd_c, taglia) < 0) {
perror("sendFile");
goto _sendMF_cleanup;
goto _sendMFN_cleanup;
}
}
if(taglia_write(taglia, mlog) < 0)
goto _sendMF_cleanup;
goto _sendMFN_cleanup;
return;
@ -103,30 +133,6 @@ _sendMFN_cleanup:
return;
}
// invio il messaggio al client e poi l'errno
void serror(char *m, long fd_c, taglia_t *taglia, char *mlog) {
if(!m) {
errno = EINVAL;
m = MESY;
}
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
perror("writen");
goto _serror_cleanup;
}
if(writen(fd_c, &errno, sizeof(errno)) < 0) {
perror("writen");
goto _serror_cleanup;
}
if(taglia_write(taglia, mlog) < 0)
goto _serror_cleanup;
return;
_serror_cleanup:
return;
}
// -----------------------------------------------------------------------------
void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) {
@ -162,7 +168,7 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
}
if(found && !create) {
if(openFileInQueue(q, file, lock, fd_c) == -1) {
if(openFileInQueue(q, filepath, lock, fd_c) == -1) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore\n", fd_c, flags, filepath);
perror("openFileInQueue");
serror(MESE, fd_c, taglia, tmp_buf);
@ -174,7 +180,7 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
}
if(!found && create) { // not found and creating new file
if(getLen(q) == queue->maxLen) { // capacity miss
if(getLen(q) == q->maxLen) { // capacity miss
removed = dequeue(q);
if(!removed) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore\n", fd_c, flags, filepath);
@ -188,14 +194,14 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
if(!f) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath);
perror("createFileT");
serror(MESE, fd_c, taglia);
serror(MESE, fd_c, taglia, tmp_buf);
return;
}
if(enqueue(q, f) != 0) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath);
perror("enqueue");
serror(MESE, fd_c, taglia);
serror(MESE, fd_c, taglia, tmp_buf);
return;
}
@ -211,14 +217,14 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
if(!f) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath);
perror("createFileT");
serror(MESE, fd_c, taglia);
serror(MESE, fd_c, taglia, tmp_buf);
return;
}
if(enqueue(q, f) != 0) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con errore del server\n", fd_c, flags, filepath);
perror("enqueue");
serror(MESE, fd_c, taglia);
serror(MESE, fd_c, taglia, tmp_buf);
return;
}
// abbiamo aggiunto un file quindi il numero di file è cambiato
@ -238,7 +244,7 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
size_t m = sizeof(tmp_buf);
if(!filepath || !q || !taglia) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile (flags = %x) sul file \"%s\" e' terminata con errore\n", fd_c, flags, filepath);
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath);
errno = EINVAL;
serror(MESY, fd_c, taglia, tmp_buf);
return;
@ -247,21 +253,21 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
fileT *f = NULL;
f = find(q, filepath);
if(!f) {
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile (flags = %x) sul file \"%s\" e' terminata con errore\n", fd_c, flags, filepath);
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath);
errno = ENOENT;
serror(MESE, fd_c, taglia, tmp_buf);
return;
}
if(f->open != 0) { // file already open
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile (flags = %x) sul file \"%s\" e' terminata con errore\n", fd_c, flags, filepath);
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath);
errno = EPERM;
serror(MENT, fd_c, taglia, tmp_buf);
destroyFile(f); // f is a copy so we need to cleen up
return;
}
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile (flags = %x) sul file \"%s\" e' terminata con successo\n", fd_c, flags, filepath);
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con successo\n", fd_c, filepath);
sendMessageFile(MEOK, f, fd_c, taglia, tmp_buf);
destroyFile(f); // f is a copy so we need to cleen up
return;
@ -355,19 +361,19 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl
if(append) {
if(appendFileInQueue(q, filepath, content, size, fd_c) == -1) {
perror("appendFileInQueue");
free(content)
free(content);
return;
}
} else {
if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) {
perror("writeFileInQueue");
free(content)
free(content);
return;
}
}
taglia_update(taglia, q, 0);
free(content)
free(content);
return;
}
@ -388,62 +394,62 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl
if(append) {
if(appendFileInQueue(q, filepath, content, size, fd_c) == -1) {
perror("appendFileInQueue");
free(content)
free(content);
return;
}
} else {
if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) {
perror("writeFileInQueue");
free(content)
free(content);
return;
}
}
taglia_write(taglia, mlog);
taglia_write(taglia, tmp_buf);
taglia_update(taglia, q, 0);
free(content)
free(content);
return;
}
void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) {
void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) {
return;
}
void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) {
void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) {
return;
}
void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) {
void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) {
return;
}
void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waitingT **waiting) {
void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) {
return;
}
int sendFile(fileT *f, long fd_c, taglia_t *taglia) {
return;
return 0;
}
int addWaiting(waitingT **waiting, char *file, int fd) {
return;
int addWaiting(waiting_t **waiting, char *file, int fd) {
return 0;
}
int removeFirstWaiting(waitingT **waiting, char *file) {
return;
int removeFirstWaiting(waiting_t **waiting, char *file) {
return 0;
}
void clearWaiting(waitingT **waiting) {
void clearWaiting(waiting_t **waiting) {
return;
}

View File

@ -1,7 +1,7 @@
#include <fileQueue.h>
#include <util.h>
#define NAMELEN 256
#define MAXNAMELEN 256
// creazione di un fileT
fileT* createFileT(char *f, int O_LOCK, int client, int open){
@ -12,30 +12,31 @@ fileT* createFileT(char *f, int O_LOCK, int client, int open){
fileT *file = malloc(sizeof(fileT));
if (f == NULL) {
if (file == NULL) {
perror("Malloc createFileT");
return NULL;
}
f->O_LOCK = (O_LOCK == 0)? 0 : 1;
f->owner = client;
f->open = (open == 0)? 0 : 1;
f->size = 0;
f->valid = 0;
file->O_LOCK = (O_LOCK == 0)? 0 : 1;
file->owner = client;
file->open = (open == 0)? 0 : 1;
file->size = 0;
file->valid = 0;
if ((f->filepath = malloc(sizeof(char)*NAMELEN)) == NULL) {
if ((file->filepath = malloc(sizeof(char)*MAXNAMELEN)) == NULL) {
perror("Malloc filepath");
destroyFile(f);
destroyFile(file);
return NULL;
}
strncpy(f->filepath, f, NAMELEN);
strncpy(file->filepath, f, MAXNAMELEN);
if ((f->data = malloc(1)) == NULL) { // così semplicemente facciamo realloc
// in seguito semplicemente facciamo realloc
if ((file->data = malloc(1)) == NULL) {
perror("Malloc content");
return NULL;
}
return f;
return file;
}
// append su fileT
@ -53,6 +54,7 @@ int writeFileT(fileT *f, void *data, size_t size) {
memcpy((char*) f->data + f->valid, data, size);
f->valid = f->valid + size;
f->size = (f->valid>f->size)?f->valid:f->size;
return 0;
}
// destroy fileT
@ -126,15 +128,15 @@ int enqueue(queueT *q, fileT* data) {
newNode->data = data;
newNode->next = NULL;
nodeT *tmp = queue->head;
nodeT *tmp = q->head;
if (q->head == NULL)
q->head = newNode;
else {
while (tmp->next)
tmp = temp->next;
tmp = tmp->next;
temp->next = newNode;
tmp->next = newNode;
}
q->tail = newNode;
@ -228,14 +230,8 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s) {
goto _end_dequeueN;
}
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_dequeueN;
}
fileT **returnList = NULL; // lista dei file rimossi
nodeT *tmp = NULL;
tmp = NULL;
returnList = calloc(1, sizeof(fileT*));
returnList[0] = NULL;

View File

@ -2,6 +2,10 @@
#ifndef _FILE_QUEUE
#define _FILE_QUEUE
#include <stdio.h>
#include <pthread.h>
#include <conn.h>
// struttura dati per gestire i file in memoria principale
typedef struct {