fixed -W, working on -r

This commit is contained in:
elvis
2022-04-25 20:24:55 +02:00
parent d8a0220ade
commit e67d3b5580
8 changed files with 72 additions and 105 deletions

View File

@ -34,36 +34,16 @@
// #define ME "52" // not used
#define MESE "55" // server error
#define printOpenedFiles(str) \
/* #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); \ */
#define debughere(str) \
fprintf(stdout, "\nIn function: %s\n", __func__); \
fprintf(stdout, "\t%s\n", str); \
fflush(stdout); \
// -----------------------------------------------------------------------------
/* structs */
typedef struct files_s {
char *filename;
int locked;
struct files_s *next;
} files_t;
@ -108,9 +88,9 @@ int closeEveryFile();
// closes and frees memory
int removeOpenFile(const char *pathname);
// adds element
int addOpenFile(const char *pathname);
int addOpenFile(const char *pathname, const int flags);
// 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
// expected is 1 if a file could be sent (if no server errors occurs)
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) {
printOpenedFiles("before");
if(!sockname){
errno = EINVAL;
return -1;
@ -179,14 +156,10 @@ int openConnection(const char* sockname, int msec, const struct timespec abstime
}
strncpy(socketName, sockname, sizeof(socketName)-1);
printOpenedFiles("after");
return 0;
}
int closeConnection(const char* sockname) {
printOpenedFiles("before");
if (!sockname || strncmp(socketName, sockname, sizeof(socketName)) != 0) {
errno = EINVAL;
return -1;
@ -207,9 +180,6 @@ int closeConnection(const char* sockname) {
}
int openFile(const char* pathname, int flags) {
printOpenedFiles("before");
if(!pathname){
errno = EINVAL;
return -1;
@ -226,12 +196,12 @@ int openFile(const char* pathname, int flags) {
errno = ENOTCONN;
return -1;
}
if(isOpen(pathname)) { // already open
return -1;
if(isOpen(pathname, flags)) { // already open
return 0;
}
// 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);
if(!cmd) {
perror("malloc");
@ -304,7 +274,7 @@ int openFile(const char* pathname, int flags) {
}
}
if(addOpenFile(pathname) == -1) {
if(addOpenFile(pathname, flags) == -1) {
perror("addOpenFile");
return -1;
}
@ -327,15 +297,10 @@ int openFile(const char* pathname, int flags) {
free(res);
free(cmd);
printOpenedFiles("after");
return 0;
}
int readFile(const char* pathname, void** buf, size_t* size) {
printOpenedFiles("before");
if(!pathname) {
errno = EINVAL;
return -1;
@ -353,7 +318,7 @@ int readFile(const char* pathname, void** buf, size_t* size) {
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;
return -1;
}
@ -454,9 +419,6 @@ int readFile(const char* pathname, void** buf, size_t* size) {
}
int readNFiles(int N, const char* dirname) {
printOpenedFiles("before");
if(!dirname) {
errno = EINVAL;
return -1;
@ -537,7 +499,6 @@ int readNFiles(int N, const char* dirname) {
}
int writeFile(const char* pathname, const char* dirname) {
printOpenedFiles("before");
if(!pathname) {
errno = EINVAL;
return -1;
@ -600,6 +561,7 @@ int writeFile(const char* pathname, const char* dirname) {
}
if (close(fdi) == -1) {
perror("closing file");
free(content);
return -1;
}
@ -719,14 +681,10 @@ int writeFile(const char* pathname, const char* dirname) {
free(cmd);
free(content);
printOpenedFiles("after");
return 0;
}
int appendToFile(const char* pathname, void* buf, size_t size, const char* dirname) {
printOpenedFiles("before");
if(!pathname || !buf) {
errno = EINVAL;
return -1;
@ -744,7 +702,7 @@ int appendToFile(const char* pathname, void* buf, size_t size, const char* dirna
return -1;
}
if (isOpen(pathname) != 1) {
if (isOpen(pathname, 0) != 1) {
errno = EPERM;
return -1;
}
@ -845,7 +803,6 @@ int appendToFile(const char* pathname, void* buf, size_t size, const char* dirna
}
int lockFile(const char* pathname) {
printOpenedFiles("before");
if(!pathname){
errno = EINVAL;
return -1;
@ -864,11 +821,15 @@ int lockFile(const char* pathname) {
}
int existing = 0;
if(isOpen(pathname)){ // file is already open
if(isOpen(pathname, 0)){ // file is already open
existing = 1;
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
return 0;
@ -926,7 +887,7 @@ _unlockFile: {
}
if(!existing) {
if(addOpenFile(pathname) == -1) {
if(addOpenFile(pathname, O_LOCK) == -1) {
perror("addOpenFile");
freeResponse(res);
free(res);
@ -943,7 +904,6 @@ _unlockFile: {
}
int unlockFile(const char* pathname) {
printOpenedFiles("before");
if(!pathname){
errno = EINVAL;
return -1;
@ -960,7 +920,7 @@ int unlockFile(const char* pathname) {
errno = ENOTCONN;
return -1;
}
if(isOpen(pathname) != 1) { // not open
if(isOpen(pathname, 0) != 1) { // not open or not locked
errno = EPERM;
return -1;
}
@ -1017,7 +977,6 @@ int unlockFile(const char* pathname) {
}
int closeFile(const char *pathname) {
printOpenedFiles("before");
if(!pathname){
errno = EINVAL;
return -1;
@ -1035,7 +994,7 @@ int closeFile(const char *pathname) {
return -1;
}
if(isOpen(pathname) != 1) { // not open
if(isOpen(pathname, 0) != 1) { // not open
errno = EPERM;
return -1;
}
@ -1095,12 +1054,10 @@ int closeFile(const char *pathname) {
free(res);
free(cmd);
printOpenedFiles("after");
return 0;
}
int removeFile(const char* pathname) {
printOpenedFiles("before");
if(!pathname){
errno = EINVAL;
return -1;
@ -1118,7 +1075,7 @@ int removeFile(const char* pathname) {
return -1;
}
if (isOpen(pathname) != 1) { // not open
if (isOpen(pathname, 0) != 1) { // not open
errno = EPERM;
return -1;
}
@ -1175,14 +1132,12 @@ int removeFile(const char* pathname) {
}
free(cmd);
printOpenedFiles("after");
return 0;
}
// -----------------------------------------------------------------------------
int setDirectory(char* Dir, int rw) {
printOpenedFiles("before");
if (!Dir) {
errno = EINVAL;
return -1;
@ -1205,7 +1160,6 @@ int setDirectory(char* Dir, int rw) {
openedFiles->rDir = malloc(strlen(Dir)+1);
strncpy(openedFiles->rDir, Dir, strlen(Dir)+1);
}
printOpenedFiles("after");
return 0;
}
@ -1219,14 +1173,12 @@ void printInfo(int p, FILE *stream) {
}
openedFiles->print = (p)? 1: 0;
openedFiles->out = stream;
printOpenedFiles("after");
return;
}
// -----------------------------------------------------------------------------
int reciveData(response_t *res, int expected) {
printOpenedFiles("before");
if(!res) {
errno = EINVAL;
return -1;
@ -1474,7 +1426,6 @@ _nofile:
}
int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf) {
printOpenedFiles("before");
if(!dirname || strcmp(dirname, "") || !rf || n<0) {
errno = EINVAL;
return -1;
@ -1530,7 +1481,6 @@ int storeFilesInDirectory(const char *dirname, int n, recivedFile_t *rf) {
}
free(basepath);
printOpenedFiles("after");
return 0;
}
@ -1551,7 +1501,7 @@ void freeResponse(response_t *res) {
return;
}
int isOpen(const char *pathname) {
int isOpen(const char *pathname, const int flags) {
if(!pathname) {
errno = EINVAL;
return -1;
@ -1563,7 +1513,7 @@ int isOpen(const char *pathname) {
files_t *tmp = openedFiles->f;
while(tmp) {
if(strcmp(tmp->filename, pathname) == 0)
if(strcmp(tmp->filename, pathname) == 0 && (tmp->locked==(flags&O_LOCK)))
return 1;
tmp = tmp->next;
}
@ -1571,8 +1521,7 @@ int isOpen(const char *pathname) {
return 0;
}
int addOpenFile(const char *pathname) {
printOpenedFiles("before");
int addOpenFile(const char *pathname, const int flags) {
if(!pathname) {
errno = EINVAL;
return -1;
@ -1585,6 +1534,7 @@ int addOpenFile(const char *pathname) {
}
size_t len = strlen(pathname)+1;
new->locked = flags&O_LOCK;
new->filename = calloc(len, sizeof(char));
if(!new->filename) {
perror("calloc");
@ -1607,12 +1557,10 @@ int addOpenFile(const char *pathname) {
}
tail->next = new;
openedFiles->numOfFiles++;
printOpenedFiles("after");
return 0;
}
int removeOpenFile(const char *pathname) {
printOpenedFiles("before");
if(!pathname) {
errno = EINVAL;
return -1;
@ -1652,7 +1600,6 @@ int removeOpenFile(const char *pathname) {
}
int closeEveryFile() {
printOpenedFiles("before");
if (openedFiles == NULL)
return 0;
@ -1687,7 +1634,8 @@ int closeEveryFile() {
openedFiles->wDir = NULL;
}
printOpenedFiles("after");
free(openedFiles);
openedFiles = NULL;
return 0;
}
@ -1706,7 +1654,5 @@ int createOpenedFiles(void) {
openedFiles->wDir = NULL;
openedFiles->print = 0;
openedFiles->out = NULL;
printOpenedFiles("after");
return 0;
}

View File

@ -306,7 +306,7 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
size_t m = sizeof(tmp_buf);
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;
serror(MESY, fd_c, taglia, tmp_buf);
return;
@ -315,7 +315,7 @@ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia) {
fileT *f = NULL;
f = find(q, filepath);
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;
serror(MESE, fd_c, taglia, tmp_buf);
return;

View File

@ -751,6 +751,7 @@ fileT* find(queueT *q, char *filepath) {
if (writeFileT(res, (tmp->data)->data, (tmp->data)->size) == -1) {
perror("writeFileT res");
destroyFile(res);
goto _end_find_in_queue;
}

View File

@ -27,7 +27,7 @@ strsep_gnu (char **stringp, const char *delim)
if (begin == NULL)
return NULL;
/* Find the end of the token. */
end = begin + strcspn (begin, delim);
end = begin + strcspn(begin, delim);
if (*end)
{
/* Terminate the token and set *STRINGP past NUL character. */