#pragma once #ifndef _API_FILE #define _API_FILE #include #include "fileQueue.h" #include "taglialegna.h" #define C_CREATE 1 #define C_LOCK 2 /* structure for the list of clients waiting for a lock */ typedef struct struct_waiting { long fd; /* client waiting */ char *file; /* file waiting the lock on */ struct struct_waiting *next; } waiting_t; /** * @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 */ void openFile(char *filepath, int flags, queueT *q, long fd_c, taglia_t *taglia); /** * @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 */ void readFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia); /** * @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 */ void readNFiles(int num, queueT *q, long fd_c, taglia_t *taglia); /** * @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 */ void writeFile(char *filepath, size_t size, queueT *q, long fd_c, taglia_t *taglia, int append); /** * @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 */ void lockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); /** * @brief Relese a lock * * @param filepath * @param q * @param fd_c client * @param taglia * @param lock lock of the waiting_t list * @param waiting */ void unlockFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); /** * @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 */ void closeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); /** * @brief Delete a file * * @param filepath * @param q * @param fd_c client * @param taglia * @param lock lock of the waiting_t list * @param waiting */ void removeFile(char *filepath, queueT *q, long fd_c, taglia_t *taglia, pthread_mutex_t *lock, waiting_t **waiting); /** * @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 */ int sendFile(fileT *f, long fd_c, taglia_t *taglia); /** * @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 */ int addWaiting(waiting_t **waiting, char *filepath, int fd_c); /** * @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 */ int removeFirstWaiting(waiting_t **waiting, char *filepath); /** * @brief Frees the waiting_t structure * * @param waiting */ void clearWaiting(waiting_t **waiting); #endif // _API_FILE