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) {
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;
return -1;
}
@ -201,7 +207,8 @@ int openFile(const char* pathname, int 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);
if(!cmd) {
perror("malloc");
@ -436,15 +443,16 @@ int readNFiles(int N, const char* dirname) {
return -1;
}
// invio al server la stringa "readNFile|$(N)"
long len = strlen("readNFile")+1+((int)log10((N?N:1))+1)+1;
// invio al server la stringa "readNFiles|$(N)"
int lenght_n = snprintf(NULL, 0, "%d", N);
long len = strlen("readNFiles")+1+lenght_n+1;
char *cmd = malloc(len);
if(!cmd) {
perror("malloc");
return -1;
}
memset(cmd, 0, len);
snprintf(cmd, len, "readNFile|%d", N);
snprintf(cmd, len, "readNFiles|%d", N);
len = strnlen(cmd, len);
// send cmd
@ -480,6 +488,14 @@ int readNFiles(int N, const char* dirname) {
free(cmd);
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
if(storeFilesInDirectory(dirname, res->numfiles, res->rf) == -1) {
perror("storeFilesindirectory");
@ -567,7 +583,8 @@ 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?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));
if(!cmd) {
perror("calloc");
@ -1216,6 +1233,7 @@ int reciveData(response_t *res, int expected) {
perror("calloc");
return -1;
}
// get files
for(int i=0;i<res->numfiles;++i) {
// read path
@ -1241,15 +1259,26 @@ int reciveData(response_t *res, int expected) {
readnres = readn(fd_skt, &res->rf[i].filelen, sizeof(int64_t));
if(readnres<=0) // readn sets errno
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));
if(!res->rf[i].file){
perror("calloc");
return -1;
}
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 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));
if(readnres<=0) // readn sets errno
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));
if(!res->rf[i].file){
perror("calloc");

View File

@ -164,7 +164,7 @@ _sendMF_cleanup:
}
// 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) {
errno = EINVAL;
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;
num = (num<=0 || num>getLen(q))? getLen(q) : num;
fileT **toSend = calloc(num, sizeof(fileT *));
int64_t ntosend = (num<=0 || num>getLen(q))? getLen(q) : num;
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);
// extract n files, we dont check if the client can actually modify
// 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);
if(!toSend[i]) {
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);
}
sendMessageFileN(MEOK, toSend, num, fd_c, taglia, tmp_buf);
sendMessageFileN(MEOK, toSend, ntosend, fd_c, taglia, tmp_buf);
free(toSend);
return;
}

View File

@ -1,8 +1,8 @@
#!/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
./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

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

View File

@ -67,13 +67,16 @@ int main(int argc, char *argv[]) {
int maxBacklog; CONFGETINT(maxBacklog, config, "socket", "backlog", NULL, 10);
ini_free(config);
queueT *queue = NULL;
taglia_t *taglia = NULL;
sigset_t mask;
sigfillset(&mask);
sigdelset(&mask, SIGPIPE); // tolgo soltanto la sigpipe
if (pthread_sigmask(SIG_SETMASK, &mask, NULL) != 0) {
fprintf(stderr, "ERROR setting mask\n");
goto _cleanup;
goto _cleanup_beforesigthread;
}
// 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;
if ( (sigaction(SIGPIPE,&s,NULL)) == -1 ) {
perror("sigaction");
goto _cleanup;
goto _cleanup_beforesigthread;
}
// remove("mysock"); maybe necessary???
// 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
if(taglia==NULL) {
perror("taglia_init");
goto _cleanup;
goto _cleanup_beforesigthread;
}
// risorse per il logfile usate nel main
char buf[2048];
@ -109,7 +112,7 @@ int main(int argc, char *argv[]) {
}
if (pipe(request_pipe) == -1) {
perror("pipe");
goto _cleanup;
goto _cleanup_beforesigthread;
}
@ -171,7 +174,7 @@ int main(int argc, char *argv[]) {
// creo la queue
queueT *queue = createQueue(maxFiles, maxSize);
queue = createQueue(maxFiles, maxSize);
// creo la lista dei client in attesa a una lock
waiting_t *waiting = NULL;
@ -437,6 +440,9 @@ int main(int argc, char *argv[]) {
return 0;
_cleanup:
pthread_cancel(sighandler_thread);
pthread_join(sighandler_thread, NULL);
_cleanup_beforesigthread:
if(queue) {
destroyQueue(queue);
}