Blob


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