Skip to content

Commit

Permalink
Fix segfault when HOME is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
EyitopeIO committed Aug 31, 2024
1 parent 194a10f commit 1496f64
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 41 deletions.
16 changes: 13 additions & 3 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 ``--cache-clear``

> [!WARNING]
> If ``--cache-location <dir>`` appears before ``--cache-clear``, 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 Expand Up @@ -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.
88 changes: 65 additions & 23 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check notice on line 72 in src/cache.c

View check run for this annotation

codefactor.io / CodeFactor

src/cache.c#L72

Use of strtrns (CWE-120, CWE-785) (flawfinder7-realpath)
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
*/
Expand Down Expand Up @@ -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_cache_home();
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);
}

Expand Down Expand Up @@ -1121,4 +1163,4 @@ bgdl: {
}

return send;
}
}
14 changes: 12 additions & 2 deletions src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
20 changes: 7 additions & 13 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_config_home();
full_path = path_append(xdg_config_home, "/httpdirfs/config");
} else {
full_path = config_path;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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\
Expand Down Expand Up @@ -412,4 +406,4 @@ HTTPDirFS options:\n\
using the insecure username / hex encoded password\n\
scheme\n\
\n");
}
}

0 comments on commit 1496f64

Please sign in to comment.