From 9eaf9cdd4952bd38e984b0112ad6d57592cdece1 Mon Sep 17 00:00:00 2001 From: elvis Date: Sun, 24 Apr 2022 01:36:43 +0200 Subject: [PATCH] bugssss --- Makefile | 2 +- lib/api/api.c | 137 ++++++++++++++++++++++++++++----------- lib/log/taglialegna.c | 2 + lib/threadpool/apiFile.c | 61 ++++++++++------- lib/utils/conn.h | 4 +- lib/utils/message.h | 2 +- src/client.c | 27 ++++---- src/server.c | 2 + src/serverWorker.c | 8 +-- 9 files changed, 163 insertions(+), 82 deletions(-) diff --git a/Makefile b/Makefile index 9668d9d..9515275 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS += -std=c99 -Wall -Werror -pedantic -g +CFLAGS += -std=c99 -Wall -Werror -pedantic #-g INCDIR = ./lib/utils ./lib/ini ./lib/threadpool ./src ./lib/log ./lib/api INCLUDES := $(patsubst %,-I %,$(INCDIR)) LDFLAGS = -L . diff --git a/lib/api/api.c b/lib/api/api.c index 330abdb..8f82b70 100644 --- a/lib/api/api.c +++ b/lib/api/api.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -40,7 +41,9 @@ typedef struct files_s { typedef struct openfiles_s { int numOfFiles; files_t *f; // list of files + int validwDir; // 1 if wDir is allocated char *wDir; // for files sent from the server after openFile + int validrDir; // 1 if rDir is allocated char *rDir; // for files read from the server char *createdAndLocked; // path of the last file that was created and locked int print; // whether to print @@ -64,7 +67,7 @@ typedef struct response_s { // ----------------------------------------------------------------------------- /* variabili globali */ -static int fd_skt; // descriptor of the socket +static long fd_skt; // descriptor of the socket static char socketName[SOCKNAMEMAX] = ""; // name of the socket static openfiles_t *openedFiles = NULL; @@ -86,6 +89,8 @@ int reciveData(response_t *res, int expected); int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf); // frees memory inside response_t element void freeResponse(response_t *res); +// creates the global variable +int createOpenedFiles(void); // ----------------------------------------------------------------------------- @@ -96,18 +101,10 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime } if(!openedFiles) { - openedFiles = calloc(1, sizeof(openedFiles)); - if(!openedFiles) { - perror("calloc"); + if(createOpenedFiles()<0) { + perror("createOpenedfiles"); return -1; } - openedFiles->f = NULL; - openedFiles->numOfFiles = 0; - openedFiles->createdAndLocked = NULL; - openedFiles->rDir = NULL; - openedFiles->wDir = NULL; - openedFiles->print = 0; - openedFiles->out = NULL; } struct sockaddr_un sa; @@ -170,17 +167,6 @@ int closeConnection(const char* sockname) { } memset(socketName, 0, SOCKNAMEMAX); - - if (openedFiles) { - if(openedFiles->createdAndLocked) - free(openedFiles->createdAndLocked); - if(openedFiles->wDir) - free(openedFiles->wDir); - if(openedFiles->rDir) - free(openedFiles->rDir); - free(openedFiles); - } - return 0; } @@ -206,7 +192,7 @@ int openFile(const char* pathname, int flags) { } // invio al server la stringa "openFile|$(pathname)|$(flags)" - long len = strlen("openFile")+1+strlen(pathname)+1+sizeof(flags)+1; + long len = strlen("openFile")+1+strlen(pathname)+1+((int)log10(flags)+1)+1; char *cmd = malloc(len); if(!cmd) { perror("malloc"); @@ -214,6 +200,7 @@ int openFile(const char* pathname, int flags) { } memset(cmd, 0, len); snprintf(cmd, len, "openFile|%s|%d", pathname, flags); + len = strnlen(cmd, len); // send cmd if (writen(fd_skt, &len, sizeof(long)) == -1) { @@ -336,6 +323,7 @@ int readFile(const char* pathname, void** buf, size_t* size) { } memset(cmd, 0, len); snprintf(cmd, len, "readFile|%s", pathname); + len = strnlen(cmd, len); // send cmd if (writen(fd_skt, &len, sizeof(long)) == -1) { @@ -440,7 +428,7 @@ int readNFiles(int N, const char* dirname) { } // invio al server la stringa "readNFile|$(N)" - long len = strlen("readNFile")+1+sizeof(N)+1; + long len = strlen("readNFile")+1+((int)log10(N)+1)+1; char *cmd = malloc(len); if(!cmd) { perror("malloc"); @@ -448,6 +436,7 @@ int readNFiles(int N, const char* dirname) { } memset(cmd, 0, len); snprintf(cmd, len, "readNFile|%d", N); + len = strnlen(cmd, len); // send cmd if (writen(fd_skt, &len, sizeof(long)) == -1) { @@ -558,7 +547,7 @@ int writeFile(const char* pathname, const char* dirname) { } if (openedFiles->print) { - fprintf(openedFiles->out, "Dimensione: %ld B\n", size); + fprintf(openedFiles->out, "Dimensione: %ldB ", size); fflush(openedFiles->out); } @@ -568,7 +557,7 @@ int writeFile(const char* pathname, const char* dirname) { } // invio al server la stringa "writeFile|$(pathname)|$(size)" - long len = strlen("writeFile")+1+strlen(pathname)+1+sizeof(size)+1; + long len = strlen("writeFile")+1+strlen(pathname)+1+((int) log10(size)+1)+1; char *cmd = malloc(len); if(!cmd) { perror("malloc"); @@ -577,6 +566,7 @@ int writeFile(const char* pathname, const char* dirname) { } memset(cmd, 0, len); snprintf(cmd, len, "writeFile|%s|%zu", pathname, size); + len = strnlen(cmd, len); // send cmd if (writen(fd_skt, &len, sizeof(long)) == -1) { @@ -596,10 +586,11 @@ int writeFile(const char* pathname, const char* dirname) { response_t *res = calloc(1, sizeof(response_t)); if(!res){ perror("calloc"); - free(cmd); free(content); + free(cmd); return -1; } + if(reciveData(res, 0) < 0) { // 0 because if we get a MEOK we don't expect files // errno is set by reciveData freeResponse(res); @@ -608,7 +599,6 @@ int writeFile(const char* pathname, const char* dirname) { free(content); return -1; } - if(res->meerrno != 0) { // errno from server freeResponse(res); errno = res->meerrno; @@ -657,13 +647,30 @@ int writeFile(const char* pathname, const char* dirname) { return -1; } + // recive response + if(reciveData(res, 0) < 0) { // 0 because if we get a MEOK we don't expect files + // errno is set by reciveData + freeResponse(res); + free(res); + free(cmd); + free(content); + return -1; + } + if(res->meerrno != 0) { // errno from server + freeResponse(res); + errno = res->meerrno; + free(res); + free(cmd); + free(content); + return -1; + } + freeResponse(res); free(res); free(cmd); free(content); - return 0; } @@ -1111,7 +1118,7 @@ int removeFile(const char* pathname) { } free(cmd); - return 1; + return 0; } // ----------------------------------------------------------------------------- @@ -1123,21 +1130,33 @@ int setDirectory(char* Dir, int rw) { } if (rw == 1) { - if(openedFiles->wDir) + if(openedFiles->validwDir) { free(openedFiles->wDir); + openedFiles->wDir = NULL; + } + openedFiles->validwDir = 1; openedFiles->wDir = malloc(strlen(Dir)+1); strncpy(openedFiles->wDir, Dir, strlen(Dir)+1); } else { - if(openedFiles->rDir) + if(openedFiles->validrDir) { free(openedFiles->rDir); + openedFiles->rDir = NULL; + } + openedFiles->validrDir = 1; openedFiles->rDir = malloc(strlen(Dir)+1); strncpy(openedFiles->rDir, Dir, strlen(Dir)+1); } - return 1; + return 0; } void printInfo(int p, FILE *stream) { // 1 prints, 0 does not print + if(!openedFiles) { + if(createOpenedFiles()<0) { + perror("createOpenedfiles"); + return; + } + } openedFiles->print = (p)? 1: 0; openedFiles->out = stream; return; @@ -1152,7 +1171,7 @@ int reciveData(response_t *res, int expected) { } int readnres; - readnres = readn(fd_skt, res->ME, sizeof(res->ME)); + readnres = readn(fd_skt, &res->ME, sizeof(res->ME)); if(readnres <= 0) { // readn sets errno return -1; @@ -1303,7 +1322,6 @@ _nofile: free(res->rf); res->rf = NULL; - if(strncmp(res->ME, MEOK, sizeof(res->ME))==0){ // simple ok response res->meerrno = 0; @@ -1457,8 +1475,12 @@ void freeResponse(response_t *res) { return; if(res->numfiles>0 && res->rf){ for(int i=0;inumfiles;++i){ - free(res->rf[i].path); - free(res->rf[i].file); + if(res->rf[i].path) + free(res->rf[i].path); + res->rf[i].path = NULL; + if(res->rf[i].file) + free(res->rf[i].file); + res->rf[i].file = NULL; } free(res->rf); } @@ -1541,7 +1563,7 @@ int removeOpenFile(const char *pathname) { openedFiles->f = tmp->next; free(tmp->filename); free(tmp); - return 1; + return 0; } files_t *prc = NULL; @@ -1563,8 +1585,9 @@ int removeOpenFile(const char *pathname) { } int closeEveryFile() { - if (!openedFiles) + if (openedFiles == NULL) return 0; + if (openedFiles->numOfFiles > 0 && openedFiles->f != NULL) { files_t *tmp = openedFiles->f; files_t *prc = NULL; @@ -1580,5 +1603,41 @@ int closeEveryFile() { } } } + if(openedFiles->createdAndLocked) { + free(openedFiles->createdAndLocked); + openedFiles->createdAndLocked = NULL; + } + if(openedFiles->validrDir) { + free(openedFiles->rDir); + openedFiles->validrDir = 0; + openedFiles->rDir = NULL; + } + if(openedFiles->validwDir) { + free(openedFiles->wDir); + openedFiles->validwDir = 0; + openedFiles->wDir = NULL; + } + + fprintf(stdout, "\nhere\n"); + fflush(stdout); + + return 0; +} + +int createOpenedFiles(void) { + openedFiles = calloc(1, sizeof(openedFiles)); + if(!openedFiles) { + perror("calloc"); + return -1; + } + openedFiles->f = NULL; + openedFiles->numOfFiles = 0; + openedFiles->createdAndLocked = NULL; + openedFiles->validrDir = 0; + openedFiles->rDir = NULL; + openedFiles->validwDir = 0; + openedFiles->wDir = NULL; + openedFiles->print = 0; + openedFiles->out = NULL; return 0; } diff --git a/lib/log/taglialegna.c b/lib/log/taglialegna.c index fa7e831..2fab1ea 100644 --- a/lib/log/taglialegna.c +++ b/lib/log/taglialegna.c @@ -118,6 +118,8 @@ size_t taglia_log(taglia_t *taglia, char *buf) { } pthread_mutex_unlock(&taglia->m); + fflush(taglia->file); + return n+m; _error_taglia_log_mutex: diff --git a/lib/threadpool/apiFile.c b/lib/threadpool/apiFile.c index 2ee0467..cc10240 100644 --- a/lib/threadpool/apiFile.c +++ b/lib/threadpool/apiFile.c @@ -59,7 +59,7 @@ void sendMessage(char *m, long fd_c, taglia_t *taglia, char *mlog) { m = MEOK; } - if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) { + if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) { perror("writen"); goto _sendM_cleanup; } @@ -213,14 +213,14 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) fileT *removed = NULL; // file che è stato rimosso 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\" e' terminata con errore\n", fd_c, flags, filepath); + 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\" e' terminato con errore\n", fd_c, flags, filepath); + 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; @@ -228,7 +228,7 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) if(found && !create) { 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); + 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"); serror(MESE, fd_c, taglia, tmp_buf); return; @@ -399,22 +399,22 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl 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\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, file non trovato\n", fd_c, append, filepath, size); errno = ENOENT; serror(MENT, fd_c, taglia, tmp_buf); destroyFile(f); return; } - // file non aperto || !append => locked || lock ma non si è proprietari - 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\n", fd_c, append, filepath, size); + // file non aperto || non append e nessuna lock || lock ma non si è proprietari + 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); errno = EPERM; serror(MENT, fd_c, taglia, tmp_buf); destroyFile(f); return; } - int 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 { @@ -422,19 +422,19 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl } destroyFile(f); // not needed anymore - fileT **removed = NULL; // array that may (worst case) hold all files to be sent to the client - - if(trueSizeAdded + getSize(q) > q->maxSize) { // writing would be more than capacity + 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 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\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 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. Ha causato una capacity miss e ha fatto espellere i seguenti file:", 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, 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; @@ -448,12 +448,12 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl destroyFile(removed[i]); } free(removed); - // now we can write the actual file + // now we can get the actual file void *content = NULL; - content = malloc(size); + content = calloc(size, sizeof(char)); if(!content) { - perror("malloc"); + perror("calloc"); return; } if((readn(fd_c, content, size)) == -1) { @@ -461,30 +461,41 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl return; } + n = 0; + 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); + 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); + 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); + 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); + sendMessage(MEOK, fd_c, taglia, tmp_buf); // non c'è ancora bisogno di rimuovere file void *content = NULL; - content = malloc(size); + content = calloc(size, sizeof(char)); if(!content) { - perror("malloc"); + perror("calloc"); return; } if(readn(fd_c, content, size) == -1) { @@ -492,23 +503,27 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl 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 = 0; 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); + 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); + serror(MESY, fd_c, taglia, tmp_buf); perror("writeFileInQueue"); free(content); return; } } - - taglia_write(taglia, tmp_buf); + 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); diff --git a/lib/utils/conn.h b/lib/utils/conn.h index 7d9fd2f..20d6818 100644 --- a/lib/utils/conn.h +++ b/lib/utils/conn.h @@ -28,7 +28,7 @@ static inline int readn(long fd, void *buf, size_t size) { int r; char *bufptr = (char*)buf; while(left>0) { - if ((r=read((int)fd ,bufptr,left)) == -1) { + if ((r=read(fd ,bufptr,left)) == -1) { if (errno == EINTR) continue; return -1; } @@ -50,7 +50,7 @@ static inline int writen(long fd, void *buf, size_t size) { int r; char *bufptr = (char*)buf; while(left>0) { - if ((r=write((int) fd, bufptr, left)) == -1) { + if ((r=write(fd, bufptr, left)) == -1) { if (errno == EINTR) continue; return -1; } diff --git a/lib/utils/message.h b/lib/utils/message.h index b81c8d9..da0ea0a 100644 --- a/lib/utils/message.h +++ b/lib/utils/message.h @@ -5,7 +5,7 @@ * tipo del messaggio */ typedef struct msg { - int len; + long len; char *str; } msg_t; diff --git a/src/client.c b/src/client.c index c07ac6d..6a1e0a8 100644 --- a/src/client.c +++ b/src/client.c @@ -107,7 +107,6 @@ int main(int argc, char* argv[]) { // lista dei comandi cmd_t *cmds = NULL; - cmds = calloc(1, sizeof(cmd_t)); int opt = 0; char args[MAXARGLENGTH]; @@ -246,7 +245,6 @@ int addCommand(cmd_t **l, char cmd, char *arg) { strncpy(new->arg, arg, strnlen(arg, MAXARGLENGTH-1)+1); } new->next = NULL; - cmd_t *tail = *l; // se lista vuota aggiungo in cima, altrimenti scorro la lista if (*l == NULL) { @@ -254,6 +252,7 @@ int addCommand(cmd_t **l, char cmd, char *arg) { return 0; } + cmd_t *tail = *l; while (tail->next) { tail = tail->next; } @@ -290,7 +289,7 @@ int execute(cmd_t *l, int print) { interval.tv_sec = (num/1000); if (print) - printf("\nt - Tempo fra due richieste: %ld ms\tEsito: ok\n", num); + fprintf(stdout, "t - Tempo fra due richieste: %ld ms\tEsito: ok\n", num); break; } @@ -300,6 +299,8 @@ int execute(cmd_t *l, int print) { tmp = tmp->next; } + fflush(stdout); + // loop that serches for -f tmp = l; while(tmp) { @@ -313,7 +314,7 @@ int execute(cmd_t *l, int print) { ok = 0; } if (print) { - printf("\nf - Connessione al socket: %s\tEsito: ", globalSocket); + fprintf(stdout, "f - Connessione al socket: %s\tEsito: ", globalSocket); if(ok) printf("ok\n"); if(!ok) @@ -329,8 +330,11 @@ int execute(cmd_t *l, int print) { default: break; } - tmp = tmp->next; + if(tmp) + tmp = tmp->next; } + fflush(stdout); + // loop that checks for consistencies: // 1) -D with no -w or -W after // 2) -d with no -r or -R after @@ -341,7 +345,7 @@ int execute(cmd_t *l, int print) { switch (tmp->name) { case 'D': if(unmachedD) { - printf("\nError: -D has no -w or -W matching after\n"); + fprintf(stdout, "\nError: -D has no -w or -W matching after\n"); return 0; } unmachedD = 1; @@ -352,7 +356,7 @@ int execute(cmd_t *l, int print) { break; case 'd': if(unmachedd) { - printf("\nError: -d has no -r or -R matching after\n"); + fprintf(stdout, "\nError: -d has no -r or -R matching after\n"); return 0; } unmachedd = 1; @@ -367,13 +371,14 @@ int execute(cmd_t *l, int print) { tmp = tmp->next; } if(unmachedD) { - printf("\nError: -D has no -w or -W matching after\n"); + fprintf(stdout, "\nError: -D has no -w or -W matching after\n"); return 0; } if(unmachedd) { - printf("\nError: -d has no -r or -R matching after\n"); + fprintf(stdout, "\nError: -d has no -r or -R matching after\n"); return 0; } + fflush(stdout); char *Dir = NULL; // -D folder char *dir = NULL; // -d folder @@ -509,7 +514,7 @@ int cmd_w(char *dirname, char *Dir, int print) { } if (print) { - printf("\nw - Scrivo i seguenti file sul server:"); + printf("\nw - Scrivo i seguenti file sul server: "); fflush(stdout); } @@ -568,7 +573,7 @@ int cmd_W(char *filelist, char *Dir, int print) { char *string = tofree; if (print == 1) { - printf("W - Scrivo i seguenti file sul server:\n"); + printf("W - Scrivo i seguenti file sul server: \n"); fflush(stdout); } diff --git a/src/server.c b/src/server.c index faefaeb..6d5a015 100644 --- a/src/server.c +++ b/src/server.c @@ -369,6 +369,7 @@ int main(int argc, char *argv[]) { perror("ERROR FATAL calloc"); goto _cleanup; } + *connfd = i; args->connfd = connfd; args->quit = &quit; args->request_pipe = request_pipe[1]; @@ -396,6 +397,7 @@ int main(int argc, char *argv[]) { } } } + fprintf(stdout, "\n"); destroyThreadPool(pool, 0); // notifico che i thread dovranno uscire clearWaiting(&waiting); // destroy waiting list diff --git a/src/serverWorker.c b/src/serverWorker.c index 288f965..e878941 100644 --- a/src/serverWorker.c +++ b/src/serverWorker.c @@ -36,7 +36,6 @@ void threadF(void *arg) { pthread_mutex_t *lock = argl->lock; waiting_t **waiting = argl->waiting; - fd_set set, tmpset; FD_ZERO(&set); FD_SET(connfd, &set); @@ -58,7 +57,6 @@ void threadF(void *arg) { break; // r!=0 and quit==0 } - // comunicate with the client msg_t str; long n; @@ -69,7 +67,7 @@ void threadF(void *arg) { } if (n==0) - goto _cleanup;; + goto _cleanup; str.str = calloc(str.len+1, sizeof(char)); if (!str.str) { perror("calloc"); @@ -84,7 +82,7 @@ void threadF(void *arg) { } str.str[str.len] = '\0'; - if(strncmp(str.str, "quit", 5)) { // il client vuole chiudere la connessione + if(strncmp(str.str, "quit", 5) == 0) { // il client vuole chiudere la connessione close(connfd); int close = -1; @@ -145,7 +143,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p perror("calloc"); return -1; } - strncpy(string, command, len-1); // strlcpy is only bsd :( + strncpy(string, command, len); char *token = NULL; char *token2 = NULL;