This commit is contained in:
elvis
2022-04-24 01:36:43 +02:00
parent 4fe23200bc
commit 9eaf9cdd49
9 changed files with 163 additions and 82 deletions

View File

@ -8,6 +8,7 @@
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>
#include <math.h>
#include <api.h>
#include <conn.h>
@ -40,7 +41,9 @@ typedef struct files_s {
typedef struct openfiles_s {
int numOfFiles;
files_t *f; // list of files
int validwDir; // 1 if wDir is allocated
char *wDir; // for files sent from the server after openFile
int validrDir; // 1 if rDir is allocated
char *rDir; // for files read from the server
char *createdAndLocked; // path of the last file that was created and locked
int print; // whether to print
@ -64,7 +67,7 @@ typedef struct response_s {
// -----------------------------------------------------------------------------
/* variabili globali */
static int fd_skt; // descriptor of the socket
static long fd_skt; // descriptor of the socket
static char socketName[SOCKNAMEMAX] = ""; // name of the socket
static openfiles_t *openedFiles = NULL;
@ -86,6 +89,8 @@ int reciveData(response_t *res, int expected);
int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf);
// frees memory inside response_t element
void freeResponse(response_t *res);
// creates the global variable
int createOpenedFiles(void);
// -----------------------------------------------------------------------------
@ -96,18 +101,10 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime
}
if(!openedFiles) {
openedFiles = calloc(1, sizeof(openedFiles));
if(!openedFiles) {
perror("calloc");
if(createOpenedFiles()<0) {
perror("createOpenedfiles");
return -1;
}
openedFiles->f = NULL;
openedFiles->numOfFiles = 0;
openedFiles->createdAndLocked = NULL;
openedFiles->rDir = NULL;
openedFiles->wDir = NULL;
openedFiles->print = 0;
openedFiles->out = NULL;
}
struct sockaddr_un sa;
@ -170,17 +167,6 @@ int closeConnection(const char* sockname) {
}
memset(socketName, 0, SOCKNAMEMAX);
if (openedFiles) {
if(openedFiles->createdAndLocked)
free(openedFiles->createdAndLocked);
if(openedFiles->wDir)
free(openedFiles->wDir);
if(openedFiles->rDir)
free(openedFiles->rDir);
free(openedFiles);
}
return 0;
}
@ -206,7 +192,7 @@ int openFile(const char* pathname, int flags) {
}
// invio al server la stringa "openFile|$(pathname)|$(flags)"
long len = strlen("openFile")+1+strlen(pathname)+1+sizeof(flags)+1;
long len = strlen("openFile")+1+strlen(pathname)+1+((int)log10(flags)+1)+1;
char *cmd = malloc(len);
if(!cmd) {
perror("malloc");
@ -214,6 +200,7 @@ int openFile(const char* pathname, int flags) {
}
memset(cmd, 0, len);
snprintf(cmd, len, "openFile|%s|%d", pathname, flags);
len = strnlen(cmd, len);
// send cmd
if (writen(fd_skt, &len, sizeof(long)) == -1) {
@ -336,6 +323,7 @@ int readFile(const char* pathname, void** buf, size_t* size) {
}
memset(cmd, 0, len);
snprintf(cmd, len, "readFile|%s", pathname);
len = strnlen(cmd, len);
// send cmd
if (writen(fd_skt, &len, sizeof(long)) == -1) {
@ -440,7 +428,7 @@ int readNFiles(int N, const char* dirname) {
}
// invio al server la stringa "readNFile|$(N)"
long len = strlen("readNFile")+1+sizeof(N)+1;
long len = strlen("readNFile")+1+((int)log10(N)+1)+1;
char *cmd = malloc(len);
if(!cmd) {
perror("malloc");
@ -448,6 +436,7 @@ int readNFiles(int N, const char* dirname) {
}
memset(cmd, 0, len);
snprintf(cmd, len, "readNFile|%d", N);
len = strnlen(cmd, len);
// send cmd
if (writen(fd_skt, &len, sizeof(long)) == -1) {
@ -558,7 +547,7 @@ int writeFile(const char* pathname, const char* dirname) {
}
if (openedFiles->print) {
fprintf(openedFiles->out, "Dimensione: %ld B\n", size);
fprintf(openedFiles->out, "Dimensione: %ldB ", size);
fflush(openedFiles->out);
}
@ -568,7 +557,7 @@ int writeFile(const char* pathname, const char* dirname) {
}
// invio al server la stringa "writeFile|$(pathname)|$(size)"
long len = strlen("writeFile")+1+strlen(pathname)+1+sizeof(size)+1;
long len = strlen("writeFile")+1+strlen(pathname)+1+((int) log10(size)+1)+1;
char *cmd = malloc(len);
if(!cmd) {
perror("malloc");
@ -577,6 +566,7 @@ int writeFile(const char* pathname, const char* dirname) {
}
memset(cmd, 0, len);
snprintf(cmd, len, "writeFile|%s|%zu", pathname, size);
len = strnlen(cmd, len);
// send cmd
if (writen(fd_skt, &len, sizeof(long)) == -1) {
@ -596,10 +586,11 @@ int writeFile(const char* pathname, const char* dirname) {
response_t *res = calloc(1, sizeof(response_t));
if(!res){
perror("calloc");
free(cmd);
free(content);
free(cmd);
return -1;
}
if(reciveData(res, 0) < 0) { // 0 because if we get a MEOK we don't expect files
// errno is set by reciveData
freeResponse(res);
@ -608,7 +599,6 @@ int writeFile(const char* pathname, const char* dirname) {
free(content);
return -1;
}
if(res->meerrno != 0) { // errno from server
freeResponse(res);
errno = res->meerrno;
@ -657,13 +647,30 @@ int writeFile(const char* pathname, const char* dirname) {
return -1;
}
// recive response
if(reciveData(res, 0) < 0) { // 0 because if we get a MEOK we don't expect files
// errno is set by reciveData
freeResponse(res);
free(res);
free(cmd);
free(content);
return -1;
}
if(res->meerrno != 0) { // errno from server
freeResponse(res);
errno = res->meerrno;
free(res);
free(cmd);
free(content);
return -1;
}
freeResponse(res);
free(res);
free(cmd);
free(content);
return 0;
}
@ -1111,7 +1118,7 @@ int removeFile(const char* pathname) {
}
free(cmd);
return 1;
return 0;
}
// -----------------------------------------------------------------------------
@ -1123,21 +1130,33 @@ int setDirectory(char* Dir, int rw) {
}
if (rw == 1) {
if(openedFiles->wDir)
if(openedFiles->validwDir) {
free(openedFiles->wDir);
openedFiles->wDir = NULL;
}
openedFiles->validwDir = 1;
openedFiles->wDir = malloc(strlen(Dir)+1);
strncpy(openedFiles->wDir, Dir, strlen(Dir)+1);
} else {
if(openedFiles->rDir)
if(openedFiles->validrDir) {
free(openedFiles->rDir);
openedFiles->rDir = NULL;
}
openedFiles->validrDir = 1;
openedFiles->rDir = malloc(strlen(Dir)+1);
strncpy(openedFiles->rDir, Dir, strlen(Dir)+1);
}
return 1;
return 0;
}
void printInfo(int p, FILE *stream) {
// 1 prints, 0 does not print
if(!openedFiles) {
if(createOpenedFiles()<0) {
perror("createOpenedfiles");
return;
}
}
openedFiles->print = (p)? 1: 0;
openedFiles->out = stream;
return;
@ -1152,7 +1171,7 @@ int reciveData(response_t *res, int expected) {
}
int readnres;
readnres = readn(fd_skt, res->ME, sizeof(res->ME));
readnres = readn(fd_skt, &res->ME, sizeof(res->ME));
if(readnres <= 0) {
// readn sets errno
return -1;
@ -1303,7 +1322,6 @@ _nofile:
free(res->rf);
res->rf = NULL;
if(strncmp(res->ME, MEOK, sizeof(res->ME))==0){
// simple ok response
res->meerrno = 0;
@ -1457,8 +1475,12 @@ void freeResponse(response_t *res) {
return;
if(res->numfiles>0 && res->rf){
for(int i=0;i<res->numfiles;++i){
free(res->rf[i].path);
free(res->rf[i].file);
if(res->rf[i].path)
free(res->rf[i].path);
res->rf[i].path = NULL;
if(res->rf[i].file)
free(res->rf[i].file);
res->rf[i].file = NULL;
}
free(res->rf);
}
@ -1541,7 +1563,7 @@ int removeOpenFile(const char *pathname) {
openedFiles->f = tmp->next;
free(tmp->filename);
free(tmp);
return 1;
return 0;
}
files_t *prc = NULL;
@ -1563,8 +1585,9 @@ int removeOpenFile(const char *pathname) {
}
int closeEveryFile() {
if (!openedFiles)
if (openedFiles == NULL)
return 0;
if (openedFiles->numOfFiles > 0 && openedFiles->f != NULL) {
files_t *tmp = openedFiles->f;
files_t *prc = NULL;
@ -1580,5 +1603,41 @@ int closeEveryFile() {
}
}
}
if(openedFiles->createdAndLocked) {
free(openedFiles->createdAndLocked);
openedFiles->createdAndLocked = NULL;
}
if(openedFiles->validrDir) {
free(openedFiles->rDir);
openedFiles->validrDir = 0;
openedFiles->rDir = NULL;
}
if(openedFiles->validwDir) {
free(openedFiles->wDir);
openedFiles->validwDir = 0;
openedFiles->wDir = NULL;
}
fprintf(stdout, "\nhere\n");
fflush(stdout);
return 0;
}
int createOpenedFiles(void) {
openedFiles = calloc(1, sizeof(openedFiles));
if(!openedFiles) {
perror("calloc");
return -1;
}
openedFiles->f = NULL;
openedFiles->numOfFiles = 0;
openedFiles->createdAndLocked = NULL;
openedFiles->validrDir = 0;
openedFiles->rDir = NULL;
openedFiles->validwDir = 0;
openedFiles->wDir = NULL;
openedFiles->print = 0;
openedFiles->out = NULL;
return 0;
}