-r is working
This commit is contained in:
2
Makefile
2
Makefile
@ -5,7 +5,7 @@ INCLUDES := $(patsubst %,-I %,$(INCDIR))
|
|||||||
LDFLAGS = -L . -lm
|
LDFLAGS = -L . -lm
|
||||||
OPTFLAGS = #-O3
|
OPTFLAGS = #-O3
|
||||||
LIBS = -lpthread
|
LIBS = -lpthread
|
||||||
SOCKET = ./cs_sock
|
SOCKET = ./socket
|
||||||
|
|
||||||
BUILD_DIR = ./build
|
BUILD_DIR = ./build
|
||||||
SRC_DIR = ./src
|
SRC_DIR = ./src
|
||||||
|
|||||||
@ -70,7 +70,7 @@ typedef struct recivedFile_s {
|
|||||||
typedef struct response_s {
|
typedef struct response_s {
|
||||||
char ME[2]; // response
|
char ME[2]; // response
|
||||||
int meerrno; // errno if sent
|
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
|
recivedFile_t *rf; // array of files
|
||||||
} response_t;
|
} response_t;
|
||||||
|
|
||||||
@ -196,8 +196,8 @@ int openFile(const char* pathname, int flags) {
|
|||||||
errno = ENOTCONN;
|
errno = ENOTCONN;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(isOpen(pathname, flags)) { // already open
|
if(isOpen(pathname, flags) == 1) { // already open
|
||||||
return -1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// invio al server la stringa "openFile|$(pathname)|$(flags)"
|
// 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)"
|
// 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);
|
char *cmd = malloc(len);
|
||||||
if(!cmd) {
|
if(!cmd) {
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
@ -567,14 +567,13 @@ 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+((int) log10(size)+1)+1;
|
long len = strlen("writeFile")+1+strlen(pathname)+1+((int) log10(size?size:1)+1)+1;
|
||||||
char *cmd = malloc(len);
|
char *cmd = calloc(len, sizeof(char));
|
||||||
if(!cmd) {
|
if(!cmd) {
|
||||||
perror("malloc");
|
perror("calloc");
|
||||||
free(content);
|
free(content);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
memset(cmd, 0, len);
|
|
||||||
snprintf(cmd, len, "writeFile|%s|%zu", pathname, size);
|
snprintf(cmd, len, "writeFile|%s|%zu", pathname, size);
|
||||||
len = strnlen(cmd, len);
|
len = strnlen(cmd, len);
|
||||||
|
|
||||||
@ -1200,12 +1199,18 @@ int reciveData(response_t *res, int expected) {
|
|||||||
// ok response
|
// ok response
|
||||||
res->meerrno = 0;
|
res->meerrno = 0;
|
||||||
// get number of files sent
|
// 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
|
if(readnres<=0) // readn sets errno
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if(res->rf)
|
if(res->rf)
|
||||||
free(res->rf);
|
free(res->rf);
|
||||||
|
|
||||||
|
if(res->numfiles < 0) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
res->rf = calloc(res->numfiles, sizeof(recivedFile_t));
|
res->rf = calloc(res->numfiles, sizeof(recivedFile_t));
|
||||||
if(!res->rf){
|
if(!res->rf){
|
||||||
perror("calloc");
|
perror("calloc");
|
||||||
@ -1513,7 +1518,7 @@ int isOpen(const char *pathname, const int flags) {
|
|||||||
|
|
||||||
files_t *tmp = openedFiles->f;
|
files_t *tmp = openedFiles->f;
|
||||||
while(tmp) {
|
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;
|
return 1;
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,8 +63,7 @@ size_t taglia_write(taglia_t *taglia, char *buf) {
|
|||||||
}
|
}
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
pthread_mutex_lock(&taglia->m);
|
pthread_mutex_lock(&taglia->m);
|
||||||
if ((n = fwrite(buf, sizeof(char), strlen(buf)+1, taglia->file)) < 0) {
|
if ((n = fwrite(buf, sizeof(char), strlen(buf), taglia->file)) < 0) {
|
||||||
// strlen conta il numero di caratteri senza EOF quindi aggiungiamo 1
|
|
||||||
perror("fwrite");
|
perror("fwrite");
|
||||||
goto _error_write;
|
goto _error_write;
|
||||||
}
|
}
|
||||||
@ -111,8 +110,7 @@ size_t taglia_log(taglia_t *taglia, char *buf) {
|
|||||||
goto _error_taglia_log_mutex;
|
goto _error_taglia_log_mutex;
|
||||||
}
|
}
|
||||||
|
|
||||||
if((m = fwrite(buf, sizeof(char), strlen(buf)+1, taglia->file)) < 0){
|
if((m = fwrite(buf, sizeof(char), strlen(buf), taglia->file)) < 0){
|
||||||
// strlen conta il numero di caratteri senza EOF quindi aggiungiamo 1
|
|
||||||
perror("ERROR: fwrite");
|
perror("ERROR: fwrite");
|
||||||
goto _error_taglia_log_mutex;
|
goto _error_taglia_log_mutex;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,7 +35,7 @@ void serror(char *m, long fd_c, taglia_t *taglia, char *mlog) {
|
|||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
m = MESY;
|
m = MESY;
|
||||||
}
|
}
|
||||||
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
|
if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) {
|
||||||
perror("writen");
|
perror("writen");
|
||||||
goto _serror_cleanup;
|
goto _serror_cleanup;
|
||||||
}
|
}
|
||||||
@ -126,17 +126,25 @@ void sendMessageFile(char *m, fileT *f, long fd_c, taglia_t *taglia, char *mlog)
|
|||||||
m = MEFP;
|
m = MEFP;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
|
if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) {
|
||||||
perror("writen");
|
perror("writen");
|
||||||
goto _sendMF_cleanup;
|
goto _sendMF_cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
int n = 1;
|
int64_t *n = calloc(1, sizeof(*n));
|
||||||
if(writen(fd_c, &n, sizeof(n)) < 0) {
|
if(n==NULL) {
|
||||||
|
perror("calloc");
|
||||||
|
goto _sendMF_cleanup;
|
||||||
|
}
|
||||||
|
*n = 1;
|
||||||
|
|
||||||
|
if(writen(fd_c, n, sizeof(int64_t)) < 0) {
|
||||||
perror("writen");
|
perror("writen");
|
||||||
goto _sendMF_cleanup;
|
goto _sendMF_cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(n);
|
||||||
|
|
||||||
if(sendFile(f, fd_c, taglia) < 0) {
|
if(sendFile(f, fd_c, taglia) < 0) {
|
||||||
perror("sendFile");
|
perror("sendFile");
|
||||||
goto _sendMF_cleanup;
|
goto _sendMF_cleanup;
|
||||||
@ -166,7 +174,7 @@ void sendMessageFileN(char *m, fileT **f, int n, long fd_c, taglia_t *taglia, ch
|
|||||||
m = MEFP;
|
m = MEFP;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(writen(fd_c, m, strnlen(m, MAXLENMESS)+1) < 0) {
|
if(writen(fd_c, m, strnlen(m, MAXLENMESS)) < 0) {
|
||||||
perror("writen");
|
perror("writen");
|
||||||
goto _sendMFN_cleanup;
|
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 found = searchFile(q, filepath); // cerco il file nella queue
|
||||||
int create = flags & 0x1; // also %2
|
int create = (flags & C_CREATE)?1:0; // also %2
|
||||||
int lock = flags >> 1 & 0x1; // also >>1%2
|
int lock = (flags & C_LOCK)?1:0; // also >>1%2
|
||||||
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à
|
||||||
@ -321,8 +329,8 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(f->open != 0) { // file already open
|
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\n", fd_c, filepath);
|
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;
|
errno = EPERM;
|
||||||
serror(MENT, fd_c, taglia, tmp_buf);
|
serror(MENT, fd_c, taglia, tmp_buf);
|
||||||
destroyFile(f); // f is a copy so we need to cleen up
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// cerco il file
|
// cerco il file
|
||||||
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, file non trovato\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;
|
||||||
|
|||||||
@ -8,6 +8,10 @@
|
|||||||
#include <taglialegna.h>
|
#include <taglialegna.h>
|
||||||
/* TODO: finire tutte le descrizioni */
|
/* TODO: finire tutte le descrizioni */
|
||||||
|
|
||||||
|
#define C_CREATE 1
|
||||||
|
#define C_LOCK 2
|
||||||
|
|
||||||
|
|
||||||
// Lista dei client in attesa su una lock
|
// Lista dei client in attesa su una lock
|
||||||
typedef struct struct_waiting {
|
typedef struct struct_waiting {
|
||||||
long fd; // client in attesa
|
long fd; // client in attesa
|
||||||
@ -15,6 +19,7 @@ typedef struct struct_waiting {
|
|||||||
struct struct_waiting *next; // puntatore al prossimo elemento della lista
|
struct struct_waiting *next; // puntatore al prossimo elemento della lista
|
||||||
} waiting_t;
|
} waiting_t;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apri o crea un nuovo file
|
* Apri o crea un nuovo file
|
||||||
* \param filepath: nome del file
|
* \param filepath: nome del file
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <fileQueue.h>
|
#include <fileQueue.h>
|
||||||
#include <util.h>
|
#include <util.h>
|
||||||
|
|
||||||
#define MAXNAMELEN 256
|
#define MAXNAMELEN 512
|
||||||
|
|
||||||
// creazione di un fileT
|
// creazione di un fileT
|
||||||
fileT* createFileT(char *f, int O_LOCK, int client, int open){
|
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->size = 0;
|
||||||
file->valid = 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");
|
perror("Malloc filepath");
|
||||||
destroyFile(file);
|
destroyFile(file);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
strncpy(file->filepath, f, MAXNAMELEN);
|
strncpy(file->filepath, f, strnlen(f, MAXNAMELEN));
|
||||||
|
|
||||||
// in seguito semplicemente facciamo realloc
|
// in seguito semplicemente facciamo realloc
|
||||||
if ((file->data = malloc(1)) == NULL) {
|
if ((file->data = malloc(1)) == NULL) {
|
||||||
@ -49,6 +51,8 @@ int writeFileT(fileT *f, void *data, size_t size) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size = (size == 0)?1:size;
|
||||||
|
|
||||||
if ((f->data = realloc(f->data, f->valid + size)) == NULL) {
|
if ((f->data = realloc(f->data, f->valid + size)) == NULL) {
|
||||||
perror("Realloc content");
|
perror("Realloc content");
|
||||||
return -1;
|
return -1;
|
||||||
@ -147,7 +151,6 @@ int enqueue(queueT *q, fileT* data) {
|
|||||||
++q->len;
|
++q->len;
|
||||||
q->size += data->size;
|
q->size += data->size;
|
||||||
|
|
||||||
|
|
||||||
UNLOCK_RETURN(&q->m, -1); // end me
|
UNLOCK_RETURN(&q->m, -1); // end me
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -318,17 +321,14 @@ int printQueue(FILE *stream, queueT *q) {
|
|||||||
|
|
||||||
LOCK_RETURN(&q->m, -1); // begin me
|
LOCK_RETURN(&q->m, -1); // begin me
|
||||||
|
|
||||||
UNLOCK_RETURN(&q->m, -1);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
nodeT *tmp = q->head;
|
nodeT *tmp = q->head;
|
||||||
|
|
||||||
fprintf(stream, "Lista file:");
|
fprintf(stream, "Lista file:\n");
|
||||||
fprintf(stream, "[Nome File] -> Dimensione in MB");
|
fprintf(stream, "[Nome File] -> Dimensione in MB\n");
|
||||||
while (tmp!=NULL) {
|
while (tmp!=NULL) {
|
||||||
float res = ((float)(tmp->data)->size)/1000000; // in MB
|
float res = ((float)(tmp->data)->size)/1000000; // in MB
|
||||||
// float res = ((float)(tmp->data)->valid)/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;
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -755,6 +755,8 @@ fileT* find(queueT *q, char *filepath) {
|
|||||||
goto _end_find_in_queue;
|
goto _end_find_in_queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
UNLOCK_RETURN(&q->m, NULL); // end me
|
UNLOCK_RETURN(&q->m, NULL); // end me
|
||||||
return res;
|
return res;
|
||||||
|
|
||||||
|
|||||||
@ -26,9 +26,9 @@
|
|||||||
static inline int readn(long fd, void *buf, size_t size) {
|
static inline int readn(long fd, void *buf, size_t size) {
|
||||||
size_t left = size;
|
size_t left = size;
|
||||||
int r;
|
int r;
|
||||||
char *bufptr = (char*)buf;
|
char *bufptr = (char*) buf;
|
||||||
while(left>0) {
|
while(left>0) {
|
||||||
if ((r=read(fd ,bufptr,left)) == -1) {
|
if ((r=read(fd, bufptr, left)) == -1) {
|
||||||
if (errno == EINTR) continue;
|
if (errno == EINTR) continue;
|
||||||
return -1;
|
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) {
|
static inline int writen(long fd, void *buf, size_t size) {
|
||||||
size_t left = size;
|
size_t left = size;
|
||||||
int r;
|
int r;
|
||||||
char *bufptr = (char*)buf;
|
char *bufptr = (char*) buf;
|
||||||
while(left>0) {
|
while(left>0) {
|
||||||
if ((r=write(fd, bufptr, left)) == -1) {
|
if ((r=write(fd, bufptr, left)) == -1) {
|
||||||
if (errno == EINTR) continue;
|
if (errno == EINTR) continue;
|
||||||
|
|||||||
14
src/client.c
14
src/client.c
@ -601,13 +601,13 @@ int cmd_W(char *filelist, char *Dir, int print) {
|
|||||||
opened = 1;
|
opened = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opened && writeFile(token, Dir) == -1) {
|
if (opened && (writeFile(token, Dir) == -1)) {
|
||||||
ok = 0;
|
ok = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opened && !ok) { // errore precedente -> elimino il file vuoto
|
if (opened && !ok) { // errore precedente -> elimino il file vuoto
|
||||||
removeFile(token);
|
removeFile(token);
|
||||||
} else if (opened && closeFile(token) == -1) { // chiudo il file
|
} else if (opened && (closeFile(token) == -1)) { // chiudo il file
|
||||||
ok = 0;
|
ok = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -671,9 +671,11 @@ int cmd_r(char *filelist, char *dir, int print) {
|
|||||||
void *buf = NULL;
|
void *buf = NULL;
|
||||||
size_t size = -1;
|
size_t size = -1;
|
||||||
|
|
||||||
// printInfo(0, stdout);
|
printInfo(0, stdout);
|
||||||
// read the content of the file
|
// read the content of the file
|
||||||
if (ok && readFile(token, &buf, &size) == -1) {
|
if (ok && readFile(token, &buf, &size) == -1) {
|
||||||
|
fprintf(stdout, "\nreadFile\n");
|
||||||
|
fflush(stdout);
|
||||||
ok = 0;
|
ok = 0;
|
||||||
}
|
}
|
||||||
if (print) {
|
if (print) {
|
||||||
@ -689,6 +691,8 @@ int cmd_r(char *filelist, char *dir, int print) {
|
|||||||
|
|
||||||
// close the file
|
// close the file
|
||||||
if (opened && closeFile(token) == -1) {
|
if (opened && closeFile(token) == -1) {
|
||||||
|
fprintf(stdout, "\ncloseFile\n");
|
||||||
|
fflush(stdout);
|
||||||
ok = 0;
|
ok = 0;
|
||||||
}
|
}
|
||||||
if (print) {
|
if (print) {
|
||||||
@ -696,9 +700,7 @@ int cmd_r(char *filelist, char *dir, int print) {
|
|||||||
|
|
||||||
if (ok) {
|
if (ok) {
|
||||||
printf("ok");
|
printf("ok");
|
||||||
}
|
} else {
|
||||||
|
|
||||||
else {
|
|
||||||
printf("errore");
|
printf("errore");
|
||||||
perror("-r");
|
perror("-r");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user