fixed -W, working on -r
This commit is contained in:
112
lib/api/api.c
112
lib/api/api.c
@ -34,36 +34,16 @@
|
|||||||
// #define ME "52" // not used
|
// #define ME "52" // not used
|
||||||
#define MESE "55" // server error
|
#define MESE "55" // server error
|
||||||
|
|
||||||
|
#define debughere(str) \
|
||||||
|
fprintf(stdout, "\nIn function: %s\n", __func__); \
|
||||||
|
fprintf(stdout, "\t%s\n", str); \
|
||||||
#define printOpenedFiles(str) \
|
fflush(stdout); \
|
||||||
|
|
||||||
|
|
||||||
/* #define printOpenedFiles(str) \ */
|
|
||||||
/* fprintf(stdout, "\nInside of function: %s\n", __func__); \ */
|
|
||||||
/* fprintf(stdout, "\t[%s]\n", str); \ */
|
|
||||||
/* fprintf(stdout, "\tcreatedAndlocked: %s\n", openedFiles->createdAndLocked); \ */
|
|
||||||
/* fprintf(stdout, "\tf: %p\n", (void *)openedFiles->f); \ */
|
|
||||||
/* fprintf(stdout, "\tnumOfFiles: %d\n", openedFiles->numOfFiles); \ */
|
|
||||||
/* fprintf(stdout, "\tout: %p\n", (void *)openedFiles->out); \ */
|
|
||||||
/* fprintf(stdout, "\tvalidrDir: %d\n", openedFiles->validrDir); \ */
|
|
||||||
/* fprintf(stdout, "\trDir: %p\n", (void *)openedFiles->rDir); \ */
|
|
||||||
/* fprintf(stdout, "\tvalidwDir: %d\n", openedFiles->validwDir); \ */
|
|
||||||
/* fprintf(stdout, "\twDir: %p\n", (void *)openedFiles->wDir); \ */
|
|
||||||
/* fflush(stdout); \ */
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
/* structs */
|
/* structs */
|
||||||
typedef struct files_s {
|
typedef struct files_s {
|
||||||
char *filename;
|
char *filename;
|
||||||
|
int locked;
|
||||||
struct files_s *next;
|
struct files_s *next;
|
||||||
} files_t;
|
} files_t;
|
||||||
|
|
||||||
@ -108,9 +88,9 @@ int closeEveryFile();
|
|||||||
// closes and frees memory
|
// closes and frees memory
|
||||||
int removeOpenFile(const char *pathname);
|
int removeOpenFile(const char *pathname);
|
||||||
// adds element
|
// adds element
|
||||||
int addOpenFile(const char *pathname);
|
int addOpenFile(const char *pathname, const int flags);
|
||||||
// checks if the file is open already
|
// checks if the file is open already
|
||||||
int isOpen(const char *pathname);
|
int isOpen(const char *pathname, const int flags);
|
||||||
// reads everything the server sent into the variable passed to the function
|
// reads everything the server sent into the variable passed to the function
|
||||||
// expected is 1 if a file could be sent (if no server errors occurs)
|
// expected is 1 if a file could be sent (if no server errors occurs)
|
||||||
int reciveData(response_t *res, int expected);
|
int reciveData(response_t *res, int expected);
|
||||||
@ -124,9 +104,6 @@ int createOpenedFiles(void);
|
|||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
int openConnection(const char* sockname, int msec, const struct timespec abstime) {
|
int openConnection(const char* sockname, int msec, const struct timespec abstime) {
|
||||||
|
|
||||||
printOpenedFiles("before");
|
|
||||||
|
|
||||||
if(!sockname){
|
if(!sockname){
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -179,14 +156,10 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime
|
|||||||
}
|
}
|
||||||
|
|
||||||
strncpy(socketName, sockname, sizeof(socketName)-1);
|
strncpy(socketName, sockname, sizeof(socketName)-1);
|
||||||
|
|
||||||
printOpenedFiles("after");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int closeConnection(const char* sockname) {
|
int closeConnection(const char* sockname) {
|
||||||
printOpenedFiles("before");
|
|
||||||
|
|
||||||
if (!sockname || strncmp(socketName, sockname, sizeof(socketName)) != 0) {
|
if (!sockname || strncmp(socketName, sockname, sizeof(socketName)) != 0) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -207,9 +180,6 @@ int closeConnection(const char* sockname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int openFile(const char* pathname, int flags) {
|
int openFile(const char* pathname, int flags) {
|
||||||
|
|
||||||
printOpenedFiles("before");
|
|
||||||
|
|
||||||
if(!pathname){
|
if(!pathname){
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -226,12 +196,12 @@ int openFile(const char* pathname, int flags) {
|
|||||||
errno = ENOTCONN;
|
errno = ENOTCONN;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(isOpen(pathname)) { // already open
|
if(isOpen(pathname, flags)) { // already open
|
||||||
return -1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// invio al server la stringa "openFile|$(pathname)|$(flags)"
|
// invio al server la stringa "openFile|$(pathname)|$(flags)"
|
||||||
long len = strlen("openFile")+1+strlen(pathname)+1+((int)log10(flags)+1)+1;
|
long len = strlen("openFile")+1+strlen(pathname)+1+((int)log10(flags?flags:1)+1)+1;
|
||||||
char *cmd = malloc(len);
|
char *cmd = malloc(len);
|
||||||
if(!cmd) {
|
if(!cmd) {
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
@ -304,7 +274,7 @@ int openFile(const char* pathname, int flags) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(addOpenFile(pathname) == -1) {
|
if(addOpenFile(pathname, flags) == -1) {
|
||||||
perror("addOpenFile");
|
perror("addOpenFile");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -327,15 +297,10 @@ int openFile(const char* pathname, int flags) {
|
|||||||
free(res);
|
free(res);
|
||||||
|
|
||||||
free(cmd);
|
free(cmd);
|
||||||
|
|
||||||
printOpenedFiles("after");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int readFile(const char* pathname, void** buf, size_t* size) {
|
int readFile(const char* pathname, void** buf, size_t* size) {
|
||||||
|
|
||||||
printOpenedFiles("before");
|
|
||||||
|
|
||||||
if(!pathname) {
|
if(!pathname) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -353,7 +318,7 @@ int readFile(const char* pathname, void** buf, size_t* size) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isOpen(pathname) != 1) { // if it's not open => error
|
if(isOpen(pathname, 0) != 1) { // if it's not open => error
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -454,9 +419,6 @@ int readFile(const char* pathname, void** buf, size_t* size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int readNFiles(int N, const char* dirname) {
|
int readNFiles(int N, const char* dirname) {
|
||||||
|
|
||||||
printOpenedFiles("before");
|
|
||||||
|
|
||||||
if(!dirname) {
|
if(!dirname) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -537,7 +499,6 @@ int readNFiles(int N, const char* dirname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int writeFile(const char* pathname, const char* dirname) {
|
int writeFile(const char* pathname, const char* dirname) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!pathname) {
|
if(!pathname) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -600,6 +561,7 @@ int writeFile(const char* pathname, const char* dirname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (close(fdi) == -1) {
|
if (close(fdi) == -1) {
|
||||||
|
perror("closing file");
|
||||||
free(content);
|
free(content);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -719,14 +681,10 @@ int writeFile(const char* pathname, const char* dirname) {
|
|||||||
free(cmd);
|
free(cmd);
|
||||||
|
|
||||||
free(content);
|
free(content);
|
||||||
|
|
||||||
printOpenedFiles("after");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname) {
|
int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname) {
|
||||||
|
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!pathname || !buf) {
|
if(!pathname || !buf) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -744,7 +702,7 @@ int appendToFile(const char* pathname, void* buf, size_t size, const char* dirna
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isOpen(pathname) != 1) {
|
if (isOpen(pathname, 0) != 1) {
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -845,7 +803,6 @@ int appendToFile(const char* pathname, void* buf, size_t size, const char* dirna
|
|||||||
}
|
}
|
||||||
|
|
||||||
int lockFile(const char* pathname) {
|
int lockFile(const char* pathname) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!pathname){
|
if(!pathname){
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -864,11 +821,15 @@ int lockFile(const char* pathname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int existing = 0;
|
int existing = 0;
|
||||||
if(isOpen(pathname)){ // file is already open
|
if(isOpen(pathname, 0)){ // file is already open
|
||||||
existing = 1;
|
existing = 1;
|
||||||
goto _unlockFile;
|
goto _unlockFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(isOpen(pathname, O_LOCK)){ // file is already locked
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (openFile(pathname, O_LOCK) == 0) // open the file with the lock
|
if (openFile(pathname, O_LOCK) == 0) // open the file with the lock
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -926,7 +887,7 @@ _unlockFile: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!existing) {
|
if(!existing) {
|
||||||
if(addOpenFile(pathname) == -1) {
|
if(addOpenFile(pathname, O_LOCK) == -1) {
|
||||||
perror("addOpenFile");
|
perror("addOpenFile");
|
||||||
freeResponse(res);
|
freeResponse(res);
|
||||||
free(res);
|
free(res);
|
||||||
@ -943,7 +904,6 @@ _unlockFile: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int unlockFile(const char* pathname) {
|
int unlockFile(const char* pathname) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!pathname){
|
if(!pathname){
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -960,7 +920,7 @@ int unlockFile(const char* pathname) {
|
|||||||
errno = ENOTCONN;
|
errno = ENOTCONN;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if(isOpen(pathname) != 1) { // not open
|
if(isOpen(pathname, 0) != 1) { // not open or not locked
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -1017,7 +977,6 @@ int unlockFile(const char* pathname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int closeFile(const char *pathname) {
|
int closeFile(const char *pathname) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!pathname){
|
if(!pathname){
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -1035,7 +994,7 @@ int closeFile(const char *pathname) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isOpen(pathname) != 1) { // not open
|
if(isOpen(pathname, 0) != 1) { // not open
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -1095,12 +1054,10 @@ int closeFile(const char *pathname) {
|
|||||||
free(res);
|
free(res);
|
||||||
free(cmd);
|
free(cmd);
|
||||||
|
|
||||||
printOpenedFiles("after");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int removeFile(const char* pathname) {
|
int removeFile(const char* pathname) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!pathname){
|
if(!pathname){
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -1118,7 +1075,7 @@ int removeFile(const char* pathname) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isOpen(pathname) != 1) { // not open
|
if (isOpen(pathname, 0) != 1) { // not open
|
||||||
errno = EPERM;
|
errno = EPERM;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -1175,14 +1132,12 @@ int removeFile(const char* pathname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
free(cmd);
|
free(cmd);
|
||||||
printOpenedFiles("after");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
int setDirectory(char* Dir, int rw) {
|
int setDirectory(char* Dir, int rw) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if (!Dir) {
|
if (!Dir) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -1205,7 +1160,6 @@ int setDirectory(char* Dir, int rw) {
|
|||||||
openedFiles->rDir = malloc(strlen(Dir)+1);
|
openedFiles->rDir = malloc(strlen(Dir)+1);
|
||||||
strncpy(openedFiles->rDir, Dir, strlen(Dir)+1);
|
strncpy(openedFiles->rDir, Dir, strlen(Dir)+1);
|
||||||
}
|
}
|
||||||
printOpenedFiles("after");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1219,14 +1173,12 @@ void printInfo(int p, FILE *stream) {
|
|||||||
}
|
}
|
||||||
openedFiles->print = (p)? 1: 0;
|
openedFiles->print = (p)? 1: 0;
|
||||||
openedFiles->out = stream;
|
openedFiles->out = stream;
|
||||||
printOpenedFiles("after");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
int reciveData(response_t *res, int expected) {
|
int reciveData(response_t *res, int expected) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!res) {
|
if(!res) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -1474,7 +1426,6 @@ _nofile:
|
|||||||
}
|
}
|
||||||
|
|
||||||
int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf) {
|
int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!dirname || strcmp(dirname, "") || !rf || n<0) {
|
if(!dirname || strcmp(dirname, "") || !rf || n<0) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -1530,7 +1481,6 @@ int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
free(basepath);
|
free(basepath);
|
||||||
printOpenedFiles("after");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1551,7 +1501,7 @@ void freeResponse(response_t *res) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int isOpen(const char *pathname) {
|
int isOpen(const char *pathname, const int flags) {
|
||||||
if(!pathname) {
|
if(!pathname) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -1563,7 +1513,7 @@ int isOpen(const char *pathname) {
|
|||||||
|
|
||||||
files_t *tmp = openedFiles->f;
|
files_t *tmp = openedFiles->f;
|
||||||
while(tmp) {
|
while(tmp) {
|
||||||
if(strcmp(tmp->filename, pathname) == 0)
|
if(strcmp(tmp->filename, pathname) == 0 && (tmp->locked==(flags&O_LOCK)))
|
||||||
return 1;
|
return 1;
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
@ -1571,8 +1521,7 @@ int isOpen(const char *pathname) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int addOpenFile(const char *pathname) {
|
int addOpenFile(const char *pathname, const int flags) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!pathname) {
|
if(!pathname) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -1585,6 +1534,7 @@ int addOpenFile(const char *pathname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t len = strlen(pathname)+1;
|
size_t len = strlen(pathname)+1;
|
||||||
|
new->locked = flags&O_LOCK;
|
||||||
new->filename = calloc(len, sizeof(char));
|
new->filename = calloc(len, sizeof(char));
|
||||||
if(!new->filename) {
|
if(!new->filename) {
|
||||||
perror("calloc");
|
perror("calloc");
|
||||||
@ -1607,12 +1557,10 @@ int addOpenFile(const char *pathname) {
|
|||||||
}
|
}
|
||||||
tail->next = new;
|
tail->next = new;
|
||||||
openedFiles->numOfFiles++;
|
openedFiles->numOfFiles++;
|
||||||
printOpenedFiles("after");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int removeOpenFile(const char *pathname) {
|
int removeOpenFile(const char *pathname) {
|
||||||
printOpenedFiles("before");
|
|
||||||
if(!pathname) {
|
if(!pathname) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
@ -1652,7 +1600,6 @@ int removeOpenFile(const char *pathname) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int closeEveryFile() {
|
int closeEveryFile() {
|
||||||
printOpenedFiles("before");
|
|
||||||
if (openedFiles == NULL)
|
if (openedFiles == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
@ -1687,7 +1634,8 @@ int closeEveryFile() {
|
|||||||
openedFiles->wDir = NULL;
|
openedFiles->wDir = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
printOpenedFiles("after");
|
free(openedFiles);
|
||||||
|
openedFiles = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1706,7 +1654,5 @@ int createOpenedFiles(void) {
|
|||||||
openedFiles->wDir = NULL;
|
openedFiles->wDir = NULL;
|
||||||
openedFiles->print = 0;
|
openedFiles->print = 0;
|
||||||
openedFiles->out = NULL;
|
openedFiles->out = NULL;
|
||||||
|
|
||||||
printOpenedFiles("after");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -306,7 +306,7 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
|
|||||||
size_t m = sizeof(tmp_buf);
|
size_t m = sizeof(tmp_buf);
|
||||||
|
|
||||||
if(!filepath || !q || !taglia) {
|
if(!filepath || !q || !taglia) {
|
||||||
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath);
|
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore del server\n", fd_c, filepath);
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
serror(MESY, fd_c, taglia, tmp_buf);
|
serror(MESY, fd_c, taglia, tmp_buf);
|
||||||
return;
|
return;
|
||||||
@ -315,7 +315,7 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
|
|||||||
fileT *f = NULL;
|
fileT *f = NULL;
|
||||||
f = find(q, filepath);
|
f = find(q, filepath);
|
||||||
if(!f) {
|
if(!f) {
|
||||||
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" e' terminata con errore\n", fd_c, filepath);
|
n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una readFile sul file \"%s\" ma il file non esiste\n", fd_c, filepath);
|
||||||
errno = ENOENT;
|
errno = ENOENT;
|
||||||
serror(MESE, fd_c, taglia, tmp_buf);
|
serror(MESE, fd_c, taglia, tmp_buf);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -751,6 +751,7 @@ fileT* find(queueT *q, char *filepath) {
|
|||||||
|
|
||||||
if (writeFileT(res, (tmp->data)->data, (tmp->data)->size) == -1) {
|
if (writeFileT(res, (tmp->data)->data, (tmp->data)->size) == -1) {
|
||||||
perror("writeFileT res");
|
perror("writeFileT res");
|
||||||
|
destroyFile(res);
|
||||||
goto _end_find_in_queue;
|
goto _end_find_in_queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -27,7 +27,7 @@ strsep_gnu (char **stringp, const char *delim)
|
|||||||
if (begin == NULL)
|
if (begin == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
/* Find the end of the token. */
|
/* Find the end of the token. */
|
||||||
end = begin + strcspn (begin, delim);
|
end = begin + strcspn(begin, delim);
|
||||||
if (*end)
|
if (*end)
|
||||||
{
|
{
|
||||||
/* Terminate the token and set *STRINGP past NUL character. */
|
/* Terminate the token and set *STRINGP past NUL character. */
|
||||||
|
|||||||
24
src/client.c
24
src/client.c
@ -196,6 +196,10 @@ int main(int argc, char* argv[]) {
|
|||||||
goto _cleanup;
|
goto _cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(cmds) {
|
||||||
|
destroyCommandList(cmds);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
_cleanup:
|
_cleanup:
|
||||||
@ -217,9 +221,9 @@ void destroyCommandList(cmd_t *l) {
|
|||||||
while (l) {
|
while (l) {
|
||||||
tmp = l;
|
tmp = l;
|
||||||
|
|
||||||
free(l->arg);
|
|
||||||
l = l->next;
|
l = l->next;
|
||||||
|
|
||||||
|
free(tmp->arg);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -322,8 +326,10 @@ int execute(cmd_t *l, int print) {
|
|||||||
if(!ok)
|
if(!ok)
|
||||||
printf("errore\n");
|
printf("errore\n");
|
||||||
}
|
}
|
||||||
if(!ok)
|
if(!ok) {
|
||||||
|
closeConnection(tmp->arg);
|
||||||
return 0; // no socket to connect, nothing to do
|
return 0; // no socket to connect, nothing to do
|
||||||
|
}
|
||||||
// we only read the first -f, no error reported if more than one is
|
// we only read the first -f, no error reported if more than one is
|
||||||
// specified
|
// specified
|
||||||
tmp = NULL;
|
tmp = NULL;
|
||||||
@ -398,7 +404,7 @@ int execute(cmd_t *l, int print) {
|
|||||||
case 'D':
|
case 'D':
|
||||||
if(Dir)
|
if(Dir)
|
||||||
free(Dir);
|
free(Dir);
|
||||||
Dir = malloc(strnlen(tmp->arg, MAXARGLENGTH)+1);
|
Dir = calloc(strnlen(tmp->arg, MAXARGLENGTH)+1, sizeof(char));
|
||||||
strncpy(Dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH));
|
strncpy(Dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH));
|
||||||
|
|
||||||
if (setDirectory(Dir, 1) == -1) {
|
if (setDirectory(Dir, 1) == -1) {
|
||||||
@ -417,7 +423,7 @@ int execute(cmd_t *l, int print) {
|
|||||||
case 'd':
|
case 'd':
|
||||||
if(dir)
|
if(dir)
|
||||||
free(dir);
|
free(dir);
|
||||||
dir = malloc(strnlen(tmp->arg, MAXARGLENGTH)+1);
|
dir = calloc(strnlen(tmp->arg, MAXARGLENGTH)+1, sizeof(char));
|
||||||
strncpy(dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH));
|
strncpy(dir, tmp->arg, strnlen(tmp->arg, MAXARGLENGTH));
|
||||||
|
|
||||||
if(setDirectory(dir, 0) == -1) {
|
if(setDirectory(dir, 0) == -1) {
|
||||||
@ -569,9 +575,10 @@ int cmd_W(char *filelist, char *Dir, int print) {
|
|||||||
perror("malloc");
|
perror("malloc");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH));
|
memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1);
|
||||||
|
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH)+1);
|
||||||
|
|
||||||
char *token;
|
char *token = NULL;
|
||||||
char *string = tofree;
|
char *string = tofree;
|
||||||
|
|
||||||
if (print == 1) {
|
if (print == 1) {
|
||||||
@ -631,13 +638,14 @@ int cmd_r(char *filelist, char *dir, int print) {
|
|||||||
|
|
||||||
// we copy filelist because we are nice
|
// we copy filelist because we are nice
|
||||||
char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1);
|
char *tofree = malloc(strnlen(filelist, MAXARGLENGTH)+1);
|
||||||
|
memset(tofree, 0, strnlen(filelist, MAXARGLENGTH)+1);
|
||||||
if(!tofree) {
|
if(!tofree) {
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH));
|
strncpy(tofree, filelist, strnlen(filelist, MAXARGLENGTH));
|
||||||
|
|
||||||
char *token;
|
char *token = NULL;
|
||||||
char *string = tofree;
|
char *string = tofree;
|
||||||
|
|
||||||
if (print) {
|
if (print) {
|
||||||
@ -663,7 +671,7 @@ int cmd_r(char *filelist, char *dir, int print) {
|
|||||||
void *buf = NULL;
|
void *buf = NULL;
|
||||||
size_t size = -1;
|
size_t size = -1;
|
||||||
|
|
||||||
printInfo(0, stdout);
|
// printInfo(0, stdout);
|
||||||
// read the content of the file
|
// read the content of the file
|
||||||
if (ok && readFile(token, &buf, &size) == -1) {
|
if (ok && readFile(token, &buf, &size) == -1) {
|
||||||
ok = 0;
|
ok = 0;
|
||||||
|
|||||||
17
src/server.c
17
src/server.c
@ -250,6 +250,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
FD_CLR(i, &set);
|
FD_CLR(i, &set);
|
||||||
close(i);
|
close(i);
|
||||||
|
free(connfd);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,7 +281,7 @@ int main(int argc, char *argv[]) {
|
|||||||
free(connfd);
|
free(connfd);
|
||||||
goto _cleanup;
|
goto _cleanup;
|
||||||
}
|
}
|
||||||
args->connfd = connfd;
|
args->connfd = *connfd;
|
||||||
args->quit = &quit;
|
args->quit = &quit;
|
||||||
args->request_pipe = request_pipe[1];
|
args->request_pipe = request_pipe[1];
|
||||||
args->q = queue;
|
args->q = queue;
|
||||||
@ -293,13 +294,13 @@ int main(int argc, char *argv[]) {
|
|||||||
int r = addToThreadPool(pool, threadF, args);
|
int r = addToThreadPool(pool, threadF, args);
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
numberOfConnections++;
|
numberOfConnections++;
|
||||||
|
free(connfd);
|
||||||
continue; // aggiunto con successo
|
continue; // aggiunto con successo
|
||||||
}
|
}
|
||||||
if (r < 0) // errore interno
|
if (r < 0) // errore interno
|
||||||
fprintf(stderr, "ERROR FATAL adding to the thread pool\n");
|
fprintf(stderr, "ERROR FATAL adding to the thread pool\n");
|
||||||
else // coda dei pendenti piena
|
else // coda dei pendenti piena
|
||||||
fprintf(stderr, "ERROR SERVER TOO BUSY\n");
|
fprintf(stderr, "ERROR SERVER TOO BUSY\n");
|
||||||
free(args);
|
|
||||||
close(*connfd);
|
close(*connfd);
|
||||||
free(connfd);
|
free(connfd);
|
||||||
continue;
|
continue;
|
||||||
@ -329,6 +330,7 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
free(connfd);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (i == signal_pipe[0]) { // controllo se devo terminare
|
if (i == signal_pipe[0]) { // controllo se devo terminare
|
||||||
@ -382,8 +384,7 @@ int main(int argc, char *argv[]) {
|
|||||||
free(connfd);
|
free(connfd);
|
||||||
goto _cleanup;
|
goto _cleanup;
|
||||||
}
|
}
|
||||||
*connfd = i;
|
args->connfd = i;
|
||||||
args->connfd = connfd;
|
|
||||||
args->quit = &quit;
|
args->quit = &quit;
|
||||||
args->request_pipe = request_pipe[1];
|
args->request_pipe = request_pipe[1];
|
||||||
args->q = queue;
|
args->q = queue;
|
||||||
@ -396,13 +397,13 @@ int main(int argc, char *argv[]) {
|
|||||||
int r = addToThreadPool(pool, threadF, args);
|
int r = addToThreadPool(pool, threadF, args);
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
numberOfConnections++;
|
numberOfConnections++;
|
||||||
|
free(connfd);
|
||||||
continue; // aggiunto con successo
|
continue; // aggiunto con successo
|
||||||
}
|
}
|
||||||
if (r < 0) // errore interno
|
if (r < 0) // errore interno
|
||||||
fprintf(stderr, "ERROR FATAL adding to the thread pool\n");
|
fprintf(stderr, "ERROR FATAL adding to the thread pool\n");
|
||||||
else // coda dei pendenti piena
|
else // coda dei pendenti piena
|
||||||
fprintf(stderr, "ERROR SERVER TOO BUSY\n");
|
fprintf(stderr, "ERROR SERVER TOO BUSY\n");
|
||||||
free(args);
|
|
||||||
close(*connfd);
|
close(*connfd);
|
||||||
free(connfd);
|
free(connfd);
|
||||||
continue;
|
continue;
|
||||||
@ -436,6 +437,12 @@ int main(int argc, char *argv[]) {
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
_cleanup:
|
_cleanup:
|
||||||
|
if(queue) {
|
||||||
|
destroyQueue(queue);
|
||||||
|
}
|
||||||
|
if(taglia) {
|
||||||
|
taglia_del(taglia);
|
||||||
|
}
|
||||||
unlink(socketName);
|
unlink(socketName);
|
||||||
free(socketName);
|
free(socketName);
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@ -27,7 +27,7 @@ void threadF(void *arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
threadT *argl = (threadT *) arg;
|
threadT *argl = (threadT *) arg;
|
||||||
int connfd = *argl->connfd;
|
long connfd = argl->connfd;
|
||||||
volatile int *quit = argl->quit;
|
volatile int *quit = argl->quit;
|
||||||
int request_pipe = argl->request_pipe;
|
int request_pipe = argl->request_pipe;
|
||||||
queueT *q = argl->q;
|
queueT *q = argl->q;
|
||||||
@ -94,7 +94,7 @@ void threadF(void *arg) {
|
|||||||
// log chiusura connessione
|
// log chiusura connessione
|
||||||
int n = 0;
|
int n = 0;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
n = snprintf(buf, sizeof(buf), "Chiusa connessione con il client %d.\n", connfd);
|
n = snprintf(buf, sizeof(buf), "Chiusa connessione con il client %ld.\n", connfd);
|
||||||
if( n<0 ) {
|
if( n<0 ) {
|
||||||
perror("snprintf");
|
perror("snprintf");
|
||||||
goto _cleanup;
|
goto _cleanup;
|
||||||
@ -117,17 +117,19 @@ void threadF(void *arg) {
|
|||||||
// log
|
// log
|
||||||
int m = 0;
|
int m = 0;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
m = snprintf(buf, sizeof(buf), "Servito una richiesta del client %d.\n", connfd);
|
m = snprintf(buf, sizeof(buf), "Servito una richiesta del client %ld.\n", connfd);
|
||||||
if( m<0 ) {
|
if( m<0 ) {
|
||||||
perror("snprintf");
|
perror("snprintf");
|
||||||
goto _cleanup;
|
goto _cleanup;
|
||||||
}
|
}
|
||||||
if( taglia_log(taglia, buf) < 0 )
|
if( taglia_log(taglia, buf) < 0 )
|
||||||
goto _cleanup;
|
goto _cleanup;
|
||||||
|
|
||||||
|
free(arg);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_cleanup:
|
_cleanup:
|
||||||
// nothing to do because no memory needs to be freed
|
free(arg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,12 +140,13 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *string = calloc(1, len);
|
char *string = calloc(1, len+1);
|
||||||
if(string == NULL) {
|
if(string == NULL) {
|
||||||
perror("calloc");
|
perror("calloc");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
strncpy(string, command, len);
|
strncpy(string, command, len);
|
||||||
|
char *tofree = string;
|
||||||
|
|
||||||
char *token = NULL;
|
char *token = NULL;
|
||||||
char *token2 = NULL;
|
char *token2 = NULL;
|
||||||
@ -237,9 +240,11 @@ int parser(int len, char *command, queueT *queue, long fd_c, taglia_t* taglia, p
|
|||||||
|
|
||||||
_parser_cleanup:
|
_parser_cleanup:
|
||||||
free(command);
|
free(command);
|
||||||
|
free(tofree);
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
_parser_end:
|
_parser_end:
|
||||||
free(command);
|
free(command);
|
||||||
|
free(tofree);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
typedef struct struct_thread {
|
typedef struct struct_thread {
|
||||||
volatile int *quit;
|
volatile int *quit;
|
||||||
int request_pipe;
|
int request_pipe;
|
||||||
long *connfd;
|
long connfd;
|
||||||
queueT *q; // puntatore alla queue dei file
|
queueT *q; // puntatore alla queue dei file
|
||||||
taglia_t *taglia; // puntatore alla struct del file di log
|
taglia_t *taglia; // puntatore alla struct del file di log
|
||||||
threadpool_t *pool; // puntatore alla threadpool
|
threadpool_t *pool; // puntatore alla threadpool
|
||||||
|
|||||||
Reference in New Issue
Block a user