closeConnection

This commit is contained in:
elvis
2022-04-21 19:56:50 +02:00
parent da707a09eb
commit fdb98cec53

View File

@ -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);
@ -36,6 +52,18 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime
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,12 +103,37 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime
nanosleep(&ts, &ts);
}
strncpy(socketName, sockname, sizeof(socketName));
return 1;
strncpy(socketName, sockname, sizeof(socketName)-1);
return 0;
}
int closeConnection(const char* sockname) {
return 1;
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) {
@ -119,6 +172,8 @@ int removeFile(const char* pathname){
return 1;
}
// -----------------------------------------------------------------------------
int setDirectory(char* Dir, int rw) {
return 1;
}
@ -126,3 +181,65 @@ int setDirectory(char* Dir, int rw){
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;
}