<commit_before>/* Copyright (C) 2005 <NAME> */

#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "hash.h"
#include "seq-range-array.h"
#include "write-full.h"
#include "dbox-file.h"
#include "dbox-keywords.h"
#include "dbox-sync.h"
#include "dbox-uidlist.h"
#include "dbox-storage.h"

#include <stddef.h>

int dbox_sync_get_file_offset(struct dbox_sync_context *ctx, uint32_t seq,
			      uint32_t *file_seq_r, uoff_t *offset_r)
{
	int ret;

	ret = dbox_file_lookup_offset(ctx->mbox, ctx->sync_view, seq,
				      file_seq_r, offset_r);
	if (ret <= 0) {
		if (ret == 0) {
			mail_storage_set_critical(STORAGE(ctx->mbox->storage),
				"Unexpectedly lost seq %u in "
				"dbox %s", seq, ctx->mbox->path);
		}
		return -1;
	}
	return 0;
}

static int dbox_sync_add_seq(struct dbox_sync_context *ctx, uint32_t seq,
                             const struct dbox_sync_rec *sync_rec)
{
	struct dbox_sync_rec new_sync_rec;
	struct dbox_sync_file_entry *entry;
	const uint32_t *file_seqs;
	unsigned int i, count;
	uint32_t file_seq;
	uoff_t offset;

	if (dbox_sync_get_file_offset(ctx, seq, &file_seq, &offset) < 0)
		return -1;

	if (file_seq == 0) {
		mail_storage_set_critical(STORAGE(ctx->mbox->storage),
			"Cached message offset lost for seq %u in "
			"dbox %s", seq, ctx->mbox->path);
		return -1;
	}

	file_seqs = array_get(&ctx->added_file_seqs, &count);
	for (i = 0; i < count; i++) {
		if (file_seqs[i] == file_seq) {
			/* already added */
			return 0;
		}
	}
	array_append(&ctx->added_file_seqs, &file_seq, 1);

	entry = hash_lookup(ctx->syncs, POINTER_CAST(file_seq));
	if (entry == NULL) {
		entry = p_new(ctx->pool, struct dbox_sync_file_entry, 1);
		entry->file_seq = file_seq;
		ARRAY_CREATE(&entry->sync_recs, ctx->pool,
			     struct dbox_sync_rec, 3);
		hash_insert(ctx->syncs, POINTER_CAST(file_seq), entry);
	}

	new_sync_rec = *sync_rec;
	new_sync_rec.seq1 = seq;
	array_append(&entry->sync_recs, &new_sync_rec, 1);
	return 0;
}

static int dbox_sync_add(struct dbox_sync_context *ctx,
			 const struct mail_index_sync_rec *sync_rec)<commit_msg>And more generic error fix..<commit_after>/* Copyright (C) 2005 <NAME> */

#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "hash.h"
#include "seq-range-array.h"
#include "write-full.h"
#include "dbox-file.h"
#include "dbox-keywords.h"
#include "dbox-sync.h"
#include "dbox-uidlist.h"
#include "dbox-storage.h"

#include <stddef.h>

int dbox_sync_get_file_offset(struct dbox_sync_context *ctx, uint32_t seq,
			      uint32_t *file_seq_r, uoff_t *offset_r)
{
	int ret;

	ret = dbox_file_lookup_offset(ctx->mbox, ctx->sync_view, seq,
				      file_seq_r, offset_r);
	if (ret <= 0) {
		if (ret == 0) {
			mail_storage_set_critical(STORAGE(ctx->mbox->storage),
				"Unexpectedly lost seq %u in "
				"dbox %s", seq, ctx->mbox->path);
		}
		return -1;
	}
	if (*file_seq_r == 0) {
		mail_storage_set_critical(STORAGE(ctx->mbox->storage),
			"Cached message offset lost for seq %u in "
			"dbox %s", seq, ctx->mbox->path);
		return -1;
	}
	return 0;
}

static int dbox_sync_add_seq(struct dbox_sync_context *ctx, uint32_t seq,
                             const struct dbox_sync_rec *sync_rec)
{
	struct dbox_sync_rec new_sync_rec;
	struct dbox_sync_file_entry *entry;
	const uint32_t *file_seqs;
	unsigned int i, count;
	uint32_t file_seq;
	uoff_t offset;

	if (dbox_sync_get_file_offset(ctx, seq, &file_seq, &offset) < 0)
		return -1;

	file_seqs = array_get(&ctx->added_file_seqs, &count);
	for (i = 0; i < count; i++) {
		if (file_seqs[i] == file_seq) {
			/* already added */
			return 0;
		}
	}
	array_append(&ctx->added_file_seqs, &file_seq, 1);

	entry = hash_lookup(ctx->syncs, POINTER_CAST(file_seq));
	if (entry == NULL) {
		entry = p_new(ctx->pool, struct dbox_sync_file_entry, 1);
		entry->file_seq = file_seq;
		ARRAY_CREATE(&entry->sync_recs, ctx->pool,
			     struct dbox_sync_rec, 3);
		hash_insert(ctx->syncs, POINTER_CAST(file_seq), entry);
	}

	new_sync_rec = *sync_rec;
	new_sync_rec.seq1 = seq;
	array_append(&entry->sync_recs, &new_sync_rec, 1);
	return 0;
}

static int dbox_sync_add(struct dbox_sync_context *ctx,
			 const struct mail_index_sync_rec *sync_rec)
{'fraction_non_alphanumeric': 0.14189997607083035, 'fraction_numerical': 0.014357501794687724, 'mean_word_length': 4.135135135135135, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5885665', 'n_tokens_mistral': 1845, 'n_tokens_neox': 1700, 'n_words': 361}