diff options
| author | 2023-08-03 09:02:08 +0200 | |
|---|---|---|
| committer | 2023-08-03 09:02:08 +0200 | |
| commit | f5a3dbf8d652dbf87f9ff27b50d11f6364551a61 (patch) | |
| tree | a3c8fabb5baa25bde203a9cc8089134b8774d723 | |
| parent | Backport from xs. (diff) | |
| download | penes-snac2-f5a3dbf8d652dbf87f9ff27b50d11f6364551a61.tar.gz penes-snac2-f5a3dbf8d652dbf87f9ff27b50d11f6364551a61.tar.xz penes-snac2-f5a3dbf8d652dbf87f9ff27b50d11f6364551a61.zip | |
Use xs_json_dump_pp() wherever possible.
| -rw-r--r-- | data.c | 22 | ||||
| -rw-r--r-- | html.c | 8 | ||||
| -rw-r--r-- | main.c | 24 | ||||
| -rw-r--r-- | mastoapi.c | 41 | ||||
| -rw-r--r-- | upgrade.c | 3 | ||||
| -rw-r--r-- | utils.c | 12 |
6 files changed, 23 insertions, 87 deletions
| @@ -643,9 +643,7 @@ int _object_add(const char *id, const xs_dict *obj, int ow) | |||
| 643 | if ((f = fopen(fn, "w")) != NULL) { | 643 | if ((f = fopen(fn, "w")) != NULL) { |
| 644 | flock(fileno(f), LOCK_EX); | 644 | flock(fileno(f), LOCK_EX); |
| 645 | 645 | ||
| 646 | xs *j = xs_json_dumps_pp(obj, 4); | 646 | xs_json_dump_pp(obj, 4, f); |
| 647 | |||
| 648 | fwrite(j, strlen(j), 1, f); | ||
| 649 | fclose(f); | 647 | fclose(f); |
| 650 | 648 | ||
| 651 | /* does this object has a parent? */ | 649 | /* does this object has a parent? */ |
| @@ -1190,9 +1188,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg) | |||
| 1190 | FILE *f; | 1188 | FILE *f; |
| 1191 | 1189 | ||
| 1192 | if ((f = fopen(fn, "w")) != NULL) { | 1190 | if ((f = fopen(fn, "w")) != NULL) { |
| 1193 | xs *j = xs_json_dumps_pp(msg, 4); | 1191 | xs_json_dump_pp(msg, 4, f); |
| 1194 | |||
| 1195 | fwrite(j, 1, strlen(j), f); | ||
| 1196 | fclose(f); | 1192 | fclose(f); |
| 1197 | 1193 | ||
| 1198 | /* get the filename of the actor object */ | 1194 | /* get the filename of the actor object */ |
| @@ -1893,9 +1889,7 @@ void notify_add(snac *snac, const char *type, const char *utype, | |||
| 1893 | noti = xs_dict_append(noti, "objid", objid); | 1889 | noti = xs_dict_append(noti, "objid", objid); |
| 1894 | 1890 | ||
| 1895 | if ((f = fopen(fn, "w")) != NULL) { | 1891 | if ((f = fopen(fn, "w")) != NULL) { |
| 1896 | xs *j = xs_json_dumps_pp(noti, 4); | 1892 | xs_json_dump_pp(noti, 4, f); |
| 1897 | |||
| 1898 | fwrite(j, strlen(j), 1, f); | ||
| 1899 | fclose(f); | 1893 | fclose(f); |
| 1900 | } | 1894 | } |
| 1901 | } | 1895 | } |
| @@ -1970,9 +1964,7 @@ static xs_dict *_enqueue_put(const char *fn, xs_dict *msg) | |||
| 1970 | FILE *f; | 1964 | FILE *f; |
| 1971 | 1965 | ||
| 1972 | if ((f = fopen(tfn, "w")) != NULL) { | 1966 | if ((f = fopen(tfn, "w")) != NULL) { |
| 1973 | xs *j = xs_json_dumps_pp(msg, 4); | 1967 | xs_json_dump_pp(msg, 4, f); |
| 1974 | |||
| 1975 | fwrite(j, strlen(j), 1, f); | ||
| 1976 | fclose(f); | 1968 | fclose(f); |
| 1977 | 1969 | ||
| 1978 | rename(tfn, fn); | 1970 | rename(tfn, fn); |
| @@ -2569,8 +2561,7 @@ void srv_archive_error(const char *prefix, const xs_str *err, | |||
| 2569 | if (req) { | 2561 | if (req) { |
| 2570 | fprintf(f, "Request headers:\n"); | 2562 | fprintf(f, "Request headers:\n"); |
| 2571 | 2563 | ||
| 2572 | xs *j = xs_json_dumps_pp(req, 4); | 2564 | xs_json_dump_pp(req, 4, f); |
| 2573 | fwrite(j, strlen(j), 1, f); | ||
| 2574 | 2565 | ||
| 2575 | fprintf(f, "\n"); | 2566 | fprintf(f, "\n"); |
| 2576 | } | 2567 | } |
| @@ -2579,8 +2570,7 @@ void srv_archive_error(const char *prefix, const xs_str *err, | |||
| 2579 | fprintf(f, "Data:\n"); | 2570 | fprintf(f, "Data:\n"); |
| 2580 | 2571 | ||
| 2581 | if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) { | 2572 | if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) { |
| 2582 | xs *j = xs_json_dumps_pp(data, 4); | 2573 | xs_json_dump_pp(data, 4, f); |
| 2583 | fwrite(j, strlen(j), 1, f); | ||
| 2584 | } | 2574 | } |
| 2585 | else | 2575 | else |
| 2586 | fprintf(f, "%s", data); | 2576 | fprintf(f, "%s", data); |
| @@ -1985,10 +1985,7 @@ int html_post_handler(const xs_dict *req, const char *q_path, | |||
| 1985 | p_vars = xs_dict_get(req, "p_vars"); | 1985 | p_vars = xs_dict_get(req, "p_vars"); |
| 1986 | 1986 | ||
| 1987 | #if 0 | 1987 | #if 0 |
| 1988 | { | 1988 | xs_json_dump_pp(p_vars, 4, stdout); |
| 1989 | xs *j1 = xs_json_dumps_pp(p_vars, 4); | ||
| 1990 | printf("%s\n", j1); | ||
| 1991 | } | ||
| 1992 | #endif | 1989 | #endif |
| 1993 | 1990 | ||
| 1994 | if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/ | 1991 | if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/ |
| @@ -2310,8 +2307,7 @@ int html_post_handler(const xs_dict *req, const char *q_path, | |||
| 2310 | rename(fn, bfn); | 2307 | rename(fn, bfn); |
| 2311 | 2308 | ||
| 2312 | if ((f = fopen(fn, "w")) != NULL) { | 2309 | if ((f = fopen(fn, "w")) != NULL) { |
| 2313 | xs *j = xs_json_dumps_pp(snac.config, 4); | 2310 | xs_json_dump_pp(snac.config, 4, f); |
| 2314 | fwrite(j, strlen(j), 1, f); | ||
| 2315 | fclose(f); | 2311 | fclose(f); |
| 2316 | } | 2312 | } |
| 2317 | else | 2313 | else |
| @@ -198,8 +198,7 @@ int main(int argc, char *argv[]) | |||
| 198 | xs *list = index_list_desc(idx, 0, 256); | 198 | xs *list = index_list_desc(idx, 0, 256); |
| 199 | xs *tl = timeline_top_level(&snac, list); | 199 | xs *tl = timeline_top_level(&snac, list); |
| 200 | 200 | ||
| 201 | xs *j = xs_json_dumps_pp(tl, 4); | 201 | xs_json_dump_pp(tl, 4, stdout); |
| 202 | printf("%s\n", j); | ||
| 203 | 202 | ||
| 204 | return 0; | 203 | return 0; |
| 205 | } | 204 | } |
| @@ -230,8 +229,7 @@ int main(int argc, char *argv[]) | |||
| 230 | enqueue_message(&snac, msg); | 229 | enqueue_message(&snac, msg); |
| 231 | 230 | ||
| 232 | if (dbglevel) { | 231 | if (dbglevel) { |
| 233 | xs *j = xs_json_dumps_pp(msg, 4); | 232 | xs_json_dump_pp(msg, 4, stdout); |
| 234 | printf("%s\n", j); | ||
| 235 | } | 233 | } |
| 236 | } | 234 | } |
| 237 | 235 | ||
| @@ -249,8 +247,7 @@ int main(int argc, char *argv[]) | |||
| 249 | enqueue_output_by_actor(&snac, msg, actor, 0); | 247 | enqueue_output_by_actor(&snac, msg, actor, 0); |
| 250 | 248 | ||
| 251 | if (dbglevel) { | 249 | if (dbglevel) { |
| 252 | xs *j = xs_json_dumps_pp(msg, 4); | 250 | xs_json_dump_pp(msg, 4, stdout); |
| 253 | printf("%s\n", j); | ||
| 254 | } | 251 | } |
| 255 | } | 252 | } |
| 256 | 253 | ||
| @@ -284,8 +281,7 @@ int main(int argc, char *argv[]) | |||
| 284 | enqueue_output_by_actor(&snac, msg, url, 0); | 281 | enqueue_output_by_actor(&snac, msg, url, 0); |
| 285 | 282 | ||
| 286 | if (dbglevel) { | 283 | if (dbglevel) { |
| 287 | xs *j = xs_json_dumps_pp(msg, 4); | 284 | xs_json_dump_pp(msg, 4, stdout); |
| 288 | printf("%s\n", j); | ||
| 289 | } | 285 | } |
| 290 | } | 286 | } |
| 291 | else { | 287 | else { |
| @@ -324,8 +320,7 @@ int main(int argc, char *argv[]) | |||
| 324 | xs *c_msg = msg_create(&snac, msg); | 320 | xs *c_msg = msg_create(&snac, msg); |
| 325 | 321 | ||
| 326 | if (dbglevel) { | 322 | if (dbglevel) { |
| 327 | xs *j = xs_json_dumps_pp(c_msg, 4); | 323 | xs_json_dump_pp(c_msg, 4, stdout); |
| 328 | printf("%s\n", j); | ||
| 329 | } | 324 | } |
| 330 | 325 | ||
| 331 | enqueue_message(&snac, c_msg); | 326 | enqueue_message(&snac, c_msg); |
| @@ -345,8 +340,7 @@ int main(int argc, char *argv[]) | |||
| 345 | printf("status: %d\n", status); | 340 | printf("status: %d\n", status); |
| 346 | 341 | ||
| 347 | if (data != NULL) { | 342 | if (data != NULL) { |
| 348 | xs *j = xs_json_dumps_pp(data, 4); | 343 | xs_json_dump_pp(data, 4, stdout); |
| 349 | printf("%s\n", j); | ||
| 350 | } | 344 | } |
| 351 | 345 | ||
| 352 | return 0; | 346 | return 0; |
| @@ -361,8 +355,7 @@ int main(int argc, char *argv[]) | |||
| 361 | printf("status: %d\n", status); | 355 | printf("status: %d\n", status); |
| 362 | 356 | ||
| 363 | if (valid_status(status)) { | 357 | if (valid_status(status)) { |
| 364 | xs *j = xs_json_dumps_pp(data, 4); | 358 | xs_json_dump_pp(data, 4, stdout); |
| 365 | printf("%s\n", j); | ||
| 366 | } | 359 | } |
| 367 | 360 | ||
| 368 | return 0; | 361 | return 0; |
| @@ -405,8 +398,7 @@ int main(int argc, char *argv[]) | |||
| 405 | c_msg = msg_create(&snac, msg); | 398 | c_msg = msg_create(&snac, msg); |
| 406 | 399 | ||
| 407 | if (dbglevel) { | 400 | if (dbglevel) { |
| 408 | xs *j = xs_json_dumps_pp(c_msg, 4); | 401 | xs_json_dump_pp(c_msg, 4, stdout); |
| 409 | printf("%s\n", j); | ||
| 410 | } | 402 | } |
| 411 | 403 | ||
| 412 | enqueue_message(&snac, c_msg); | 404 | enqueue_message(&snac, c_msg); |
| @@ -39,8 +39,7 @@ int app_add(const char *id, const xs_dict *app) | |||
| 39 | fn = xs_str_cat(fn, ".json"); | 39 | fn = xs_str_cat(fn, ".json"); |
| 40 | 40 | ||
| 41 | if ((f = fopen(fn, "w")) != NULL) { | 41 | if ((f = fopen(fn, "w")) != NULL) { |
| 42 | xs *j = xs_json_dumps_pp(app, 4); | 42 | xs_json_dump_pp(app, 4, f); |
| 43 | fwrite(j, strlen(j), 1, f); | ||
| 44 | fclose(f); | 43 | fclose(f); |
| 45 | } | 44 | } |
| 46 | else | 45 | else |
| @@ -104,8 +103,7 @@ int token_add(const char *id, const xs_dict *token) | |||
| 104 | fn = xs_str_cat(fn, ".json"); | 103 | fn = xs_str_cat(fn, ".json"); |
| 105 | 104 | ||
| 106 | if ((f = fopen(fn, "w")) != NULL) { | 105 | if ((f = fopen(fn, "w")) != NULL) { |
| 107 | xs *j = xs_json_dumps_pp(token, 4); | 106 | xs_json_dump_pp(token, 4, f); |
| 108 | fwrite(j, strlen(j), 1, f); | ||
| 109 | fclose(f); | 107 | fclose(f); |
| 110 | } | 108 | } |
| 111 | else | 109 | else |
| @@ -170,11 +168,6 @@ int oauth_get_handler(const xs_dict *req, const char *q_path, | |||
| 170 | if (!xs_startswith(q_path, "/oauth/")) | 168 | if (!xs_startswith(q_path, "/oauth/")) |
| 171 | return 0; | 169 | return 0; |
| 172 | 170 | ||
| 173 | /* { | ||
| 174 | xs *j = xs_json_dumps_pp(req, 4); | ||
| 175 | printf("oauth get:\n%s\n", j); | ||
| 176 | }*/ | ||
| 177 | |||
| 178 | int status = 404; | 171 | int status = 404; |
| 179 | xs_dict *msg = xs_dict_get(req, "q_vars"); | 172 | xs_dict *msg = xs_dict_get(req, "q_vars"); |
| 180 | xs *cmd = xs_replace_n(q_path, "/oauth", "", 1); | 173 | xs *cmd = xs_replace_n(q_path, "/oauth", "", 1); |
| @@ -236,11 +229,6 @@ int oauth_post_handler(const xs_dict *req, const char *q_path, | |||
| 236 | if (!xs_startswith(q_path, "/oauth/")) | 229 | if (!xs_startswith(q_path, "/oauth/")) |
| 237 | return 0; | 230 | return 0; |
| 238 | 231 | ||
| 239 | /* { | ||
| 240 | xs *j = xs_json_dumps_pp(req, 4); | ||
| 241 | printf("oauth post:\n%s\n", j); | ||
| 242 | }*/ | ||
| 243 | |||
| 244 | int status = 404; | 232 | int status = 404; |
| 245 | 233 | ||
| 246 | char *i_ctype = xs_dict_get(req, "content-type"); | 234 | char *i_ctype = xs_dict_get(req, "content-type"); |
| @@ -946,10 +934,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path, | |||
| 946 | return 0; | 934 | return 0; |
| 947 | 935 | ||
| 948 | srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path)); | 936 | srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path)); |
| 949 | /* { | ||
| 950 | xs *j = xs_json_dumps_pp(req, 4); | ||
| 951 | printf("mastoapi get:\n%s\n", j); | ||
| 952 | }*/ | ||
| 953 | 937 | ||
| 954 | int status = 404; | 938 | int status = 404; |
| 955 | xs_dict *args = xs_dict_get(req, "q_vars"); | 939 | xs_dict *args = xs_dict_get(req, "q_vars"); |
| @@ -1707,10 +1691,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 1707 | return 0; | 1691 | return 0; |
| 1708 | 1692 | ||
| 1709 | srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path)); | 1693 | srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path)); |
| 1710 | /* { | ||
| 1711 | xs *j = xs_json_dumps_pp(req, 4); | ||
| 1712 | printf("mastoapi post:\n%s\n", j); | ||
| 1713 | }*/ | ||
| 1714 | 1694 | ||
| 1715 | int status = 404; | 1695 | int status = 404; |
| 1716 | xs *args = NULL; | 1696 | xs *args = NULL; |
| @@ -1724,11 +1704,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 1724 | if (args == NULL) | 1704 | if (args == NULL) |
| 1725 | return 400; | 1705 | return 400; |
| 1726 | 1706 | ||
| 1727 | /* { | ||
| 1728 | xs *j = xs_json_dumps_pp(args, 4); | ||
| 1729 | printf("%s\n", j); | ||
| 1730 | }*/ | ||
| 1731 | |||
| 1732 | xs *cmd = xs_replace_n(q_path, "/api", "", 1); | 1707 | xs *cmd = xs_replace_n(q_path, "/api", "", 1); |
| 1733 | 1708 | ||
| 1734 | snac snac = {0}; | 1709 | snac snac = {0}; |
| @@ -1785,10 +1760,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 1785 | if (strcmp(cmd, "/v1/statuses") == 0) { /** **/ | 1760 | if (strcmp(cmd, "/v1/statuses") == 0) { /** **/ |
| 1786 | if (logged_in) { | 1761 | if (logged_in) { |
| 1787 | /* post a new Note */ | 1762 | /* post a new Note */ |
| 1788 | /* { | ||
| 1789 | xs *j = xs_json_dumps_pp(args, 4); | ||
| 1790 | printf("%s\n", j); | ||
| 1791 | }*/ | ||
| 1792 | const char *content = xs_dict_get(args, "status"); | 1763 | const char *content = xs_dict_get(args, "status"); |
| 1793 | const char *mid = xs_dict_get(args, "in_reply_to_id"); | 1764 | const char *mid = xs_dict_get(args, "in_reply_to_id"); |
| 1794 | const char *visibility = xs_dict_get(args, "visibility"); | 1765 | const char *visibility = xs_dict_get(args, "visibility"); |
| @@ -2009,10 +1980,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path, | |||
| 2009 | else | 1980 | else |
| 2010 | if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/ | 1981 | if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/ |
| 2011 | if (logged_in) { | 1982 | if (logged_in) { |
| 2012 | /* { | ||
| 2013 | xs *j = xs_json_dumps_pp(args, 4); | ||
| 2014 | printf("%s\n", j); | ||
| 2015 | }*/ | ||
| 2016 | const xs_list *file = xs_dict_get(args, "file"); | 1983 | const xs_list *file = xs_dict_get(args, "file"); |
| 2017 | const char *desc = xs_dict_get(args, "description"); | 1984 | const char *desc = xs_dict_get(args, "description"); |
| 2018 | 1985 | ||
| @@ -2222,10 +2189,6 @@ int mastoapi_put_handler(const xs_dict *req, const char *q_path, | |||
| 2222 | return 0; | 2189 | return 0; |
| 2223 | 2190 | ||
| 2224 | srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path)); | 2191 | srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path)); |
| 2225 | /* { | ||
| 2226 | xs *j = xs_json_dumps_pp(req, 4); | ||
| 2227 | printf("mastoapi put:\n%s\n", j); | ||
| 2228 | }*/ | ||
| 2229 | 2192 | ||
| 2230 | int status = 404; | 2193 | int status = 404; |
| 2231 | xs *args = NULL; | 2194 | xs *args = NULL; |
| @@ -312,8 +312,7 @@ int snac_upgrade(xs_str **error) | |||
| 312 | FILE *f; | 312 | FILE *f; |
| 313 | 313 | ||
| 314 | if ((f = fopen(fn, "w")) != NULL) { | 314 | if ((f = fopen(fn, "w")) != NULL) { |
| 315 | xs *j = xs_json_dumps_pp(srv_config, 4); | 315 | xs_json_dump_pp(srv_config, 4, f); |
| 316 | fwrite(j, strlen(j), 1, f); | ||
| 317 | fclose(f); | 316 | fclose(f); |
| 318 | 317 | ||
| 319 | srv_log(xs_fmt("disk layout upgraded %s after %d changes", fn, changed)); | 318 | srv_log(xs_fmt("disk layout upgraded %s after %d changes", fn, changed)); |
| @@ -194,8 +194,7 @@ int snac_init(const char *basedir) | |||
| 194 | return 1; | 194 | return 1; |
| 195 | } | 195 | } |
| 196 | 196 | ||
| 197 | xs *j = xs_json_dumps_pp(srv_config, 4); | 197 | xs_json_dump_pp(srv_config, 4, f); |
| 198 | fwrite(j, strlen(j), 1, f); | ||
| 199 | fclose(f); | 198 | fclose(f); |
| 200 | 199 | ||
| 201 | printf("Done.\n"); | 200 | printf("Done.\n"); |
| @@ -276,8 +275,7 @@ int adduser(const char *uid) | |||
| 276 | return 1; | 275 | return 1; |
| 277 | } | 276 | } |
| 278 | else { | 277 | else { |
| 279 | xs *j = xs_json_dumps_pp(config, 4); | 278 | xs_json_dump_pp(config, 4, f); |
| 280 | fwrite(j, strlen(j), 1, f); | ||
| 281 | fclose(f); | 279 | fclose(f); |
| 282 | } | 280 | } |
| 283 | 281 | ||
| @@ -292,8 +290,7 @@ int adduser(const char *uid) | |||
| 292 | return 1; | 290 | return 1; |
| 293 | } | 291 | } |
| 294 | else { | 292 | else { |
| 295 | xs *j = xs_json_dumps_pp(key, 4); | 293 | xs_json_dump_pp(key, 4, f); |
| 296 | fwrite(j, strlen(j), 1, f); | ||
| 297 | fclose(f); | 294 | fclose(f); |
| 298 | } | 295 | } |
| 299 | 296 | ||
| @@ -319,8 +316,7 @@ int resetpwd(snac *snac) | |||
| 319 | snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd); | 316 | snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd); |
| 320 | 317 | ||
| 321 | if ((f = fopen(fn, "w")) != NULL) { | 318 | if ((f = fopen(fn, "w")) != NULL) { |
| 322 | xs *j = xs_json_dumps_pp(snac->config, 4); | 319 | xs_json_dump_pp(snac->config, 4, f); |
| 323 | fwrite(j, strlen(j), 1, f); | ||
| 324 | fclose(f); | 320 | fclose(f); |
| 325 | 321 | ||
| 326 | printf("New password for user %s is %s\n", snac->uid, clear_pwd); | 322 | printf("New password for user %s is %s\n", snac->uid, clear_pwd); |