From 4229c625bfb7751532f5705774c4544276b088a1 Mon Sep 17 00:00:00 2001 From: elvis Date: Wed, 20 Apr 2022 18:25:55 +0200 Subject: [PATCH] Fixed bug in serverworker, added -w directory traversal --- src/client.c | 111 ++++++++++++++++++++++++++++++++++++--------- src/serverWorker.c | 1 - 2 files changed, 89 insertions(+), 23 deletions(-) diff --git a/src/client.c b/src/client.c index 2be4b31..8d0336b 100644 --- a/src/client.c +++ b/src/client.c @@ -1,15 +1,17 @@ +#include +#include +#include #include #include #include #include -#include -#include -#include -#include #include -#include +#include +#include +#include #include +#include #define UNIX_PATH_MAX 256 #define MAXARGLENGTH 256 @@ -25,14 +27,6 @@ typedef struct cmd_s { struct cmd_s *next; // puntatore al prossimo comando nella lista } cmd_t; -// struttura dati per il comando -w -typedef struct cmdw_s { - char *dir; - int print; - int tot; - int curn; -} cmdw_t; - // ----------------------------------------------------------------------------- // helper functions @@ -87,6 +81,12 @@ int cmd_u(char *filelist, int print); // -c int cmd_c(char *filelist, int print); +// compare files +int compare(const FTSENT ** first, const FTSENT ** second) { + return (strcmp((*first)->fts_name, (*second)->fts_name)); +} + + // ----------------------------------------------------------------------------- // MAIN int main(int argc, char* argv[]) { @@ -110,8 +110,6 @@ int main(int argc, char* argv[]) { // lista dei comandi cmd_t *cmds = NULL; cmds = calloc(1, sizeof(cmd_t)); - // struttra per -w - cmdw_t *cmdw = NULL; int opt = 0; char args[MAXARGLENGTH]; @@ -146,13 +144,6 @@ int main(int argc, char* argv[]) { fprintf(stderr, "Il comando -w necessita di un argomento.\n"); goto _cleanup; } - if(!cmdw) { - cmdw = calloc(1, sizeof(cmdw_t)); - if(!cmdw) { - perror("calloc"); - goto _cleanup; - } - } memset(args, 0, MAXARGLENGTH); strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1); addCommand(&cmds, opt, args); @@ -486,10 +477,86 @@ int cmd_w(char *dirname, char *Dir, int print) { errno = EINVAL; return -1; } + int num; + + // we copy dirname because we are nice + char *tofree = malloc(strnlen(dirname, MAXARGLENGTH)+1); + if(!tofree) { + perror("malloc"); + return -1; + } + strncpy(tofree, dirname, strnlen(dirname, MAXARGLENGTH)); + + char *firstArg; + char *secondArg = tofree; + firstArg = strsep_gnu(&secondArg, ","); // secondArg has the number of files + + if (!secondArg) { + num = -1; + } else if (secondArg[0] == 'n' && secondArg[1] == '=') { + char *number = &secondArg[2]; + for (int i = 0; i < strlen(number); ++i) { + if (!isdigit(number[i])) { + free(tofree); + errno = EINVAL; + return -1; + } + } + num = (int) strtol(number, NULL, 10); + num = (num==0)?-1:num; + } else { + free(tofree); + errno = EINVAL; + return -1; + } + + if (print) { + printf("\nw - Scrivo i seguenti file sul server:"); + fflush(stdout); + } + + // we use fts to traverse all files in the directory recursively + FTS *fhandle = NULL; + FTSENT *child = NULL; + FTSENT *parent = NULL; + + fhandle = fts_open(&firstArg, FTS_COMFOLLOW, &compare); + + if(fhandle != NULL) { + // we check for num == 0 so that -1 yields all files in folder + while (num!=0 && (parent = fts_read(fhandle)) != NULL) { + child = fts_children(fhandle, 0); + + while (num!=0 && (child != NULL)) { // for all children in folder + if(child->fts_info == FTS_F) { // if child is a file + char *tmp = malloc(child->fts_namelen + child->fts_pathlen + 2); + snprintf(tmp, child->fts_namelen + child->fts_pathlen + 2, "%s/%s", child->fts_path, child->fts_name); + + if(print) { + printf("%s\n", 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); + + free(tmp); + --num; + } + child = child->fts_link; + } + } + fts_close(fhandle); + } + free(tofree); return 0; } int cmd_W(char *filelist, char *Dir, int print) { + + + + return 0; } diff --git a/src/serverWorker.c b/src/serverWorker.c index b071811..288f965 100644 --- a/src/serverWorker.c +++ b/src/serverWorker.c @@ -146,7 +146,6 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p return -1; } strncpy(string, command, len-1); // strlcpy is only bsd :( - string[len-1] = '\0'; char *token = NULL; char *token2 = NULL;