-r is working

This commit is contained in:
elvis
2022-04-27 21:18:25 +02:00
parent 5151418090
commit 8841ca95a0
8 changed files with 65 additions and 43 deletions

View File

@ -35,7 +35,7 @@ void serror(char *m, long fd_c, taglia_t *taglia, char *mlog) {
errno = EINVAL;
m = MESY;
}
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) {
perror("writen");
goto _serror_cleanup;
}
@ -126,17 +126,25 @@ void sendMessageFile(char *m, fileT *f, long fd_c, taglia_t *taglia, char *mlog)
m = MEFP;
}
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) {
perror("writen");
goto _sendMF_cleanup;
}
int n = 1;
if(writen(fd_c, &n, sizeof(n)) < 0) {
int64_t *n = calloc(1, sizeof(*n));
if(n==NULL) {
perror("calloc");
goto _sendMF_cleanup;
}
*n = 1;
if(writen(fd_c, n, sizeof(int64_t)) < 0) {
perror("writen");
goto _sendMF_cleanup;
}
free(n);
if(sendFile(f, fd_c, taglia) < 0) {
perror("sendFile");
goto _sendMF_cleanup;
@ -166,7 +174,7 @@ void sendMessageFileN(char *m, fileT **f, int n, long fd_c, taglia_t *taglia, ch
m = MEFP;
}
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) {
perror("writen");
goto _sendMFN_cleanup;
}
@ -208,8 +216,8 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia)
}
int found = searchFile(q, filepath); // cerco il file nella queue
int create = flags & 0x1; // also %2
int lock = flags >> 1 & 0x1; // also >>1%2
int create = (flags & C_CREATE)?1:0; // also %2
int lock = (flags & C_LOCK)?1:0; // also >>1%2
fileT *removed = NULL; // file che è stato rimosso
if(found && create) { // si vuole creare il file ma esiste già
@ -321,8 +329,8 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
return;
}
if(f->open != 0) { // file already open
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath);
if(f->open == 0) { // file not already open
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore, file non aperto\n", fd_c, filepath);
errno = EPERM;
serror(MENT, fd_c, taglia, tmp_buf);
destroyFile(f); // f is a copy so we need to cleen up
@ -395,9 +403,11 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl
return;
}
// cerco il file
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, file non trovato\n", fd_c, append, filepath, size);
errno = ENOENT;

View File

@ -8,6 +8,10 @@
#include <taglialegna.h>
/* TODO: finire tutte le descrizioni */
#define C_CREATE 1
#define C_LOCK 2
// Lista dei client in attesa su una lock
typedef struct struct_waiting {
long fd; // client in attesa
@ -15,6 +19,7 @@ typedef struct struct_waiting {
struct struct_waiting *next; // puntatore al prossimo elemento della lista
} waiting_t;
/**
* Apri o crea un nuovo file
* \param filepath: nome del file

View File

@ -1,10 +1,12 @@
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fileQueue.h>
#include <util.h>
#define MAXNAMELEN 256
#define MAXNAMELEN 512
// creazione di un fileT
fileT* createFileT(char *f, int O_LOCK, int client, int open){
@ -26,13 +28,13 @@ fileT* createFileT(char *f, int O_LOCK, int client, int open){
file->size = 0;
file->valid = 0;
if ((file->filepath = malloc(sizeof(char)*MAXNAMELEN)) == NULL) {
if ((file->filepath = calloc(strnlen(f, MAXNAMELEN)+1, sizeof(char))) == NULL) {
perror("Malloc filepath");
destroyFile(file);
return NULL;
}
strncpy(file->filepath, f, MAXNAMELEN);
strncpy(file->filepath, f, strnlen(f, MAXNAMELEN));
// in seguito semplicemente facciamo realloc
if ((file->data = malloc(1)) == NULL) {
@ -49,6 +51,8 @@ int writeFileT(fileT *f, void *data, size_t size) {
return -1;
}
size = (size == 0)?1:size;
if ((f->data = realloc(f->data, f->valid + size)) == NULL) {
perror("Realloc content");
return -1;
@ -147,7 +151,6 @@ int enqueue(queueT *q, fileT* data) {
++q->len;
q->size += data->size;
UNLOCK_RETURN(&q->m, -1); // end me
return 0;
@ -318,17 +321,14 @@ int printQueue(FILE *stream, queueT *q) {
LOCK_RETURN(&q->m, -1); // begin me
UNLOCK_RETURN(&q->m, -1);
return 0;
nodeT *tmp = q->head;
fprintf(stream, "Lista file:");
fprintf(stream, "[Nome File] -> Dimensione in MB");
fprintf(stream, "Lista file:\n");
fprintf(stream, "[Nome File] -> Dimensione in MB\n");
while (tmp!=NULL) {
float res = ((float)(tmp->data)->size)/1000000; // in MB
// float res = ((float)(tmp->data)->valid)/1000000; // in MB
fprintf(stream, "[%s]\t-> %f MB\n", (tmp->data)->filepath, res);
fprintf(stream, "[%s] -> %f MB\n", (tmp->data)->filepath, res);
tmp = tmp->next;
}
@ -755,6 +755,8 @@ fileT* find(queueT *q, char *filepath) {
goto _end_find_in_queue;
}
UNLOCK_RETURN(&q->m, NULL); // end me
return res;