This commit is contained in:
elvis
2022-04-24 01:36:43 +02:00
parent 4fe23200bc
commit 9eaf9cdd49
9 changed files with 163 additions and 82 deletions

View File

@ -1,5 +1,5 @@
CC = gcc 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 INCDIR = ./lib/utils ./lib/ini ./lib/threadpool ./src ./lib/log ./lib/api
INCLUDES := $(patsubst %,-I %,$(INCDIR)) INCLUDES := $(patsubst %,-I %,$(INCDIR))
LDFLAGS = -L . LDFLAGS = -L .

View File

@ -8,6 +8,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <math.h>
#include <api.h> #include <api.h>
#include <conn.h> #include <conn.h>
@ -40,7 +41,9 @@ typedef struct files_s {
typedef struct openfiles_s { typedef struct openfiles_s {
int numOfFiles; int numOfFiles;
files_t *f; // list of files files_t *f; // list of files
int validwDir; // 1 if wDir is allocated
char *wDir; // for files sent from the server after openFile 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 *rDir; // for files read from the server
char *createdAndLocked; // path of the last file that was created and locked char *createdAndLocked; // path of the last file that was created and locked
int print; // whether to print int print; // whether to print
@ -64,7 +67,7 @@ typedef struct response_s {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
/* variabili globali */ /* 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 char socketName[SOCKNAMEMAX] = ""; // name of the socket
static openfiles_t *openedFiles = NULL; 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); int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf);
// frees memory inside response_t element // frees memory inside response_t element
void freeResponse(response_t *res); 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) { if(!openedFiles) {
openedFiles = calloc(1, sizeof(openedFiles)); if(createOpenedFiles()<0) {
if(!openedFiles) { perror("createOpenedfiles");
perror("calloc");
return -1; 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; struct sockaddr_un sa;
@ -170,17 +167,6 @@ int closeConnection(const char* sockname) {
} }
memset(socketName, 0, SOCKNAMEMAX); 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; return 0;
} }
@ -206,7 +192,7 @@ int openFile(const char* pathname, int flags) {
} }
// invio al server la stringa "openFile|$(pathname)|$(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); char *cmd = malloc(len);
if(!cmd) { if(!cmd) {
perror("malloc"); perror("malloc");
@ -214,6 +200,7 @@ int openFile(const char* pathname, int flags) {
} }
memset(cmd, 0, len); memset(cmd, 0, len);
snprintf(cmd, len, "openFile|%s|%d", pathname, flags); snprintf(cmd, len, "openFile|%s|%d", pathname, flags);
len = strnlen(cmd, len);
// send cmd // send cmd
if (writen(fd_skt, &len, sizeof(long)) == -1) { 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); memset(cmd, 0, len);
snprintf(cmd, len, "readFile|%s", pathname); snprintf(cmd, len, "readFile|%s", pathname);
len = strnlen(cmd, len);
// send cmd // send cmd
if (writen(fd_skt, &len, sizeof(long)) == -1) { 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)" // 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); char *cmd = malloc(len);
if(!cmd) { if(!cmd) {
perror("malloc"); perror("malloc");
@ -448,6 +436,7 @@ int readNFiles(int N, const char* dirname) {
} }
memset(cmd, 0, len); memset(cmd, 0, len);
snprintf(cmd, len, "readNFile|%d", N); snprintf(cmd, len, "readNFile|%d", N);
len = strnlen(cmd, len);
// send cmd // send cmd
if (writen(fd_skt, &len, sizeof(long)) == -1) { if (writen(fd_skt, &len, sizeof(long)) == -1) {
@ -558,7 +547,7 @@ int writeFile(const char* pathname, const char* dirname) {
} }
if (openedFiles->print) { if (openedFiles->print) {
fprintf(openedFiles->out, "Dimensione: %ld B\n", size); fprintf(openedFiles->out, "Dimensione: %ldB ", size);
fflush(openedFiles->out); fflush(openedFiles->out);
} }
@ -568,7 +557,7 @@ int writeFile(const char* pathname, const char* dirname) {
} }
// invio al server la stringa "writeFile|$(pathname)|$(size)" // 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); char *cmd = malloc(len);
if(!cmd) { if(!cmd) {
perror("malloc"); perror("malloc");
@ -577,6 +566,7 @@ int writeFile(const char* pathname, const char* dirname) {
} }
memset(cmd, 0, len); memset(cmd, 0, len);
snprintf(cmd, len, "writeFile|%s|%zu", pathname, size); snprintf(cmd, len, "writeFile|%s|%zu", pathname, size);
len = strnlen(cmd, len);
// send cmd // send cmd
if (writen(fd_skt, &len, sizeof(long)) == -1) { 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)); response_t *res = calloc(1, sizeof(response_t));
if(!res){ if(!res){
perror("calloc"); perror("calloc");
free(cmd);
free(content); free(content);
free(cmd);
return -1; return -1;
} }
if(reciveData(res, 0) < 0) { // 0 because if we get a MEOK we don't expect files if(reciveData(res, 0) < 0) { // 0 because if we get a MEOK we don't expect files
// errno is set by reciveData // errno is set by reciveData
freeResponse(res); freeResponse(res);
@ -608,7 +599,6 @@ int writeFile(const char* pathname, const char* dirname) {
free(content); free(content);
return -1; return -1;
} }
if(res->meerrno != 0) { // errno from server if(res->meerrno != 0) { // errno from server
freeResponse(res); freeResponse(res);
errno = res->meerrno; errno = res->meerrno;
@ -657,13 +647,30 @@ int writeFile(const char* pathname, const char* dirname) {
return -1; 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); freeResponse(res);
free(res); free(res);
free(cmd); free(cmd);
free(content); free(content);
return 0; return 0;
} }
@ -1111,7 +1118,7 @@ int removeFile(const char* pathname) {
} }
free(cmd); free(cmd);
return 1; return 0;
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@ -1123,21 +1130,33 @@ int setDirectory(char* Dir, int rw) {
} }
if (rw == 1) { if (rw == 1) {
if(openedFiles->wDir) if(openedFiles->validwDir) {
free(openedFiles->wDir); free(openedFiles->wDir);
openedFiles->wDir = NULL;
}
openedFiles->validwDir = 1;
openedFiles->wDir = malloc(strlen(Dir)+1); openedFiles->wDir = malloc(strlen(Dir)+1);
strncpy(openedFiles->wDir, Dir, strlen(Dir)+1); strncpy(openedFiles->wDir, Dir, strlen(Dir)+1);
} else { } else {
if(openedFiles->rDir) if(openedFiles->validrDir) {
free(openedFiles->rDir); free(openedFiles->rDir);
openedFiles->rDir = NULL;
}
openedFiles->validrDir = 1;
openedFiles->rDir = malloc(strlen(Dir)+1); openedFiles->rDir = malloc(strlen(Dir)+1);
strncpy(openedFiles->rDir, Dir, strlen(Dir)+1); strncpy(openedFiles->rDir, Dir, strlen(Dir)+1);
} }
return 1; return 0;
} }
void printInfo(int p, FILE *stream) { void printInfo(int p, FILE *stream) {
// 1 prints, 0 does not print // 1 prints, 0 does not print
if(!openedFiles) {
if(createOpenedFiles()<0) {
perror("createOpenedfiles");
return;
}
}
openedFiles->print = (p)? 1: 0; openedFiles->print = (p)? 1: 0;
openedFiles->out = stream; openedFiles->out = stream;
return; return;
@ -1152,7 +1171,7 @@ int reciveData(response_t *res, int expected) {
} }
int readnres; int readnres;
readnres = readn(fd_skt, res->ME, sizeof(res->ME)); readnres = readn(fd_skt, &res->ME, sizeof(res->ME));
if(readnres <= 0) { if(readnres <= 0) {
// readn sets errno // readn sets errno
return -1; return -1;
@ -1303,7 +1322,6 @@ _nofile:
free(res->rf); free(res->rf);
res->rf = NULL; res->rf = NULL;
if(strncmp(res->ME, MEOK, sizeof(res->ME))==0){ if(strncmp(res->ME, MEOK, sizeof(res->ME))==0){
// simple ok response // simple ok response
res->meerrno = 0; res->meerrno = 0;
@ -1457,8 +1475,12 @@ void freeResponse(response_t *res) {
return; return;
if(res->numfiles>0 && res->rf){ if(res->numfiles>0 && res->rf){
for(int i=0;i<res->numfiles;++i){ for(int i=0;i<res->numfiles;++i){
free(res->rf[i].path); if(res->rf[i].path)
free(res->rf[i].file); 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); free(res->rf);
} }
@ -1541,7 +1563,7 @@ int removeOpenFile(const char *pathname) {
openedFiles->f = tmp->next; openedFiles->f = tmp->next;
free(tmp->filename); free(tmp->filename);
free(tmp); free(tmp);
return 1; return 0;
} }
files_t *prc = NULL; files_t *prc = NULL;
@ -1563,8 +1585,9 @@ int removeOpenFile(const char *pathname) {
} }
int closeEveryFile() { int closeEveryFile() {
if (!openedFiles) if (openedFiles == NULL)
return 0; return 0;
if (openedFiles->numOfFiles > 0 && openedFiles->f != NULL) { if (openedFiles->numOfFiles > 0 && openedFiles->f != NULL) {
files_t *tmp = openedFiles->f; files_t *tmp = openedFiles->f;
files_t *prc = NULL; 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; return 0;
} }

View File

@ -118,6 +118,8 @@ size_t taglia_log(taglia_t *taglia, char *buf) {
} }
pthread_mutex_unlock(&taglia->m); pthread_mutex_unlock(&taglia->m);
fflush(taglia->file);
return n+m; return n+m;
_error_taglia_log_mutex: _error_taglia_log_mutex:

View File

@ -59,7 +59,7 @@ void sendMessage(char *m, long fd_c, taglia_t *taglia, char *mlog) {
m = MEOK; m = MEOK;
} }
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) { if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) {
perror("writen"); perror("writen");
goto _sendM_cleanup; 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 fileT *removed = NULL; // file che è stato rimosso
if(found && create) { // si vuole creare il file ma esiste già 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; errno = EEXIST;
serror(MENT, fd_c, taglia, tmp_buf); serror(MENT, fd_c, taglia, tmp_buf);
return; return;
} }
if(!found && !create) { // si vuole aprire e non creare un file inesistente 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; errno = ENOENT;
serror(MENT, fd_c, taglia, tmp_buf); serror(MENT, fd_c, taglia, tmp_buf);
return; return;
@ -228,7 +228,7 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
if(found && !create) { if(found && !create) {
if(openFileInQueue(q, filepath, 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); 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"); perror("openFileInQueue");
serror(MESE, fd_c, taglia, tmp_buf); serror(MESE, fd_c, taglia, tmp_buf);
return; return;
@ -399,22 +399,22 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl
fileT *f = NULL; fileT *f = NULL;
f = find(q, filepath); f = find(q, filepath);
if(!f) { // file is not present 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; errno = ENOENT;
serror(MENT, fd_c, taglia, tmp_buf); serror(MENT, fd_c, taglia, tmp_buf);
destroyFile(f); destroyFile(f);
return; return;
} }
// file non aperto || !append => locked || lock ma non si è proprietari // 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)) { 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); 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; errno = EPERM;
serror(MENT, fd_c, taglia, tmp_buf); serror(MENT, fd_c, taglia, tmp_buf);
destroyFile(f); destroyFile(f);
return; return;
} }
int trueSizeAdded = 0; // we may have alredy some space allocated long trueSizeAdded = 0; // we may have alredy some space allocated
if(append) { if(append) {
trueSizeAdded = size - f->size + f->valid; trueSizeAdded = size - f->size + f->valid;
} else { } 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 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); removed = dequeueN(q, filepath, trueSizeAdded);
if(!removed) { // internal error 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; errno = ENOENT;
serror(MESY, fd_c, taglia, tmp_buf); serror(MESY, fd_c, taglia, tmp_buf);
return; return;
} }
int ln = 0; int ln = 0;
fileT *tmp = removed[ln]; 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) { while(tmp!=NULL) {
n += snprintf(tmp_buf+n, m-n, " \"%s\"", tmp->filepath); n += snprintf(tmp_buf+n, m-n, " \"%s\"", tmp->filepath);
++ln; ++ln;
@ -448,12 +448,12 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl
destroyFile(removed[i]); destroyFile(removed[i]);
} }
free(removed); free(removed);
// now we can write the actual file
// now we can get the actual file
void *content = NULL; void *content = NULL;
content = malloc(size); content = calloc(size, sizeof(char));
if(!content) { if(!content) {
perror("malloc"); perror("calloc");
return; return;
} }
if((readn(fd_c, content, size)) == -1) { 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; return;
} }
n = 0;
if(append) { if(append) {
if(appendFileInQueue(q, filepath, content, size, fd_c) == -1) { 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"); perror("appendFileInQueue");
free(content); free(content);
return; return;
} }
} else { } else {
if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) { 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"); perror("writeFileInQueue");
free(content); free(content);
return; 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); taglia_update(taglia, q, 0);
free(content); free(content);
return; 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 // non c'è ancora bisogno di rimuovere file
void *content = NULL; void *content = NULL;
content = malloc(size); content = calloc(size, sizeof(char));
if(!content) { if(!content) {
perror("malloc"); perror("calloc");
return; return;
} }
if(readn(fd_c, content, size) == -1) { 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; 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(append) {
if(appendFileInQueue(q, filepath, content, size, fd_c) == -1) { 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"); perror("appendFileInQueue");
free(content); free(content);
return; return;
} }
} else { } else {
if(writeFileInQueue(q, filepath, content, size, fd_c) == -1) { 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"); perror("writeFileInQueue");
free(content); free(content);
return; 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);
taglia_write(taglia, tmp_buf); sendMessage(MEOK, fd_c, taglia, tmp_buf);
taglia_update(taglia, q, 0); taglia_update(taglia, q, 0);

View File

@ -28,7 +28,7 @@ static inline int readn(long fd, void *buf, size_t size) {
int r; int r;
char *bufptr = (char*)buf; char *bufptr = (char*)buf;
while(left>0) { while(left>0) {
if ((r=read((int)fd ,bufptr,left)) == -1) { if ((r=read(fd ,bufptr,left)) == -1) {
if (errno == EINTR) continue; if (errno == EINTR) continue;
return -1; return -1;
} }
@ -50,7 +50,7 @@ static inline int writen(long fd, void *buf, size_t size) {
int r; int r;
char *bufptr = (char*)buf; char *bufptr = (char*)buf;
while(left>0) { while(left>0) {
if ((r=write((int) fd, bufptr, left)) == -1) { if ((r=write(fd, bufptr, left)) == -1) {
if (errno == EINTR) continue; if (errno == EINTR) continue;
return -1; return -1;
} }

View File

@ -5,7 +5,7 @@
* tipo del messaggio * tipo del messaggio
*/ */
typedef struct msg { typedef struct msg {
int len; long len;
char *str; char *str;
} msg_t; } msg_t;

View File

@ -107,7 +107,6 @@ int main(int argc, char* argv[]) {
// lista dei comandi // lista dei comandi
cmd_t *cmds = NULL; cmd_t *cmds = NULL;
cmds = calloc(1, sizeof(cmd_t));
int opt = 0; int opt = 0;
char args[MAXARGLENGTH]; 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); strncpy(new->arg, arg, strnlen(arg, MAXARGLENGTH-1)+1);
} }
new->next = NULL; new->next = NULL;
cmd_t *tail = *l;
// se lista vuota aggiungo in cima, altrimenti scorro la lista // se lista vuota aggiungo in cima, altrimenti scorro la lista
if (*l == NULL) { if (*l == NULL) {
@ -254,6 +252,7 @@ int addCommand(cmd_t **l, char cmd, char *arg) {
return 0; return 0;
} }
cmd_t *tail = *l;
while (tail->next) { while (tail->next) {
tail = tail->next; tail = tail->next;
} }
@ -290,7 +289,7 @@ int execute(cmd_t *l, int print) {
interval.tv_sec = (num/1000); interval.tv_sec = (num/1000);
if (print) 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; break;
} }
@ -300,6 +299,8 @@ int execute(cmd_t *l, int print) {
tmp = tmp->next; tmp = tmp->next;
} }
fflush(stdout);
// loop that serches for -f // loop that serches for -f
tmp = l; tmp = l;
while(tmp) { while(tmp) {
@ -313,7 +314,7 @@ int execute(cmd_t *l, int print) {
ok = 0; ok = 0;
} }
if (print) { if (print) {
printf("\nf - Connessione al socket: %s\tEsito: ", globalSocket); fprintf(stdout, "f - Connessione al socket: %s\tEsito: ", globalSocket);
if(ok) if(ok)
printf("ok\n"); printf("ok\n");
if(!ok) if(!ok)
@ -329,8 +330,11 @@ int execute(cmd_t *l, int print) {
default: default:
break; break;
} }
tmp = tmp->next; if(tmp)
tmp = tmp->next;
} }
fflush(stdout);
// loop that checks for consistencies: // loop that checks for consistencies:
// 1) -D with no -w or -W after // 1) -D with no -w or -W after
// 2) -d with no -r or -R after // 2) -d with no -r or -R after
@ -341,7 +345,7 @@ int execute(cmd_t *l, int print) {
switch (tmp->name) { switch (tmp->name) {
case 'D': case 'D':
if(unmachedD) { 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; return 0;
} }
unmachedD = 1; unmachedD = 1;
@ -352,7 +356,7 @@ int execute(cmd_t *l, int print) {
break; break;
case 'd': case 'd':
if(unmachedd) { 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; return 0;
} }
unmachedd = 1; unmachedd = 1;
@ -367,13 +371,14 @@ int execute(cmd_t *l, int print) {
tmp = tmp->next; tmp = tmp->next;
} }
if(unmachedD) { 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; return 0;
} }
if(unmachedd) { 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; return 0;
} }
fflush(stdout);
char *Dir = NULL; // -D folder char *Dir = NULL; // -D folder
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) { if (print) {
printf("\nw - Scrivo i seguenti file sul server:"); printf("\nw - Scrivo i seguenti file sul server: ");
fflush(stdout); fflush(stdout);
} }
@ -568,7 +573,7 @@ int cmd_W(char *filelist, char *Dir, int print) {
char *string = tofree; char *string = tofree;
if (print == 1) { if (print == 1) {
printf("W - Scrivo i seguenti file sul server:\n"); printf("W - Scrivo i seguenti file sul server: \n");
fflush(stdout); fflush(stdout);
} }

View File

@ -369,6 +369,7 @@ int main(int argc, char *argv[]) {
perror("ERROR FATAL calloc"); perror("ERROR FATAL calloc");
goto _cleanup; goto _cleanup;
} }
*connfd = i;
args->connfd = connfd; args->connfd = connfd;
args->quit = &quit; args->quit = &quit;
args->request_pipe = request_pipe[1]; 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 destroyThreadPool(pool, 0); // notifico che i thread dovranno uscire
clearWaiting(&waiting); // destroy waiting list clearWaiting(&waiting); // destroy waiting list

View File

@ -36,7 +36,6 @@ void threadF(void *arg) {
pthread_mutex_t *lock = argl->lock; pthread_mutex_t *lock = argl->lock;
waiting_t **waiting = argl->waiting; waiting_t **waiting = argl->waiting;
fd_set set, tmpset; fd_set set, tmpset;
FD_ZERO(&set); FD_ZERO(&set);
FD_SET(connfd, &set); FD_SET(connfd, &set);
@ -58,7 +57,6 @@ void threadF(void *arg) {
break; // r!=0 and quit==0 break; // r!=0 and quit==0
} }
// comunicate with the client // comunicate with the client
msg_t str; msg_t str;
long n; long n;
@ -69,7 +67,7 @@ void threadF(void *arg) {
} }
if (n==0) if (n==0)
goto _cleanup;; goto _cleanup;
str.str = calloc(str.len+1, sizeof(char)); str.str = calloc(str.len+1, sizeof(char));
if (!str.str) { if (!str.str) {
perror("calloc"); perror("calloc");
@ -84,7 +82,7 @@ void threadF(void *arg) {
} }
str.str[str.len] = '\0'; 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); close(connfd);
int close = -1; int close = -1;
@ -145,7 +143,7 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
perror("calloc"); perror("calloc");
return -1; return -1;
} }
strncpy(string, command, len-1); // strlcpy is only bsd :( strncpy(string, command, len);
char *token = NULL; char *token = NULL;
char *token2 = NULL; char *token2 = NULL;