Fixed bug in serverworker, added -w directory traversal
This commit is contained in:
111
src/client.c
111
src/client.c
@ -1,15 +1,17 @@
|
|||||||
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <fts.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fts.h>
|
#include <sys/types.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <api.h>
|
#include <api.h>
|
||||||
|
#include <strsep_gnu.h>
|
||||||
|
|
||||||
#define UNIX_PATH_MAX 256
|
#define UNIX_PATH_MAX 256
|
||||||
#define MAXARGLENGTH 256
|
#define MAXARGLENGTH 256
|
||||||
@ -25,14 +27,6 @@ typedef struct cmd_s {
|
|||||||
struct cmd_s *next; // puntatore al prossimo comando nella lista
|
struct cmd_s *next; // puntatore al prossimo comando nella lista
|
||||||
} cmd_t;
|
} cmd_t;
|
||||||
|
|
||||||
// struttura dati per il comando -w
|
|
||||||
typedef struct cmdw_s {
|
|
||||||
char *dir;
|
|
||||||
int print;
|
|
||||||
int tot;
|
|
||||||
int curn;
|
|
||||||
} cmdw_t;
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// helper functions
|
// helper functions
|
||||||
|
|
||||||
@ -87,6 +81,12 @@ int cmd_u(char *filelist, int print);
|
|||||||
// -c
|
// -c
|
||||||
int cmd_c(char *filelist, int print);
|
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
|
// MAIN
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
@ -110,8 +110,6 @@ int main(int argc, char* argv[]) {
|
|||||||
// lista dei comandi
|
// lista dei comandi
|
||||||
cmd_t *cmds = NULL;
|
cmd_t *cmds = NULL;
|
||||||
cmds = calloc(1, sizeof(cmd_t));
|
cmds = calloc(1, sizeof(cmd_t));
|
||||||
// struttra per -w
|
|
||||||
cmdw_t *cmdw = NULL;
|
|
||||||
|
|
||||||
int opt = 0;
|
int opt = 0;
|
||||||
char args[MAXARGLENGTH];
|
char args[MAXARGLENGTH];
|
||||||
@ -146,13 +144,6 @@ int main(int argc, char* argv[]) {
|
|||||||
fprintf(stderr, "Il comando -w necessita di un argomento.\n");
|
fprintf(stderr, "Il comando -w necessita di un argomento.\n");
|
||||||
goto _cleanup;
|
goto _cleanup;
|
||||||
}
|
}
|
||||||
if(!cmdw) {
|
|
||||||
cmdw = calloc(1, sizeof(cmdw_t));
|
|
||||||
if(!cmdw) {
|
|
||||||
perror("calloc");
|
|
||||||
goto _cleanup;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
memset(args, 0, MAXARGLENGTH);
|
memset(args, 0, MAXARGLENGTH);
|
||||||
strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1);
|
strncpy(args, optarg, strnlen(optarg, MAXARGLENGTH-1)+1);
|
||||||
addCommand(&cmds, opt, args);
|
addCommand(&cmds, opt, args);
|
||||||
@ -486,10 +477,86 @@ int cmd_w(char *dirname, char *Dir, int print) {
|
|||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_W(char *filelist, char *Dir, int print) {
|
int cmd_W(char *filelist, char *Dir, int print) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -146,7 +146,6 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
strncpy(string, command, len-1); // strlcpy is only bsd :(
|
strncpy(string, command, len-1); // strlcpy is only bsd :(
|
||||||
string[len-1] = '\0';
|
|
||||||
|
|
||||||
char *token = NULL;
|
char *token = NULL;
|
||||||
char *token2 = NULL;
|
char *token2 = NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user