diff --git a/src/cache.c b/src/cache.c index 18044d3..9942943 100644 --- a/src/cache.c +++ b/src/cache.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -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)) { @@ -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. diff --git a/src/cache.h b/src/cache.h index 797279c..fc38726 100644 --- a/src/cache.h +++ b/src/cache.h @@ -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 diff --git a/src/main.c b/src/main.c index 4c817b8..dca2995 100644 --- a/src/main.c +++ b/src/main.c @@ -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 = @@ -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; @@ -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\