diff options
Diffstat (limited to 'src/packet_storage.c')
| -rw-r--r-- | src/packet_storage.c | 28 |
1 files changed, 21 insertions, 7 deletions
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) { |
