working -R

This commit is contained in:
elvis
2022-04-29 21:04:45 +02:00
parent 6a7b018f25
commit 59ac9663c9
5 changed files with 69 additions and 38 deletions

View File

@ -160,7 +160,13 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime
} }
int closeConnection(const char* sockname) { int closeConnection(const char* sockname) {
if (!sockname || strncmp(socketName, sockname, sizeof(socketName)) != 0) { if (!sockname) {
errno = EINVAL;
return -1;
}
// if closing a different socket -> error
if(strncmp(socketName, "", sizeof(socketName)) != 0
&& strncmp(socketName, sockname, sizeof(socketName)) != 0) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
@ -201,7 +207,8 @@ 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+((int)log10(flags?flags:1)+1)+1; int leng_flags = snprintf(NULL, 0, "%d", flags);
long len = strlen("openFile")+1+strlen(pathname)+1+leng_flags+1;
char *cmd = malloc(len); char *cmd = malloc(len);
if(!cmd) { if(!cmd) {
perror("malloc"); perror("malloc");
@ -436,15 +443,16 @@ int readNFiles(int N, const char* dirname) {
return -1; return -1;
} }
// invio al server la stringa "readNFile|$(N)" // invio al server la stringa "readNFiles|$(N)"
long len = strlen("readNFile")+1+((int)log10((N?N:1))+1)+1; int lenght_n = snprintf(NULL, 0, "%d", N);
long len = strlen("readNFiles")+1+lenght_n+1;
char *cmd = malloc(len); char *cmd = malloc(len);
if(!cmd) { if(!cmd) {
perror("malloc"); perror("malloc");
return -1; return -1;
} }
memset(cmd, 0, len); memset(cmd, 0, len);
snprintf(cmd, len, "readNFile|%d", N); snprintf(cmd, len, "readNFiles|%d", N);
len = strnlen(cmd, len); len = strnlen(cmd, len);
// send cmd // send cmd
@ -480,6 +488,14 @@ int readNFiles(int N, const char* dirname) {
free(cmd); free(cmd);
return -1; return -1;
} }
if(openedFiles->print){
for(int i=0;i<res->numfiles;++i) {
fprintf(openedFiles->out, "%d) %s\n", i, res->rf[i].path);
}
fflush(openedFiles->out);
}
// save files to directory // save files to directory
if(storeFilesInDirectory(dirname, res->numfiles, res->rf) == -1) { if(storeFilesInDirectory(dirname, res->numfiles, res->rf) == -1) {
perror("storeFilesindirectory"); perror("storeFilesindirectory");
@ -567,7 +583,8 @@ 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?size:1)+1)+1; int leng_size = snprintf(NULL, 0, "%zu", size);
long len = strlen("writeFile")+1+strlen(pathname)+1+leng_size+1;
char *cmd = calloc(len, sizeof(char)); char *cmd = calloc(len, sizeof(char));
if(!cmd) { if(!cmd) {
perror("calloc"); perror("calloc");
@ -1216,6 +1233,7 @@ int reciveData(response_t *res, int expected) {
perror("calloc"); perror("calloc");
return -1; return -1;
} }
// get files // get files
for(int i=0;i<res->numfiles;++i) { for(int i=0;i<res->numfiles;++i) {
// read path // read path
@ -1241,15 +1259,26 @@ int reciveData(response_t *res, int expected) {
readnres = readn(fd_skt, &res->rf[i].filelen, sizeof(int64_t)); readnres = readn(fd_skt, &res->rf[i].filelen, sizeof(int64_t));
if(readnres<=0) // readn sets errno if(readnres<=0) // readn sets errno
return -1; return -1;
if(res->rf[i].filelen == 0) {
res->rf[i].file = calloc(1, sizeof(char));
if(!res->rf[i].file){
perror("calloc");
return -1;
}
continue;
}
res->rf[i].file = calloc(res->rf[i].filelen, sizeof(char)); res->rf[i].file = calloc(res->rf[i].filelen, sizeof(char));
if(!res->rf[i].file){ if(!res->rf[i].file){
perror("calloc"); perror("calloc");
return -1; return -1;
} }
readnres = readn(fd_skt, res->rf[i].file, res->rf[i].filelen); readnres = readn(fd_skt, res->rf[i].file, res->rf[i].filelen);
if(readnres<=0) // readn sets errno if(readnres<=0) {// readn sets errno
return -1; return -1;
} }
}
return 0; return 0;
} }
if(strncmp(res->ME, MEFP, sizeof(res->ME))==0){ if(strncmp(res->ME, MEFP, sizeof(res->ME))==0){
@ -1292,6 +1321,16 @@ int reciveData(response_t *res, int expected) {
readnres = readn(fd_skt, &res->rf[i].filelen, sizeof(int64_t)); readnres = readn(fd_skt, &res->rf[i].filelen, sizeof(int64_t));
if(readnres<=0) // readn sets errno if(readnres<=0) // readn sets errno
return -1; return -1;
if(res->rf[i].filelen == 0) {
res->rf[i].file = calloc(1, sizeof(char));
if(!res->rf[i].file){
perror("calloc");
return -1;
}
continue;
}
res->rf[i].file = calloc(res->rf[i].filelen, sizeof(char)); res->rf[i].file = calloc(res->rf[i].filelen, sizeof(char));
if(!res->rf[i].file){ if(!res->rf[i].file){
perror("calloc"); perror("calloc");

View File

@ -164,7 +164,7 @@ _sendMF_cleanup:
} }
// invio il messaggio al client e poi n file // invio il messaggio al client e poi n file
void sendMessageFileN(char *m, fileT **f, int n, long fd_c, taglia_t *taglia, char *mlog) { void sendMessageFileN(char *m, fileT **f, int64_t n, long fd_c, taglia_t *taglia, char *mlog) {
if(!f) { if(!f) {
errno = EINVAL; errno = EINVAL;
char errmlog[2*LOGBUFSIZE] = "Errore negli argomenti alla funzione sendMessagefile (fileT == NULL). Messaggio originale:\n\t"; char errmlog[2*LOGBUFSIZE] = "Errore negli argomenti alla funzione sendMessagefile (fileT == NULL). Messaggio originale:\n\t";
@ -360,13 +360,13 @@ void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia){
} }
int oldNum = num; int oldNum = num;
num = (num<=0 || num>getLen(q))? getLen(q) : num; int64_t ntosend = (num<=0 || num>getLen(q))? getLen(q) : num;
fileT **toSend = calloc(num, sizeof(fileT *)); fileT **toSend = calloc(ntosend, sizeof(fileT *));
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFile (n = %d) e' terminata con successo. File inviati:\n", fd_c, num); n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readNFile (n = %d) e' terminata con successo. File inviati:\n", fd_c, num);
// extract n files, we dont check if the client can actually modify // extract n files, we dont check if the client can actually modify
// the files since we add them back immediatly // the files since we add them back immediatly
for(int i=0;i<num;++i) { /* TODO: fix here */ for(int i=0;i<ntosend;++i) {
toSend[i] = dequeue(q); toSend[i] = dequeue(q);
if(!toSend[i]) { if(!toSend[i]) {
n = 0; n = 0;
@ -386,7 +386,7 @@ void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia){
n += snprintf(tmp_buf+n, m-n, "\t%d) \"%s\"\n", i, toSend[i]->filepath); n += snprintf(tmp_buf+n, m-n, "\t%d) \"%s\"\n", i, toSend[i]->filepath);
} }
sendMessageFileN(MEOK, toSend, num, fd_c, taglia, tmp_buf); sendMessageFileN(MEOK, toSend, ntosend, fd_c, taglia, tmp_buf);
free(toSend); free(toSend);
return; return;
} }

View File

@ -1,8 +1,8 @@
#!/bin/sh #!/bin/sh
./client -t 200 -f mysock -w testFiles/1 -D Wdir -W testFiles/1/file1 -r testFiles/1/file1 -d Rdir -R n=3 -p ./client -t 200 -f socket -w testFiles/1 -D Wdir -W testFiles/1/file1 -r testFiles/1/file1 -d Rdir -R n=3 -p
exit 0 exit 0
./client -t 200 -f mysock -w test -D Wdir -W test/filepesante -r test/filepesante -d Rdir -R n=3 -l test/filepesante -u test/filepesante -c test/filepesante -p ./client -t 200 -f socket -w test -D Wdir -W test/filepesante -r test/filepesante -d Rdir -R n=3 -l test/filepesante -u test/filepesante -c test/filepesante -p
exit 0 exit 0

View File

@ -747,24 +747,13 @@ int cmd_R(char *numStr, char *dir, int print) {
if(!numStr) // skips the step of converting n from string to int if(!numStr) // skips the step of converting n from string to int
goto skipGetNumber; goto skipGetNumber;
// we copy numStr because we are nice if(strlen(numStr) < 2)
char *tofree = malloc(strnlen(numStr, MAXARGLENGTH)+1); goto skipGetNumber;
if(!tofree) {
perror("malloc");
return -1;
}
strncpy(tofree, numStr, strnlen(numStr, MAXARGLENGTH));
char *secondArg = tofree; if (numStr[0] == 'n' && numStr[1] == '=') {
strsep_gnu(&secondArg, ","); char *number = &numStr[2];
if (!secondArg) {
n = -1;
} else if (secondArg[0] == 'n' && secondArg[1] == '=') {
char *number = &secondArg[2];
for (int i = 0; i < strlen(number); ++i) { for (int i = 0; i < strlen(number); ++i) {
if (!isdigit(number[i])) { if (!isdigit(number[i])) {
free(tofree);
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
@ -772,13 +761,10 @@ int cmd_R(char *numStr, char *dir, int print) {
n = (int) strtol(number, NULL, 10); n = (int) strtol(number, NULL, 10);
n = (n==0)?-1:n; n = (n==0)?-1:n;
} else { } else {
free(tofree);
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
free(tofree);
skipGetNumber: skipGetNumber:
if (readNFiles(n, dir) == -1) { if (readNFiles(n, dir) == -1) {
return -1; return -1;

View File

@ -67,13 +67,16 @@ int main(int argc, char *argv[]) {
int maxBacklog; CONFGETINT(maxBacklog, config, "socket", "backlog", NULL, 10); int maxBacklog; CONFGETINT(maxBacklog, config, "socket", "backlog", NULL, 10);
ini_free(config); ini_free(config);
queueT *queue = NULL;
taglia_t *taglia = NULL;
sigset_t mask; sigset_t mask;
sigfillset(&mask); sigfillset(&mask);
sigdelset(&mask, SIGPIPE); // tolgo soltanto la sigpipe sigdelset(&mask, SIGPIPE); // tolgo soltanto la sigpipe
if (pthread_sigmask(SIG_SETMASK, &mask, NULL) != 0) { if (pthread_sigmask(SIG_SETMASK, &mask, NULL) != 0) {
fprintf(stderr, "ERROR setting mask\n"); fprintf(stderr, "ERROR setting mask\n");
goto _cleanup; goto _cleanup_beforesigthread;
} }
// ignoro SIGPIPE per evitare di essere terminato da una scrittura su un socket // ignoro SIGPIPE per evitare di essere terminato da una scrittura su un socket
@ -82,18 +85,18 @@ int main(int argc, char *argv[]) {
s.sa_handler = SIG_IGN; s.sa_handler = SIG_IGN;
if ( (sigaction(SIGPIPE,&s,NULL)) == -1 ) { if ( (sigaction(SIGPIPE,&s,NULL)) == -1 ) {
perror("sigaction"); perror("sigaction");
goto _cleanup; goto _cleanup_beforesigthread;
} }
// remove("mysock"); maybe necessary??? // remove("mysock"); maybe necessary???
// creo la struttura per il log // creo la struttura per il log
taglia_t *taglia = taglia_init(logFile, 0); taglia = taglia_init(logFile, 0);
free(logFile); // free del nome del file free(logFile); // free del nome del file
if(taglia==NULL) { if(taglia==NULL) {
perror("taglia_init"); perror("taglia_init");
goto _cleanup; goto _cleanup_beforesigthread;
} }
// risorse per il logfile usate nel main // risorse per il logfile usate nel main
char buf[2048]; char buf[2048];
@ -109,7 +112,7 @@ int main(int argc, char *argv[]) {
} }
if (pipe(request_pipe) == -1) { if (pipe(request_pipe) == -1) {
perror("pipe"); perror("pipe");
goto _cleanup; goto _cleanup_beforesigthread;
} }
@ -171,7 +174,7 @@ int main(int argc, char *argv[]) {
// creo la queue // creo la queue
queueT *queue = createQueue(maxFiles, maxSize); queue = createQueue(maxFiles, maxSize);
// creo la lista dei client in attesa a una lock // creo la lista dei client in attesa a una lock
waiting_t *waiting = NULL; waiting_t *waiting = NULL;
@ -437,6 +440,9 @@ int main(int argc, char *argv[]) {
return 0; return 0;
_cleanup: _cleanup:
pthread_cancel(sighandler_thread);
pthread_join(sighandler_thread, NULL);
_cleanup_beforesigthread:
if(queue) { if(queue) {
destroyQueue(queue); destroyQueue(queue);
} }