Files
progettoso/lib/api/api.c
2022-04-21 20:08:22 +02:00

253 lines
5.3 KiB
C

#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>
#include <api.h>
#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
int print; // whether to print
FILE *out; // where to print
} openfiles_t;
// -----------------------------------------------------------------------------
/* variabili globali */
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);
// -----------------------------------------------------------------------------
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;
openedFiles->print = 0;
openedFiles->out = NULL;
}
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
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) {
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);
}
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 readFile(const char* pathname, void** buf, size_t* size) {
return 1;
}
int readNFiles(int N, const char* dirname) {
return 1;
}
int writeFile(const char* pathname, const char* dirname) {
return 1;
}
int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname) {
return 1;
}
int lockFile(const char* pathname) {
return 1;
}
int unlockFile(const char* pathname) {
return 1;
}
int closeFile(const char *pathname) {
return 1;
}
int removeFile(const char* pathname) {
return 1;
}
// -----------------------------------------------------------------------------
int setDirectory(char* Dir, int rw) {
return 1;
}
void printInfo(int p, FILE *stream) {
// 1 prints, 0 does not print
openedFiles->print = (p)? 1: 0;
openedFiles->out = stream;
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;
}