From 96bda3794a6a96021dce0f29c4860b4276954784 Mon Sep 17 00:00:00 2001 From: moon Date: Tue, 16 Sep 2025 11:45:09 +0330 Subject: fix duplicate + better propagation --- src/packet_storage.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src/packet_storage.c') diff --git a/src/packet_storage.c b/src/packet_storage.c index 5781661..922cc45 100644 --- a/src/packet_storage.c +++ b/src/packet_storage.c @@ -141,17 +141,31 @@ void save_author_packet_hash(const public_key author, const hash packet_hash) { const char *dir = "packets_by_author"; mkdir(dir, 0755); - char author_hex[ 65 ]; + char author_hex[65]; sprintf(author_hex, hash_fmt, hash_ref(author)); - char filename[ 256 ]; + char filename[256]; snprintf(filename, sizeof(filename), "%s/%s.list", dir, author_hex); - FILE *f = fopen(filename, "ab"); - if (!f) return; - fwrite(packet_hash, 32, 1, f); - fputc('\n', f); - fclose(f); + FILE *f = fopen(filename, "rb"); + if (f) { + unsigned char buf[64]; + while (fread(buf, 1, 64, f) >= 32) { + int c = fgetc(f); + (void)c; + if (memcmp(buf, packet_hash, 32) == 0) { + fclose(f); + return; + } + } + fclose(f); + } + + FILE *fa = fopen(filename, "ab"); + if (!fa) return; + fwrite(packet_hash, 32, 1, fa); + fputc('\n', fa); + fclose(fa); } void save_recipient_packet_hash(const recipient recipient, const hash packet_hash) { -- cgit v1.2.3