diff --git a/lib/threadpool/apiFile.c b/lib/threadpool/apiFile.c index cabdc5b..d8c69e8 100644 --- a/lib/threadpool/apiFile.c +++ b/lib/threadpool/apiFile.c @@ -7,6 +7,7 @@ #include #define MAXLENMESS 512 +#define LOGBUFSIZE 2048 #define MEOK "20" //OK #define MEHE "21" // help message @@ -74,7 +75,7 @@ _sendM_cleanup: void sendMessageFile(char *m, fileT *f, long fd_c, taglia_t *taglia, char *mlog) { if(!f) { errno = EINVAL; - char errmlog[2048] = "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; @@ -106,7 +107,7 @@ _sendMF_cleanup: void sendMessageFileN(char *m, fileT **f, int n, long fd_c, taglia_t *taglia, char *mlog) { if(!f) { errno = EINVAL; - char errmlog[2048] = "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; @@ -140,7 +141,7 @@ _sendMFN_cleanup: void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) { // messaggio da scrivere sul logfile - char tmp_buf[2048]; + char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); @@ -245,7 +246,7 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) { // messaggio da scrivere sul logfile - char tmp_buf[2048]; + char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); @@ -280,15 +281,55 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) { } -void readNFiles(char *numStr, queueT *q, long fd_c, taglia_t *taglia){ - // TODO +void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia){ + // messaggio da scrivere sul 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); + errno = EINVAL; + serror(MESY, fd_c, taglia, tmp_buf); + return; + } + + int oldNum = num; + num = (num<=0 || num>getLen(q))? getLen(q) : num; + fileT **toSend = calloc(num, 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 + for(int i=0;ifilepath); + } + + sendMessageFileN(MEOK, toSend, num, fd_c, taglia, tmp_buf); + free(toSend); return; } void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append) { // messaggio da scrivere sul logfile - char tmp_buf[2048]; + char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); @@ -423,7 +464,7 @@ 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 - char tmp_buf[2048]; + char tmp_buf[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_buf); diff --git a/lib/threadpool/apiFile.h b/lib/threadpool/apiFile.h index 2dc915b..c87ec3a 100644 --- a/lib/threadpool/apiFile.h +++ b/lib/threadpool/apiFile.h @@ -20,7 +20,7 @@ typedef struct struct_waiting { * \param filepath: nome del file * \param flags: * \param q: queue in cui inserire il file - * \param fd_c: + * \param fd_c: owner * \param taglia_t: */ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia); @@ -29,7 +29,7 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia); // Invia al client $n file qualsiasi dalla queue -void readNFiles(char *numStr, queueT *q, long fd_c, taglia_t *taglia); +void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia); // Scrivi dati su un file giĆ  creato (append o overwrite) void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append); diff --git a/lib/threadpool/fileQueue.h b/lib/threadpool/fileQueue.h index ae45b0a..8195a2b 100644 --- a/lib/threadpool/fileQueue.h +++ b/lib/threadpool/fileQueue.h @@ -93,7 +93,6 @@ int enqueue(queueT *q, fileT* data); */ fileT* dequeue(queueT *q); - /** * Estrae fileT dalla coda fino ad ottenere abbastanza spazio in q. * \param q: puntatore alla coda @@ -112,6 +111,7 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s); */ void voidDequeue(queueT *q); + /** * Stampa l'intero contenuto della coda. * \param stream: file su cui stampare diff --git a/src/serverWorker.c b/src/serverWorker.c index 3008ba5..c09c2b8 100644 --- a/src/serverWorker.c +++ b/src/serverWorker.c @@ -180,7 +180,9 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p if(!token2) goto _parser_cleanup; - readNFiles(token2, queue, fd_c, taglia); + int n = (int) strtol(token2, NULL, 10); + + readNFiles(n, queue, fd_c, taglia); goto _parser_end; }