Skip to content

Commit 2835201

Browse files
committed
Closing issue #33, now set a default cache directory
1 parent 99b530e commit 2835201

9 files changed

Lines changed: 110 additions & 29 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=1.1.4
1+
VERSION=1.1.5
22

33
CFLAGS+= -g -O2 -Wall -Wextra -Wshadow\
44
-D_FILE_OFFSET_BITS=64 -DVERSION=\"$(VERSION)\" \

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,27 @@ the filesystem is visiting.
2323

2424
Useful options:
2525

26-
-f Run HTTPDirFS in foreground
2726
-u --username HTTP authentication username
2827
-p --password HTTP authentication password
2928
-P --proxy Proxy for libcurl, for more details refer to
3029
https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html
3130
--proxy-username Username for the proxy
3231
--proxy-password Password for the proxy
33-
--cache Set a cache folder, by default this is disabled
32+
--cache Enable cache, by default this is disabled
33+
--cache-location Set a custom cache location, by default it is
34+
located in ${XDG_CACHE_HOME}/httpdirfs
3435
--dl-seg-size The size of each download segment in MB,
3536
default to 8MB.
3637
--max-seg-count The maximum number of download segments a file
37-
can have. By default it is set to 1048576. This
38-
means the maximum memory usage per file is 1MB
39-
memory. This allows caching file up to 8TB in
38+
can have. By default it is set to 128*1024. This
39+
means the maximum memory usage per file is 128KB
40+
memory. This allows caching file up to 1TB in
4041
size, assuming you are using the default segment
4142
size.
4243
--max-conns The maximum number of network connections that
4344
libcurl is allowed to make, default to 10.
4445
--retry-wait The waiting interval in seconds before making an
45-
HTTP request, after encountering an error,
46+
HTTP request, after encountering an error,
4647
default to 5 seconds.
4748
--user-agent The user agent string, default to "HTTPDirFS".
4849

src/cache.c

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,74 @@ static char *META_DIR;
5252
*/
5353
static char *DATA_DIR;
5454

55-
void CacheSystem_init(const char *path)
55+
/**
56+
* \brief Calculate cache system directory
57+
*/
58+
static char *CacheSystem_calc_dir(const char *url)
5659
{
60+
char *xdg_cache_home = getenv("XDG_CACHE_HOME");
61+
if (!xdg_cache_home) {
62+
char *home = getenv("HOME");
63+
char *xdg_cache_home_default = "/.cache";
64+
xdg_cache_home = path_append(home, xdg_cache_home_default);
65+
}
66+
if (mkdir(xdg_cache_home, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
67+
&& (errno != EEXIST)) {
68+
fprintf(stderr, "CacheSystem_calc_dir(): mkdir(): %s\n",
69+
strerror(errno));
70+
}
71+
char *cache_dir_root = path_append(xdg_cache_home, "/httpdirfs/");
72+
if (mkdir(cache_dir_root, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
73+
&& (errno != EEXIST)) {
74+
fprintf(stderr, "CacheSystem_calc_dir(): mkdir(): %s\n",
75+
strerror(errno));
76+
}
77+
78+
char *fn = path_append(cache_dir_root, "/CACHEDIR.TAG");
79+
FILE *fp = fopen(fn, "w");
80+
if (fn) {
81+
fprintf(fp,
82+
"Signature: 8a477f597d28d172789f06886806bc55\n\
83+
# This file is a cache directory tag created by httpdirfs.\n\
84+
# For information about cache directory tags, see:\n\
85+
# http://www.brynosaurus.com/cachedir/\n");
86+
} else {
87+
fprintf(stderr, "CacheSystem_calc_dir(): fopen(%s): %s", fn,
88+
strerror(errno));
89+
}
90+
if (ferror(fp)) {
91+
fprintf(stderr,
92+
"CacheSystem_calc_dir(): fwrite(): encountered error!\n");
93+
}
94+
if (fclose(fp)) {
95+
fprintf(stderr, "CacheSystem_calc_dir(): fclose(%s): %s\n", fn,
96+
strerror(errno));
97+
}
98+
CURL* c = curl_easy_init();
99+
char *escaped_url = curl_easy_escape(c, url, 0);
100+
char *full_path = path_append(cache_dir_root, escaped_url);
101+
if (mkdir(full_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
102+
&& (errno != EEXIST)) {
103+
fprintf(stderr, "CacheSystem_calc_dir(): mkdir(): %s\n",
104+
strerror(errno));
105+
}
106+
free(cache_dir_root);
107+
curl_free(escaped_url);
108+
curl_easy_cleanup(c);
109+
return full_path;
110+
}
111+
112+
void CacheSystem_init(const char *path, int url_supplied)
113+
{
114+
if (url_supplied) {
115+
path = CacheSystem_calc_dir(path);
116+
}
117+
118+
fprintf(stderr, "CacheSystem_init(): directory: %s\n", path);
57119
DIR* dir;
58120

59-
/*
60-
* Check if the top-level cache directory exists, if not, exit the
61-
* program. We don't want to unintentionally create a folder
62-
*/
63121
dir = opendir(path);
64-
if (dir) {
65-
closedir(dir);
66-
} else {
122+
if (!dir) {
67123
fprintf(stderr,
68124
"CacheSystem_init(): opendir(): %s\n", strerror(errno));
69125
exit(EXIT_FAILURE);
@@ -208,7 +264,7 @@ static int Meta_write(Cache *cf)
208264
/* Error checking for fwrite */
209265
if (ferror(fp)) {
210266
fprintf(stderr,
211-
"Meta_write(): fwrite(): encountered error (from ferror)!\n");
267+
"Meta_write(): fwrite(): encountered error!\n");
212268
return -1;
213269
}
214270

@@ -297,7 +353,7 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
297353
if (ferror(cf->dfp)) {
298354
/* filesystem error */
299355
fprintf(stderr,
300-
"Data_read(): fread(): encountered error (from ferror)!\n");
356+
"Data_read(): fread(): encountered error!\n");
301357
}
302358
}
303359

@@ -338,7 +394,7 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
338394
if (ferror(cf->dfp)) {
339395
/* filesystem error */
340396
fprintf(stderr,
341-
"Data_write(): fwrite(): encountered error (from ferror)!\n");
397+
"Data_write(): fwrite(): encountered error!\n");
342398
}
343399
}
344400

src/cache.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ extern int DATA_BLK_SZ;
5858
*/
5959
extern int MAX_SEGBC;
6060

61-
6261
/**
6362
* \brief initialise the cache system directories
6463
* \details This function basically sets up the following variables:
@@ -68,7 +67,7 @@ extern int MAX_SEGBC;
6867
* If these directories do not exist, they will be created.
6968
* \note Called by parse_arg_list(), verified to be working
7069
*/
71-
void CacheSystem_init(const char *dir);
70+
void CacheSystem_init(const char *path, int path_supplied);
7271

7372
/**
7473
* \brief Create directories under the cache directory structure, if they do

src/link.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,12 @@ static void LinkTable_fill(LinkTable *linktbl)
195195
strncpy(this_link->f_url, url, MAX_PATH_LEN);
196196
free(url);
197197
char *unescaped_linkname;
198-
unescaped_linkname = curl_easy_unescape(NULL, this_link->linkname,
198+
CURL* c = curl_easy_init();
199+
unescaped_linkname = curl_easy_unescape(c, this_link->linkname,
199200
0, NULL);
200201
strncpy(this_link->linkname, unescaped_linkname, MAX_FILENAME_LEN);
201202
curl_free(unescaped_linkname);
203+
curl_easy_cleanup(c);
202204
Link_get_stat(this_link);
203205
}
204206
/* Block until the LinkTable is filled up */
@@ -313,7 +315,8 @@ HTTP %ld\n", url, http_resp);
313315

314316
int skip_fill = 0;
315317
char *unescaped_path;
316-
unescaped_path = curl_easy_unescape(NULL, url + ROOT_LINK_OFFSET, 0, NULL);
318+
CURL* c = curl_easy_init();
319+
unescaped_path = curl_easy_unescape(c, url + ROOT_LINK_OFFSET, 0, NULL);
317320
if (CACHE_SYSTEM_INIT) {
318321
CacheDir_create(unescaped_path);
319322
LinkTable *disk_linktbl;
@@ -344,6 +347,7 @@ HTTP %ld\n", url, http_resp);
344347
}
345348

346349
curl_free(unescaped_path);
350+
curl_easy_cleanup(c);
347351

348352
LinkTable_print(linktbl);
349353
return linktbl;

src/main.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,13 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
128128
{"proxy", required_argument, NULL, 'P'}, /* 5 */
129129
{"proxy-username", required_argument, NULL, 'L'}, /* 6 */
130130
{"proxy-password", required_argument, NULL, 'L'}, /* 7 */
131-
{"cache", required_argument, NULL, 'L'}, /* 8 */
131+
{"cache", no_argument, NULL, 'L'}, /* 8 */
132132
{"dl-seg-size", required_argument, NULL, 'L'}, /* 9 */
133133
{"max-seg-count", required_argument, NULL, 'L'}, /* 10 */
134134
{"max-conns", required_argument, NULL, 'L'}, /* 11 */
135135
{"user-agent", required_argument, NULL, 'L'}, /* 12 */
136136
{"retry-wait", required_argument, NULL, 'L'}, /* 13 */
137+
{"cache-location", required_argument, NULL, 'L'}, /* 14 */
137138
{0, 0, 0, 0}
138139
};
139140
while ((c =
@@ -181,7 +182,7 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
181182
NETWORK_CONFIG.proxy_pass = strdup(optarg);
182183
break;
183184
case 8:
184-
CacheSystem_init(optarg);
185+
NETWORK_CONFIG.cache_enabled = 1;
185186
break;
186187
case 9:
187188
DATA_BLK_SZ = atoi(optarg) * 1024 * 1024;
@@ -198,6 +199,9 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
198199
case 13:
199200
HTTP_429_WAIT = atoi(optarg);
200201
break;
202+
case 14:
203+
NETWORK_CONFIG.cache_dir = strdup(optarg);
204+
break;
201205
default:
202206
fprintf(stderr, "Error: Invalid option\n");
203207
add_arg(fuse_argv, fuse_argc, "--help");
@@ -250,13 +254,15 @@ static void print_http_options()
250254
https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html\n\
251255
--proxy-username Username for the proxy\n\
252256
--proxy-password Password for the proxy\n\
253-
--cache Set a cache folder, by default this is disabled\n\
257+
--cache Enable cache, by default this is disabled\n\
258+
--cache-location Set a custom cache location, by default it is \n\
259+
located in ${XDG_CACHE_HOME}/httpdirfs \n\
254260
--dl-seg-size The size of each download segment in MB,\n\
255261
default to 8MB.\n\
256262
--max-seg-count The maximum number of download segments a file\n\
257-
can have. By default it is set to 1048576. This\n\
258-
means the maximum memory usage per file is 1MB\n\
259-
memory. This allows caching file up to 8TB in\n\
263+
can have. By default it is set to 128*1024. This\n\
264+
means the maximum memory usage per file is 128KB\n\
265+
memory. This allows caching file up to 1TB in\n\
260266
size, assuming you are using the default segment\n\
261267
size.\n\
262268
--max-conns The maximum number of network connections that\n\

src/network.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "network.h"
22

3+
#include "cache.h"
4+
35
#include <openssl/crypto.h>
46

57
#include <errno.h>
@@ -217,6 +219,8 @@ void network_config_init()
217219
NETWORK_CONFIG.proxy_pass = NULL;
218220
NETWORK_CONFIG.max_conns = DEFAULT_NETWORK_MAX_CONNS;
219221
NETWORK_CONFIG.user_agent = "HTTPDirFS";
222+
NETWORK_CONFIG.cache_enabled = 0;
223+
NETWORK_CONFIG.cache_dir = NULL;
220224
}
221225

222226
LinkTable *network_init(const char *url)
@@ -288,6 +292,15 @@ LinkTable *network_init(const char *url)
288292
ROOT_LINK_OFFSET += 1;
289293
}
290294

295+
/* ----------- Enable cache system --------------------*/
296+
if (NETWORK_CONFIG.cache_enabled) {
297+
if (NETWORK_CONFIG.cache_dir) {
298+
CacheSystem_init(NETWORK_CONFIG.cache_dir, 0);
299+
} else {
300+
CacheSystem_init(url, 1);
301+
}
302+
}
303+
291304
/* ----------- Create the root link table --------------*/
292305
ROOT_LINK_TBL = LinkTable_new(url);
293306
return ROOT_LINK_TBL;

src/network.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef struct {
3535
long max_conns;
3636
char *user_agent;
3737
int http_429_wait;
38+
char *cache_dir;
39+
int cache_enabled;
3840
} NetworkConfigStruct;
3941

4042
/** \brief The waiting time after getting HTTP 429 */

src/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* \details This function appends a path with the next level, while taking the
2323
* trailing slash of the upper level into account.
2424
*
25-
* Please free the char * pointer after use.
25+
* Please free the char * after use.
2626
*/
2727
char *path_append(const char *path, const char *filename);
2828

0 commit comments

Comments
 (0)