Skip to content

Commit

Permalink
Add option to clear cache
Browse files Browse the repository at this point in the history
  • Loading branch information
EyitopeIO committed Aug 29, 2024
1 parent e3f8bc2 commit 9946e0a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <ftw.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Expand Down Expand Up @@ -144,6 +145,24 @@ void CacheSystem_init(const char *path, int url_supplied)
CACHE_SYSTEM_INIT = 1;
}

#define NA __attribute__ ((unused))
static int ntfw_cb(const char *fpath, NA const struct stat *sb, NA int typeflag, NA struct FTW *ftwbuf)
{
return remove(fpath);
}

void CacheSystem_clear(const char *path, int url_supplied)
{
if (url_supplied) {
path = CacheSystem_calc_dir(path);
}

lprintf(debug, "%s\n", path);

nftw(path, ntfw_cb, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
}


/**
* \brief read a metadata file
* \return 0 on success, errno on error.
Expand Down
10 changes: 10 additions & 0 deletions src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ extern char *META_DIR;
*/
void CacheSystem_init(const char *path, int url_supplied);

/**
* \brief clear the cache system directories
* \details This function basically clears these paths on disk:
* - META_DIR
* - DATA_DIR
*
* If these directories exist, they will be deleted.
*/
void CacheSystem_clear(const char *path, int url_supplied);

/**
* \brief Create directories under the cache directory structure, if they do
* not already exist
Expand Down
2 changes: 2 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ void Config_init(void)
/*--------------- Cache related ---------------*/
CONFIG.cache_enabled = 0;

CONFIG.cache_clear = 0;

CONFIG.cache_dir = NULL;

CONFIG.data_blksz = DEFAULT_DATA_BLKSZ;
Expand Down
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ typedef struct {
/*--------------- Cache related ---------------*/
/** \brief Whether cache mode is enabled */
int cache_enabled;
/** \brief Whether to clear any previous cache */
int cache_clear;
/** \brief The cache location*/
char *cache_dir;
/** \brief The size of each download segment for cache mode */
Expand Down
15 changes: 15 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ int main(int argc, char **argv)
activate Sonic mode.\n");
exit(EXIT_FAILURE);
}

if (CONFIG.cache_clear) {
if (CONFIG.cache_dir) {
CacheSystem_clear(CONFIG.cache_dir, 0);
} else {
CacheSystem_clear(base_url, 1);
}
exit(EXIT_SUCCESS);
}

if (!LinkSystem_init(base_url)) {
fprintf(stderr, "Network initialisation failed.\n");
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -203,6 +213,7 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
{ "proxy-cacert", required_argument, NULL, 'L' }, /* 24 */
{ "refresh-timeout", required_argument, NULL, 'L' }, /* 25 */
{ "http-header", required_argument, NULL, 'L' }, /* 26 */
{ "cache-clear", no_argument, NULL, 'L' }, /* 27 */
{ 0, 0, 0, 0 }
};
while ((c =
Expand Down Expand Up @@ -314,6 +325,9 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
CONFIG.http_headers =
curl_slist_append(CONFIG.http_headers, strdup(optarg));
break;
case 27:
CONFIG.cache_clear = 1;
break;
default:
fprintf(stderr, "see httpdirfs -h for usage\n");
return 1;
Expand Down Expand Up @@ -369,6 +383,7 @@ HTTPDirFS options:\n\
--cache Enable cache (default: off)\n\
--cache-location Set a custom cache location\n\
(default: \"${XDG_CACHE_HOME}/httpdirfs\")\n\
--cache-clear Clear any previous cache and exit\n\
--cacert Certificate authority for the server\n\
--dl-seg-size Set cache download segment size, in MB (default: 8)\n\
Note: this setting is ignored if previously\n\
Expand Down

0 comments on commit 9946e0a

Please sign in to comment.