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 82dc0c1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
50 changes: 44 additions & 6 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 All @@ -34,23 +35,33 @@ static pthread_mutex_t cf_lock;
*/
static char *DATA_DIR;

/**
* \brief Calculate cache system directory path
*/
static char *CacheSystem_calc_dir(const char *url)
static char *CacheSystem_get_cache_home()
{
if (CONFIG.cache_dir) {
return CONFIG.cache_dir;
}
char *xdg_cache_home = getenv("XDG_CACHE_HOME");
if (!xdg_cache_home) {
char *home = getenv("HOME");
char *xdg_cache_home_default = "/.cache";
xdg_cache_home = path_append(home, xdg_cache_home_default);
}
return xdg_cache_home;
}

/**
* \brief Calculate cache system directory path
*/
static char *CacheSystem_calc_dir(const char *url)
{
char *cache_home = CacheSystem_get_cache_home();

if (mkdir
(xdg_cache_home, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
(cache_home, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
char *cache_dir_root = path_append(xdg_cache_home, "/httpdirfs/");
char *cache_dir_root = path_append(cache_home, "/httpdirfs/");
if (mkdir
(cache_dir_root, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
Expand Down Expand Up @@ -144,6 +155,33 @@ void CacheSystem_init(const char *path, int url_supplied)
CACHE_SYSTEM_INIT = 1;
}

static int ntfw_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
(void) sb;
(void) typeflag;
(void) ftwbuf;
return remove(fpath);
}

void CacheSystem_clear(const char *path)
{
char *cache_root_dir;
if (path) {
cache_root_dir = strdup(path);
} else {
char *cache_home = CacheSystem_get_cache_home();
cache_root_dir = path_append(cache_home, "/httpdirfs/");
FREE(cache_home);
}

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

nftw(cache_root_dir, ntfw_cb, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
FREE(cache_root_dir);

exit(EXIT_SUCCESS);
}

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

/**
* \brief clear the content of the cache directory
*/
void CacheSystem_clear(const char *path);

/**
* \brief Create directories under the cache directory structure, if they do
* not already exist
Expand Down
9 changes: 9 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,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 +315,13 @@ 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:
if (CONFIG.cache_dir) {
CacheSystem_clear(CONFIG.cache_dir);
} else {
CacheSystem_clear(NULL);
}
break;
default:
fprintf(stderr, "see httpdirfs -h for usage\n");
return 1;
Expand Down Expand Up @@ -369,6 +377,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 cache directory 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 82dc0c1

Please sign in to comment.