| 1154 | } |
| 1155 | |
| 1156 | static int agent_json_edit(cbm_agent_client_id_t id, const char *config_path, |
| 1157 | const char *binary_path, bool remove) { |
| 1158 | char *canonical = agent_json_canonical(id, binary_path); |
| 1159 | if (!canonical) { |
| 1160 | return CBM_AGENT_EDIT_ERROR; |
| 1161 | } |
| 1162 | char *document = NULL; |
| 1163 | size_t length = 0U; |
| 1164 | int read_result = cbm_json_like_read_document(config_path, &document, &length); |
| 1165 | if (read_result < 0) { |
| 1166 | free(canonical); |
| 1167 | return CBM_AGENT_EDIT_ERROR; |
| 1168 | } |
| 1169 | size_t entry_start = 0U; |
| 1170 | size_t entry_end = 0U; |
| 1171 | int find_result = read_result == 1 |
| 1172 | ? 1 |
| 1173 | : agent_json_find_entry(document, length, agent_json_section(id), |
| 1174 | &entry_start, &entry_end); |
| 1175 | if (find_result < 0) { |
| 1176 | free(document); |
| 1177 | free(canonical); |
| 1178 | return CBM_AGENT_EDIT_ERROR; |
| 1179 | } |
| 1180 | if (find_result == 0 && !agent_json_owned(document, entry_start, entry_end, canonical)) { |
| 1181 | free(document); |
| 1182 | free(canonical); |
| 1183 | return CBM_AGENT_EDIT_FOREIGN; |
| 1184 | } |
| 1185 | if ((remove && find_result == 1) || (!remove && find_result == 0)) { |
| 1186 | free(document); |
| 1187 | free(canonical); |
| 1188 | return CBM_AGENT_EDIT_OK; |
| 1189 | } |
| 1190 | const char *section = agent_json_section(id); |
| 1191 | const char *path[1] = {section}; |
| 1192 | int edit_result = |
| 1193 | remove |
| 1194 | ? cbm_json_like_remove_entry_if_unchanged(config_path, path, section ? 1U : 0U, |
| 1195 | AGENT_ENTRY_KEY, document, length) |
| 1196 | : cbm_json_like_upsert_entry_if_unchanged(config_path, path, section ? 1U : 0U, |
| 1197 | AGENT_ENTRY_KEY, canonical, |
| 1198 | read_result == 1 ? NULL : document, length); |
| 1199 | free(document); |
| 1200 | free(canonical); |
| 1201 | return edit_result == 0 ? CBM_AGENT_EDIT_OK : CBM_AGENT_EDIT_ERROR; |
| 1202 | } |
| 1203 | |
| 1204 | static size_t agent_next_line(const char *data, size_t length, size_t start, size_t *content_end) { |
| 1205 | size_t end = start; |
no test coverage detected