From f547da3d733908a5d42254c46c6ad3f44a42e51a Mon Sep 17 00:00:00 2001 From: elvis Date: Sat, 7 May 2022 21:16:35 +0200 Subject: [PATCH] style for files taglialegna.* apiFile.* --- lib/log/taglialegna.c | 57 ++-- lib/log/taglialegna.h | 65 +++-- lib/threadpool/apiFile.c | 543 +++++++++++++++++++++++++-------------- lib/threadpool/apiFile.h | 145 +++++++++-- 4 files changed, 551 insertions(+), 259 deletions(-) diff --git a/lib/log/taglialegna.c b/lib/log/taglialegna.c index 8788b81..9b4de2b 100644 --- a/lib/log/taglialegna.c +++ b/lib/log/taglialegna.c @@ -7,8 +7,8 @@ #include #include -#include -#include +#include "taglialegna.h" +#include "fileQueue.h" taglia_t *taglia_init(char *file, int n, ...) { int max_files; @@ -17,19 +17,19 @@ taglia_t *taglia_init(char *file, int n, ...) { taglia_t *new = calloc(1, sizeof(taglia_t)); if(new == NULL) { - perror("calloc"); + perror("taglia_init: calloc"); return NULL; } va_list ap; - va_start(ap, n); // nota: si ha sempre default argument promotion, - // ma noi leggiamo solo int quindi ignoriamo il problema + va_start(ap, n); /* note: by default we have argument promotion, + * but we read only int so thats not a problem */ max_files = (n<1)?0:va_arg(ap, int); max_size = (n<2)?0:va_arg(ap, int); cache_misses = (n<3)?0:va_arg(ap, int); va_end(ap); if ( (new->file = fopen(file, "w")) == NULL ) { - perror("fopen logfile"); + perror("taglia_init: fopen"); goto _error_init; } @@ -38,7 +38,7 @@ taglia_t *taglia_init(char *file, int n, ...) { new->cache_misses = cache_misses; if (pthread_mutex_init(&new->m, NULL) != 0) { - perror("mutex init"); + perror("taglia_init: pthread_mutex_init"); fclose(new->file); goto _error_init; } @@ -64,7 +64,7 @@ size_t taglia_write(taglia_t *taglia, char *buf) { size_t n = 0; pthread_mutex_lock(&taglia->m); if ((n = fwrite(buf, sizeof(char), strlen(buf), taglia->file)) < 0) { - perror("fwrite"); + perror("taglia_write: fwrite"); goto _error_write; } pthread_mutex_unlock(&taglia->m); @@ -83,8 +83,8 @@ size_t taglia_log(taglia_t *taglia, char *buf) { } time_t now = time(NULL); - if(now == (time_t)(-1)) { // errore a ottenere l'ora - perror("ERROR: time"); + if(now == (time_t)(-1)) { + perror("taglia_write: time"); goto _error_taglia_log; } struct tm tmp_buf; @@ -92,26 +92,28 @@ size_t taglia_log(taglia_t *taglia, char *buf) { now_tm = gmtime_r(&now, &tmp_buf); if(!now_tm){ - perror("ERROR: gmtime_r"); + perror("taglia_write: gmtime_r"); goto _error_taglia_log; } - char buff[15]; // 13 only should strictly needed + char buff[15]; /* 13 only should be strictly needed */ if(!strftime(buff, sizeof(buff), "[%T] ", now_tm)) { - perror("ERROR: strftime"); + perror("taglia_write: strftime"); goto _error_taglia_log; } size_t n, m; pthread_mutex_lock(&taglia->m); - if((n = fwrite(buff, sizeof(char), strnlen(buff, sizeof(buff)), taglia->file)) < 0){ - perror("ERROR: fwrite"); + n = fwrite(buff, sizeof(char), strnlen(buff, sizeof(buff)), taglia->file); + if(n < 0){ + perror("taglia_write: fwrite"); goto _error_taglia_log_mutex; } - if((m = fwrite(buf, sizeof(char), strlen(buf), taglia->file)) < 0){ - perror("ERROR: fwrite"); + m = fwrite(buf, sizeof(char), strlen(buf), taglia->file); + if(m < 0){ + perror("taglia_write: fwrite"); goto _error_taglia_log_mutex; } pthread_mutex_unlock(&taglia->m); @@ -156,21 +158,27 @@ int taglia_stats(taglia_t *taglia, FILE *stream) { double res = ((double) taglia->max_size)/((double) 1000000); - fprintf(stream, "Numero di file massimo memorizzato nel server: %zu\n", taglia->max_files); - // fprintf(stream, "Dimensione massima in Mbytes raggiunta dal file storage: %.2lf MB\n", res); - fprintf(stream, "Dimensione massima in Mbytes raggiunta dal file storage: %lf MB\n", res); - fprintf(stream, "Numero di volte in cui l’algoritmo di rimpiazzamento della cache è stato eseguito per selezionare uno o più file \"vittima\": %zu\n", taglia->cache_misses); + fprintf(stream, "Numero di file massimo memorizzato nel server: %zu\n", + taglia->max_files); + /* fprintf(stream, "Dimensione massima in Mbytes raggiunta dal file storage: %.2lf MB\n", res); */ + fprintf(stream, "Dimensione massima in Mbytes raggiunta dal file storage: %lf MB\n", + res); + fprintf(stream, "Numero di volte in cui l’algoritmo di rimpiazzamento della cache è stato eseguito per selezionare uno o più file \"vittima\": %zu\n", + taglia->cache_misses); fflush(stream); char tmp_buf[2048]; int n = 0; size_t m = sizeof(tmp_buf); - n += snprintf(tmp_buf+n, m-n, "Numero di file massimo memorizzato nel server: %zu\n", taglia->max_files); + n += snprintf(tmp_buf+n, m-n, "Numero di file massimo memorizzato nel server: %zu\n", + taglia->max_files); if(mcache_misses); + n += snprintf(tmp_buf+n, m-n, "Numero di volte in cui l’algoritmo di rimpiazzamento della cache è stato eseguito per selezionare uno o più file \"vittima\": %zu\n", + taglia->cache_misses); if(mm); @@ -182,5 +190,6 @@ int taglia_stats(taglia_t *taglia, FILE *stream) { _error_taglia_stats: pthread_mutex_unlock(&taglia->m); + perror("taglia_stats: snprintf"); return -1; } diff --git a/lib/log/taglialegna.h b/lib/log/taglialegna.h index ecd6fc6..4cd331c 100644 --- a/lib/log/taglialegna.h +++ b/lib/log/taglialegna.h @@ -5,39 +5,74 @@ #include #include -#include +#include "fileQueue.h" typedef struct taglia_s { FILE *file; size_t max_files; long max_size; size_t cache_misses; - pthread_mutex_t m; // per sincronizzare le scritture al file + pthread_mutex_t m; /* per sincronizzare le scritture al file */ } taglia_t; -// crea una nuova istanza -// \param file: file su cui scrivere i dati -// \param n: numero di parametri opzionali -// parametri opzionali in questo ordine: -// max_files, max_size, cache_misses -// \return istanza creata, NULL se errore + +/** + * @brief Creates new instance + * + * @param file path to file to write into + * @param n number of optional parameters, in order: + * int max_files, int max_size, int cache_misses + * @return created istance, NULL if error + */ taglia_t *taglia_init(char *file, int n, ...); -// elimina un'istanza deallocando e chiudendo le rispettive risorse + +/** + * @brief Deallocates instance + * + * @param taglia instance to deallocate + */ void taglia_del(taglia_t *taglia); -// scrive quello contenuto dentro a buf sul file -// \return numero di char scritti, -1 se errore + +/** + * @brief Writes buffer to the log + * + * @param taglia + * @param buf NULL terminated string to write + * @return number of char written, -1 if error occurred + */ size_t taglia_write(taglia_t *taglia, char *buf); -// scrive il contenuto di buf sul file con ora associata + +/** + * @brief Writes buffer to the log with timestamp + * + * @param taglia + * @param buf NULL terminated string to write + * @return number of char written, -1 if error occurred + */ size_t taglia_log(taglia_t *taglia, char *buf); -// update delle statistiche -// \return 0 successo, -1 errore + +/** + * @brief Updates statistics + * + * @param taglia + * @param queue structure that holds the files + * @param miss number of cache misses to add + * @return 1 if success, -1 if error occurred + */ int taglia_update(taglia_t *taglia, queueT *queue, int miss); -// print delle statistiche attuali su stream + +/** + * @brief Writes stats to the stream and to the log + * + * @param taglia + * @param stream + * @return 0 if success, -1 if error occurred + */ int taglia_stats(taglia_t *taglia, FILE *stream); #endif /* _TAGLIALEGNA */ diff --git a/lib/threadpool/apiFile.c b/lib/threadpool/apiFile.c index 8df0450..b79d9ac 100644 --- a/lib/threadpool/apiFile.c +++ b/lib/threadpool/apiFile.c @@ -3,44 +3,44 @@ #include #include -#include -#include -#include -#include +#include "conn.h" +#include "apiFile.h" +#include "fileQueue.h" +#include "taglialegna.h" #define MAXLENMESS 512 #define LOGBUFSIZE 2048 -#define MEOK "20" //OK -// #define ME "21" // not used -// #define ME "22" // not used -#define MEFP "25" // file purged +#define MEOK "20" /* OK */ +// #define ME "21" /* not used */ +// #define ME "22" /* not used */ +#define MEFP "25" /* file purged */ -// #define ME "40" // not used -// #define ME "41" // not used -#define MESD "42" // shutting down -#define MENT "45" // requested file action not taken +// #define ME "40" /* not used */ +// #define ME "41" /* not used */ +#define MESD "42" /* shutting down */ +#define MENT "45" /* requested file action not taken */ -#define MESY "50" // syntax error -// #define ME "51" // not used -// #define ME "52" // not used -#define MESE "55" // server error +#define MESY "50" /* syntax error */ +// #define ME "51" /* not used */ +// #define ME "52" /* not used */ +#define MESE "55" /* server error */ // ----------------------------------------------------------------------------- -/* funzioni ausiliarie */ +// helper functions -// invio il messaggio al client e poi l'errno +/* send to the client the message m and then 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)) < 0) { - perror("writen"); + perror("serror: writen"); goto _serror_cleanup; } if(writen(fd_c, &errno, sizeof(errno)) < 0) { - perror("writen"); + perror("serror: writen"); goto _serror_cleanup; } @@ -53,14 +53,14 @@ _serror_cleanup: return; } -// invio il messaggio al client +/* send to the client the message m */ void sendMessage(char *m, long fd_c, taglia_t *taglia, char *mlog) { if(!m) { m = MEOK; } if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) { - perror("writen"); + perror("sendMessage: writen"); goto _sendM_cleanup; } @@ -73,32 +73,32 @@ _sendM_cleanup: return; } -// invia un file +/* send to the client the file f */ int sendFile(fileT *f, long fd_c, taglia_t *taglia) { if(!f || !taglia) { errno = EINVAL; return -1; } - // send filepath dimension, then filepath + /* send filepath dimension, then filepath */ int64_t filepathLength = (int64_t) strnlen(f->filepath, MAXLENMESS)+1; if (writen(fd_c, &filepathLength, sizeof(filepathLength)) < 0) { - perror("writen"); + perror("sendFile: writen"); return -1; } if (writen(fd_c, f->filepath, (size_t) filepathLength) < 0) { - perror("writen"); + perror("sendFile: writen"); return -1; } - // send file dimension, then file + /* send file dimension, then file */ int64_t validLength = (int64_t) f->valid; if (writen(fd_c, &validLength, sizeof(validLength)) < 0) { - perror("writen"); + perror("sendFile: writen"); return -1; } - if(validLength != 0) { // if file has length 0 dont write + if(validLength != 0) { /* if file has length 0 dont write */ if (writen(fd_c, f->data, validLength) < 0) { - perror("writen"); + perror("sendFile: writen"); return -1; } } @@ -107,53 +107,58 @@ int sendFile(fileT *f, long fd_c, taglia_t *taglia) { int n = 0; size_t m = sizeof(tmp_log); - n += snprintf(tmp_log+n, m-n, "Inviato file \"%s\", di dimensione %"PRId64" Bytes al client %ld .\n", f->filepath, validLength, fd_c); + n += snprintf(tmp_log+n, m-n, "Inviato file \"%s\", di dimensione %"PRId64" Bytes al client %ld .\n", + f->filepath, + validLength, + fd_c); if(taglia_write(taglia, tmp_log) < 0) { - perror("taglia_write"); + perror("sendFile: taglia_write"); return 1; } return 0; } -// invio il messaggio al client e poi il file +/* send to the client the message m then the file f */ void sendMessageFile(char *m, fileT *f, long fd_c, taglia_t *taglia, char *mlog) { if(!f) { errno = EINVAL; - char errmlog[2*LOGBUFSIZE] = "Errore negli argomenti alla funzione sendMessagefile (fileT == NULL). Messaggio originale:\n\t"; + char errmlog[2*LOGBUFSIZE] = + "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; + serror(MESY, fd_c, taglia, errmlog); } if(!m) { m = MEFP; } if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) { - perror("writen"); + perror("sendMessageFile: writen"); goto _sendMF_cleanup; } int64_t *n = calloc(1, sizeof(*n)); if(!n) { - perror("calloc"); + perror("sendMessageFile: calloc"); goto _sendMF_cleanup; } *n = 1; if(writen(fd_c, n, sizeof(int64_t)) < 0) { - perror("writen"); + perror("sendMessageFile: writen"); goto _sendMF_cleanup; } free(n); if(sendFile(f, fd_c, taglia) < 0) { - perror("sendFile"); + perror("sendMessageFile: sendFile"); goto _sendMF_cleanup; } if(taglia_write(taglia, mlog) < 0) { - perror("log write"); + perror("sendMessageFile: taglia_write"); goto _sendMF_cleanup; } @@ -163,11 +168,13 @@ _sendMF_cleanup: return; } -// invio il messaggio al client e poi n file +/* send to the client the message m then n files */ void sendMessageFileN(char *m, fileT **f, int64_t n, long fd_c, taglia_t *taglia, char *mlog) { if(!f) { errno = EINVAL; - char errmlog[2*LOGBUFSIZE] = "Errore negli argomenti alla funzione sendMessagefile (fileT == NULL). Messaggio originale:\n\t"; + char errmlog[2*LOGBUFSIZE] = + "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; @@ -177,18 +184,18 @@ void sendMessageFileN(char *m, fileT **f, int64_t n, long fd_c, taglia_t *taglia } if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) { - perror("writen"); + perror("sendMessageFileN: writen"); goto _sendMFN_cleanup; } if(write(fd_c, &n, sizeof(n)) < 0) { - perror("writen"); + perror("sendMessageFileN: writen"); goto _sendMFN_cleanup; } for(int i=0; i>1%2 - fileT *removed = NULL; // file che è stato rimosso + int found = searchFile(q, filepath); /* search the file in the queue */ + int create = (flags & C_CREATE)?1:0; /* also %2 */ + int lock = (flags & C_LOCK)?1:0; /* also >>1%2 */ + fileT *removed = NULL; /* file that was removed */ - if(found && create) { // si vuole creare il file ma esiste già - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" ma il file esiste già\n", fd_c, flags, filepath); + if(found && create) { /* file found but want to create new file */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" ma il file esiste già\n", + fd_c, + flags, + filepath); errno = EEXIST; serror(MENT, fd_c, taglia, tmp_buf); return; } - if(!found && !create) { // si vuole aprire e non creare un file inesistente - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" ma il file non esiste\n", fd_c, flags, filepath); + if(!found && !create) { /* file not found but expected */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" ma il file non esiste\n", + fd_c, + flags, + filepath); errno = ENOENT; serror(MENT, fd_c, taglia, tmp_buf); return; } - if(found && !create) { + if(found && !create) { /* file found */ 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 del server\n", fd_c, flags, filepath); - perror("openFileInQueue"); + 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("openFile: openFileInQueue"); serror(MESE, fd_c, taglia, tmp_buf); return; } - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (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 openFile (flags = %x) sul file \"%s\" e' terminata con successo\n", + fd_c, + flags, + filepath); sendMessage(MEOK, fd_c, taglia, tmp_buf); return; } - if(!found && create) { // not found and creating new file - if(getLen(q) == q->maxLen) { // capacity miss + if(!found && create) { /* not found and creating new file */ + 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); - perror("dequeue"); + 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("openFile: dequeue"); serror(MESE, fd_c, taglia, tmp_buf); return; } - fileT *f = createFileT(filepath, lock, fd_c, 1); // create new file + /* create new file */ + fileT *f = createFileT(filepath, lock, fd_c, 1); 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"); + 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("openFile: createFileT"); 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"); + 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("openFile: enqueue"); serror(MESE, fd_c, taglia, tmp_buf); return; } - taglia_update(taglia, q, 1); // removed only one file - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" ha causato una capacity miss. File espulso \"%s\"\n", fd_c, flags, filepath, removed->filepath); + taglia_update(taglia, q, 1); /* removed only one file */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" ha causato una capacity miss. File espulso \"%s\"\n", + fd_c, + flags, + filepath, + removed->filepath); sendMessageFile(MEFP, removed, fd_c, taglia, tmp_buf); destroyFile(removed); return; } + /* no capacity miss */ fileT *f = createFileT(filepath, lock, fd_c, 1); 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"); + 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("openFile: createFileT"); 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"); + 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("openFile: enqueue"); serror(MESE, fd_c, taglia, tmp_buf); return; } - // abbiamo aggiunto un file quindi il numero di file è cambiato - // quindi bisogna fare un update del log + /* added a file, the number of files changed so update the log */ taglia_update(taglia, q, 0); - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con successo\n", fd_c, flags, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" e' terminato con successo\n", + fd_c, + flags, + filepath); sendMessage(MEOK, fd_c, taglia, tmp_buf); return; } - // codice non raggiungibile + /* unreachable code */ return; } void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) { - // messaggio da scrivere sul logfile + /* message to write to the logfile */ char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); if(!filepath || !q || !taglia) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore del server\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore del server\n", + fd_c, + filepath); errno = EINVAL; serror(MESY, fd_c, taglia, tmp_buf); return; @@ -325,35 +372,44 @@ 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 sul file \"%s\" ma il file non esiste\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" ma il file non esiste\n", + fd_c, + filepath); errno = ENOENT; serror(MESE, fd_c, taglia, tmp_buf); return; } - if(f->open == 0) { // file not already open - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore, file non aperto\n", fd_c, filepath); + if(f->open == 0) { /* file not already open */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore, file non aperto\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 + 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 sul file \"%s\" di dimensione = %ld e' terminata con successo\n", fd_c, filepath, f->valid); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" di dimensione = %ld e' terminata con successo\n", + fd_c, + filepath, + f->valid); sendMessageFile(MEOK, f, fd_c, taglia, tmp_buf); - destroyFile(f); // f is a copy so we need to cleen up + destroyFile(f); /* f is a copy so we need to cleen up */ return; } -void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia){ - // messaggio da scrivere sul logfile +void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia) { + /* message to write to the logfile */ char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); if(!q || !taglia) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFile (n = %d) e' terminata con errore\n", fd_c, num); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFile (n = %d) e' terminata con errore\n", + fd_c, + num); errno = EINVAL; serror(MESY, fd_c, taglia, tmp_buf); return; @@ -363,31 +419,41 @@ void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia){ int64_t ntosend = (num<=0 || num>getLen(q))? getLen(q) : num; fileT **toSend = calloc(ntosend, sizeof(fileT *)); - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFile (n = %d) e' terminata con successo. File inviati:\n", fd_c, num); - // extract n files, we dont check if the client can actually modify - // the files since we add them back immediatly + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFile (n = %d) e' terminata con successo. File inviati:\n", + fd_c, + num); + /* extract n files, we dont check if the client can actually modify + * the files since we add them back immediatly */ size_t tot = 0; for(int i=0;ivalid; - n += snprintf(tmp_buf+n, m-n, "\t%d) \"%s\" dim = %ld\n", i, toSend[i]->filepath, toSend[i]->valid); + n += snprintf(tmp_buf+n, m-n, "\t%d) \"%s\" dim = %ld\n", + i, + toSend[i]->filepath, + toSend[i]->valid); } - n += snprintf(tmp_buf+n, m-n, "readNFile dimensione totale = %ld\n", tot); + n += snprintf(tmp_buf+n, m-n, "readNFile dimensione totale = %ld\n", + tot); sendMessageFileN(MEOK, toSend, ntosend, fd_c, taglia, tmp_buf); free(toSend); @@ -396,67 +462,92 @@ void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia){ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append) { - // messaggio da scrivere sul logfile + /* message to write to the logfile */ char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); if(!filepath || !q || !taglia) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\" e' terminata con errore\n", fd_c, append, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\" e' terminata con errore\n", + fd_c, + append, + filepath); errno = EINVAL; serror(MESY, fd_c, taglia, tmp_buf); return; } - // cerco il file + /* search the file */ fileT *f = NULL; f = find(q, filepath); - if(!f) { // file is not present - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con errore, file non trovato\n", fd_c, append, filepath, size); + if(!f) { /* file is not present */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con errore, file non trovato\n", + fd_c, + append, + filepath, + size); errno = ENOENT; serror(MENT, fd_c, taglia, tmp_buf); destroyFile(f); return; } - // file non aperto || non append e nessuna lock || lock ma non si è proprietari + /* file not open || no append and no lock || not the owner of the lock */ if(!f->open || (!append && !f->O_LOCK) || (f->O_LOCK && f->owner != fd_c)) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con errore, non proprietario del file\n", fd_c, append, filepath, size); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con errore, non proprietario del file\n", + fd_c, + append, + filepath, + size); errno = EPERM; serror(MENT, fd_c, taglia, tmp_buf); destroyFile(f); return; } - long trueSizeAdded = 0; // we may have alredy some space allocated + long trueSizeAdded = 0; /* we may have alredy some space allocated */ if(append) { trueSizeAdded = size - f->size + f->valid; } else { trueSizeAdded = (size>f->size)? size-f->size : 0; } - destroyFile(f); // not needed anymore + destroyFile(f); /* not needed anymore */ - if(trueSizeAdded > q->maxSize) { // removing all files would not be enought - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld ma il file e' piu' grande della dimensione massima\n", fd_c, append, filepath, size); + if(trueSizeAdded > q->maxSize) { /* removing all files would not be enought */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld ma il file e' piu' grande della dimensione massima\n", + fd_c, + append, + filepath, + size); errno = EFBIG; serror(MENT, fd_c, taglia, tmp_buf); return; } if(trueSizeAdded + getSize(q) > q->maxSize) { - // writing would be more than capacity - fileT **removed = NULL; // array that may (worst case) hold all files to be sent to the client + /* writing would be more than capacity */ + fileT **removed = NULL; /* array that may (worst case) hold all files to + * be sent to the client */ removed = dequeueN(q, filepath, trueSizeAdded); - if(!removed) { // internal error - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con errore del server\n", fd_c, append, filepath, size); + if(!removed) { /* internal error */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con errore del server\n", + fd_c, + append, + filepath, + size); errno = ENOENT; serror(MESY, fd_c, taglia, tmp_buf); return; } int ln = 0; fileT *tmp = removed[ln]; - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con successo, allocando %ld B in memoria. Ha causato una capacity miss e ha fatto espellere i seguenti file:", fd_c, append, filepath, size, trueSizeAdded); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con successo, allocando %ld B in memoria. Ha causato una capacity miss e ha fatto espellere i seguenti file:", + fd_c, + append, + filepath, + size, + trueSizeAdded); while(tmp!=NULL) { n += snprintf(tmp_buf+n, m-n, " \"%s\"", tmp->filepath); ++ln; @@ -471,15 +562,15 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl } free(removed); - // now we can get the actual file + /* now we can get the actual file */ void *content = NULL; content = calloc(size, sizeof(char)); if(!content) { - perror("calloc"); + perror("writeFile: calloc"); return; } if((readn(fd_c, content, size)) == -1) { - perror("readn"); + perror("writeFile: readn"); return; } @@ -487,41 +578,57 @@ 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) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, errore del server\n", fd_c, append, filepath, size); + perror("writeFile: appendFileInQueue"); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, errore del server\n", + fd_c, + append, + filepath, + size); serror(MESY, fd_c, taglia, tmp_buf); - perror("appendFileInQueue"); free(content); return; } } else { if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, errore del server\n", fd_c, append, filepath, size); + perror("writeFile: writeFileInQueue"); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, errore del server\n", + fd_c, + append, + filepath, + size); serror(MESY, fd_c, taglia, tmp_buf); - perror("writeFileInQueue"); free(content); return; } } - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con successo\n", fd_c, append, filepath, size); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con successo\n", + fd_c, + append, + filepath, + size); sendMessage(MEOK, fd_c, taglia, tmp_buf); taglia_update(taglia, q, 0); free(content); return; } - // no dequeue - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, accettata la richiesta.\n", fd_c, append, filepath, size); + /* no dequeue */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, accettata la richiesta.\n", + fd_c, + append, + filepath, + size); sendMessage(MEOK, fd_c, taglia, tmp_buf); - // non c'è ancora bisogno di rimuovere file + /* no reason to remove the file yet */ void *content = NULL; content = calloc(size, sizeof(char)); if(!content) { - perror("calloc"); + perror("writeFile: calloc"); return; } if(readn(fd_c, content, size) == -1) { - perror("readn"); + perror("writeFile: readn"); return; } @@ -529,22 +636,34 @@ 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) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, errore del server\n", fd_c, append, filepath, size); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, errore del server\n", + fd_c, + append, + filepath, + size); serror(MESY, fd_c, taglia, tmp_buf); - perror("appendFileInQueue"); + perror("writeFile: appendFileInQueue"); free(content); return; } } else { if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, errore del server\n", fd_c, append, filepath, size); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, errore del server\n", + fd_c, + append, + filepath, + size); serror(MESY, fd_c, taglia, tmp_buf); - perror("writeFileInQueue"); + perror("writeFile: writeFileInQueue"); free(content); return; } } - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con successo\n", fd_c, append, filepath, size); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una writeFile (append = %x) sul file \"%s\", dimensione = %ld, e' terminata con successo\n", + fd_c, + append, + filepath, + size); sendMessage(MEOK, fd_c, taglia, tmp_buf); taglia_update(taglia, q, 0); @@ -555,62 +674,70 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) { - // messaggio da scrivere sul logfile + /* message to write to the logfile */ char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); if(!filepath || !q || !taglia || !lock || !waiting) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" e' terminata con errore.\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" e' terminata con errore.\n", + fd_c, + filepath); errno = EINVAL; serror(MESY, fd_c, taglia, tmp_buf); return; } - // cerco il file da impostare in modalita' locked + /* search the file to lock */ fileT *f = NULL; f = find(q, filepath); - if(!f) { // file is not present - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" ma risulta assente.\n", fd_c, filepath); + if(!f) { /* file is not present */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" ma risulta assente.\n", + fd_c, + filepath); errno = ENOENT; serror(MENT, fd_c, taglia, tmp_buf); destroyFile(f); return; } - // provo a prendere la lock + /* try to lock */ if (openFileInQueue(q, filepath, 1, fd_c) == -1) { - // non siamo riusciti a prendere la lock al file, quindi aspettiamo che - // venga rilasciata mettendoci in attesa nella waiting list + /* unable to aquire the lock, wait to be released by inserting into + * the waiting queue */ if (errno == EPERM) { - if (pthread_mutex_lock(lock) == -1) { // begin ME - perror("lock"); + if (pthread_mutex_lock(lock) == -1) { /* begin ME */ + perror("lockFile: pthread_mutex_lock"); destroyFile(f); return; } if (addWaiting(waiting, filepath, fd_c) == -1) { - perror("addWaiting"); + perror("lockFile: addWaiting"); pthread_mutex_unlock(lock); destroyFile(f); return; } - if (pthread_mutex_unlock(lock) == -1) { // end ME - perror("unlock"); + if (pthread_mutex_unlock(lock) == -1) { /* end ME */ + perror("lockFile: pthread_mutex_unlock"); } destroyFile(f); return; } - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" e' terminata con errore.\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" e' terminata con errore.\n", + fd_c, + filepath); errno = ENOLCK; serror(MESE, fd_c, taglia, tmp_buf); destroyFile(f); return; } - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" e' terminata con successo\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" e' terminata con successo\n", + fd_c, + filepath); sendMessage(MEOK, fd_c, taglia, tmp_buf); destroyFile(f); return; @@ -618,38 +745,46 @@ void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mu void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) { - // messaggio da scrivere sul logfile + /* message to write to the logfile */ char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); if(!filepath || !q || !taglia || !lock || !waiting) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una unlockFile sul file \"%s\" e' terminata con errore.\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una unlockFile sul file \"%s\" e' terminata con errore.\n", + fd_c, + filepath); errno = EINVAL; serror(MESY, fd_c, taglia, tmp_buf); return; } - // cerco il file su cui rilasciare la lock - if(!searchFile(q, filepath)) { // file e' assente - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una unlockFile sul file \"%s\" ma risulta assente.\n", fd_c, filepath); + /* search the file */ + if(!searchFile(q, filepath)) { /* file does not exist */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una unlockFile sul file \"%s\" ma risulta assente.\n", + fd_c, + filepath); errno = ENOENT; serror(MENT, fd_c, taglia, tmp_buf); return; } if (unlockFileInQueue(q, filepath, fd_c) != 0) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una unlockFile sul file \"%s\" ma non è stata rilasciata la lock.\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una unlockFile sul file \"%s\" ma non è stata rilasciata la lock.\n", + fd_c, + filepath); serror(MENT, fd_c, taglia, tmp_buf); return; } - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una unlockFile sul file \"%s\" e' terminata con successo\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una unlockFile sul file \"%s\" e' terminata con successo\n", + fd_c, + filepath); sendMessage(MEOK, fd_c, taglia, tmp_buf); - // segnalo a un client in lista d'attesa che una lock e' stata rilasciata - if (pthread_mutex_lock(lock) == -1) { // begin ME - perror("lock"); + /* signal another waiting client that a lock has been released */ + if (pthread_mutex_lock(lock) == -1) { /* begin ME */ + perror("unlockFile: pthread_mutex_lock"); return; } @@ -657,12 +792,12 @@ void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_ waked = removeFirstWaiting(waiting, filepath); if (waked != -1) { - // we lock the file for the other client + /* we lock the file for the other client */ lockFile(filepath, q, waked, taglia, lock, waiting); } - if (pthread_mutex_unlock(lock) == -1) { // end ME - perror("unlock"); + if (pthread_mutex_unlock(lock) == -1) { /* end ME */ + perror("unlockFile: pthread_mutex_unlock"); return; } return; @@ -670,38 +805,45 @@ void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_ void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) { - // messaggio da scrivere sul logfile + /* message to write to the logfile */ char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); if(!filepath || !q || !taglia || !lock || !waiting) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una closeFile sul file \"%s\" e' terminata con errore.\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una closeFile sul file \"%s\" e' terminata con errore.\n", + fd_c, + filepath); errno = EINVAL; serror(MESY, fd_c, taglia, tmp_buf); return; } - // cerco il file da chiudere - if(!searchFile(q, filepath)) { // file e' assente - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una closeFile sul file \"%s\" ma risulta assente.\n", fd_c, filepath); + if(!searchFile(q, filepath)) { /* file does not exist */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una closeFile sul file \"%s\" ma risulta assente.\n", + fd_c, + filepath); errno = ENOENT; serror(MENT, fd_c, taglia, tmp_buf); return; } if (closeFileInQueue(q, filepath, fd_c) == -1) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una closeFile sul file \"%s\" e' terminata con errore.\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una closeFile sul file \"%s\" e' terminata con errore.\n", + fd_c, + filepath); serror(MENT, fd_c, taglia, tmp_buf); return; } - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una closeFile sul file \"%s\" e' terminata con successo\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una closeFile sul file \"%s\" e' terminata con successo\n", + fd_c, + filepath); sendMessage(MEOK, fd_c, taglia, tmp_buf); - // segnalo a un client in lista d'attesa che una lock e' stata rilasciata - if (pthread_mutex_lock(lock) == -1) { // begin ME - perror("lock"); + /* signal another waiting client that a lock has been released */ + if (pthread_mutex_lock(lock) == -1) { /* begin ME */ + perror("closeFile: pthread_mutex_lock"); return; } @@ -709,12 +851,12 @@ void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_m waked = removeFirstWaiting(waiting, filepath); if (waked != -1) { - // we lock the file for the other client + /* we lock the file for the other client */ lockFile(filepath, q, waked, taglia, lock, waiting); } - if (pthread_mutex_unlock(lock) == -1) { // end ME - perror("unlock"); + if (pthread_mutex_unlock(lock) == -1) { /* end ME */ + perror("closeFile: pthread_mutex_unlock"); return; } return; @@ -722,38 +864,46 @@ void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_m void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) { - // messaggio da scrivere sul logfile + /* message to write to the logfile */ char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); if(!filepath || !q || !taglia || !lock || !waiting) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una removeFile sul file \"%s\" e' terminata con errore.\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una removeFile sul file \"%s\" e' terminata con errore.\n", + fd_c, + filepath); errno = EINVAL; serror(MESY, fd_c, taglia, tmp_buf); return; } - // cerco il file da eliminare - if(!searchFile(q, filepath)) { // file e' assente - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una removeFile sul file \"%s\" ma risulta assente.\n", fd_c, filepath); + /* search for the file to delete */ + if(!searchFile(q, filepath)) { /* file does not exist */ + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una removeFile sul file \"%s\" ma risulta assente.\n", + fd_c, + filepath); errno = ENOENT; serror(MENT, fd_c, taglia, tmp_buf); return; } if (removeFileFromQueue(q, filepath, fd_c) == -1) { - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una removeFile sul file \"%s\" e' terminata con errore del server.\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una removeFile sul file \"%s\" e' terminata con errore del server.\n", + fd_c, + filepath); serror(MENT, fd_c, taglia, tmp_buf); return; } - n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una removeFile sul file \"%s\" e' terminata con successo\n", fd_c, filepath); + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una removeFile sul file \"%s\" e' terminata con successo\n", + fd_c, + filepath); sendMessage(MEOK, fd_c, taglia, tmp_buf); - // segnalo a un client in lista d'attesa che un file e' stato rimosso - if (pthread_mutex_lock(lock) == -1) { // begin ME - perror("lock"); + /* signal another waiting client that a lock has been released */ + if (pthread_mutex_lock(lock) == -1) { /* begin ME */ + perror("removeFile: pthread_mutex_lock"); return; } @@ -761,12 +911,12 @@ void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_ waked = removeFirstWaiting(waiting, filepath); if (waked != -1) { - // we lock the file for the other client + /* we lock the file for the other client */ lockFile(filepath, q, waked, taglia, lock, waiting); } - if (pthread_mutex_unlock(lock) == -1) { // end ME - perror("unlock"); + if (pthread_mutex_unlock(lock) == -1) { /* end ME */ + perror("removeFile: pthread_mutex_unlock"); return; } return; @@ -774,7 +924,9 @@ void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_ // ----------------------------------------------------------------------------- -/* funzioni ausiliarie relative ai client in attesa di lock */ +// helper functions for clients waiting locks + +/* add a client to the waiting list */ int addWaiting(waiting_t **waiting, char *filepath, int fd_c) { if(!waiting || !filepath) { errno = EINVAL; @@ -783,12 +935,12 @@ int addWaiting(waiting_t **waiting, char *filepath, int fd_c) { waiting_t *new = calloc(1, sizeof(waiting_t)); if (!new) { - perror("calloc"); + perror("addWaiting: calloc"); return -1; } new->file = calloc(MAXLENMESS, sizeof(char)); if (!new->file) { - perror("calloc"); + perror("addWaiting: calloc"); free(new); return -1; } @@ -797,13 +949,13 @@ int addWaiting(waiting_t **waiting, char *filepath, int fd_c) { new->fd = fd_c; new->next = NULL; - // se la lista è vuota + /* if the list is empty */ if(*waiting == NULL) { *waiting = new; return 0; } - // se la lista ha almeno un elemento + /* if the list has at least 1 element */ waiting_t *tail = *waiting; while(tail->next) { tail = tail->next; @@ -813,14 +965,15 @@ int addWaiting(waiting_t **waiting, char *filepath, int fd_c) { return 0; } -// remove first who is waiting on the lock for the file +/* remove first who is waiting on the lock for the file + * if no one is waiting for the lock it returns -1 */ int removeFirstWaiting(waiting_t **waiting, char *filepath) { if(!waiting || !filepath) { errno = EINVAL; return -1; } - // none waiting + /* none waiting */ if(*waiting == NULL) { return -1; } @@ -829,7 +982,7 @@ int removeFirstWaiting(waiting_t **waiting, char *filepath) { waiting_t *prec = NULL; long fd_c = -1; - // first one waiting + /* first one waiting */ if (strcmp(curr->file, filepath) == 0) { fd_c = curr->fd; *waiting = curr->next; @@ -839,7 +992,7 @@ int removeFirstWaiting(waiting_t **waiting, char *filepath) { return fd_c; } - // more than one waiting + /* more than one waiting */ while (curr->next) { prec = curr; curr = curr->next; @@ -857,7 +1010,7 @@ int removeFirstWaiting(waiting_t **waiting, char *filepath) { return -1; } -// destroy waiting structure +/* free waiting structure */ void clearWaiting(waiting_t **waiting) { if(!waiting) { return; diff --git a/lib/threadpool/apiFile.h b/lib/threadpool/apiFile.h index 75c2597..9454339 100644 --- a/lib/threadpool/apiFile.h +++ b/lib/threadpool/apiFile.h @@ -4,64 +4,159 @@ #include -#include -#include -/* TODO: finire tutte le descrizioni */ +#include "fileQueue.h" +#include "taglialegna.h" #define C_CREATE 1 #define C_LOCK 2 - -// Lista dei client in attesa su una lock +/* structure for the list of clients waiting for a lock */ typedef struct struct_waiting { - long fd; // client in attesa - char *file; // file su cui si vuole acquisire la lock - struct struct_waiting *next; // puntatore al prossimo elemento della lista + long fd; /* client waiting */ + char *file; /* file waiting the lock on */ + struct struct_waiting *next; } waiting_t; /** - * Apri o crea un nuovo file - * \param filepath: nome del file - * \param flags: - * \param q: queue in cui inserire il file - * \param fd_c: owner - * \param taglia_t: + * @brief Open or create new file + * + * @param filepath name of the file + * @param flags + * @param q queue to insert the file into + * @param fd_c owner + * @param taglia */ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia); -// Leggi un file e invialo al client + +/** + * @brief Read a file and send it to a client + * + * @param filepath name of the file + * @param q queue where the file is + * @param fd_c client + * @param taglia + */ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia); -// Invia al client $n file qualsiasi dalla queue + +/** + * @brief Send to the client n files from the queue + * + * @param num number of files to send + * @param q + * @param fd_c client + * @param taglia + */ void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia); -// Scrivi dati su un file già creato (append o overwrite) + +/** + * @brief Write to a file + * + * @param filepath + * @param size + * @param q + * @param fd_c client + * @param taglia + * @param append true if append, false if overwrite + */ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append); -// Acquisisci lock di un file + +/** + * @brief Get the lock of a file + * + * @param filepath + * @param q + * @param fd_c client + * @param taglia + * @param lock lock of the waiting_t list + * @param waiting + */ void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); -// Rilascia una Lock di un file + +/** + * @brief Relese a lock + * + * @param filepath + * @param q + * @param fd_c client + * @param taglia + * @param lock lock of the waiting_t list + * @param waiting + */ void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); -// Chiudi un file + +/** + * @brief Close a file and relese locks + * + * @param filepath + * @param q + * @param fd_c client + * @param taglia + * @param lock lock of the waiting_t list + * @param waiting + */ void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); -// Rimuovi un file + +/** + * @brief Delete a file + * + * @param filepath + * @param q + * @param fd_c client + * @param taglia + * @param lock lock of the waiting_t list + * @param waiting + */ void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); -// Funzione ausiliaria che invia un file al client +/** + * @brief Send a file to the client + * + * @param f file to send + * @param fd_c client + * @param taglia + * @return 0 if success, -1 if error + */ int sendFile(fileT *f, long fd_c, taglia_t *taglia); -// Aggiunge una coppia client/file alla coda in attesa di ottenere una lock +/** + * @brief Add the client to the waiting list for the lock on the file + * + * @pre the lock of the waiting list must be acquired + * + * @param waiting + * @param filepath + * @param fd_c client + * @return 0 if success, -1 if error + */ int addWaiting(waiting_t **waiting, char *filepath, int fd_c); -// Ottiene il primo client in attesa su una lock di un determinato file + +/** + * @brief Delete a file + * + * @pre the lock of the waiting list must be acquired + * + * @param waiting + * @param filepath + * @return the file descriptor of the client or -1 if no found + */ int removeFirstWaiting(waiting_t **waiting, char *filepath); -// Distrugge la lista d'attesa e ne libera la memoria + +/** + * @brief Frees the waiting_t structure + * + * @param waiting + */ void clearWaiting(waiting_t **waiting); #endif // _API_FILE