working -R
This commit is contained in:
@ -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,14 +1259,25 @@ 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;
|
||||
}
|
||||
@ -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");
|
||||
|
||||
Reference in New Issue
Block a user