Refactor: send_files() and send_files_multithreaded() share massive code duplication #151

Closed
opened 2026-07-29 18:21:37 +02:00 by TapTap · 0 comments
Owner

Description

The two main transfer entry points in src/client/client_send.c contain significant duplicated code:

1. Dry-run logic is duplicated (51 identical lines)

  • send_files() lines 361–380
  • send_files_multithreaded() lines 501–520

Both functions create a scanner, iterate chunks, count files/bytes, print output, and return — with identical logic.

2. Scanner creation is duplicated (9 identical arguments)

  • send_files() lines 416–419
  • send_files_multithreaded() (inside scan_directory_multithreaded) lines 300–304

Both call directory_scanner_create() with the same 9 arguments: root_directory, use_metadata, chunk_size, exclude_patterns, exclude_count, include_patterns, include_count, max_size, min_size.

3. Connection setup is duplicated (3 connection paths, same pattern)

The TCP / TLS / SSH connection logic appears in both send_files() (lines 383–410) and send_chunks_multithreaded() (lines 225–252) — the code is structurally identical.

4. Manifest sending is duplicated

The code to serialize and send ArrayList* manifest via STATUS_MANIFEST appears in both send_files() (lines 462–478) and send_chunks_multithreaded() (lines 264–273).

Location

src/client/client_send.c:361-380, :416-419, :462-478, :500-520 and src/client/client_send.c:225-252, :264-273, :300-304

Suggested Fix

  1. Extract dry-run into a helper: static int dry_run(Config* config).
  2. Extract connection setup into: static Client* connect_client(Config* config).
  3. Extract manifest sending into: static bool send_manifest(int fd, ArrayList* manifest).
  4. Keep scanner creation co-located but avoid repeated 9-arg verbosity.

Severity

Medium

Category

Quality / Maintainability

## Description The two main transfer entry points in `src/client/client_send.c` contain significant duplicated code: ### 1. Dry-run logic is duplicated (51 identical lines) - `send_files()` lines 361–380 - `send_files_multithreaded()` lines 501–520 Both functions create a scanner, iterate chunks, count files/bytes, print output, and return — with identical logic. ### 2. Scanner creation is duplicated (9 identical arguments) - `send_files()` lines 416–419 - `send_files_multithreaded()` (inside `scan_directory_multithreaded`) lines 300–304 Both call `directory_scanner_create()` with the same 9 arguments: root_directory, use_metadata, chunk_size, exclude_patterns, exclude_count, include_patterns, include_count, max_size, min_size. ### 3. Connection setup is duplicated (3 connection paths, same pattern) The TCP / TLS / SSH connection logic appears in both `send_files()` (lines 383–410) and `send_chunks_multithreaded()` (lines 225–252) — the code is structurally identical. ### 4. Manifest sending is duplicated The code to serialize and send `ArrayList* manifest` via `STATUS_MANIFEST` appears in both `send_files()` (lines 462–478) and `send_chunks_multithreaded()` (lines 264–273). ## Location `src/client/client_send.c:361-380`, `:416-419`, `:462-478`, `:500-520` and `src/client/client_send.c:225-252`, `:264-273`, `:300-304` ## Suggested Fix 1. Extract dry-run into a helper: `static int dry_run(Config* config)`. 2. Extract connection setup into: `static Client* connect_client(Config* config)`. 3. Extract manifest sending into: `static bool send_manifest(int fd, ArrayList* manifest)`. 4. Keep scanner creation co-located but avoid repeated 9-arg verbosity. ## Severity Medium ## Category Quality / Maintainability
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: TapTap/FastSync#151