-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

@ -5,7 +5,7 @@ INCLUDES := $(patsubst %,-I %,$(INCDIR))
LDFLAGS = -L . -lm
OPTFLAGS = #-O3
LIBS = -lpthread
SOCKET = ./cs_sock
SOCKET = ./socket
BUILD_DIR = ./build
SRC_DIR = ./src

View File

@ -70,7 +70,7 @@ typedef struct recivedFile_s {
typedef struct response_s {
char ME[2]; // response
int meerrno; // errno if sent
int numfiles; // number of files if sent
int64_t numfiles; // number of files if sent
recivedFile_t *rf; // array of files
} response_t;
@ -196,8 +196,8 @@ int openFile(const char* pathname, int flags) {
errno = ENOTCONN;
return -1;
}
if(isOpen(pathname, flags)) { // already open
return -1;
if(isOpen(pathname, flags) == 1) { // already open
return 0;
}
// invio al server la stringa "openFile|$(pathname)|$(flags)"
@ -437,7 +437,7 @@ int readNFiles(int N, const char* dirname) {
}
// invio al server la stringa "readNFile|$(N)"
long len = strlen("readNFile")+1+((int)log10(N)+1)+1;
long len = strlen("readNFile")+1+((int)log10((N?N:1))+1)+1;
char *cmd = malloc(len);
if(!cmd) {
perror("malloc");
@ -567,14 +567,13 @@ int writeFile(const char* pathname, const char* dirname) {
}
// invio al server la stringa "writeFile|$(pathname)|$(size)"
long len = strlen("writeFile")+1+strlen(pathname)+1+((int) log10(size)+1)+1;
char *cmd = malloc(len);
long len = strlen("writeFile")+1+strlen(pathname)+1+((int) log10(size?size:1)+1)+1;
char *cmd = calloc(len, sizeof(char));
if(!cmd) {
perror("malloc");
perror("calloc");
free(content);
return -1;
}
memset(cmd, 0, len);
snprintf(cmd, len, "writeFile|%s|%zu", pathname, size);
len = strnlen(cmd, len);
@ -1200,12 +1199,18 @@ int reciveData(response_t *res, int expected) {
// ok response
res->meerrno = 0;
// get number of files sent
readnres = readn(fd_skt, &res->numfiles, sizeof(int));
readnres = readn(fd_skt, &res->numfiles, sizeof(int64_t));
if(readnres<=0) // readn sets errno
return -1;
if(res->rf)
free(res->rf);
if(res->numfiles < 0) {
errno = EINVAL;
return -1;
}
res->rf = calloc(res->numfiles, sizeof(recivedFile_t));
if(!res->rf){
perror("calloc");
@ -1513,7 +1518,7 @@ int isOpen(const char *pathname, const int flags) {
files_t *tmp = openedFiles->f;
while(tmp) {
if(strcmp(tmp->filename, pathname) == 0 && (tmp->locked==(flags&O_LOCK)))
if(strcmp(tmp->filename, pathname) == 0 && (tmp->locked || !(flags&O_LOCK)))
return 1;
tmp = tmp->next;
}

View File

@ -63,8 +63,7 @@ size_t taglia_write(taglia_t *taglia, char *buf) {
}
size_t n = 0;
pthread_mutex_lock(&taglia->m);
if ((n = fwrite(buf, sizeof(char), strlen(buf)+1, taglia->file)) < 0) {
// strlen conta il numero di caratteri senza EOF quindi aggiungiamo 1
if ((n = fwrite(buf, sizeof(char), strlen(buf), taglia->file)) < 0) {
perror("fwrite");
goto _error_write;
}
@ -111,8 +110,7 @@ size_t taglia_log(taglia_t *taglia, char *buf) {
goto _error_taglia_log_mutex;
}
if((m = fwrite(buf, sizeof(char), strlen(buf)+1, taglia->file)) < 0){
// strlen conta il numero di caratteri senza EOF quindi aggiungiamo 1
if((m = fwrite(buf, sizeof(char), strlen(buf), taglia->file)) < 0){
perror("ERROR: fwrite");
goto _error_taglia_log_mutex;
}

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;

View File

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

View File

@ -601,13 +601,13 @@ int cmd_W(char *filelist, char *Dir, int print) {
opened = 1;
}
if (opened && writeFile(token, Dir) == -1) {
if (opened && (writeFile(token, Dir) == -1)) {
ok = 0;
}
if (opened && !ok) { // errore precedente -> elimino il file vuoto
removeFile(token);
} else if (opened && closeFile(token) == -1) { // chiudo il file
} else if (opened && (closeFile(token) == -1)) { // chiudo il file
ok = 0;
}
@ -671,9 +671,11 @@ int cmd_r(char *filelist, char *dir, int print) {
void *buf = NULL;
size_t size = -1;
// printInfo(0, stdout);
printInfo(0, stdout);
// read the content of the file
if (ok && readFile(token, &buf, &size) == -1) {
fprintf(stdout, "\nreadFile\n");
fflush(stdout);
ok = 0;
}
if (print) {
@ -689,6 +691,8 @@ int cmd_r(char *filelist, char *dir, int print) {
// close the file
if (opened && closeFile(token) == -1) {
fprintf(stdout, "\ncloseFile\n");
fflush(stdout);
ok = 0;
}
if (print) {
@ -696,9 +700,7 @@ int cmd_r(char *filelist, char *dir, int print) {
if (ok) {
printf("ok");
}
else {
} else {
printf("errore");
perror("-r");
}