From 72cb3ec3ea41ffe18ae86df0197418bdddd044c2 Mon Sep 17 00:00:00 2001 From: elvis Date: Wed, 20 Apr 2022 22:34:06 +0200 Subject: [PATCH] Logic to -w --- lib/api/api.h | 3 +++ src/client.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/lib/api/api.h b/lib/api/api.h index 5266956..4e8e652 100644 --- a/lib/api/api.h +++ b/lib/api/api.h @@ -4,6 +4,9 @@ #include +#define O_CREATE 1 +#define O_LOCK 2 + // struttura dati per la lista dei file aperti typedef struct openFile_s { char *filename; // nome del file diff --git a/src/client.c b/src/client.c index 8d0336b..85ed4af 100644 --- a/src/client.c +++ b/src/client.c @@ -553,10 +553,68 @@ int cmd_w(char *dirname, char *Dir, int print) { } int cmd_W(char *filelist, char *Dir, int print) { + if(!filelist) { + errno = EINVAL; + return -1; + } + // we copy filelist because we are nice + char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); + if(!tofree) { + perror("malloc"); + return -1; + } + strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)); + char *token; + char *string = tofree; + if (print == 1) { + printf("W - Scrivo i seguenti file sul server:\n"); + fflush(stdout); + } + while ((token = strsep(&string, ",")) != NULL) { + int ok = 1; + int opened = 0; + if (print == 1) { + printf("%s\n", token); + fflush(stdout); + } + + // creo il file in modalita' locked + if (openFile(token, O_CREATE | O_LOCK) == -1) { + ok = 0; + } else { // creato con successo + opened = 1; + } + + if (opened && writeFile(token, Dir) == -1) { + ok = 0; + } + + if (opened && !ok) { // errore precedente -> elimino il file vuoto + removeFile(token); + } else if (opened && closeFile(token) == -1) { // chiudo il file + ok = 0; + } + + if (print != 0) { + printf("Esito: "); + + if (ok) + printf("ok"); + else { + printf("errore"); + perror("-w"); + } + + printf("\n"); + fflush(stdout); + } + } + + free(tofree); return 0; }