| 413 | } |
| 414 | |
| 415 | static bool gf_read_capture(cbm_git_facts_t *facts, const char *path, size_t length, |
| 416 | cbm_git_bytes_t *out, cbm_git_facts_error_t *error) { |
| 417 | *out = (cbm_git_bytes_t){0}; |
| 418 | if (length == SIZE_MAX) |
| 419 | return gf_limit(facts, error, "Captured output is too large"); |
| 420 | unsigned char *bytes = gf_allocate(facts, &facts->arena, length + 1, error); |
| 421 | if (!bytes) { |
| 422 | if (error->status == CBM_GIT_FACTS_OOM) |
| 423 | gf_error(error, CBM_GIT_FACTS_OOM, "Cannot retain captured output"); |
| 424 | return false; |
| 425 | } |
| 426 | FILE *file = cbm_fopen(path, "rb"); |
| 427 | if (!file) |
| 428 | return gf_error(error, CBM_GIT_FACTS_IO, "Cannot read captured output"); |
| 429 | bool ok = true; |
| 430 | size_t used = 0; |
| 431 | while (used < length) { |
| 432 | if (!gf_gate(facts, error)) { |
| 433 | ok = false; |
| 434 | break; |
| 435 | } |
| 436 | size_t chunk = length - used; |
| 437 | if (chunk > 65536) |
| 438 | chunk = 65536; |
| 439 | size_t got = fread(bytes + used, 1, chunk, file); |
| 440 | used += got; |
| 441 | if (got != chunk) { |
| 442 | ok = gf_error(error, CBM_GIT_FACTS_IO, "Captured output was truncated or unreadable"); |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | if (ok && (fgetc(file) != EOF || ferror(file))) |
| 447 | ok = gf_error(error, CBM_GIT_FACTS_IO, "Captured output changed while being read"); |
| 448 | if (fclose(file) != 0 && ok) |
| 449 | ok = gf_error(error, CBM_GIT_FACTS_IO, "Cannot close captured output"); |
| 450 | if (ok) |
| 451 | ok = gf_gate(facts, error); |
| 452 | if (!ok) |
| 453 | return false; |
| 454 | bytes[length] = '\0'; /* convenience sentinel; length remains authoritative */ |
| 455 | *out = (cbm_git_bytes_t){bytes, length}; |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | static void gf_command_diagnostic(cbm_git_facts_error_t *error, const gf_result_t *result) { |
| 460 | error->exit_code = result->exit_code; |
no test coverage detected