diff --git a/README.md b/README.md
index 84b9d18..f2afc3f 100644
--- a/README.md
+++ b/README.md
@@ -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 ``--cache-clear``
+
+> [!WARNING]
+> If ``--cache-location
`` appears before ``--cache-clear``, the entire
+> directory ```` 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
@@ -268,4 +278,4 @@ compatibility patches.
a whole bunch of code improvements and the improved build system.
- I would like to thank [-Archivist](https://www.reddit.com/user/-Archivist/)
for not providing FTP or WebDAV access to his server. This piece of software was
-written in direct response to his appalling behaviour.
+written in direct response to his appalling behaviour.
\ No newline at end of file
diff --git a/src/cache.c b/src/cache.c
index 9942943..f3a8481 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -35,20 +35,66 @@ static pthread_mutex_t cf_lock;
*/
static char *DATA_DIR;
-static char *CacheSystem_get_cache_home()
+
+/**
+ * \brief Return the fullpath to the cache or config directory
+ *
+ * \param[in] cache_or_config 1 to get cache home, 0 for config
+ */
+static char *CacheSystem_get_home(int cache_or_config)
{
- if (CONFIG.cache_dir) {
- return CONFIG.cache_dir;
+ 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 *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 *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 *cur_dir = realpath("./", NULL);
+ if (!cur_dir) {
+ lprintf(fatal, "Could not create cache or config directory");
+ }
+ home = path_append(cur_dir, xdg_default);
+ } else {
+ home = path_append(user_home, xdg_default);
+ }
+ } else {
+ home = path_append(xdg_home, xdg_default);
+ }
}
- return xdg_cache_home;
+ return home;
}
+char *CacheSystem_get_cache_home()
+{
+ return CacheSystem_get_home(1);
+}
+
+char *CacheSystem_get_config_home()
+{
+ return CacheSystem_get_home(0);
+}
+
+
/**
* \brief Calculate cache system directory path
*/
@@ -163,22 +209,18 @@ static int ntfw_cb(const char *fpath, const struct stat *sb, int typeflag, struc
return remove(fpath);
}
-void CacheSystem_clear(const char *path)
+void CacheSystem_clear()
{
- char *cache_root_dir;
- if (path) {
- cache_root_dir = strdup(path);
+ 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 {
- char *cache_home = CacheSystem_get_cache_home();
- cache_root_dir = path_append(cache_home, "/httpdirfs/");
- FREE(cache_home);
+ cache_del = path_append(cache_home, "/httpdirfs/");
}
-
- lprintf(debug, "%s\n", path);
-
- nftw(cache_root_dir, ntfw_cb, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
- FREE(cache_root_dir);
-
+ nftw(cache_del, ntfw_cb, 64, FTW_DEPTH | FTW_PHYS | FTW_MOUNT);
+ FREE(cache_home);
exit(EXIT_SUCCESS);
}
@@ -1121,4 +1163,4 @@ bgdl: {
}
return send;
-}
+}
\ No newline at end of file
diff --git a/src/cache.h b/src/cache.h
index fc38726..82886c9 100644
--- a/src/cache.h
+++ b/src/cache.h
@@ -95,7 +95,17 @@ void CacheSystem_init(const char *path, int url_supplied);
/**
* \brief clear the content of the cache directory
*/
-void CacheSystem_clear(const char *path);
+void CacheSystem_clear();
+
+/**
+ * \brief Return cache directory
+ */
+char *CacheSystem_get_cache_home();
+
+/**
+ * \brief Return config directory
+ */
+char *CacheSystem_get_config_home();
/**
* \brief Create directories under the cache directory structure, if they do
@@ -148,4 +158,4 @@ void Cache_delete(const char *fn);
*/
long Cache_read(Cache *cf, char *const output_buf, const off_t len,
const off_t offset_start);
-#endif
+#endif
\ No newline at end of file
diff --git a/src/main.c b/src/main.c
index dca2995..4b267e4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,6 @@
#include "fuse_local.h"
#include "link.h"
+#include "cache.h"
#include "log.h"
#include "util.h"
@@ -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_config_home();
full_path = path_append(xdg_config_home, "/httpdirfs/config");
} else {
full_path = config_path;
@@ -316,11 +312,7 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
curl_slist_append(CONFIG.http_headers, strdup(optarg));
break;
case 27:
- if (CONFIG.cache_dir) {
- CacheSystem_clear(CONFIG.cache_dir);
- } else {
- CacheSystem_clear(NULL);
- }
+ CacheSystem_clear();
break;
default:
fprintf(stderr, "see httpdirfs -h for usage\n");
@@ -377,7 +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\
- --cache-clear Clear cache directory and exit\n\
+ --cache-clear 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\
@@ -412,4 +406,4 @@ HTTPDirFS options:\n\
using the insecure username / hex encoded password\n\
scheme\n\
\n");
-}
+}
\ No newline at end of file