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 30, 2024
1 parent e3f8bc2 commit fc97fc7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 16 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,20 @@ This feature was implemented due to Github
## Permanent cache system

You can cache the files you have accessed permanently on your hard drive by
using the ``--cache`` flag. The file it caches persist across sessions.
using the ``--cache`` flag. The file it caches persist across sessions, but
can clear the cache using ``--clear-cache``

> [!WARNING]
> If ``--cache-location <dir>`` appears before ``--clear-cache``, the entire
> directory ``<dir>`` will be deleted instead. Take caution when specifying
> non-empty directories to be used as cache.
By default, the cache files are stored under ``${XDG_CACHE_HOME}/httpdirfs``,
which by default is ``${HOME}/.cache/httpdirfs``. Each HTTP directory gets its
``${HOME}/.cache/httpdirfs``, or the current working directory ``./.cache``,
whichever is found first. By default, ``${XDG_CACHE_HOME}/httpdirfs`` is
normally ``${HOME}/.cache/httpdirfs``.

Each HTTP directory gets its
own cache folder, they are named using the escaped URL of the HTTP directory.

Once a segment of the file has been downloaded once, it won't be downloaded
Expand Down
82 changes: 74 additions & 8 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,65 @@ static pthread_mutex_t cf_lock;
*/
static char *DATA_DIR;

/**
* \brief Return the home config or cache directory
*/
char *CacheSystem_get_home(int cache_or_config)
{
const char *xdg_home;
const char *xdg_default;
if (cache_or_config) {
xdg_home = "XDG_CACHE_HOME";
xdg_default = "/.cache";
} else {
xdg_home = "XDG_CONFIG_HOME";
xdg_default = "/.config";
}

char *home;

if (CONFIG.cache_dir && cache_or_config) {
return CONFIG.cache_dir;
} else {
/*
* "XDG_XYZ_HOME/HOME/.cache" > "HOME/.cache" > "./.cache"
*/
char *xdg = getenv(xdg_home);
if (!xdg) {
char *user_home = getenv("HOME");
if (!user_home) {
/*
* XDG_XYZ_HOME and HOME already are full paths. Not relying
* on environment PWD since it too may be undefined.
*/
char *curdir = realpath("./", NULL);

Check notice on line 69 in src/cache.c

View check run for this annotation

codefactor.io / CodeFactor

src/cache.c#L69

Use of strtrns (CWE-120, CWE-785) (flawfinder7-realpath)
if (!curdir) {
lprintf(fatal, "Could not create cache or config directory");
}
home = path_append(curdir, xdg_default);
} else {
home = path_append(user_home, xdg_default);
}
} else {
home = path_append(xdg_home, xdg_default);
}
}
return home;
}

/**
* \brief Calculate cache system directory path
*/
static char *CacheSystem_calc_dir(const char *url)
{
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);
}
char *cache_home = CacheSystem_get_home(1);

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 +187,29 @@ 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()
{
char *cache_home = CacheSystem_get_home(1);
char *cache_del;
lprintf(debug, "%s\n", cache_home);
if (CONFIG.cache_dir) {
cache_del = cache_home;
} else {
cache_del = path_append(cache_home, "/httpdirfs/");
}
nftw(cache_del, ntfw_cb, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
FREE(cache_home);
exit(EXIT_SUCCESS);
}

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

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

/**
* \brief Return the fullpath to the cache directory
*
* \param[in] cache_or_config 1 to get cache home, 0 for config
*/
char *CacheSystem_get_home(int cache_or_config);

/**
* \brief Create directories under the cache directory structure, if they do
* not already exist
Expand Down
15 changes: 9 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "fuse_local.h"
#include "link.h"
#include "cache.h"
#include "log.h"
#include "util.h"

Expand Down Expand Up @@ -118,12 +119,7 @@ void parse_config_file(char ***argv, int *argc)
{
char *full_path;
if (!config_path) {
char *xdg_config_home = getenv("XDG_CONFIG_HOME");
if (!xdg_config_home) {
char *home = getenv("HOME");
char *xdg_config_home_default = "/.config";
xdg_config_home = path_append(home, xdg_config_home_default);
}
char *xdg_config_home = CacheSystem_get_home(0);
full_path = path_append(xdg_config_home, "/httpdirfs/config");
} else {
full_path = config_path;
Expand Down Expand Up @@ -203,6 +199,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 */
{ "clear-cache", no_argument, NULL, 'L' }, /* 27 */
{ 0, 0, 0, 0 }
};
while ((c =
Expand Down Expand Up @@ -314,6 +311,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:
CacheSystem_clear();
break;
default:
fprintf(stderr, "see httpdirfs -h for usage\n");
return 1;
Expand Down Expand Up @@ -369,6 +369,9 @@ HTTPDirFS options:\n\
--cache Enable cache (default: off)\n\
--cache-location Set a custom cache location\n\
(default: \"${XDG_CACHE_HOME}/httpdirfs\")\n\
--clear-cache Delete the cache directory or the custom location\n\
specifid with `--cache-location`, if the option is\n\
seen first. Then exit in either case.\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 fc97fc7

Please sign in to comment.