| 2294 | } |
| 2295 | |
| 2296 | static int jl_remove_entry(const char *file_path, const char *const *object_path, size_t path_len, |
| 2297 | const char *entry_key, bool enforce_expected, |
| 2298 | const char *expected_content, size_t expected_length) { |
| 2299 | if (jl_validate_arguments(file_path, object_path, path_len, entry_key) != 0) { |
| 2300 | return -1; |
| 2301 | } |
| 2302 | char *source = NULL; |
| 2303 | size_t source_length = 0; |
| 2304 | bool missing = false; |
| 2305 | jl_file_snapshot_t snapshot; |
| 2306 | if (jl_read_file(file_path, &source, &source_length, &missing, &snapshot) != 0) { |
| 2307 | return -1; |
| 2308 | } |
| 2309 | if (enforce_expected) { |
| 2310 | bool expected_missing = expected_content == NULL; |
| 2311 | bool content_matches = expected_missing |
| 2312 | ? missing |
| 2313 | : !missing && source_length == expected_length && |
| 2314 | (expected_length == 0U || |
| 2315 | memcmp(source, expected_content, expected_length) == 0); |
| 2316 | if (!content_matches) { |
| 2317 | free(source); |
| 2318 | return -1; |
| 2319 | } |
| 2320 | } |
| 2321 | if (missing || source_length == 0U) { |
| 2322 | free(source); |
| 2323 | return 0; |
| 2324 | } |
| 2325 | |
| 2326 | size_t object_start = 0; |
| 2327 | if (jl_validate_document(source, source_length, &object_start) != 0) { |
| 2328 | free(source); |
| 2329 | return -1; |
| 2330 | } |
| 2331 | for (size_t i = 0; i < path_len; i++) { |
| 2332 | jl_object_t object; |
| 2333 | if (jl_scan_object(source, source_length, object_start, object_path[i], &object) != 0 || |
| 2334 | object.match_count > 1U) { |
| 2335 | free(source); |
| 2336 | return -1; |
| 2337 | } |
| 2338 | if (object.match_count == 0U) { |
| 2339 | free(source); |
| 2340 | return 0; |
| 2341 | } |
| 2342 | if (source[object.match.value_start] != '{') { |
| 2343 | free(source); |
| 2344 | return -1; |
| 2345 | } |
| 2346 | object_start = object.match.value_start; |
| 2347 | } |
| 2348 | |
| 2349 | jl_object_t object; |
| 2350 | if (jl_scan_object(source, source_length, object_start, entry_key, &object) != 0 || |
| 2351 | object.match_count > 1U) { |
| 2352 | free(source); |
| 2353 | return -1; |
no test coverage detected