quality: send_chunk has deeply nested if-else with duplicated file-sending loops #46

Closed
opened 2026-07-20 14:59:20 +02:00 by TapTap · 0 comments
Owner

In src/client/client_send.c:128-181, the send_chunk function has three separate code paths with near-identical loops for sending individual files:

  1. Path A (chunk serialization, lines 129-144): Compresses/serializes the entire chunk and sends as one block
  2. Path B (sendfile, lines 145-161): Iterates over files using file_send_sendfile, with incremental check option
  3. Path C (single calls, lines 162-179): Iterates over files using file_send_single_calls, with incremental check option

Paths B and C have identical structure:

for (int i = 0; i < chunk->element_count; i++) {
    if (config->use_incremental) {
        int rc = send_file_incremental(client, chunk->items[i], config, send_fn);
        if (rc == 1) continue;
        if (rc < 0) return -1;
    } else {
        if (!send_status(client->file_descriptor, STATUS_NEXT))
            return -1;
        if (!send_fn(...))
            return -1;
    }
}

The only difference is the send function pointer. This duplicated code should be unified. Additionally, the function has too many responsibilities (serialization dispatch + per-file sending + incremental logic).

Suggested Fix: Extract a shared helper function for per-file sending that accepts the send function as a parameter, eliminating the duplicated loop structure.

Severity: low

In src/client/client_send.c:128-181, the send_chunk function has three separate code paths with near-identical loops for sending individual files: 1. **Path A (chunk serialization, lines 129-144)**: Compresses/serializes the entire chunk and sends as one block 2. **Path B (sendfile, lines 145-161)**: Iterates over files using file_send_sendfile, with incremental check option 3. **Path C (single calls, lines 162-179)**: Iterates over files using file_send_single_calls, with incremental check option Paths B and C have identical structure: ```c for (int i = 0; i < chunk->element_count; i++) { if (config->use_incremental) { int rc = send_file_incremental(client, chunk->items[i], config, send_fn); if (rc == 1) continue; if (rc < 0) return -1; } else { if (!send_status(client->file_descriptor, STATUS_NEXT)) return -1; if (!send_fn(...)) return -1; } } ``` The only difference is the send function pointer. This duplicated code should be unified. Additionally, the function has too many responsibilities (serialization dispatch + per-file sending + incremental logic). Suggested Fix: Extract a shared helper function for per-file sending that accepts the send function as a parameter, eliminating the duplicated loop structure. Severity: low
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: TapTap/FastSync#46