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

@ -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);