| 2361 | } |
| 2362 | |
| 2363 | static int jl_remove_entry(const char *file_path, const char *const *object_path, size_t path_len, |
| 2364 | const char *entry_key, bool enforce_expected, |
| 2365 | const char *expected_content, size_t expected_length) { |
| 2366 | if (jl_validate_arguments(file_path, object_path, path_len, entry_key) != 0) { |
| 2367 | return -1; |
| 2368 | } |
| 2369 | char *source = NULL; |
| 2370 | size_t source_length = 0; |
| 2371 | bool missing = false; |
| 2372 | jl_file_snapshot_t snapshot; |
| 2373 | if (jl_read_file(file_path, &source, &source_length, &missing, &snapshot) != 0) { |
| 2374 | return -1; |
| 2375 | } |
| 2376 | if (enforce_expected) { |
| 2377 | bool expected_missing = expected_content == NULL; |
| 2378 | bool content_matches = expected_missing |
| 2379 | ? missing |
| 2380 | : !missing && source_length == expected_length && |
| 2381 | (expected_length == 0U || |
| 2382 | memcmp(source, expected_content, expected_length) == 0); |
| 2383 | if (!content_matches) { |
| 2384 | free(source); |
| 2385 | return -1; |
| 2386 | } |
| 2387 | } |
| 2388 | if (missing || source_length == 0U) { |
| 2389 | free(source); |
| 2390 | return 0; |
| 2391 | } |
| 2392 | |
| 2393 | size_t object_start = 0; |
| 2394 | if (jl_validate_document(source, source_length, &object_start) != 0) { |
| 2395 | free(source); |
| 2396 | return -1; |
| 2397 | } |
| 2398 | for (size_t i = 0; i < path_len; i++) { |
| 2399 | jl_object_t object; |
| 2400 | if (jl_scan_object(source, source_length, object_start, object_path[i], &object) != 0 || |
| 2401 | object.match_count > 1U) { |
| 2402 | free(source); |
| 2403 | return -1; |
| 2404 | } |
| 2405 | if (object.match_count == 0U) { |
| 2406 | free(source); |
| 2407 | return 0; |
| 2408 | } |
| 2409 | if (source[object.match.value_start] != '{') { |
| 2410 | free(source); |
| 2411 | return -1; |
| 2412 | } |
| 2413 | object_start = object.match.value_start; |
| 2414 | } |
| 2415 | |
| 2416 | jl_object_t object; |
| 2417 | if (jl_scan_object(source, source_length, object_start, entry_key, &object) != 0 || |
| 2418 | object.match_count > 1U) { |
| 2419 | free(source); |
| 2420 | return -1; |
no test coverage detected