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;
}