GET /api/index-status — returns status of all index jobs */
| 1797 | |
| 1798 | /* GET /api/index-status — returns status of all index jobs */ |
| 1799 | static void handle_index_status(cbm_http_server_t *server, cbm_http_conn_t *c) { |
| 1800 | /* Size the body for a full table: every slot's framing plus its root and |
| 1801 | * error message at the widest escape. A fixed 2 KB stack buffer used to |
| 1802 | * clamp here, so one long resolved root (the field holds 4 KB) came back |
| 1803 | * as a cut JSON body with status 200. The append helpers keep clamping, |
| 1804 | * so this calculation is not the only thing keeping pos in range. */ |
| 1805 | size_t buf_size = 2 + (size_t)MAX_INDEX_JOBS * (INDEX_STATUS_ENTRY_FRAMING + |
| 1806 | JSON_ESCAPE_MAX_EXPANSION * |
| 1807 | (INDEX_JOB_ROOT_CAP + INDEX_JOB_ERROR_CAP)); |
| 1808 | char *buf = cbm_alloc(CBM_MEM_CLASS_OTHER, buf_size); |
| 1809 | if (!buf) { |
| 1810 | cbm_http_replyf(c, 500, g_cors_json, "{\"error\":\"oom\"}"); |
| 1811 | return; |
| 1812 | } |
| 1813 | int pos = 0; |
| 1814 | http_appendf(buf, buf_size, &pos, "["); |
| 1815 | for (int i = 0; i < MAX_INDEX_JOBS; i++) { |
| 1816 | int st = atomic_load(&server->index_jobs[i].status); |
| 1817 | if (st == 0) |
| 1818 | continue; |
| 1819 | if (pos > 1) |
| 1820 | http_appendf(buf, buf_size, &pos, ","); |
| 1821 | const char *ss = st == 1 ? "indexing" : st == 2 ? "done" : "error"; |
| 1822 | /* Both fields are free-form, so escape them — a quote in a path would |
| 1823 | * otherwise end its JSON string early. */ |
| 1824 | http_appendf(buf, buf_size, &pos, "{\"slot\":%d,\"status\":\"%s\",\"path\":\"", i, ss); |
| 1825 | http_append_json_escaped(buf, buf_size, &pos, server->index_jobs[i].root_path); |
| 1826 | http_appendf(buf, buf_size, &pos, "\",\"error\":\""); |
| 1827 | http_append_json_escaped(buf, buf_size, &pos, |
| 1828 | st == 3 ? server->index_jobs[i].error_msg : ""); |
| 1829 | http_appendf(buf, buf_size, &pos, "\"}"); |
| 1830 | } |
| 1831 | http_appendf(buf, buf_size, &pos, "]"); |
| 1832 | if ((size_t)pos >= buf_size) { |
| 1833 | pos = (int)buf_size - 1; |
| 1834 | } |
| 1835 | buf[pos] = '\0'; |
| 1836 | cbm_http_reply_buf(c, 200, g_cors_json, buf, (size_t)pos); |
| 1837 | cbm_free(CBM_MEM_CLASS_OTHER, buf); |
| 1838 | } |
| 1839 | |
| 1840 | static void unwatch_project(cbm_http_server_t *srv, const char *name) { |
| 1841 | if (srv && srv->watcher) { |
no test coverage detected