added function readNfiles
This commit is contained in:
@ -7,6 +7,7 @@
|
|||||||
#include <taglialegna.h>
|
#include <taglialegna.h>
|
||||||
|
|
||||||
#define MAXLENMESS 512
|
#define MAXLENMESS 512
|
||||||
|
#define LOGBUFSIZE 2048
|
||||||
|
|
||||||
#define MEOK "20" //OK
|
#define MEOK "20" //OK
|
||||||
#define MEHE "21" // help message
|
#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) {
|
void sendMessageFile(char *m, fileT *f, long fd_c, taglia_t *taglia, char *mlog) {
|
||||||
if(!f) {
|
if(!f) {
|
||||||
errno = EINVAL;
|
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);
|
strncat(errmlog, mlog, sizeof(errmlog)-strlen(errmlog)-1);
|
||||||
serror(MESY, fd_c, taglia, errmlog);
|
serror(MESY, fd_c, taglia, errmlog);
|
||||||
goto _sendMF_cleanup;
|
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) {
|
void sendMessageFileN(char *m, fileT **f, int n, long fd_c, taglia_t *taglia, char *mlog) {
|
||||||
if(!f) {
|
if(!f) {
|
||||||
errno = EINVAL;
|
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);
|
strncat(errmlog, mlog, sizeof(errmlog)-strlen(errmlog)-1);
|
||||||
serror(MESY, fd_c, taglia, errmlog);
|
serror(MESY, fd_c, taglia, errmlog);
|
||||||
goto _sendMFN_cleanup;
|
goto _sendMFN_cleanup;
|
||||||
@ -140,7 +141,7 @@ _sendMFN_cleanup:
|
|||||||
|
|
||||||
void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) {
|
void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) {
|
||||||
// messaggio da scrivere sul logfile
|
// messaggio da scrivere sul logfile
|
||||||
char tmp_buf[2048];
|
char tmp_buf[LOGBUFSIZE];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
size_t m = sizeof(tmp_buf);
|
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) {
|
void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
|
||||||
// messaggio da scrivere sul logfile
|
// messaggio da scrivere sul logfile
|
||||||
char tmp_buf[2048];
|
char tmp_buf[LOGBUFSIZE];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
size_t m = sizeof(tmp_buf);
|
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){
|
void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia){
|
||||||
// TODO
|
// 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;i<num;++i) { /* TODO: fix here */
|
||||||
|
toSend[i] = dequeue(q);
|
||||||
|
if(!toSend[i]) {
|
||||||
|
n = 0;
|
||||||
|
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFiles (n = %d) e' terminato con errore del server\n", fd_c, oldNum);
|
||||||
|
perror("dequeue");
|
||||||
|
serror(MESE, fd_c, taglia, tmp_buf);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int tmp = enqueue(q, toSend[i]);
|
||||||
|
if(tmp<0) {
|
||||||
|
n = 0;
|
||||||
|
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFiles (n = %d) e' terminato con errore del server\n", fd_c, oldNum);
|
||||||
|
perror("enqueue");
|
||||||
|
serror(MESE, fd_c, taglia, tmp_buf);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
n += snprintf(tmp_buf+n, m-n, "\t%d) \"%s\"\n", i, toSend[i]->filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessageFileN(MEOK, toSend, num, fd_c, taglia, tmp_buf);
|
||||||
|
free(toSend);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append) {
|
void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append) {
|
||||||
// messaggio da scrivere sul logfile
|
// messaggio da scrivere sul logfile
|
||||||
char tmp_buf[2048];
|
char tmp_buf[LOGBUFSIZE];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
size_t m = sizeof(tmp_buf);
|
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) {
|
void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) {
|
||||||
// messaggio da scrivere sul logfile
|
// messaggio da scrivere sul logfile
|
||||||
char tmp_buf[2048];
|
char tmp_buf[LOGBUFSIZE];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
size_t m = sizeof(tmp_buf);
|
size_t m = sizeof(tmp_buf);
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,7 @@ typedef struct struct_waiting {
|
|||||||
* \param filepath: nome del file
|
* \param filepath: nome del file
|
||||||
* \param flags:
|
* \param flags:
|
||||||
* \param q: queue in cui inserire il file
|
* \param q: queue in cui inserire il file
|
||||||
* \param fd_c:
|
* \param fd_c: owner
|
||||||
* \param taglia_t:
|
* \param taglia_t:
|
||||||
*/
|
*/
|
||||||
void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia);
|
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);
|
void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia);
|
||||||
|
|
||||||
// Invia al client $n file qualsiasi dalla queue
|
// 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)
|
// 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);
|
void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append);
|
||||||
|
|||||||
@ -93,7 +93,6 @@ int enqueue(queueT *q, fileT* data);
|
|||||||
*/
|
*/
|
||||||
fileT* dequeue(queueT *q);
|
fileT* dequeue(queueT *q);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Estrae fileT dalla coda fino ad ottenere abbastanza spazio in q.
|
* Estrae fileT dalla coda fino ad ottenere abbastanza spazio in q.
|
||||||
* \param q: puntatore alla coda
|
* \param q: puntatore alla coda
|
||||||
@ -112,6 +111,7 @@ fileT ** dequeueN(queueT *q, char *filepath, size_t s);
|
|||||||
*/
|
*/
|
||||||
void voidDequeue(queueT *q);
|
void voidDequeue(queueT *q);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stampa l'intero contenuto della coda.
|
* Stampa l'intero contenuto della coda.
|
||||||
* \param stream: file su cui stampare
|
* \param stream: file su cui stampare
|
||||||
|
|||||||
@ -180,7 +180,9 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
|
|||||||
if(!token2)
|
if(!token2)
|
||||||
goto _parser_cleanup;
|
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;
|
goto _parser_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user