2 35e1f40a 2021-03-14 op * Copyright (c) 2021 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 eb9cbcba 2022-01-03 op #include "session.h"
38 f63b8f73 2022-04-24 op #include "telescope.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];
72 f63b8f73 2022-04-24 op select_non_dot(const struct dirent *d)
74 f63b8f73 2022-04-24 op return strcmp(d->d_name, ".");
78 f63b8f73 2022-04-24 op select_non_dotdot(const struct dirent *d)
80 f63b8f73 2022-04-24 op return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
84 f63b8f73 2022-04-24 op send_dir(struct tab *tab, const char *path)
86 f63b8f73 2022-04-24 op struct dirent **names;
87 f63b8f73 2022-04-24 op int (*selector)(const struct dirent *) = select_non_dot;
92 f63b8f73 2022-04-24 op * need something to fake a redirect
95 f63b8f73 2022-04-24 op if (!has_suffix(path, "/")) {
96 f63b8f73 2022-04-24 op if (asprintf(&s, "%s/", path) == -1)
98 f63b8f73 2022-04-24 op send_hdr(peerid, 30, s);
104 f63b8f73 2022-04-24 op if (!strcmp(path, "/"))
105 f63b8f73 2022-04-24 op selector = select_non_dotdot;
107 f63b8f73 2022-04-24 op if ((len = scandir(path, &names, selector, alphasort)) == -1) {
108 f63b8f73 2022-04-24 op load_page_from_str(tab, "# failure reading the directory\n");
112 f63b8f73 2022-04-24 op parser_init(tab, gemtext_initparser);
113 f63b8f73 2022-04-24 op parser_parsef(tab, "# Index of %s\n\n", path);
115 f63b8f73 2022-04-24 op for (i = 0; i < len; ++i) {
116 f63b8f73 2022-04-24 op const char *sufx = "";
118 f63b8f73 2022-04-24 op if (names[i]->d_type == DT_DIR)
121 f63b8f73 2022-04-24 op parser_parsef(tab, "=> %s%s\n", names[i]->d_name, sufx);
124 f63b8f73 2022-04-24 op parser_free(tab);
129 f63b8f73 2022-04-24 op is_dir(FILE *fp)
131 f63b8f73 2022-04-24 op struct stat sb;
133 f63b8f73 2022-04-24 op if (fstat(fileno(fp), &sb) == -1)
136 f63b8f73 2022-04-24 op return S_ISDIR(sb.st_mode);
139 f63b8f73 2022-04-24 op static parserinit
140 65124267 2021-08-13 op file_type(const char *path)
142 21268f10 2022-04-24 op const struct mapping {
143 fb4dc49f 2021-08-13 op const char *ext;
144 f63b8f73 2022-04-24 op parserinit fn;
146 f63b8f73 2022-04-24 op {"diff", textpatch_initparser},
147 f63b8f73 2022-04-24 op {"gemini", gemtext_initparser},
148 f63b8f73 2022-04-24 op {"gmi", gemtext_initparser},
149 f63b8f73 2022-04-24 op {"markdown", textplain_initparser},
150 f63b8f73 2022-04-24 op {"md", textplain_initparser},
151 f63b8f73 2022-04-24 op {"patch", gemtext_initparser},
152 fb4dc49f 2021-08-13 op {NULL, NULL},
154 21268f10 2022-04-24 op const char *dot;
156 65124267 2021-08-13 op if ((dot = strrchr(path, '.')) == NULL)
157 f63b8f73 2022-04-24 op return textplain_initparser;
161 65124267 2021-08-13 op for (m = ms; m->ext != NULL; ++m)
162 65124267 2021-08-13 op if (!strcmp(m->ext, dot))
163 f63b8f73 2022-04-24 op return m->fn;
165 f63b8f73 2022-04-24 op return textplain_initparser;
169 f63b8f73 2022-04-24 op fs_load_url(struct tab *tab, const char *url)
171 f63b8f73 2022-04-24 op const char *bpath = "bookmarks.gmi", *fallback = "# Not found\n";
172 f63b8f73 2022-04-24 op parserinit initfn = gemtext_initparser;
173 f63b8f73 2022-04-24 op char path[PATH_MAX];
174 f63b8f73 2022-04-24 op FILE *fp = NULL;
176 f63b8f73 2022-04-24 op char buf[BUFSIZ];
177 f63b8f73 2022-04-24 op struct page {
178 f63b8f73 2022-04-24 op const char *name;
179 f63b8f73 2022-04-24 op const char *path;
180 f63b8f73 2022-04-24 op const uint8_t *data;
182 f63b8f73 2022-04-24 op } pages[] = {
183 f63b8f73 2022-04-24 op {"about", NULL, about_about, about_about_len},
184 f63b8f73 2022-04-24 op {"blank", NULL, about_blank, about_blank_len},
185 f63b8f73 2022-04-24 op {"bookmarks", bpath, bookmarks, bookmarks_len},
186 f63b8f73 2022-04-24 op {"crash", NULL, about_crash, about_crash_len},
187 f63b8f73 2022-04-24 op {"help", NULL, about_help, about_help_len},
188 f63b8f73 2022-04-24 op {"license", NULL, about_license, about_license_len},
189 f63b8f73 2022-04-24 op {"new", NULL, about_new, about_new_len},
190 f63b8f73 2022-04-24 op }, *page = NULL;
192 f63b8f73 2022-04-24 op if (!strncmp(url, "about:", 6)) {
195 f63b8f73 2022-04-24 op for (i = 0; page == NULL && i < nitems(pages); ++i) {
196 f63b8f73 2022-04-24 op if (!strcmp(url, pages[i].name))
197 f63b8f73 2022-04-24 op page = &pages[i];
200 f63b8f73 2022-04-24 op if (page == NULL)
203 f63b8f73 2022-04-24 op strlcpy(path, data_path_base, sizeof(path));
204 f63b8f73 2022-04-24 op strlcat(path, "/", sizeof(path));
205 f63b8f73 2022-04-24 op if (page->path != NULL)
206 f63b8f73 2022-04-24 op strlcat(path, page->path, sizeof(path));
208 f63b8f73 2022-04-24 op strlcat(path, "page/about_", sizeof(path));
209 f63b8f73 2022-04-24 op strlcat(path, page->name, sizeof(path));
210 f63b8f73 2022-04-24 op strlcat(path, ".gmi", sizeof(path));
213 f63b8f73 2022-04-24 op fallback = page->data;
214 f63b8f73 2022-04-24 op } else if (!strncmp(url, "file://", 7)) {
216 f63b8f73 2022-04-24 op strlcpy(path, url, sizeof(path));
217 f63b8f73 2022-04-24 op initfn = file_type(url);
221 f63b8f73 2022-04-24 op if ((fp = fopen(path, "r")) == NULL)
224 f63b8f73 2022-04-24 op if (is_dir(fp)) {
226 f63b8f73 2022-04-24 op send_dir(tab, path);
230 f63b8f73 2022-04-24 op parser_init(tab, initfn);
234 f63b8f73 2022-04-24 op r = fread(buf, 1, sizeof(buf), fp);
235 f63b8f73 2022-04-24 op if (!parser_parse(tab, buf, r))
237 f63b8f73 2022-04-24 op if (r != sizeof(buf))
240 f63b8f73 2022-04-24 op parser_free(tab);
243 f63b8f73 2022-04-24 op if (fp != NULL)
246 f63b8f73 2022-04-24 op load_page_from_str(tab, fallback);
249 eb2ed626 2021-10-07 op static size_t
250 eb2ed626 2021-10-07 op join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
252 eb2ed626 2021-10-07 op strlcpy(buf, lhs, buflen);
253 eb2ed626 2021-10-07 op return strlcat(buf, rhs, buflen);
257 eb2ed626 2021-10-07 op getenv_default(char *buf, const char *name, const char *def, size_t buflen)
260 eb2ed626 2021-10-07 op char *home, *env;
262 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
263 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
265 eb2ed626 2021-10-07 op if ((env = getenv(name)) != NULL)
266 eb2ed626 2021-10-07 op ret = strlcpy(buf, env, buflen);
268 eb2ed626 2021-10-07 op ret = join_path(buf, home, def, buflen);
270 eb2ed626 2021-10-07 op if (ret >= buflen)
271 eb2ed626 2021-10-07 op errx(1, "buffer too small for %s", name);
275 eb2ed626 2021-10-07 op mkdirs(const char *path, mode_t mode)
277 444dad86 2021-10-07 op char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
279 eb2ed626 2021-10-07 op strlcpy(copy, path, sizeof(copy));
280 444dad86 2021-10-07 op strlcpy(orig, path, sizeof(orig));
281 eb2ed626 2021-10-07 op parent = dirname(copy);
282 eb2ed626 2021-10-07 op if (!strcmp(parent, "/"))
284 eb2ed626 2021-10-07 op mkdirs(parent, mode);
286 444dad86 2021-10-07 op if (mkdir(orig, mode) != 0) {
287 eb2ed626 2021-10-07 op if (errno == EEXIST)
289 444dad86 2021-10-07 op err(1, "can't mkdir %s", orig);
294 6c8ddace 2022-01-03 op init_paths(void)
296 7e60a21a 2022-01-03 op char xdg_config_base[PATH_MAX];
297 7e60a21a 2022-01-03 op char xdg_data_base[PATH_MAX];
298 7e60a21a 2022-01-03 op char xdg_cache_base[PATH_MAX];
299 7e60a21a 2022-01-03 op char old_path[PATH_MAX];
301 7e60a21a 2022-01-03 op struct stat info;
303 eb2ed626 2021-10-07 op /* old path */
304 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
305 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
306 eb2ed626 2021-10-07 op join_path(old_path, home, "/.telescope", sizeof(old_path));
308 eb2ed626 2021-10-07 op /* if ~/.telescope exists, use that instead of xdg dirs */
309 eb2ed626 2021-10-07 op if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
310 eb2ed626 2021-10-07 op join_path(config_path_base, home, "/.telescope",
311 eb2ed626 2021-10-07 op sizeof(config_path_base));
312 eb2ed626 2021-10-07 op join_path(data_path_base, home, "/.telescope",
313 eb2ed626 2021-10-07 op sizeof(data_path_base));
314 eb2ed626 2021-10-07 op join_path(cache_path_base, home, "/.telescope",
315 eb2ed626 2021-10-07 op sizeof(cache_path_base));
319 eb2ed626 2021-10-07 op /* xdg paths */
320 eb2ed626 2021-10-07 op getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
321 eb2ed626 2021-10-07 op sizeof(xdg_config_base));
322 eb2ed626 2021-10-07 op getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
323 eb2ed626 2021-10-07 op sizeof(xdg_data_base));
324 eb2ed626 2021-10-07 op getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
325 eb2ed626 2021-10-07 op sizeof(xdg_cache_base));
327 eb2ed626 2021-10-07 op join_path(config_path_base, xdg_config_base, "/telescope",
328 eb2ed626 2021-10-07 op sizeof(config_path_base));
329 eb2ed626 2021-10-07 op join_path(data_path_base, xdg_data_base, "/telescope",
330 eb2ed626 2021-10-07 op sizeof(data_path_base));
331 eb2ed626 2021-10-07 op join_path(cache_path_base, xdg_cache_base, "/telescope",
332 eb2ed626 2021-10-07 op sizeof(cache_path_base));
334 eb2ed626 2021-10-07 op mkdirs(xdg_config_base, S_IRWXU);
335 eb2ed626 2021-10-07 op mkdirs(xdg_data_base, S_IRWXU);
336 eb2ed626 2021-10-07 op mkdirs(xdg_cache_base, S_IRWXU);
338 eb2ed626 2021-10-07 op mkdirs(config_path_base, S_IRWXU);
339 eb2ed626 2021-10-07 op mkdirs(data_path_base, S_IRWXU);
340 eb2ed626 2021-10-07 op mkdirs(cache_path_base, S_IRWXU);
344 eb2ed626 2021-10-07 op fs_init(void)
346 6c8ddace 2022-01-03 op init_paths();
348 4cf6ba13 2022-02-11 op join_path(ctlsock_path, cache_path_base, "/ctl",
349 4cf6ba13 2022-02-11 op sizeof(ctlsock_path));
350 eb2ed626 2021-10-07 op join_path(config_path, config_path_base, "/config",
351 eb2ed626 2021-10-07 op sizeof(config_path));
352 eb2ed626 2021-10-07 op join_path(lockfile_path, cache_path_base, "/lock",
353 eb2ed626 2021-10-07 op sizeof(lockfile_path));
354 eb2ed626 2021-10-07 op join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
355 eb2ed626 2021-10-07 op sizeof(bookmark_file));
356 eb2ed626 2021-10-07 op join_path(known_hosts_file, data_path_base, "/known_hosts",
357 eb2ed626 2021-10-07 op sizeof(known_hosts_file));
358 eb2ed626 2021-10-07 op join_path(known_hosts_tmp, cache_path_base,
359 eb2ed626 2021-10-07 op "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
360 eb2ed626 2021-10-07 op join_path(session_file, cache_path_base, "/session",
361 de6a6a40 2022-04-24 op sizeof(session_file));
362 de6a6a40 2022-04-24 op join_path(session_file_tmp, cache_path_base, "/session.XXXXXXXXXX",
363 eb2ed626 2021-10-07 op sizeof(session_file));
364 9e97090d 2022-02-26 op join_path(history_file, cache_path_base, "/history",
365 de6a6a40 2022-04-24 op sizeof(history_file));
366 de6a6a40 2022-04-24 op join_path(history_file_tmp, cache_path_base, "/history.XXXXXXXXXX",
367 9e97090d 2022-02-26 op sizeof(history_file));
368 eb2ed626 2021-10-07 op join_path(crashed_file, cache_path_base, "/crashed",
369 eb2ed626 2021-10-07 op sizeof(crashed_file));