2019-04-21 00:46:08 +02:00
|
|
|
#ifndef UTIL_H
|
|
|
|
#define UTIL_H
|
|
|
|
/**
|
|
|
|
* \file util.h
|
|
|
|
* \brief utility functions
|
|
|
|
*/
|
|
|
|
|
2019-09-01 22:36:58 +02:00
|
|
|
#include <pthread.h>
|
2019-09-01 02:21:40 +02:00
|
|
|
#include <stdint.h>
|
2019-09-03 23:11:23 +02:00
|
|
|
#include <stdio.h>
|
2019-09-01 02:21:40 +02:00
|
|
|
|
2019-09-01 09:52:18 +02:00
|
|
|
/**
|
|
|
|
* \brief the maximum length of a path and a URL.
|
|
|
|
* \details This corresponds the maximum path length under Ext4.
|
|
|
|
*/
|
|
|
|
#define MAX_PATH_LEN 4096
|
|
|
|
|
2019-10-21 03:11:54 +02:00
|
|
|
/**
|
|
|
|
* \brief the maximum length of a filename.
|
|
|
|
* \details This corresponds the filename length under Ext4.
|
|
|
|
*/
|
2019-09-01 09:52:18 +02:00
|
|
|
#define MAX_FILENAME_LEN 255
|
|
|
|
|
2019-10-23 22:04:25 +02:00
|
|
|
/**
|
|
|
|
* \brief the default user agent string
|
|
|
|
*/
|
|
|
|
#define DEFAULT_USER_AGENT "HTTPDirFS-" VERSION
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief configuration data structure
|
|
|
|
* \note The opening curly bracket should be at line 39, so the code belong
|
|
|
|
* lines up with the initialisation code in util.c
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/** \brief HTTP username */
|
|
|
|
char *http_username;
|
|
|
|
/** \brief HTTP password */
|
|
|
|
char *http_password;
|
|
|
|
/** \brief HTTP proxy URL */
|
|
|
|
char *proxy;
|
|
|
|
/** \brief HTTP proxy username */
|
|
|
|
char *proxy_username;
|
|
|
|
/** \brief HTTP proxy password */
|
|
|
|
char *proxy_password;
|
|
|
|
/** \brief HTTP maximum connection count */
|
|
|
|
long max_conns;
|
|
|
|
/** \brief HTTP user agent*/
|
|
|
|
char *user_agent;
|
|
|
|
/** \brief The waiting time after getting HTTP 429 (too many requests) */
|
|
|
|
int http_wait_sec;
|
|
|
|
|
|
|
|
/** \brief Whether cache mode is enabled */
|
|
|
|
int cache_enabled;
|
|
|
|
/** \brief The cache location*/
|
|
|
|
char *cache_dir;
|
|
|
|
/** \brief The size of each download segment for cache mode */
|
|
|
|
int data_blksz;
|
|
|
|
/** \brief The maximum segment count for a single cache file */
|
|
|
|
int max_segbc;
|
|
|
|
|
2019-10-23 22:36:08 +02:00
|
|
|
/** \brief Whether we are using the Sonic mode */
|
2019-10-23 22:04:25 +02:00
|
|
|
int sonic_mode;
|
2019-10-23 22:36:08 +02:00
|
|
|
/** \brief The Sonic server username */
|
2019-10-23 22:04:25 +02:00
|
|
|
char *sonic_username;
|
2019-10-23 22:36:08 +02:00
|
|
|
/** \brief The Sonic server password */
|
2019-10-23 22:04:25 +02:00
|
|
|
char *sonic_password;
|
|
|
|
} ConfigStruct;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief The Configuration data structure
|
|
|
|
*/
|
|
|
|
extern ConfigStruct CONFIG;
|
|
|
|
|
2019-09-03 23:11:23 +02:00
|
|
|
/**
|
|
|
|
* \brief append a path
|
|
|
|
* \details This function appends a path with the next level, while taking the
|
|
|
|
* trailing slash of the upper level into account.
|
2019-10-24 01:44:18 +02:00
|
|
|
* \note You need to free the char * after use.
|
2019-09-03 23:11:23 +02:00
|
|
|
*/
|
|
|
|
char *path_append(const char *path, const char *filename);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief division, but rounded to the nearest integer rather than truncating
|
|
|
|
*/
|
|
|
|
int64_t round_div(int64_t a, int64_t b);
|
|
|
|
|
2019-09-01 09:52:18 +02:00
|
|
|
/**
|
2019-10-21 04:28:57 +02:00
|
|
|
* \brief wrapper for pthread_mutex_lock(), with error handling
|
2019-09-01 09:52:18 +02:00
|
|
|
*/
|
2019-09-01 22:36:58 +02:00
|
|
|
void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x);
|
2019-09-01 02:21:40 +02:00
|
|
|
|
2019-09-01 09:52:18 +02:00
|
|
|
/**
|
2019-10-21 04:28:57 +02:00
|
|
|
* \brief wrapper for pthread_mutex_unlock(), with error handling
|
2019-09-01 09:52:18 +02:00
|
|
|
*/
|
2019-09-01 22:36:58 +02:00
|
|
|
void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x);
|
|
|
|
|
2019-09-03 15:47:12 +02:00
|
|
|
/**
|
2019-10-21 04:28:57 +02:00
|
|
|
* \brief wrapper for exit(EXIT_FAILURE), with error handling
|
2019-09-03 15:47:12 +02:00
|
|
|
*/
|
2019-09-03 23:37:27 +02:00
|
|
|
void exit_failure(void);
|
2019-09-03 15:47:12 +02:00
|
|
|
|
2019-04-25 00:58:26 +02:00
|
|
|
/**
|
2019-09-03 23:11:23 +02:00
|
|
|
* \brief erase a string from the terminal
|
2019-04-25 00:58:26 +02:00
|
|
|
*/
|
2019-09-03 23:11:23 +02:00
|
|
|
void erase_string(FILE *file, size_t max_len, char *s);
|
2019-04-25 00:58:26 +02:00
|
|
|
|
2019-10-21 04:28:57 +02:00
|
|
|
/**
|
|
|
|
* \brief generate the salt for authentication string
|
|
|
|
* \details this effectively generates a UUID string, which we use as the salt
|
|
|
|
* \return a pointer to a 37-char array with the salt.
|
|
|
|
*/
|
|
|
|
char *generate_salt();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief generate the md5sum of a string
|
|
|
|
* \param[in] str a character array for the input string
|
|
|
|
* \return a pointer to a 33-char array with the salt
|
|
|
|
*/
|
|
|
|
char *generate_md5sum(const char *str);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief wrapper for calloc(), with error handling
|
|
|
|
*/
|
|
|
|
void *CALLOC(size_t nmemb, size_t size);
|
|
|
|
|
2019-10-23 22:04:25 +02:00
|
|
|
/**
|
|
|
|
* \brief initialise the configuration data structure
|
|
|
|
*/
|
|
|
|
void Config_init(void);
|
|
|
|
|
2019-04-21 00:46:08 +02:00
|
|
|
#endif
|