comments for client.c

This commit is contained in:
elvis
2022-05-04 23:42:12 +02:00
parent f64dd54b84
commit 1aa5f2e1d9
2 changed files with 276 additions and 297 deletions

View File

@ -28,247 +28,247 @@
#include "ini.h" #include "ini.h"
struct ini_t { struct ini_t {
char *data; char *data;
char *end; char *end;
}; };
/* Case insensitive string compare */ /* Case insensitive string compare */
static int strcmpci(const char *a, const char *b) { static int strcmpci(const char *a, const char *b) {
for (;;) { for (;;) {
int d = tolower(*a) - tolower(*b); int d = tolower(*a) - tolower(*b);
if (d != 0 || !*a) { if (d != 0 || !*a) {
return d; return d;
}
a++, b++;
} }
a++, b++;
}
} }
/* Returns the next string in the split data */ /* Returns the next string in the split data */
static char* next(ini_t *ini, char *p) { static char* next(ini_t *ini, char *p) {
p += strlen(p); p += strlen(p);
while (p < ini->end && *p == '\0') { while (p < ini->end && *p == '\0') {
p++; p++;
} }
return p; return p;
} }
static void trim_back(ini_t *ini, char *p) { static void trim_back(ini_t *ini, char *p) {
while (p >= ini->data && (*p == ' ' || *p == '\t' || *p == '\r')) { while (p >= ini->data && (*p == ' ' || *p == '\t' || *p == '\r')) {
*p-- = '\0'; *p-- = '\0';
} }
} }
static char* discard_line(ini_t *ini, char *p) { static char* discard_line(ini_t *ini, char *p) {
while (p < ini->end && *p != '\n') { while (p < ini->end && *p != '\n') {
*p++ = '\0'; *p++ = '\0';
} }
return p; return p;
} }
static char *unescape_quoted_value(ini_t *ini, char *p) { static char *unescape_quoted_value(ini_t *ini, char *p) {
/* Use `q` as write-head and `p` as read-head, `p` is always ahead of `q` /* Use `q` as write-head and `p` as read-head, `p` is always ahead of `q`
* as escape sequences are always larger than their resultant data */ * as escape sequences are always larger than their resultant data */
char *q = p; char *q = p;
p++; p++;
while (p < ini->end && *p != '"' && *p != '\r' && *p != '\n') { while (p < ini->end && *p != '"' && *p != '\r' && *p != '\n') {
if (*p == '\\') { if (*p == '\\') {
/* Handle escaped char */ /* Handle escaped char */
p++; p++;
switch (*p) { switch (*p) {
default : *q = *p; break; default : *q = *p; break;
case 'r' : *q = '\r'; break; case 'r' : *q = '\r'; break;
case 'n' : *q = '\n'; break; case 'n' : *q = '\n'; break;
case 't' : *q = '\t'; break; case 't' : *q = '\t'; break;
case '\r' : case '\r' :
case '\n' : case '\n' :
case '\0' : goto end; case '\0' : goto end;
} }
} else { } else {
/* Handle normal char */ /* Handle normal char */
*q = *p; *q = *p;
}
q++, p++;
} }
q++, p++;
}
end: end:
return q; return q;
} }
/* Splits data in place into strings containing section-headers, keys and /* Splits data in place into strings containing section-headers, keys and
* values using one or more '\0' as a delimiter. Unescapes quoted values */ * values using one or more '\0' as a delimiter. Unescapes quoted values */
static void split_data(ini_t *ini) { static void split_data(ini_t *ini) {
char *value_start, *line_start; char *value_start, *line_start;
char *p = ini->data; char *p = ini->data;
while (p < ini->end) { while (p < ini->end) {
switch (*p) { switch (*p) {
case '\r': case '\r':
case '\n': case '\n':
case '\t': case '\t':
case ' ': case ' ':
*p = '\0'; *p = '\0';
/* Fall through */ /* Fall through */
case '\0': case '\0':
p++; p++;
break; break;
case '[': case '[':
p += strcspn(p, "]\n"); p += strcspn(p, "]\n");
*p = '\0'; *p = '\0';
break; break;
case ';': case ';':
p = discard_line(ini, p); p = discard_line(ini, p);
break; break;
default: default:
line_start = p; line_start = p;
p += strcspn(p, "=\n"); p += strcspn(p, "=\n");
/* Is line missing a '='? */ /* Is line missing a '='? */
if (*p != '=') { if (*p != '=') {
p = discard_line(ini, line_start); p = discard_line(ini, line_start);
break; break;
} }
trim_back(ini, p - 1); trim_back(ini, p - 1);
/* Replace '=' and whitespace after it with '\0' */ /* Replace '=' and whitespace after it with '\0' */
do { do {
*p++ = '\0'; *p++ = '\0';
} while (*p == ' ' || *p == '\r' || *p == '\t'); } while (*p == ' ' || *p == '\r' || *p == '\t');
/* Is a value after '=' missing? */ /* Is a value after '=' missing? */
if (*p == '\n' || *p == '\0') { if (*p == '\n' || *p == '\0') {
p = discard_line(ini, line_start); p = discard_line(ini, line_start);
break; break;
} }
if (*p == '"') { if (*p == '"') {
/* Handle quoted string value */ /* Handle quoted string value */
value_start = p; value_start = p;
p = unescape_quoted_value(ini, p); p = unescape_quoted_value(ini, p);
/* Was the string empty? */ /* Was the string empty? */
if (p == value_start) { if (p == value_start) {
p = discard_line(ini, line_start); p = discard_line(ini, line_start);
break; break;
} }
/* Discard the rest of the line after the string value */ /* Discard the rest of the line after the string value */
p = discard_line(ini, p); p = discard_line(ini, p);
} else { } else {
/* Handle normal value */ /* Handle normal value */
p += strcspn(p, "\n"); p += strcspn(p, "\n");
trim_back(ini, p - 1); trim_back(ini, p - 1);
} }
break; break;
}
} }
}
} }
ini_t* ini_load(const char *filename) { ini_t* ini_load(const char *filename) {
ini_t *ini = NULL; ini_t *ini = NULL;
FILE *fp = NULL; FILE *fp = NULL;
int n, sz; int n, sz;
/* Init ini struct */ /* Init ini struct */
ini = malloc(sizeof(*ini)); ini = malloc(sizeof(*ini));
if (!ini) { if (!ini) {
goto fail; goto fail;
} }
memset(ini, 0, sizeof(*ini)); memset(ini, 0, sizeof(*ini));
/* Open file */ /* Open file */
fp = fopen(filename, "rb"); fp = fopen(filename, "rb");
if (!fp) { if (!fp) {
goto fail; goto fail;
} }
/* Get file size */ /* Get file size */
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
sz = ftell(fp); sz = ftell(fp);
rewind(fp); rewind(fp);
/* Load file content into memory, null terminate, init end var */ /* Load file content into memory, null terminate, init end var */
ini->data = malloc(sz + 1); ini->data = malloc(sz + 1);
ini->data[sz] = '\0'; ini->data[sz] = '\0';
ini->end = ini->data + sz; ini->end = ini->data + sz;
n = fread(ini->data, 1, sz, fp); n = fread(ini->data, 1, sz, fp);
if (n != sz) { if (n != sz) {
goto fail; goto fail;
} }
/* Prepare data */ /* Prepare data */
split_data(ini); split_data(ini);
/* Clean up and return */ /* Clean up and return */
fclose(fp); fclose(fp);
return ini; return ini;
fail: fail:
if (fp) fclose(fp); if (fp) fclose(fp);
if (ini) ini_free(ini); if (ini) ini_free(ini);
return NULL; return NULL;
} }
void ini_free(ini_t *ini) { void ini_free(ini_t *ini) {
free(ini->data); free(ini->data);
free(ini); free(ini);
} }
const char* ini_get(ini_t *ini, const char *section, const char *key) { const char* ini_get(ini_t *ini, const char *section, const char *key) {
char *current_section = ""; char *current_section = "";
char *val; char *val;
char *p = ini->data; char *p = ini->data;
if (*p == '\0') { if (*p == '\0') {
p = next(ini, p); p = next(ini, p);
}
while (p < ini->end) {
if (*p == '[') {
/* Handle section */
current_section = p + 1;
} else {
/* Handle key */
val = next(ini, p);
if (!section || !strcmpci(section, current_section)) {
if (!strcmpci(p, key)) {
return val;
}
}
p = val;
} }
p = next(ini, p); while (p < ini->end) {
} if (*p == '[') {
/* Handle section */
current_section = p + 1;
return NULL; } else {
/* Handle key */
val = next(ini, p);
if (!section || !strcmpci(section, current_section)) {
if (!strcmpci(p, key)) {
return val;
}
}
p = val;
}
p = next(ini, p);
}
return NULL;
} }
int ini_sget( int ini_sget(
ini_t *ini, const char *section, const char *key, ini_t *ini, const char *section, const char *key,
const char *scanfmt, void *dst const char *scanfmt, void *dst
) { ) {
const char *val = ini_get(ini, section, key); const char *val = ini_get(ini, section, key);
if (!val) { if (!val) {
return 0; return 0;
} }
if (scanfmt) { if (scanfmt) {
sscanf(val, scanfmt, dst); sscanf(val, scanfmt, dst);
} else { } else {
*((const char**) dst) = val; *((const char**) dst) = val;
} }
return 1; return 1;
} }

View File

@ -1,5 +1,4 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <signal.h> #include <signal.h>
@ -7,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
// fts uses this type but is not defined anywhere
typedef unsigned short u_short; typedef unsigned short u_short;
#include <sys/stat.h> #include <sys/stat.h>
#include <fts.h> #include <fts.h>
@ -19,47 +19,42 @@ typedef unsigned short u_short;
#define UNIX_PATH_MAX 256 #define UNIX_PATH_MAX 256
#define MAXARGLENGTH 256 #define MAXARGLENGTH 256
// path of socket
static char globalSocket[UNIX_PATH_MAX] = ""; static char globalSocket[UNIX_PATH_MAX] = "";
// structure of the command list
// struttura della lista dei comandi
typedef struct cmd_s { typedef struct cmd_s {
char name; // nome del comando char name; // name of the command
char *arg; // (eventuale) argomento del comando char *arg; // argument of the command
struct cmd_s *next; // puntatore al prossimo comando nella lista struct cmd_s *next; // pointer to next element
} cmd_t; } cmd_t;
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// helper functions // helper functions
// libera la memoria della lista dei comandi // frees command list's memory
void destroyCommandList(cmd_t *l); void destroyCommandList(cmd_t *l);
// aggiunge un comando alla lista // adds a command to the list
int addCommand(cmd_t **l, char cmd, char *arg); int addCommand(cmd_t **l, char cmd, char *arg);
// riordina i comandi // reorder the list
int reorderCommandList(cmd_t **l); int reorderCommandList(cmd_t **l);
// esegue tutti i comandi nella lista // execute all commands
int execute(cmd_t *l, int print); int execute(cmd_t *l, int print);
int printList(cmd_t *l); // close connection before the exit
// per chiudere la connessione prima dell'uscita
void cleanup() { void cleanup() {
if (strncmp(globalSocket, "", 2) != 0) { if (strncmp(globalSocket, "", 2) != 0) {
closeConnection(globalSocket); closeConnection(globalSocket);
strncpy(globalSocket, "", 2); strncpy(globalSocket, "", 2);
} }
} }
// compare files // to compare files (fts)
int compare(const FTSENT ** first, const FTSENT ** second) { int compare(const FTSENT ** first, const FTSENT ** second) {
return (strcmp((*first)->fts_name, (*second)->fts_name)); return (strcmp((*first)->fts_name, (*second)->fts_name));
} }
// -h // -h
static void usage(const char *argv0) { static void usage(const char *argv0) {
// TODO change this
printf("Uso: %s\n", argv0); printf("Uso: %s\n", argv0);
printf("-h: stampa il presente messaggio d'aiuto.\n"); printf("-h: stampa il presente messaggio d'aiuto.\n");
printf("-f filename: connettiti al socket AF_UNIX 'filename'.\n"); printf("-f filename: connettiti al socket AF_UNIX 'filename'.\n");
@ -105,7 +100,7 @@ int main(int argc, char* argv[]) {
struct sigaction siga; struct sigaction siga;
// ignoro il segnale SIGPIPE // ignore SIGPIPE
memset(&siga, 0, sizeof(siga)); memset(&siga, 0, sizeof(siga));
siga.sa_handler = SIG_IGN; siga.sa_handler = SIG_IGN;
if (sigaction(SIGPIPE, &siga, NULL) == -1) { if (sigaction(SIGPIPE, &siga, NULL) == -1) {
@ -113,7 +108,7 @@ int main(int argc, char* argv[]) {
return 1; return 1;
} }
// lista dei comandi // list of commands
cmd_t *cmds = NULL; cmd_t *cmds = NULL;
int opt = 0; int opt = 0;
@ -122,13 +117,13 @@ int main(int argc, char* argv[]) {
int print = 0; int print = 0;
char f = 0; char f = 0;
// add args to list // add args to command list
while ((opt = getopt(argc, argv, ":hpf:t:w:W:D:r:R:d:l:u:c:")) != -1) { while ((opt = getopt(argc, argv, ":hpf:t:w:W:D:r:R:d:l:u:c:")) != -1) {
switch (opt) { switch (opt) {
case 'h': // help message case 'h': // help message
usage(argv[0]); usage(argv[0]);
goto _cleanup; goto _cleanup;
case 'f': // socket name case 'f': // name of socket
if(!f) { if(!f) {
if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') { if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') {
fprintf(stderr, "Il comando -f necessita di un argomento.\n"); fprintf(stderr, "Il comando -f necessita di un argomento.\n");
@ -141,10 +136,11 @@ int main(int argc, char* argv[]) {
} }
break; break;
case 'p': // print to stdout case 'p': // print to stdout
printInfo(1, stdout); if(!print)
printInfo(1, stdout);
print|=1; print|=1;
break; break;
case 'w': // send files from folder (n is read after) case 'w': // send file from folder (n is read later)
if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') { if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') {
fprintf(stderr, "Il comando -w necessita di un argomento.\n"); fprintf(stderr, "Il comando -w necessita di un argomento.\n");
goto _cleanup; goto _cleanup;
@ -154,13 +150,13 @@ int main(int argc, char* argv[]) {
addCommand(&cmds, opt, args); addCommand(&cmds, opt, args);
break; break;
case 'W': // files to send separated by ',' case 'W': // files to send separated by ','
case 'D': // directory to store recived files case 'D': // dir to write file into
case 'r': // files to read from server separated by ',' case 'r': // files to read from server separated by ','
case 'd': // directory to store read files case 'd': // dir to write read files into
case 't': // time in ms between requests case 't': // time in ms between requests
case 'l': // file to request lock of separated by ',' case 'l': // file to request lock on separated by ','
case 'u': // file to relese lock of separated by ',' case 'u': // file to relese lock on separated by ','
case 'c': // files to remove separated by ',' case 'c': // file to delete separated by ','
if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') { if (optarg && strnlen(optarg, MAXARGLENGTH) > 0 && optarg[0] == '-') {
fprintf(stderr, "Il comando -%c necessita di un argomento.\n", optopt); fprintf(stderr, "Il comando -%c necessita di un argomento.\n", optopt);
goto _cleanup; goto _cleanup;
@ -228,7 +224,7 @@ void destroyCommandList(cmd_t *l) {
} }
cmd_t *tmp; cmd_t *tmp;
// scorro tutta la lista e libero la memoria // visit every node and free memory
while (l) { while (l) {
tmp = l; tmp = l;
@ -239,20 +235,6 @@ void destroyCommandList(cmd_t *l) {
} }
} }
int printList(cmd_t *l) {
if(!l) {
errno = EINVAL;
return -1;
}
cmd_t *tmp = l;
while(tmp) {
fprintf(stdout, "%c with args: [%s]\n", tmp->name, tmp->arg);
tmp = tmp->next;
}
return 0;
}
int addCommand(cmd_t **l, char cmd, char *arg) { int addCommand(cmd_t **l, char cmd, char *arg) {
if(!l) { if(!l) {
errno = EINVAL; errno = EINVAL;
@ -277,7 +259,7 @@ int addCommand(cmd_t **l, char cmd, char *arg) {
} }
new->next = NULL; new->next = NULL;
// se lista vuota aggiungo in cima, altrimenti scorro la lista // if empty list -> add to the top
if (*l == NULL) { if (*l == NULL) {
*l = new; *l = new;
return 0; return 0;
@ -293,6 +275,7 @@ int addCommand(cmd_t **l, char cmd, char *arg) {
} }
// inverts the order of -D and -d angainst -w, -W or -r, -R // inverts the order of -D and -d angainst -w, -W or -r, -R
// and adds -D/-d /dev/null after
int reorderCommandList(cmd_t **l) { int reorderCommandList(cmd_t **l) {
if(!l) { if(!l) {
errno = EINVAL; errno = EINVAL;
@ -309,24 +292,25 @@ int reorderCommandList(cmd_t **l) {
switch (tmp->name) { switch (tmp->name) {
case 'd': { case 'd': {
if(prev == NULL || (prev->name != 'r' && prev->name != 'R')) { if(prev == NULL || (prev->name != 'r' && prev->name != 'R')) {
fprintf(stdout, "\nError: -d has no -r or -R matching before\n"); fprintf(stderr, "Il comando -d necessita -r o -R prima\n");
return -1; return -1;
} }
// invert tmp and prev { // invert tmp and prev
cmd_t t; cmd_t t;
t.arg = tmp->arg; t.arg = tmp->arg;
t.name = tmp->name; t.name = tmp->name;
tmp->arg = prev->arg; tmp->arg = prev->arg;
tmp->name = prev->name; tmp->name = prev->name;
prev->arg = t.arg; prev->arg = t.arg;
prev->name = t.name; prev->name = t.name;
}
if(!tmp->next) if(!tmp->next)
break; break;
// add redirect to dev/null for next command if it's r or R // add redirect to /dev/null for next command if it's r or R
if(tmp->next->name == 'r' || tmp->next->name == 'R') { if(tmp->next->name == 'r' || tmp->next->name == 'R') {
cmd_t *new = calloc(1, sizeof(*new)); cmd_t *new = calloc(1, sizeof(*new));
if(!new){ if(!new){
@ -351,19 +335,20 @@ int reorderCommandList(cmd_t **l) {
} }
case 'D': { case 'D': {
if(prev == NULL || (prev->name != 'w' && prev->name != 'W')) { if(prev == NULL || (prev->name != 'w' && prev->name != 'W')) {
fprintf(stdout, "\nError: -D has no -w or -W matching before\n"); fprintf(stderr, "Il comando -D necessita -w o -W prima\n");
return -1; return -1;
} }
// invert tmp and prev { // invert tmp and prev
cmd_t t; cmd_t t;
t.arg = tmp->arg; t.arg = tmp->arg;
t.name = tmp->name; t.name = tmp->name;
tmp->arg = prev->arg; tmp->arg = prev->arg;
tmp->name = prev->name; tmp->name = prev->name;
prev->arg = t.arg; prev->arg = t.arg;
prev->name = t.name; prev->name = t.name;
}
if(!tmp->next) if(!tmp->next)
break; break;
@ -415,14 +400,14 @@ int execute(cmd_t *l, int print) {
// loop that serches for -t // loop that serches for -t
while(tmp) { while(tmp) {
switch(tmp->name) { switch(tmp->name) {
case 't': {// time in ms between requests case 't': { // time in ms between requests
long num; long num;
num = strtol(tmp->arg, NULL, 10); num = strtol(tmp->arg, NULL, 10);
if(num==0 && errno==EINVAL) { if(num==0 && errno==EINVAL) {
perror("Invalid time specified after -t"); perror("Invalid time specified after -t");
return -1; return -1;
} }
// milliseconds converted to nanoseconds // milliseconds to nanoseconds
interval.tv_nsec = (num%1000) * 1000000; interval.tv_nsec = (num%1000) * 1000000;
// seconds // seconds
interval.tv_sec = (num/1000); interval.tv_sec = (num/1000);
@ -481,6 +466,8 @@ int execute(cmd_t *l, int print) {
// loop that checks for consistencies: // loop that checks for consistencies:
// 1) -D with no -w or -W after // 1) -D with no -w or -W after
// 2) -d with no -r or -R after // 2) -d with no -r or -R after
// reduntant since we also reorder before but this function could be used
// alone so we have to check
tmp = l; tmp = l;
int unmachedD = 0; int unmachedD = 0;
int unmachedd = 0; int unmachedd = 0;
@ -488,7 +475,7 @@ int execute(cmd_t *l, int print) {
switch (tmp->name) { switch (tmp->name) {
case 'D': case 'D':
if(unmachedD) { if(unmachedD) {
fprintf(stdout, "\nError: -D has no -w or -W matching after\n"); fprintf(stderr, "Il comando -D necessita -w o -W dopo\n");
return 0; return 0;
} }
unmachedD = 1; unmachedD = 1;
@ -499,7 +486,7 @@ int execute(cmd_t *l, int print) {
break; break;
case 'd': case 'd':
if(unmachedd) { if(unmachedd) {
fprintf(stdout, "\nError: -d has no -r or -R matching after\n"); fprintf(stderr, "Il comando -d necessita -r o -R dopo\n");
return 0; return 0;
} }
unmachedd = 1; unmachedd = 1;
@ -514,11 +501,11 @@ int execute(cmd_t *l, int print) {
tmp = tmp->next; tmp = tmp->next;
} }
if(unmachedD) { if(unmachedD) {
fprintf(stdout, "\nError: -D has no -w or -W matching after\n"); fprintf(stderr, "Il comando -D necessita -w o -W dopo\n");
return 0; return 0;
} }
if(unmachedd) { if(unmachedd) {
fprintf(stdout, "\nError: -d has no -r or -R matching after\n"); fprintf(stderr, "Il comando -d necessita -r o -R dopo\n");
return 0; return 0;
} }
fflush(stdout); fflush(stdout);
@ -547,7 +534,7 @@ int execute(cmd_t *l, int print) {
perror("-D"); perror("-D");
} }
if (print) if (print)
printf("\nD - Cartella per le scritture: %s\tEsito: ok\n", Dir); printf("D - Cartella per le scritture: %s [Esito: ok]\n", Dir);
break; break;
case 'r': case 'r':
cmd_r(tmp->arg, dir, print); cmd_r(tmp->arg, dir, print);
@ -566,7 +553,7 @@ int execute(cmd_t *l, int print) {
perror("-d"); perror("-d");
} }
if(print) if(print)
printf("\nd - Cartella per le letture: %s\tEsito: ok\n", dir); printf("d - Cartella per le letture: %s [Esito: ok]\n", dir);
break; break;
case 'l': case 'l':
cmd_l(tmp->arg, print); cmd_l(tmp->arg, print);
@ -602,6 +589,8 @@ int execute(cmd_t *l, int print) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// funzioni relative ai comandi // funzioni relative ai comandi
// -f
int cmd_f(char *socket) { int cmd_f(char *socket) {
if(!socket) { if(!socket) {
errno = EINVAL; errno = EINVAL;
@ -618,6 +607,7 @@ int cmd_f(char *socket) {
return 0; return 0;
} }
// -w
int cmd_w(char *dirname, char *Dir, int print) { int cmd_w(char *dirname, char *Dir, int print) {
if(!dirname) { if(!dirname) {
errno = EINVAL; errno = EINVAL;
@ -625,7 +615,7 @@ int cmd_w(char *dirname, char *Dir, int print) {
} }
int num; int num;
// we copy dirname because we are nice // copy dirname because we modify it after
char *tofree = calloc(strnlen(dirname, MAXARGLENGTH)+1, sizeof(char)); char *tofree = calloc(strnlen(dirname, MAXARGLENGTH)+1, sizeof(char));
if(!tofree) { if(!tofree) {
perror("malloc"); perror("malloc");
@ -656,12 +646,11 @@ int cmd_w(char *dirname, char *Dir, int print) {
return -1; return -1;
} }
if (print) { if (print)
printf("\nw - Scrivo i seguenti file sul server: \n"); printf("w - Scrivo i seguenti file sul server:\n");
fflush(stdout);
}
// we use fts to traverse all files in the directory recursively // we use fts to traverse all files in the directory recursively
// and write them to a list so that we can open them later
FTS *fhandle = NULL; FTS *fhandle = NULL;
FTSENT *child = NULL; FTSENT *child = NULL;
FTSENT *parent = NULL; FTSENT *parent = NULL;
@ -694,7 +683,7 @@ int cmd_w(char *dirname, char *Dir, int print) {
if(print) { if(print) {
printf("%s [", listFiles[i]); printf("%s [", listFiles[i]);
} }
// we send the file with the other function but set print to 0 // we send the file with cmd_W but set print to 0
int r = cmd_W(listFiles[i], Dir, 0); int r = cmd_W(listFiles[i], Dir, 0);
if(print && !r) { if(print && !r) {
printf("Esito: ok"); printf("Esito: ok");
@ -714,13 +703,14 @@ int cmd_w(char *dirname, char *Dir, int print) {
return 0; return 0;
} }
// -W
int cmd_W(char *filelist, char *Dir, int print) { int cmd_W(char *filelist, char *Dir, int print) {
if(!filelist) { if(!filelist) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
// we copy filelist because we are nice // we copy filelist because we modify it later
char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1);
if(!tofree) { if(!tofree) {
perror("malloc"); perror("malloc");
@ -733,11 +723,10 @@ int cmd_W(char *filelist, char *Dir, int print) {
char *string = tofree; char *string = tofree;
if (print == 1) { if (print == 1) {
printf("W - Scrivo i seguenti file sul server: \n"); printf("W - Scrivo i seguenti file sul server:\n");
fflush(stdout);
} }
int r = 0; int r = 0; // return value
while ((token = strsep_gnu(&string, ",")) != NULL) { while ((token = strsep_gnu(&string, ",")) != NULL) {
int ok = 1; int ok = 1;
int opened = 0; int opened = 0;
@ -746,20 +735,21 @@ int cmd_W(char *filelist, char *Dir, int print) {
fflush(stdout); fflush(stdout);
} }
// creo il file in modalita' locked // create the file locked
if (openFile(token, O_CREATE | O_LOCK) == -1) { if (openFile(token, O_CREATE | O_LOCK) == -1) {
ok = 0; ok = 0;
} else { // creato con successo } else { // success
opened = 1; opened = 1;
} }
// write to the file
if (opened && (writeFile(token, Dir) == -1)) { if (opened && (writeFile(token, Dir) == -1)) {
ok = 0; ok = 0;
} }
if (opened && !ok) { // errore precedente -> elimino il file vuoto if (opened && !ok) { // previous error -> delete the empty file
removeFile(token); removeFile(token);
} else if (opened && (closeFile(token) == -1)) { // chiudo il file } else if (opened && (closeFile(token) == -1)) { // close the file
ok = 0; ok = 0;
} }
@ -767,13 +757,12 @@ int cmd_W(char *filelist, char *Dir, int print) {
printf("Esito: "); printf("Esito: ");
if (ok) if (ok)
printf("ok"); printf("ok]\n");
else { else {
printf("errore"); printf("errore]\n");
perror("-w"); perror("-W");
} }
printf("]\n");
fflush(stdout); fflush(stdout);
} }
if(!ok) if(!ok)
@ -784,13 +773,14 @@ int cmd_W(char *filelist, char *Dir, int print) {
return r; return r;
} }
// -r
int cmd_r(char *filelist, char *dir, int print) { int cmd_r(char *filelist, char *dir, int print) {
if (!filelist) { if (!filelist) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
// we copy filelist because we are nice // we copy filelist because we modify it later
char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1); char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1);
memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1); memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1);
if(!tofree) { if(!tofree) {
@ -813,9 +803,9 @@ int cmd_r(char *filelist, char *dir, int print) {
if (print != 0) { if (print != 0) {
printf("%s ", token); printf("%s ", token);
fflush(stdout);
} }
// open the file
if (openFile(token, 0) == -1) { if (openFile(token, 0) == -1) {
ok = 0; ok = 0;
} else { } else {
@ -828,8 +818,7 @@ int cmd_r(char *filelist, char *dir, int print) {
printInfo(0, stdout); printInfo(0, stdout);
// read the content of the file // read the content of the file
if (ok && readFile(token, &buf, &size) == -1) { if (ok && readFile(token, &buf, &size) == -1) {
fprintf(stdout, "\nreadFile\n"); fprintf(stderr, "Errore durante la readFile\n");
fflush(stdout);
ok = 0; ok = 0;
} }
if (print) { if (print) {
@ -845,22 +834,18 @@ int cmd_r(char *filelist, char *dir, int print) {
// close the file // close the file
if (opened && closeFile(token) == -1) { if (opened && closeFile(token) == -1) {
fprintf(stdout, "\ncloseFile\n"); fprintf(stderr, "Errore durante la closeFile\n");
fflush(stdout);
ok = 0; ok = 0;
} }
if (print) { if (print) {
printf("[Esito: "); printf("[Esito: ");
if (ok) { if (ok) {
printf("ok"); printf("ok]\n");
} else { } else {
printf("errore"); printf("errore]\n");
perror("-r"); perror("-r");
} }
}
if (print) {
printf("]\n");
fflush(stdout); fflush(stdout);
} }
} }
@ -873,10 +858,10 @@ int cmd_r(char *filelist, char *dir, int print) {
printf("I file letti non sono stati memorizzati su disco.\n"); printf("I file letti non sono stati memorizzati su disco.\n");
fflush(stdout); fflush(stdout);
} }
return 0; return 0;
} }
// -R
int cmd_R(char *numStr, char *dir, int print) { int cmd_R(char *numStr, char *dir, int print) {
if (print) { if (print) {
printf("R - Leggo i seguenti file dal server:\n"); printf("R - Leggo i seguenti file dal server:\n");
@ -884,7 +869,6 @@ int cmd_R(char *numStr, char *dir, int print) {
} }
int n = -1; int n = -1;
if(!numStr) // skips the step of converting n from string to int if(!numStr) // skips the step of converting n from string to int
goto skipGetNumber; goto skipGetNumber;
@ -920,13 +904,14 @@ skipGetNumber:
return 0; return 0;
} }
// -l
int cmd_l(char *filelist, int print) { int cmd_l(char *filelist, int print) {
if (!filelist) { if (!filelist) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
// we copy filelist because we are nice // we copy filelist because we modify it later
char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char)); char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char));
if(!tofree) { if(!tofree) {
perror("malloc"); perror("malloc");
@ -950,15 +935,12 @@ int cmd_l(char *filelist, int print) {
if (lockFile(token) == -1) { if (lockFile(token) == -1) {
if (print != 0) { if (print != 0) {
printf("errore"); printf("errore]\n");
fflush(stdout);
perror("-l"); perror("-l");
} }
} else if(print != 0) { } else if(print != 0) {
printf("ok"); printf("ok]\n");
}
if (print != 0) {
printf("]\n");
fflush(stdout); fflush(stdout);
} }
} }
@ -966,13 +948,14 @@ int cmd_l(char *filelist, int print) {
return 0; return 0;
} }
// -u
int cmd_u(char *filelist, int print) { int cmd_u(char *filelist, int print) {
if (!filelist) { if (!filelist) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
// we copy filelist because we are nice // we copy filelist because we modify it later
char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char)); char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char));
if(!tofree) { if(!tofree) {
perror("calloc"); perror("calloc");
@ -996,15 +979,12 @@ int cmd_u(char *filelist, int print) {
if (unlockFile(token) == -1) { if (unlockFile(token) == -1) {
if (print != 0) { if (print != 0) {
printf("errore"); printf("errore]\n");
fflush(stdout);
perror("-u"); perror("-u");
} }
} else if(print != 0) { } else if(print != 0) {
printf("ok"); printf("ok]\n");
}
if (print != 0) {
printf("]\n");
fflush(stdout); fflush(stdout);
} }
} }
@ -1012,13 +992,14 @@ int cmd_u(char *filelist, int print) {
return 0; return 0;
} }
// -c
int cmd_c(char *filelist, int print) { int cmd_c(char *filelist, int print) {
if (!filelist) { if (!filelist) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
// we copy filelist because we are nice // we copy filelist because we modify it later
char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char)); char *tofree = calloc(strnlen(filelist, MAXARGLENGTH)+1, sizeof(char));
if(!tofree) { if(!tofree) {
perror("calloc"); perror("calloc");
@ -1042,24 +1023,22 @@ int cmd_c(char *filelist, int print) {
if (lockFile(token) == -1) { if (lockFile(token) == -1) {
if (print) { if (print) {
printf("errore"); printf("errore]\n");
fflush(stdout);
perror("-c"); perror("-c");
} }
} else { } else {
if (removeFile(token) == -1) { if (removeFile(token) == -1) {
if (print) { if (print) {
printf("errore"); printf("errore]\n");
fflush(stdout);
perror("-c"); perror("-c");
} }
} else if (print) { } else if (print) {
printf("ok"); printf("ok]\n");
fflush(stdout);
} }
} }
if (print != 0) {
printf("]\n");
fflush(stdout);
}
} }
free(tofree); free(tofree);
return 0; return 0;