comments for client.c
This commit is contained in:
195
src/client.c
195
src/client.c
@ -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
|
||||||
|
if(!print)
|
||||||
printInfo(1, stdout);
|
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,10 +292,10 @@ 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;
|
||||||
@ -322,11 +305,12 @@ int reorderCommandList(cmd_t **l) {
|
|||||||
|
|
||||||
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,10 +335,10 @@ 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;
|
||||||
@ -364,6 +348,7 @@ int reorderCommandList(cmd_t **l) {
|
|||||||
|
|
||||||
prev->arg = t.arg;
|
prev->arg = t.arg;
|
||||||
prev->name = t.name;
|
prev->name = t.name;
|
||||||
|
}
|
||||||
|
|
||||||
if(!tmp->next)
|
if(!tmp->next)
|
||||||
break;
|
break;
|
||||||
@ -422,7 +407,7 @@ int execute(cmd_t *l, int print) {
|
|||||||
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");
|
||||||
@ -734,10 +724,9 @@ int cmd_W(char *filelist, char *Dir, int print) {
|
|||||||
|
|
||||||
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,25 +1023,23 @@ 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");
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (print != 0) {
|
|
||||||
printf("]\n");
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
free(tofree);
|
free(tofree);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user