commit 2b80873
default
·
2023-04-22 06:02:23 +0000 UTC
parent 51bee24
History functions fail on ids with /.
M
data.c
+20,
-11
1@@ -1409,14 +1409,19 @@ xs_str *static_get_meta(snac *snac, const char *id)
2 }
3
4
5-d_char *_history_fn(snac *snac, char *id)
6+/** history **/
7+
8+xs_str *_history_fn(snac *snac, const char *id)
9 /* gets the filename for the history */
10 {
11- return xs_fmt("%s/history/%s", snac->basedir, id);
12+ if (strchr(id, '/'))
13+ return NULL;
14+ else
15+ return xs_fmt("%s/history/%s", snac->basedir, id);
16 }
17
18
19-double history_mtime(snac *snac, char * id)
20+double history_mtime(snac *snac, const char *id)
21 {
22 double t = 0.0;
23 xs *fn = _history_fn(snac, id);
24@@ -1428,26 +1433,26 @@ double history_mtime(snac *snac, char * id)
25 }
26
27
28-void history_add(snac *snac, char *id, char *content, int size)
29+void history_add(snac *snac, const char *id, const char *content, int size)
30 /* adds something to the history */
31 {
32 xs *fn = _history_fn(snac, id);
33 FILE *f;
34
35- if ((f = fopen(fn, "w")) != NULL) {
36+ if (fn && (f = fopen(fn, "w")) != NULL) {
37 fwrite(content, size, 1, f);
38 fclose(f);
39 }
40 }
41
42
43-d_char *history_get(snac *snac, char *id)
44+xs_str *history_get(snac *snac, const char *id)
45 {
46- d_char *content = NULL;
47+ xs_str *content = NULL;
48 xs *fn = _history_fn(snac, id);
49 FILE *f;
50
51- if ((f = fopen(fn, "r")) != NULL) {
52+ if (fn && (f = fopen(fn, "r")) != NULL) {
53 content = xs_readall(f);
54 fclose(f);
55 }
56@@ -1456,14 +1461,18 @@ d_char *history_get(snac *snac, char *id)
57 }
58
59
60-int history_del(snac *snac, char *id)
61+int history_del(snac *snac, const char *id)
62 {
63 xs *fn = _history_fn(snac, id);
64- return unlink(fn);
65+
66+ if (fn)
67+ return unlink(fn);
68+ else
69+ return -1;
70 }
71
72
73-d_char *history_list(snac *snac)
74+xs_list *history_list(snac *snac)
75 {
76 xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
77
M
snac.h
+5,
-5
1@@ -133,11 +133,11 @@ void static_put(snac *snac, const char *id, const char *data, int size);
2 void static_put_meta(snac *snac, const char *id, const char *str);
3 xs_str *static_get_meta(snac *snac, const char *id);
4
5-double history_mtime(snac *snac, char *id);
6-void history_add(snac *snac, char *id, char *content, int size);
7-d_char *history_get(snac *snac, char *id);
8-int history_del(snac *snac, char *id);
9-d_char *history_list(snac *snac);
10+double history_mtime(snac *snac, const char *id);
11+void history_add(snac *snac, const char *id, const char *content, int size);
12+xs_str *history_get(snac *snac, const char *id);
13+int history_del(snac *snac, const char *id);
14+xs_list *history_list(snac *snac);
15
16 void lastlog_write(snac *snac);
17