-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.c
387 lines (325 loc) · 9.3 KB
/
fs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright (c) 2021, 2024 Omar Polo <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Handles config and runtime files
*/
#include "compat.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pages.h"
#include "parser.h"
#include "telescope.h"
#include "session.h"
#include "utils.h"
#include "fs.h"
#ifndef nitems
#define nitems(x) (sizeof(x) / sizeof(x[0]))
#endif
static int select_non_dot(const struct dirent *);
static int select_non_dotdot(const struct dirent *);
static size_t join_path(char*, const char*, const char*, size_t);
static void getenv_default(char*, const char*, const char*, size_t);
static void mkdirs(const char*, mode_t);
static void init_paths(void);
/*
* Where to store user data. These are all equal to ~/.telescope if
* it exists.
*/
char config_path_base[PATH_MAX];
char data_path_base[PATH_MAX];
char cache_path_base[PATH_MAX];
char ctlsock_path[PATH_MAX];
char config_path[PATH_MAX];
char lockfile_path[PATH_MAX];
char bookmark_file[PATH_MAX];
char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
char crashed_file[PATH_MAX];
char session_file[PATH_MAX], session_file_tmp[PATH_MAX];
char history_file[PATH_MAX], history_file_tmp[PATH_MAX];
char cert_dir[PATH_MAX], cert_dir_tmp[PATH_MAX];
char certs_file[PATH_MAX], certs_file_tmp[PATH_MAX];
char cwd[PATH_MAX];
static int
select_non_dot(const struct dirent *d)
{
return strcmp(d->d_name, ".");
}
static int
select_non_dotdot(const struct dirent *d)
{
return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
}
static void
send_dir(struct tab *tab, const char *path)
{
struct buffer *buffer = &tab->buffer;
struct dirent **names;
int (*selector)(const struct dirent *) = select_non_dot;
int i, len;
#if notyet
/*
* need something to fake a redirect
*/
if (!has_suffix(path, "/")) {
xasprintf(&s, "%s/", path);
send_hdr(peerid, 30, s);
free(s);
return;
}
#endif
if (!strcmp(path, "/"))
selector = select_non_dotdot;
if ((len = scandir(path, &names, selector, alphasort)) == -1) {
load_page_from_str(tab, "# failure reading the directory\n");
return;
}
parser_init(buffer, &gemtext_parser);
parser_parsef(buffer, "# Index of %s\n\n", path);
for (i = 0; i < len; ++i) {
const char *sufx = "";
if (names[i]->d_type == DT_DIR)
sufx = "/";
parser_parsef(buffer, "=> %s%s\n", names[i]->d_name, sufx);
}
parser_free(tab);
free(names);
}
static int
is_dir(FILE *fp)
{
struct stat sb;
if (fstat(fileno(fp), &sb) == -1)
return 0;
return S_ISDIR(sb.st_mode);
}
static const struct parser *
file_type(const char *path)
{
const struct mapping {
const char *ext;
const struct parser *parser;
} ms[] = {
{"diff", &textpatch_parser},
{"gemini", &gemtext_parser},
{"gmi", &gemtext_parser},
{"markdown", &textplain_parser},
{"md", &textplain_parser},
{"patch", &gemtext_parser},
{NULL, NULL},
}, *m;
const char *dot;
if ((dot = strrchr(path, '.')) == NULL)
return &textplain_parser;
dot++;
for (m = ms; m->ext != NULL; ++m)
if (!strcmp(m->ext, dot))
return m->parser;
return &textplain_parser;
}
void
fs_load_url(struct tab *tab, const char *url)
{
const char *bpath = "bookmarks.gmi", *fallback = "# Not found\n";
const struct parser *parser = &gemtext_parser;
char path[PATH_MAX];
FILE *fp = NULL;
size_t i;
char buf[BUFSIZ];
struct page {
const char *name;
const char *path;
const uint8_t *data;
} pages[] = {
{"about", NULL, about_about},
{"blank", NULL, about_blank},
{"bookmarks", bpath, bookmarks},
{"crash", NULL, about_crash},
{"help", NULL, about_help},
{"license", NULL, about_license},
{"new", NULL, about_new},
}, *page = NULL;
if (!strncmp(url, "about:", 6)) {
url += 6;
for (i = 0; page == NULL && i < nitems(pages); ++i) {
if (!strcmp(url, pages[i].name))
page = &pages[i];
}
if (page == NULL)
goto done;
strlcpy(path, data_path_base, sizeof(path));
strlcat(path, "/", sizeof(path));
if (page->path != NULL)
strlcat(path, page->path, sizeof(path));
else {
strlcat(path, "page/about_", sizeof(path));
strlcat(path, page->name, sizeof(path));
strlcat(path, ".gmi", sizeof(path));
}
fallback = page->data;
} else if (!strncmp(url, "file://", 7)) {
url += 7;
strlcpy(path, url, sizeof(path));
parser = file_type(url);
} else
goto done;
if ((fp = fopen(path, "r")) == NULL)
goto done;
if (is_dir(fp)) {
send_dir(tab, path);
goto done;
}
parser_init(&tab->buffer, parser);
for (;;) {
size_t r;
r = fread(buf, 1, sizeof(buf), fp);
if (!parser_parse(&tab->buffer, buf, r))
break;
if (r != sizeof(buf))
break;
}
parser_free(tab);
done:
if (fp != NULL)
fclose(fp);
else
load_page_from_str(tab, fallback);
}
static size_t
join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
{
strlcpy(buf, lhs, buflen);
return strlcat(buf, rhs, buflen);
}
static void
getenv_default(char *buf, const char *name, const char *def, size_t buflen)
{
size_t ret;
char *home, *env;
if ((home = getenv("HOME")) == NULL)
errx(1, "HOME is not defined");
if ((env = getenv(name)) != NULL)
ret = strlcpy(buf, env, buflen);
else
ret = join_path(buf, home, def, buflen);
if (ret >= buflen)
errx(1, "buffer too small for %s", name);
}
static void
mkdirs(const char *path, mode_t mode)
{
char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
strlcpy(copy, path, sizeof(copy));
strlcpy(orig, path, sizeof(orig));
parent = dirname(copy);
if (!strcmp(parent, "/"))
return;
mkdirs(parent, mode);
if (mkdir(orig, mode) != 0) {
if (errno == EEXIST)
return;
err(1, "can't mkdir %s", orig);
}
}
static void
init_paths(void)
{
char xdg_config_base[PATH_MAX];
char xdg_data_base[PATH_MAX];
char xdg_cache_base[PATH_MAX];
char old_path[PATH_MAX];
char *home;
struct stat info;
if (getcwd(cwd, sizeof(cwd)) == NULL)
err(1, "getcwd failed");
/* old path */
if ((home = getenv("HOME")) == NULL)
errx(1, "HOME is not defined");
join_path(old_path, home, "/.telescope", sizeof(old_path));
/* if ~/.telescope exists, use that instead of xdg dirs */
if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
join_path(config_path_base, home, "/.telescope",
sizeof(config_path_base));
join_path(data_path_base, home, "/.telescope",
sizeof(data_path_base));
join_path(cache_path_base, home, "/.telescope",
sizeof(cache_path_base));
return;
}
/* xdg paths */
getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
sizeof(xdg_config_base));
getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
sizeof(xdg_data_base));
getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
sizeof(xdg_cache_base));
join_path(config_path_base, xdg_config_base, "/telescope",
sizeof(config_path_base));
join_path(data_path_base, xdg_data_base, "/telescope",
sizeof(data_path_base));
join_path(cache_path_base, xdg_cache_base, "/telescope",
sizeof(cache_path_base));
mkdirs(xdg_config_base, S_IRWXU);
mkdirs(xdg_data_base, S_IRWXU);
mkdirs(xdg_cache_base, S_IRWXU);
mkdirs(config_path_base, S_IRWXU);
mkdirs(data_path_base, S_IRWXU);
mkdirs(cache_path_base, S_IRWXU);
}
int
fs_init(void)
{
init_paths();
join_path(ctlsock_path, cache_path_base, "/ctl",
sizeof(ctlsock_path));
join_path(config_path, config_path_base, "/config",
sizeof(config_path));
join_path(lockfile_path, cache_path_base, "/lock",
sizeof(lockfile_path));
join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
sizeof(bookmark_file));
join_path(known_hosts_file, data_path_base, "/known_hosts",
sizeof(known_hosts_file));
join_path(known_hosts_tmp, cache_path_base,
"/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
join_path(session_file, cache_path_base, "/session",
sizeof(session_file));
join_path(session_file_tmp, cache_path_base, "/session.XXXXXXXXXX",
sizeof(session_file_tmp));
join_path(history_file, cache_path_base, "/history",
sizeof(history_file));
join_path(history_file_tmp, cache_path_base, "/history.XXXXXXXXXX",
sizeof(history_file_tmp));
join_path(cert_dir, data_path_base, "/certs/",
sizeof(cert_dir));
join_path(cert_dir_tmp, data_path_base, "/certs/id.XXXXXXXXXX",
sizeof(cert_dir_tmp));
join_path(certs_file, config_path_base, "/certs.conf",
sizeof(certs_file));
join_path(certs_file_tmp, config_path_base, "/certs.conf.XXXXXXXXXX",
sizeof(certs_file_tmp));
join_path(crashed_file, cache_path_base, "/crashed",
sizeof(crashed_file));
mkdirs(cert_dir, S_IRWXU);
return 0;
}