Files
progettoso/lib/threadpool/apiFile.h

163 lines
3.4 KiB
C
Raw Normal View History

2022-03-16 23:45:39 +01:00
#pragma once
#ifndef _API_FILE
#define _API_FILE
#include <pthread.h>
#include "fileQueue.h"
#include "taglialegna.h"
2022-03-16 23:45:39 +01:00
2022-04-27 21:18:25 +02:00
#define C_CREATE 1
#define C_LOCK 2
/* structure for the list of clients waiting for a lock */
2022-03-18 20:49:28 +01:00
typedef struct struct_waiting {
long fd; /* client waiting */
char *file; /* file waiting the lock on */
struct struct_waiting *next;
2022-03-22 19:35:06 +01:00
} waiting_t;
2022-03-18 20:49:28 +01:00
2022-04-27 21:18:25 +02:00
2022-03-16 23:45:39 +01:00
/**
* @brief Open or create new file
*
* @param filepath name of the file
* @param flags
* @param q queue to insert the file into
* @param fd_c owner
* @param taglia
2022-03-16 23:45:39 +01:00
*/
2022-03-31 22:26:44 +02:00
void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia);
2022-03-16 23:45:39 +01:00
/**
* @brief Read a file and send it to a client
*
* @param filepath name of the file
* @param q queue where the file is
* @param fd_c client
* @param taglia
*/
2022-03-31 22:26:44 +02:00
void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia);
2022-03-16 23:45:39 +01:00
/**
* @brief Send to the client n files from the queue
*
* @param num number of files to send
* @param q
* @param fd_c client
* @param taglia
*/
2022-04-09 22:43:26 +02:00
void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia);
2022-03-16 23:45:39 +01:00
/**
* @brief Write to a file
*
* @param filepath
* @param size
* @param q
* @param fd_c client
* @param taglia
* @param append true if append, false if overwrite
*/
2022-03-31 22:26:44 +02:00
void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append);
2022-03-16 23:45:39 +01:00
/**
* @brief Get the lock of a file
*
* @param filepath
* @param q
* @param fd_c client
* @param taglia
* @param lock lock of the waiting_t list
* @param waiting
*/
2022-04-04 22:31:14 +02:00
void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting);
2022-03-16 23:45:39 +01:00
/**
* @brief Relese a lock
*
* @param filepath
* @param q
* @param fd_c client
* @param taglia
* @param lock lock of the waiting_t list
* @param waiting
*/
2022-03-31 22:26:44 +02:00
void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting);
2022-03-16 23:45:39 +01:00
/**
* @brief Close a file and relese locks
*
* @param filepath
* @param q
* @param fd_c client
* @param taglia
* @param lock lock of the waiting_t list
* @param waiting
*/
2022-03-31 22:26:44 +02:00
void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting);
2022-03-16 23:45:39 +01:00
/**
* @brief Delete a file
*
* @param filepath
* @param q
* @param fd_c client
* @param taglia
* @param lock lock of the waiting_t list
* @param waiting
*/
2022-03-31 22:26:44 +02:00
void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting);
2022-03-16 23:45:39 +01:00
/**
* @brief Send a file to the client
*
* @param f file to send
* @param fd_c client
* @param taglia
* @return 0 if success, -1 if error
*/
2022-03-31 22:26:44 +02:00
int sendFile(fileT *f, long fd_c, taglia_t *taglia);
2022-03-16 23:45:39 +01:00
/**
* @brief Add the client to the waiting list for the lock on the file
*
* @pre the lock of the waiting list must be acquired
*
* @param waiting
* @param filepath
* @param fd_c client
* @return 0 if success, -1 if error
*/
2022-04-10 20:51:43 +02:00
int addWaiting(waiting_t **waiting, char *filepath, int fd_c);
2022-03-16 23:45:39 +01:00
/**
* @brief Delete a file
*
* @pre the lock of the waiting list must be acquired
*
* @param waiting
* @param filepath
* @return the file descriptor of the client or -1 if no found
*/
2022-04-10 20:51:43 +02:00
int removeFirstWaiting(waiting_t **waiting, char *filepath);
2022-03-16 23:45:39 +01:00
/**
* @brief Frees the waiting_t structure
*
* @param waiting
*/
2022-03-22 19:35:06 +01:00
void clearWaiting(waiting_t **waiting);
2022-03-16 23:45:39 +01:00
#endif // _API_FILE