diff --git a/config.ini b/config.ini index 1e94259..979be50 100644 --- a/config.ini +++ b/config.ini @@ -5,7 +5,7 @@ pending = 20 [files] -MaxFiles = 20 +MaxFiles = 2 MaxSize = 10000000 [log] diff --git a/lib/api/api.c b/lib/api/api.c index f884c73..44bf0d9 100644 --- a/lib/api/api.c +++ b/lib/api/api.c @@ -1259,7 +1259,6 @@ int reciveData(response_t *res, int expected) { readnres = readn(fd_skt, &res->numfiles, sizeof(int)); if(readnres<=0) // readn sets errno return -1; - if(res->rf) free(res->rf); res->rf = calloc(res->numfiles, sizeof(recivedFile_t)); @@ -1267,6 +1266,7 @@ int reciveData(response_t *res, int expected) { perror("calloc"); return -1; } + // get files for(int i=0;inumfiles;++i) { // read path @@ -1350,10 +1350,11 @@ _nofile: // file purged 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); res->rf = calloc(res->numfiles, sizeof(recivedFile_t)); @@ -1386,6 +1387,16 @@ _nofile: readnres = readn(fd_skt, &res->rf[i].filelen, sizeof(int64_t)); if(readnres<=0) // readn sets errno return -1; + + // file has lenght 0 -> allocate memory but dont read + 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"); diff --git a/lib/threadpool/apiFile.c b/lib/threadpool/apiFile.c index b80890e..4eaaf7b 100644 --- a/lib/threadpool/apiFile.c +++ b/lib/threadpool/apiFile.c @@ -96,16 +96,18 @@ int sendFile(fileT *f, long fd_c, taglia_t *taglia) { perror("writen"); return -1; } - if (writen(fd_c, f->data, f->valid) < 0) { - perror("writen"); - return -1; + if(validLength != 0) { // if file has length 0 dont write + if (writen(fd_c, f->data, validLength) < 0) { + perror("writen"); + return -1; + } } char tmp_log[LOGBUFSIZE]; int n = 0; size_t m = sizeof(tmp_log); - n += snprintf(tmp_log+n, m-n, "File \"%s\", di dimensione %"PRId64" Bytes al client %ld .\n", f->filepath, validLength, fd_c); + n += snprintf(tmp_log+n, m-n, "Inviato file \"%s\", di dimensione %"PRId64" Bytes al client %ld .\n", f->filepath, validLength, fd_c); if(taglia_write(taglia, tmp_log) < 0) { perror("taglia_write"); return 1; @@ -132,7 +134,7 @@ void sendMessageFile(char *m, fileT *f, long fd_c, taglia_t *taglia, char *mlog) } int64_t *n = calloc(1, sizeof(*n)); - if(n==NULL) { + if(!n) { perror("calloc"); goto _sendMF_cleanup; } @@ -275,7 +277,7 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) taglia_update(taglia, q, 1); // removed only one file n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una openFile (flags = %x) sul file \"%s\" ha causato una capacity miss. File espulso \"%s\"\n", fd_c, flags, filepath, removed->filepath); sendMessageFile(MEFP, removed, fd_c, taglia, tmp_buf); - free(removed); + destroyFile(removed); return; } diff --git a/src/client.c b/src/client.c index e23db8b..8a9ee58 100644 --- a/src/client.c +++ b/src/client.c @@ -491,7 +491,7 @@ int cmd_w(char *dirname, char *Dir, int print) { int num; // we copy dirname because we are nice - char *tofree = malloc(strnlen(dirname, MAXARGLENGTH)+1); + char *tofree = calloc(strnlen(dirname, MAXARGLENGTH)+1, sizeof(char)); if(!tofree) { perror("malloc"); return -1; @@ -522,7 +522,7 @@ int cmd_w(char *dirname, char *Dir, int print) { } if (print) { - printf("\nw - Scrivo i seguenti file sul server: "); + printf("\nw - Scrivo i seguenti file sul server: \n"); fflush(stdout); } @@ -544,12 +544,22 @@ int cmd_w(char *dirname, char *Dir, int print) { snprintf(tmp, child->fts_namelen + child->fts_pathlen + 2, "%s/%s", child->fts_path, child->fts_name); if(print) { - printf("%s\n", tmp); + printf("%s [", tmp); } // we send the file with the other function but set print to // 0 since we do the printing before - cmd_W(tmp, Dir, 0); + int r = cmd_W(tmp, Dir, 0); + + if(print && !r) { + printf("Esito: ok"); + } else if (print && r) { + printf("Esito: errore"); + } + + if(print) { + printf("]\n"); + } free(tmp); --num; @@ -586,6 +596,7 @@ int cmd_W(char *filelist, char *Dir, int print) { fflush(stdout); } + int r = 0; while ((token = strsep_gnu(&string, ",")) != NULL) { int ok = 1; int opened = 0; @@ -624,10 +635,12 @@ int cmd_W(char *filelist, char *Dir, int print) { printf("]\n"); fflush(stdout); } + if(!ok) + r = -1; } free(tofree); - return 0; + return r; } int cmd_r(char *filelist, char *dir, int print) {