From fdb98cec53c3680869a2fcf2c10b3cd807c8bcee Mon Sep 17 00:00:00 2001 From: elvis Date: Thu, 21 Apr 2022 19:56:50 +0200 Subject: [PATCH] closeConnection --- lib/api/api.c | 169 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 143 insertions(+), 26 deletions(-) diff --git a/lib/api/api.c b/lib/api/api.c index f292289..465fc35 100644 --- a/lib/api/api.c +++ b/lib/api/api.c @@ -11,17 +11,33 @@ #define SOCKNAMEMAX 256 // should be at least 108 +// ----------------------------------------------------------------------------- +/* structs */ +typedef struct files_s { + char *filename; + struct files_s *next; +} files_t; + +typedef struct openfiles_s { + int numOfFiles; + files_t *f; + char *wDir; // for files sent from the server after openFile + char *rDir; // for files read from the server +} openfiles_t; + // ----------------------------------------------------------------------------- /* variabili globali */ -int fd_skt; // descriptor of the socket -char socketName[SOCKNAMEMAX] = ""; // name of the socket - - - +static int fd_skt; // descriptor of the socket +static char socketName[SOCKNAMEMAX] = ""; // name of the socket +static openfiles_t *openedFiles = NULL; // ----------------------------------------------------------------------------- /* funzioni ausiliarie */ +// closes every file opened by the client +int closeEveryFile(); +// closes and frees memory +int removeOpenFile(const char *pathname); @@ -30,12 +46,24 @@ char socketName[SOCKNAMEMAX] = ""; // name of the socket // ----------------------------------------------------------------------------- -int openConnection(const char* sockname, int msec, const struct timespec abstime){ +int openConnection(const char* sockname, int msec, const struct timespec abstime) { if(!sockname){ errno = EINVAL; return -1; } + if(!openedFiles) { + openedFiles = calloc(1, sizeof(openedFiles)); + if(!openedFiles) { + perror("calloc"); + return -1; + } + openedFiles->f = NULL; + openedFiles->numOfFiles = 0; + openedFiles->rDir = NULL; + openedFiles->wDir = NULL; + } + struct sockaddr_un sa; struct timespec ts; ts.tv_sec = msec/1000; @@ -45,7 +73,7 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime long long elapsedTime; // select socket - strncpy(sa.sun_path, sockname, sizeof(sa.sun_path)); + strncpy(sa.sun_path, sockname, sizeof(sa.sun_path)-1); sa.sun_family = AF_UNIX; if ((fd_skt = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { @@ -75,54 +103,143 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime nanosleep(&ts, &ts); } - strncpy(socketName, sockname, sizeof(socketName)); + strncpy(socketName, sockname, sizeof(socketName)-1); + return 0; +} + +int closeConnection(const char* sockname) { + if (!sockname || strncmp(socketName, sockname, sizeof(socketName)) != 0) { + errno = EINVAL; + return -1; + } + // close every file + if (closeEveryFile() == -1) { + perror("closeEveryFile"); + return -1; + } + // close socket + if (close(fd_skt) == -1) { + // return errno from close + return -1; + } + + memset(socketName, 0, SOCKNAMEMAX); + + if (openedFiles) { + if(openedFiles->wDir) + free(openedFiles->wDir); + if(openedFiles->rDir) + free(openedFiles->rDir); + free(openedFiles); + } + + return 0; +} + +int openFile(const char* pathname, int flags) { return 1; } -int closeConnection(const char* sockname){ +int readFile(const char* pathname, void** buf, size_t* size) { return 1; } -int openFile(const char* pathname, int flags){ +int readNFiles(int N, const char* dirname) { return 1; } -int readFile(const char* pathname, void** buf, size_t* size){ +int writeFile(const char* pathname, const char* dirname) { return 1; } -int readNFiles(int N, const char* dirname){ +int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname) { return 1; } -int writeFile(const char* pathname, const char* dirname){ +int lockFile(const char* pathname) { return 1; } -int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname){ +int unlockFile(const char* pathname) { return 1; } -int lockFile(const char* pathname){ +int closeFile(const char *pathname) { return 1; } -int unlockFile(const char* pathname){ +int removeFile(const char* pathname) { return 1; } -int closeFile(const char *pathname){ +// ----------------------------------------------------------------------------- + +int setDirectory(char* Dir, int rw) { return 1; } -int removeFile(const char* pathname){ - return 1; -} - -int setDirectory(char* Dir, int rw){ - return 1; -} - -void printInfo(int p){ +void printInfo(int p) { return; } + +// ----------------------------------------------------------------------------- + +int removeOpenFile(const char *pathname) { + if(!pathname) { + errno = EINVAL; + return -1; + } + + if(!openedFiles || !openedFiles->f || openedFiles->numOfFiles == 0) { + errno = ENOENT; + return -1; + } + + files_t *tmp = openedFiles->f; + + // the element to remove is the first + if(strcmp(tmp->filename, pathname) == 0) { + openedFiles->f = tmp->next; + free(tmp->filename); + free(tmp); + return 1; + } + + files_t *prc = NULL; + + while(tmp->next) { + prc = tmp; + tmp = tmp->next; + if(strcmp(tmp->filename, pathname) == 0) { + prc->next = tmp->next; + free(tmp->filename); + free(tmp); + return 0; + } + } + + // file not found + errno = ENOENT; + return -1; +} + +int closeEveryFile() { + if (!openedFiles) + return 0; + if (openedFiles->numOfFiles > 0 && openedFiles->f != NULL) { + files_t *tmp = openedFiles->f; + files_t *prc = NULL; + while(tmp){ + prc = tmp; + tmp = tmp->next; + + if(closeFile(prc->filename) != 0) { + if(removeOpenFile(prc->filename) != 0) { + perror("removeOpenfile"); + } + openedFiles->numOfFiles--; + } + } + } + return 0; +}