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 16f8392
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
31 changes: 31 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,36 @@ 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
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 directory
* \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);

/**
* \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
4 changes: 3 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ 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 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
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 16f8392

Please sign in to comment.