2022-04-21 18:56:47 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
2022-04-16 21:21:04 +02:00
|
|
|
#include <api.h>
|
|
|
|
|
|
2022-04-21 18:56:47 +02:00
|
|
|
#define SOCKNAMEMAX 256 // should be at least 108
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2022-04-21 19:56:50 +02:00
|
|
|
/* 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
|
2022-04-21 20:08:22 +02:00
|
|
|
int print; // whether to print
|
|
|
|
|
FILE *out; // where to print
|
2022-04-21 19:56:50 +02:00
|
|
|
} openfiles_t;
|
2022-04-16 21:21:04 +02:00
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
/* variabili globali */
|
|
|
|
|
static int fd_skt; // descriptor of the socket
|
|
|
|
|
static char socketName[SOCKNAMEMAX] = ""; // name of the socket
|
|
|
|
|
static openfiles_t *openedFiles = NULL;
|
2022-04-16 21:21:04 +02:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
/* funzioni ausiliarie */
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
// closes every file opened by the client
|
|
|
|
|
int closeEveryFile();
|
|
|
|
|
// closes and frees memory
|
|
|
|
|
int removeOpenFile(const char *pathname);
|
2022-04-16 21:21:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2022-04-21 19:56:50 +02:00
|
|
|
int openConnection(const char* sockname, int msec, const struct timespec abstime) {
|
2022-04-21 18:56:47 +02:00
|
|
|
if(!sockname){
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
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;
|
2022-04-21 20:08:22 +02:00
|
|
|
openedFiles->print = 0;
|
|
|
|
|
openedFiles->out = NULL;
|
2022-04-21 19:56:50 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-21 18:56:47 +02:00
|
|
|
struct sockaddr_un sa;
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
ts.tv_sec = msec/1000;
|
|
|
|
|
ts.tv_nsec = (msec % 1000) * 1000000;
|
|
|
|
|
struct timeval t1, t2; // for gettimeofday
|
|
|
|
|
long long start, end; // to calculate the time elapsed
|
|
|
|
|
long long elapsedTime;
|
|
|
|
|
|
|
|
|
|
// select socket
|
2022-04-21 19:56:50 +02:00
|
|
|
strncpy(sa.sun_path, sockname, sizeof(sa.sun_path)-1);
|
2022-04-21 18:56:47 +02:00
|
|
|
sa.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
|
|
if ((fd_skt = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
perror("socket");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(gettimeofday(&t1, NULL) != 0) {
|
|
|
|
|
perror("gettimeofday");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
start = (long long) (t1.tv_sec * 1000000000 + t1.tv_usec * 1000);
|
|
|
|
|
|
|
|
|
|
while (connect(fd_skt, (struct sockaddr*) &sa, sizeof(sa)) == -1) {
|
|
|
|
|
gettimeofday(&t2, NULL);
|
|
|
|
|
end = (long long) t2.tv_sec * 1000000000 + t2.tv_usec * 1000;
|
|
|
|
|
elapsedTime = (end - start);
|
|
|
|
|
|
|
|
|
|
// return if more time has elapsed than allowed
|
|
|
|
|
if (elapsedTime > (abstime.tv_sec * 1000000000) + abstime.tv_nsec) {
|
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nanosleep(&ts, &ts);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
strncpy(socketName, sockname, sizeof(socketName)-1);
|
|
|
|
|
return 0;
|
2022-04-16 21:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
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;
|
2022-04-16 21:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
int openFile(const char* pathname, int flags) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
int readFile(const char* pathname, void** buf, size_t* size) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
int readNFiles(int N, const char* dirname) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
int writeFile(const char* pathname, const char* dirname) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
int lockFile(const char* pathname) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
int unlockFile(const char* pathname) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
int closeFile(const char *pathname) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
int removeFile(const char* pathname) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 19:56:50 +02:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
int setDirectory(char* Dir, int rw) {
|
2022-04-16 21:21:04 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 20:08:22 +02:00
|
|
|
void printInfo(int p, FILE *stream) {
|
|
|
|
|
// 1 prints, 0 does not print
|
|
|
|
|
openedFiles->print = (p)? 1: 0;
|
|
|
|
|
openedFiles->out = stream;
|
2022-04-16 21:21:04 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2022-04-21 19:56:50 +02:00
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|