Skip to content

Commit 9fa9351

Browse files
Update archive.c
1 parent da23c36 commit 9fa9351

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

code/logic/archive.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,34 +329,48 @@ fossil_io_archive_t* fossil_io_archive_create(const char *path, fossil_io_archiv
329329
// ======================================================
330330

331331
bool fossil_io_archive_get_stats(fossil_io_archive_t *archive, fossil_io_archive_stats_t *stats) {
332-
if (!archive || !stats) return false;
333-
332+
if (!archive || !stats)
333+
return false;
334+
335+
if (!archive->entries || archive->entry_count == 0) {
336+
// Handle empty archive safely
337+
stats->total_entries = 0;
338+
stats->total_size = 0;
339+
stats->compressed_size = 0;
340+
stats->compression_ratio = 0.0;
341+
return true;
342+
}
343+
334344
stats->total_entries = 0;
335345
stats->total_size = 0;
336346
stats->compressed_size = 0;
337347

348+
// Optional: track seen entry names or IDs to avoid duplicates
349+
// (requires unique identifier or path)
338350
for (size_t i = 0; i < archive->entry_count; i++) {
339-
const fossil_io_archive_entry_t *entry = &archive->entries[i];
340-
bool duplicate = false;
351+
fossil_io_archive_entry_t *entry = &archive->entries[i];
352+
353+
if (!entry || !entry->name)
354+
continue; // skip invalid entries safely
341355

342-
// Check if we've seen this entry name before
356+
// Skip duplicates by comparing with previous entries
357+
bool duplicate = false;
343358
for (size_t j = 0; j < i; j++) {
344-
if (strcmp(archive->entries[j].name, entry->name) == 0) {
359+
if (archive->entries[j].name && strcmp(entry->name, archive->entries[j].name) == 0) {
345360
duplicate = true;
346361
break;
347362
}
348363
}
349-
350-
if (duplicate) continue;
364+
if (duplicate)
365+
continue;
351366

352367
stats->total_entries++;
353368
stats->total_size += entry->size;
354369
stats->compressed_size += entry->compressed_size;
355370
}
356371

357-
stats->compression_ratio =
358-
stats->total_size > 0 ?
359-
(double)stats->compressed_size / stats->total_size : 0.0;
372+
stats->compression_ratio = stats->total_size > 0 ?
373+
(double)stats->compressed_size / (double)stats->total_size : 0.0;
360374

361375
return true;
362376
}

0 commit comments

Comments
 (0)