2 0ec5e323 2024-02-03 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
4 35e1f40a 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 35e1f40a 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 35e1f40a 2021-03-14 op * copyright notice and this permission notice appear in all copies.
8 35e1f40a 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 35e1f40a 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 35e1f40a 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 35e1f40a 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 35e1f40a 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 35e1f40a 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 35e1f40a 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 eb2ed626 2021-10-07 op * Handles config and runtime files
21 786e6deb 2021-07-21 op #include "compat.h"
23 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
24 65124267 2021-08-13 op #include <sys/types.h>
26 65124267 2021-08-13 op #include <dirent.h>
27 35e1f40a 2021-03-14 op #include <errno.h>
28 35e1f40a 2021-03-14 op #include <limits.h>
29 eb2ed626 2021-10-07 op #include <libgen.h>
30 35e1f40a 2021-03-14 op #include <stdio.h>
31 35e1f40a 2021-03-14 op #include <stdlib.h>
32 35e1f40a 2021-03-14 op #include <string.h>
33 35e1f40a 2021-03-14 op #include <unistd.h>
35 5a824be4 2021-07-13 op #include "pages.h"
36 f63b8f73 2022-04-24 op #include "parser.h"
37 c1d27b0e 2024-06-14 op #include "telescope.h"
38 eb9cbcba 2022-01-03 op #include "session.h"
39 9d65b1d9 2022-01-11 op #include "utils.h"
41 f63b8f73 2022-04-24 op #include "fs.h"
43 f63b8f73 2022-04-24 op #ifndef nitems
44 f63b8f73 2022-04-24 op #define nitems(x) (sizeof(x) / sizeof(x[0]))
47 5cbe1763 2021-08-13 op static int select_non_dot(const struct dirent *);
48 5cbe1763 2021-08-13 op static int select_non_dotdot(const struct dirent *);
49 eb2ed626 2021-10-07 op static size_t join_path(char*, const char*, const char*, size_t);
50 eb2ed626 2021-10-07 op static void getenv_default(char*, const char*, const char*, size_t);
51 eb2ed626 2021-10-07 op static void mkdirs(const char*, mode_t);
52 6c8ddace 2022-01-03 op static void init_paths(void);
55 f88fbabc 2021-11-27 op * Where to store user data. These are all equal to ~/.telescope if
58 fb3d08c1 2021-10-07 op char config_path_base[PATH_MAX];
59 fb3d08c1 2021-10-07 op char data_path_base[PATH_MAX];
60 fb3d08c1 2021-10-07 op char cache_path_base[PATH_MAX];
62 4cf6ba13 2022-02-11 op char ctlsock_path[PATH_MAX];
63 eb2ed626 2021-10-07 op char config_path[PATH_MAX];
64 fb3d08c1 2021-10-07 op char lockfile_path[PATH_MAX];
65 fb3d08c1 2021-10-07 op char bookmark_file[PATH_MAX];
66 fb3d08c1 2021-10-07 op char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
67 fb3d08c1 2021-10-07 op char crashed_file[PATH_MAX];
68 de6a6a40 2022-04-24 op char session_file[PATH_MAX], session_file_tmp[PATH_MAX];
69 de6a6a40 2022-04-24 op char history_file[PATH_MAX], history_file_tmp[PATH_MAX];
70 f0e62b85 2024-02-15 op char cert_dir[PATH_MAX], cert_dir_tmp[PATH_MAX];
71 d35e18b3 2024-02-04 op char certs_file[PATH_MAX], certs_file_tmp[PATH_MAX];
73 26e95b82 2024-01-16 op char cwd[PATH_MAX];
76 f63b8f73 2022-04-24 op select_non_dot(const struct dirent *d)
78 f63b8f73 2022-04-24 op return strcmp(d->d_name, ".");
82 f63b8f73 2022-04-24 op select_non_dotdot(const struct dirent *d)
84 f63b8f73 2022-04-24 op return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
88 f63b8f73 2022-04-24 op send_dir(struct tab *tab, const char *path)
90 c1d27b0e 2024-06-14 op struct buffer *buffer = &tab->buffer;
91 f63b8f73 2022-04-24 op struct dirent **names;
92 f63b8f73 2022-04-24 op int (*selector)(const struct dirent *) = select_non_dot;
97 f63b8f73 2022-04-24 op * need something to fake a redirect
100 f63b8f73 2022-04-24 op if (!has_suffix(path, "/")) {
101 3d89457c 2024-06-18 thomas.ad xasprintf(&s, "%s/", path);
102 f63b8f73 2022-04-24 op send_hdr(peerid, 30, s);
108 f63b8f73 2022-04-24 op if (!strcmp(path, "/"))
109 f63b8f73 2022-04-24 op selector = select_non_dotdot;
111 f63b8f73 2022-04-24 op if ((len = scandir(path, &names, selector, alphasort)) == -1) {
112 f63b8f73 2022-04-24 op load_page_from_str(tab, "# failure reading the directory\n");
116 c1d27b0e 2024-06-14 op parser_init(buffer, &gemtext_parser);
117 c1d27b0e 2024-06-14 op parser_parsef(buffer, "# Index of %s\n\n", path);
119 f63b8f73 2022-04-24 op for (i = 0; i < len; ++i) {
120 f63b8f73 2022-04-24 op const char *sufx = "";
122 f63b8f73 2022-04-24 op if (names[i]->d_type == DT_DIR)
125 c1d27b0e 2024-06-14 op parser_parsef(buffer, "=> %s%s\n", names[i]->d_name, sufx);
128 f63b8f73 2022-04-24 op parser_free(tab);
133 f63b8f73 2022-04-24 op is_dir(FILE *fp)
135 f63b8f73 2022-04-24 op struct stat sb;
137 f63b8f73 2022-04-24 op if (fstat(fileno(fp), &sb) == -1)
140 f63b8f73 2022-04-24 op return S_ISDIR(sb.st_mode);
143 fd1c80ce 2024-06-14 op static const struct parser *
144 65124267 2021-08-13 op file_type(const char *path)
146 21268f10 2022-04-24 op const struct mapping {
147 fd1c80ce 2024-06-14 op const char *ext;
148 fd1c80ce 2024-06-14 op const struct parser *parser;
150 c1d27b0e 2024-06-14 op {"diff", &textpatch_parser},
151 c1d27b0e 2024-06-14 op {"gemini", &gemtext_parser},
152 c1d27b0e 2024-06-14 op {"gmi", &gemtext_parser},
153 c1d27b0e 2024-06-14 op {"markdown", &textplain_parser},
154 c1d27b0e 2024-06-14 op {"md", &textplain_parser},
155 c1d27b0e 2024-06-14 op {"patch", &gemtext_parser},
156 fb4dc49f 2021-08-13 op {NULL, NULL},
158 21268f10 2022-04-24 op const char *dot;
160 65124267 2021-08-13 op if ((dot = strrchr(path, '.')) == NULL)
161 c1d27b0e 2024-06-14 op return &textplain_parser;
165 65124267 2021-08-13 op for (m = ms; m->ext != NULL; ++m)
166 65124267 2021-08-13 op if (!strcmp(m->ext, dot))
167 c1d27b0e 2024-06-14 op return m->parser;
169 c1d27b0e 2024-06-14 op return &textplain_parser;
173 f63b8f73 2022-04-24 op fs_load_url(struct tab *tab, const char *url)
175 fd1c80ce 2024-06-14 op const char *bpath = "bookmarks.gmi", *fallback = "# Not found\n";
176 fd1c80ce 2024-06-14 op const struct parser *parser = &gemtext_parser;
177 fd1c80ce 2024-06-14 op char path[PATH_MAX];
178 fd1c80ce 2024-06-14 op FILE *fp = NULL;
180 fd1c80ce 2024-06-14 op char buf[BUFSIZ];
181 f63b8f73 2022-04-24 op struct page {
182 f63b8f73 2022-04-24 op const char *name;
183 f63b8f73 2022-04-24 op const char *path;
184 f63b8f73 2022-04-24 op const uint8_t *data;
185 f63b8f73 2022-04-24 op } pages[] = {
186 47eaa0e0 2024-06-09 op {"about", NULL, about_about},
187 47eaa0e0 2024-06-09 op {"blank", NULL, about_blank},
188 47eaa0e0 2024-06-09 op {"bookmarks", bpath, bookmarks},
189 47eaa0e0 2024-06-09 op {"crash", NULL, about_crash},
190 47eaa0e0 2024-06-09 op {"help", NULL, about_help},
191 47eaa0e0 2024-06-09 op {"license", NULL, about_license},
192 47eaa0e0 2024-06-09 op {"new", NULL, about_new},
193 f63b8f73 2022-04-24 op }, *page = NULL;
195 f63b8f73 2022-04-24 op if (!strncmp(url, "about:", 6)) {
198 f63b8f73 2022-04-24 op for (i = 0; page == NULL && i < nitems(pages); ++i) {
199 f63b8f73 2022-04-24 op if (!strcmp(url, pages[i].name))
200 f63b8f73 2022-04-24 op page = &pages[i];
203 f63b8f73 2022-04-24 op if (page == NULL)
206 f63b8f73 2022-04-24 op strlcpy(path, data_path_base, sizeof(path));
207 f63b8f73 2022-04-24 op strlcat(path, "/", sizeof(path));
208 f63b8f73 2022-04-24 op if (page->path != NULL)
209 f63b8f73 2022-04-24 op strlcat(path, page->path, sizeof(path));
211 f63b8f73 2022-04-24 op strlcat(path, "page/about_", sizeof(path));
212 f63b8f73 2022-04-24 op strlcat(path, page->name, sizeof(path));
213 f63b8f73 2022-04-24 op strlcat(path, ".gmi", sizeof(path));
216 f63b8f73 2022-04-24 op fallback = page->data;
217 f63b8f73 2022-04-24 op } else if (!strncmp(url, "file://", 7)) {
219 f63b8f73 2022-04-24 op strlcpy(path, url, sizeof(path));
220 c1d27b0e 2024-06-14 op parser = file_type(url);
224 f63b8f73 2022-04-24 op if ((fp = fopen(path, "r")) == NULL)
227 f63b8f73 2022-04-24 op if (is_dir(fp)) {
228 f63b8f73 2022-04-24 op send_dir(tab, path);
232 c1d27b0e 2024-06-14 op parser_init(&tab->buffer, parser);
236 f63b8f73 2022-04-24 op r = fread(buf, 1, sizeof(buf), fp);
237 c1d27b0e 2024-06-14 op if (!parser_parse(&tab->buffer, buf, r))
239 f63b8f73 2022-04-24 op if (r != sizeof(buf))
242 f63b8f73 2022-04-24 op parser_free(tab);
245 f63b8f73 2022-04-24 op if (fp != NULL)
248 f63b8f73 2022-04-24 op load_page_from_str(tab, fallback);
251 eb2ed626 2021-10-07 op static size_t
252 eb2ed626 2021-10-07 op join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
254 eb2ed626 2021-10-07 op strlcpy(buf, lhs, buflen);
255 eb2ed626 2021-10-07 op return strlcat(buf, rhs, buflen);
259 eb2ed626 2021-10-07 op getenv_default(char *buf, const char *name, const char *def, size_t buflen)
262 eb2ed626 2021-10-07 op char *home, *env;
264 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
265 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
267 eb2ed626 2021-10-07 op if ((env = getenv(name)) != NULL)
268 eb2ed626 2021-10-07 op ret = strlcpy(buf, env, buflen);
270 eb2ed626 2021-10-07 op ret = join_path(buf, home, def, buflen);
272 eb2ed626 2021-10-07 op if (ret >= buflen)
273 eb2ed626 2021-10-07 op errx(1, "buffer too small for %s", name);
277 eb2ed626 2021-10-07 op mkdirs(const char *path, mode_t mode)
279 444dad86 2021-10-07 op char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
281 eb2ed626 2021-10-07 op strlcpy(copy, path, sizeof(copy));
282 444dad86 2021-10-07 op strlcpy(orig, path, sizeof(orig));
283 eb2ed626 2021-10-07 op parent = dirname(copy);
284 eb2ed626 2021-10-07 op if (!strcmp(parent, "/"))
286 eb2ed626 2021-10-07 op mkdirs(parent, mode);
288 444dad86 2021-10-07 op if (mkdir(orig, mode) != 0) {
289 eb2ed626 2021-10-07 op if (errno == EEXIST)
291 444dad86 2021-10-07 op err(1, "can't mkdir %s", orig);
296 6c8ddace 2022-01-03 op init_paths(void)
298 7e60a21a 2022-01-03 op char xdg_config_base[PATH_MAX];
299 7e60a21a 2022-01-03 op char xdg_data_base[PATH_MAX];
300 7e60a21a 2022-01-03 op char xdg_cache_base[PATH_MAX];
301 7e60a21a 2022-01-03 op char old_path[PATH_MAX];
303 7e60a21a 2022-01-03 op struct stat info;
305 26e95b82 2024-01-16 op if (getcwd(cwd, sizeof(cwd)) == NULL)
306 26e95b82 2024-01-16 op err(1, "getcwd failed");
308 eb2ed626 2021-10-07 op /* old path */
309 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
310 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
311 eb2ed626 2021-10-07 op join_path(old_path, home, "/.telescope", sizeof(old_path));
313 eb2ed626 2021-10-07 op /* if ~/.telescope exists, use that instead of xdg dirs */
314 eb2ed626 2021-10-07 op if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
315 eb2ed626 2021-10-07 op join_path(config_path_base, home, "/.telescope",
316 eb2ed626 2021-10-07 op sizeof(config_path_base));
317 eb2ed626 2021-10-07 op join_path(data_path_base, home, "/.telescope",
318 eb2ed626 2021-10-07 op sizeof(data_path_base));
319 eb2ed626 2021-10-07 op join_path(cache_path_base, home, "/.telescope",
320 eb2ed626 2021-10-07 op sizeof(cache_path_base));
324 eb2ed626 2021-10-07 op /* xdg paths */
325 eb2ed626 2021-10-07 op getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
326 eb2ed626 2021-10-07 op sizeof(xdg_config_base));
327 eb2ed626 2021-10-07 op getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
328 eb2ed626 2021-10-07 op sizeof(xdg_data_base));
329 eb2ed626 2021-10-07 op getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
330 eb2ed626 2021-10-07 op sizeof(xdg_cache_base));
332 eb2ed626 2021-10-07 op join_path(config_path_base, xdg_config_base, "/telescope",
333 eb2ed626 2021-10-07 op sizeof(config_path_base));
334 eb2ed626 2021-10-07 op join_path(data_path_base, xdg_data_base, "/telescope",
335 eb2ed626 2021-10-07 op sizeof(data_path_base));
336 eb2ed626 2021-10-07 op join_path(cache_path_base, xdg_cache_base, "/telescope",
337 eb2ed626 2021-10-07 op sizeof(cache_path_base));
339 eb2ed626 2021-10-07 op mkdirs(xdg_config_base, S_IRWXU);
340 eb2ed626 2021-10-07 op mkdirs(xdg_data_base, S_IRWXU);
341 eb2ed626 2021-10-07 op mkdirs(xdg_cache_base, S_IRWXU);
343 eb2ed626 2021-10-07 op mkdirs(config_path_base, S_IRWXU);
344 eb2ed626 2021-10-07 op mkdirs(data_path_base, S_IRWXU);
345 eb2ed626 2021-10-07 op mkdirs(cache_path_base, S_IRWXU);
349 eb2ed626 2021-10-07 op fs_init(void)
351 6c8ddace 2022-01-03 op init_paths();
353 4cf6ba13 2022-02-11 op join_path(ctlsock_path, cache_path_base, "/ctl",
354 4cf6ba13 2022-02-11 op sizeof(ctlsock_path));
355 eb2ed626 2021-10-07 op join_path(config_path, config_path_base, "/config",
356 eb2ed626 2021-10-07 op sizeof(config_path));
357 eb2ed626 2021-10-07 op join_path(lockfile_path, cache_path_base, "/lock",
358 eb2ed626 2021-10-07 op sizeof(lockfile_path));
359 eb2ed626 2021-10-07 op join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
360 eb2ed626 2021-10-07 op sizeof(bookmark_file));
361 eb2ed626 2021-10-07 op join_path(known_hosts_file, data_path_base, "/known_hosts",
362 eb2ed626 2021-10-07 op sizeof(known_hosts_file));
363 eb2ed626 2021-10-07 op join_path(known_hosts_tmp, cache_path_base,
364 eb2ed626 2021-10-07 op "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
365 eb2ed626 2021-10-07 op join_path(session_file, cache_path_base, "/session",
366 de6a6a40 2022-04-24 op sizeof(session_file));
367 de6a6a40 2022-04-24 op join_path(session_file_tmp, cache_path_base, "/session.XXXXXXXXXX",
368 0ec5e323 2024-02-03 op sizeof(session_file_tmp));
369 9e97090d 2022-02-26 op join_path(history_file, cache_path_base, "/history",
370 de6a6a40 2022-04-24 op sizeof(history_file));
371 de6a6a40 2022-04-24 op join_path(history_file_tmp, cache_path_base, "/history.XXXXXXXXXX",
372 0ec5e323 2024-02-03 op sizeof(history_file_tmp));
373 d35e18b3 2024-02-04 op join_path(cert_dir, data_path_base, "/certs/",
374 d35e18b3 2024-02-04 op sizeof(cert_dir));
375 f0e62b85 2024-02-15 op join_path(cert_dir_tmp, data_path_base, "/certs/id.XXXXXXXXXX",
376 f0e62b85 2024-02-15 op sizeof(cert_dir_tmp));
377 64b6a451 2024-02-05 op join_path(certs_file, config_path_base, "/certs.conf",
378 d35e18b3 2024-02-04 op sizeof(certs_file));
379 609eaf39 2024-02-05 op join_path(certs_file_tmp, config_path_base, "/certs.conf.XXXXXXXXXX",
380 d35e18b3 2024-02-04 op sizeof(certs_file_tmp));
381 eb2ed626 2021-10-07 op join_path(crashed_file, cache_path_base, "/crashed",
382 eb2ed626 2021-10-07 op sizeof(crashed_file));
384 d35e18b3 2024-02-04 op mkdirs(cert_dir, S_IRWXU);