added function lockFile

This commit is contained in:
elvis
2022-04-09 01:11:46 +02:00
parent 1045fa6eed
commit 029edc22fa
2 changed files with 64 additions and 1 deletions

View File

@ -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;
}

View File

@ -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;
}