From 029edc22fa4ba93f5849d7c812ad2f3509a81287 Mon Sep 17 00:00:00 2001 From: elvis Date: Sat, 9 Apr 2022 01:11:46 +0200 Subject: [PATCH] added function lockFile --- lib/threadpool/apiFile.c | 62 ++++++++++++++++++++++++++++++++++++++ lib/threadpool/fileQueue.c | 3 +- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/lib/threadpool/apiFile.c b/lib/threadpool/apiFile.c index fff79d7..cabdc5b 100644 --- a/lib/threadpool/apiFile.c +++ b/lib/threadpool/apiFile.c @@ -237,6 +237,9 @@ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia) sendMessage(MEOK, fd_c, taglia, tmp_buf); return; } + + // codice non raggiungibile + return; } @@ -296,6 +299,7 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl return; } + // cerco il file fileT *f = NULL; f = find(q, filepath); if(!f) { // file is not present @@ -418,6 +422,64 @@ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *tagl void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting) { + // messaggio da scrivere sul logfile + char tmp_buf[2048]; + int n = 0; + size_t m = sizeof(tmp_buf); + + if(!filepath || !q || !taglia || !lock || !waiting) { + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" e' terminata con errore.\n", fd_c, filepath); + errno = EINVAL; + serror(MESY, fd_c, taglia, tmp_buf); + return; + } + + // cerco il file da impostare in modalita' locked + fileT *f = NULL; + f = find(q, filepath); + if(!f) { // file is not present + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" ma risulta assente.\n", fd_c, filepath); + errno = ENOENT; + serror(MENT, fd_c, taglia, tmp_buf); + destroyFile(f); + return; + } + + // provo a prendere la lock + if (openFileInQueue(q, filepath, 1, fd_c) == -1) { + // non siamo riusciti a prendere la lock al file, quindi aspettiamo che + // venga rilasciata mettendoci in attesa nella waiting list + if (errno == EPERM) { + if (pthread_mutex_lock(lock) == -1) { // begin ME + perror("lock"); + destroyFile(f); + return; + } + + if (addWaiting(waiting, filepath, fd_c) == -1) { + perror("addWaiting"); + pthread_mutex_unlock(lock); + destroyFile(f); + return; + } + + if (pthread_mutex_unlock(lock) == -1) { // end ME + perror("unlock"); + } + destroyFile(f); + return; + } + + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" e' terminata con errore.\n", fd_c, filepath); + errno = ENOLCK; + serror(MESE, fd_c, taglia, tmp_buf); + destroyFile(f); + return; + } + + n += snprintf(tmp_buf+n, m-n, "Client %ld ha richiesto una lockFile sul file \"%s\" e' terminata con successo\n", fd_c, filepath); + sendMessage(MEOK, fd_c, taglia, tmp_buf); + destroyFile(f); return; } diff --git a/lib/threadpool/fileQueue.c b/lib/threadpool/fileQueue.c index a87f7ee..82890ca 100644 --- a/lib/threadpool/fileQueue.c +++ b/lib/threadpool/fileQueue.c @@ -460,7 +460,8 @@ int openFileInQueue(queueT *q, char *filepath, int O_LOCK, int owner) { goto _end_open_file_queue; } - if((tmp->data)->O_LOCK == 1 && (tmp->data)->owner != owner) { // lock non del owner + // lock non del owner + if((tmp->data)->O_LOCK == 1 && (tmp->data)->owner != owner) { errno = EPERM; goto _end_open_file_queue; }