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 002d65f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
30 changes: 30 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,35 @@ 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 *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);
}
cache_root_dir = path_append(xdg_cache_home, "/httpdirfs/");
}

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

nftw(cache_root_dir, ntfw_cb, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
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
2 changes: 1 addition & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ typedef struct {
/** \brief Refresh directory listing after refresh_timeout seconds*/
int refresh_timeout;
/*--------------- Cache related ---------------*/
/** \brief Whether cache mode is enabled */
/** \brief Whether to clear cache directory */
int cache_enabled;
/** \brief The cache location*/
char *cache_dir;
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 002d65f

Please sign in to comment.