| 1170 | } |
| 1171 | |
| 1172 | static int agent_json_edit(cbm_agent_client_id_t id, const char *config_path, |
| 1173 | const char *binary_path, bool remove) { |
| 1174 | char *canonical = agent_json_canonical(id, binary_path); |
| 1175 | if (!canonical) { |
| 1176 | return CBM_AGENT_EDIT_ERROR; |
| 1177 | } |
| 1178 | char *document = NULL; |
| 1179 | size_t length = 0U; |
| 1180 | int read_result = cbm_json_like_read_document(config_path, &document, &length); |
| 1181 | if (read_result < 0) { |
| 1182 | free(canonical); |
| 1183 | return CBM_AGENT_EDIT_ERROR; |
| 1184 | } |
| 1185 | size_t entry_start = 0U; |
| 1186 | size_t entry_end = 0U; |
| 1187 | int find_result = read_result == 1 |
| 1188 | ? 1 |
| 1189 | : agent_json_find_entry(document, length, agent_json_section(id), |
| 1190 | &entry_start, &entry_end); |
| 1191 | if (find_result < 0) { |
| 1192 | free(document); |
| 1193 | free(canonical); |
| 1194 | return CBM_AGENT_EDIT_ERROR; |
| 1195 | } |
| 1196 | if (find_result == 0 && !agent_json_owned(document, entry_start, entry_end, canonical)) { |
| 1197 | free(document); |
| 1198 | free(canonical); |
| 1199 | return CBM_AGENT_EDIT_FOREIGN; |
| 1200 | } |
| 1201 | if ((remove && find_result == 1) || (!remove && find_result == 0)) { |
| 1202 | free(document); |
| 1203 | free(canonical); |
| 1204 | return CBM_AGENT_EDIT_OK; |
| 1205 | } |
| 1206 | const char *section = agent_json_section(id); |
| 1207 | const char *path[1] = {section}; |
| 1208 | int edit_result = |
| 1209 | remove |
| 1210 | ? cbm_json_like_remove_entry_if_unchanged(config_path, path, section ? 1U : 0U, |
| 1211 | AGENT_ENTRY_KEY, document, length) |
| 1212 | : cbm_json_like_upsert_entry_if_unchanged(config_path, path, section ? 1U : 0U, |
| 1213 | AGENT_ENTRY_KEY, canonical, |
| 1214 | read_result == 1 ? NULL : document, length); |
| 1215 | free(document); |
| 1216 | free(canonical); |
| 1217 | return edit_result == 0 ? CBM_AGENT_EDIT_OK : CBM_AGENT_EDIT_ERROR; |
| 1218 | } |
| 1219 | |
| 1220 | static size_t agent_next_line(const char *data, size_t length, size_t start, size_t *content_end) { |
| 1221 | size_t end = start; |
no test coverage detected