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

#include "lib.h"
#include "istream.h"
#include "index-mail.h"
#include "mbox-storage.h"
#include "mbox-file.h"
#include "mbox-lock.h"
#include "mbox-sync-private.h"
#include "istream-raw-mbox.h"
#include "istream-header-filter.h"

#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

static int mbox_mail_seek(struct index_mail *mail)
{
	struct mbox_transaction_context *t =
		(struct mbox_transaction_context *)mail->trans;
	struct index_mailbox *ibox = mail->ibox;
	enum mbox_sync_flags sync_flags = 0;
	int ret, deleted;

	if (mail->mail.mail.expunged)
		return 0;

__again:
	if (ibox->mbox_lock_type == F_UNLCK) {
		sync_flags |= MBOX_SYNC_LOCK_READING;
		if (mbox_sync(ibox, sync_flags) < 0)
			return -1;

		/* refresh index file after mbox has been locked to make
		   sure we get only up-to-date mbox offsets. */
		if (mail_index_refresh(ibox->index) < 0) {
			mail_storage_set_index_error(ibox);
			return -1;
		}

		i_assert(ibox->mbox_lock_type != F_UNLCK);
		t->mbox_lock_id = ibox->mbox_lock_id;
	}

	if (mbox_file_open_stream(ibox) < 0)
		return -1;

	ret = mbox_file_seek(ibox, mail->trans->trans_view,
			     mail->mail.mail.seq, &deleted);
	if (ret < 0) {
		if (deleted) {
			mail->mail.mail.expunged = TRUE;
			return 0;
		}
		return -1;
	}

	if (ret == 0) {
		/* we'll need to re-sync it completely */
		if (ibox->mbox_lock_type == F_RDLCK) {
			if (ibox->mbox_lock_id == t->mbox_lock_id)
				t->mbox_lock_id = 0;
			(void)mbox_unlock(mail->ibox, ibox->mbox_lock_id);
			ibox->mbox_lock_id = 0;
			i_assert(ibox->mbox_lock_type == F_UNLCK);
		}

		sync_flags |= MBOX_SYNC_UNDIRTY;
		goto __again;
	}

	return 1;
}

static time_t mbox_mail_get_received_date(struct mail *_mail)
{
	struct index_mail *mail = (struct index_mail *)_mail;
	struct index_mail_data *data = &mail->data;

	(void)index_mail_get_received_date(_mail);
	if (data->received_date != (time_t)-1)
		return data->received_date;

	if (mbox_mail_seek(mail) <= 0)
		return (time_t)-1;
	data->received_date =
		istream_raw_mbox_get_received_time(mail->ibox->mbox_stream);
	if (data->received_date == (time_t)-1) {
		/* it's broken and conflicts with our "not found"
		   return value. change it. */
		data->received_date = 0;
	}

	mail_cache_add(mail->trans->cache_trans, mail->data.seq,
		       MAIL_CACHE_RECEIVED_DATE,
		       &data->received_date, sizeof(data->received_date));
	return data->received_date;
}

static const char *
mbox_mail_get_special(struct mail *_mail, enum mail_fetch_field field)
{
	struct index_mail *mail = (struct index_mail *)_mail;

	if (field == MAIL_FETCH_FROM_ENVELOPE) {
		if (mbox_mail_seek(mail) <= 0)
			return NULL;

		return istream_raw_mbox_get_sender(mail->ibox->mbox_stream);

	}

	return index_mail_get_special(_mail, field);
}

static uoff_t mbox_mail_get_physical_size(struct mail *_mail)
{
	struct index_mail *mail = (struct index_mail *)_mail;
	struct index_mail_data *data = &mail->data;
	struct istream *stream;
	uoff_t hdr_offset, body_offset, body_size;

	/* our header size varies, so don't do any caching */
	stream = mail->ibox->mbox_stream;
	hdr_offset = istream_raw_mbox_get_header_offset(stream);
	body_offset = istream_raw_mbox_get_body_offset(stream);
	body_size = istream_raw_mbox_get_body_size(stream, (uoff_t)-1);

	data->physical_size = (body_offset - hdr_offset) + body_size;
	return data->physical_size;

}<commit_msg>get_physical_size() was crashing if mbox hadn't been opened yet.<commit_after>/* Copyright (C) 2003 <NAME> */

#include "lib.h"
#include "istream.h"
#include "index-mail.h"
#include "mbox-storage.h"
#include "mbox-file.h"
#include "mbox-lock.h"
#include "mbox-sync-private.h"
#include "istream-raw-mbox.h"
#include "istream-header-filter.h"

#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

static int mbox_mail_seek(struct index_mail *mail)
{
	struct mbox_transaction_context *t =
		(struct mbox_transaction_context *)mail->trans;
	struct index_mailbox *ibox = mail->ibox;
	enum mbox_sync_flags sync_flags = 0;
	int ret, deleted;

	if (mail->mail.mail.expunged)
		return 0;

__again:
	if (ibox->mbox_lock_type == F_UNLCK) {
		sync_flags |= MBOX_SYNC_LOCK_READING;
		if (mbox_sync(ibox, sync_flags) < 0)
			return -1;

		/* refresh index file after mbox has been locked to make
		   sure we get only up-to-date mbox offsets. */
		if (mail_index_refresh(ibox->index) < 0) {
			mail_storage_set_index_error(ibox);
			return -1;
		}

		i_assert(ibox->mbox_lock_type != F_UNLCK);
		t->mbox_lock_id = ibox->mbox_lock_id;
	}

	if (mbox_file_open_stream(ibox) < 0)
		return -1;

	ret = mbox_file_seek(ibox, mail->trans->trans_view,
			     mail->mail.mail.seq, &deleted);
	if (ret < 0) {
		if (deleted) {
			mail->mail.mail.expunged = TRUE;
			return 0;
		}
		return -1;
	}

	if (ret == 0) {
		/* we'll need to re-sync it completely */
		if (ibox->mbox_lock_type == F_RDLCK) {
			if (ibox->mbox_lock_id == t->mbox_lock_id)
				t->mbox_lock_id = 0;
			(void)mbox_unlock(mail->ibox, ibox->mbox_lock_id);
			ibox->mbox_lock_id = 0;
			i_assert(ibox->mbox_lock_type == F_UNLCK);
		}

		sync_flags |= MBOX_SYNC_UNDIRTY;
		goto __again;
	}

	return 1;
}

static time_t mbox_mail_get_received_date(struct mail *_mail)
{
	struct index_mail *mail = (struct index_mail *)_mail;
	struct index_mail_data *data = &mail->data;

	(void)index_mail_get_received_date(_mail);
	if (data->received_date != (time_t)-1)
		return data->received_date;

	if (mbox_mail_seek(mail) <= 0)
		return (time_t)-1;
	data->received_date =
		istream_raw_mbox_get_received_time(mail->ibox->mbox_stream);
	if (data->received_date == (time_t)-1) {
		/* it's broken and conflicts with our "not found"
		   return value. change it. */
		data->received_date = 0;
	}

	mail_cache_add(mail->trans->cache_trans, mail->data.seq,
		       MAIL_CACHE_RECEIVED_DATE,
		       &data->received_date, sizeof(data->received_date));
	return data->received_date;
}

static const char *
mbox_mail_get_special(struct mail *_mail, enum mail_fetch_field field)
{
	struct index_mail *mail = (struct index_mail *)_mail;

	if (field == MAIL_FETCH_FROM_ENVELOPE) {
		if (mbox_mail_seek(mail) <= 0)
			return NULL;

		return istream_raw_mbox_get_sender(mail->ibox->mbox_stream);

	}

	return index_mail_get_special(_mail, field);
}

static uoff_t mbox_mail_get_physical_size(struct mail *_mail)
{
	struct index_mail *mail = (struct index_mail *)_mail;
	struct index_mail_data *data = &mail->data;
	struct istream *stream;
	uoff_t hdr_offset, body_offset, body_size;

	if (mbox_mail_seek(mail) <= 0)
		return (uoff_t)-1;

	/* our header size varies, so don't do any caching */
	stream = mail->ibox->mbox_stream;
	hdr_offset = istream_raw_mbox_get_header_offset(stream);
	body_offset = istream_raw_mbox_get_body_offset(stream);
	body_size = istream_raw_mbox_get_body_size(stream, (uoff_t)-1);

	data->physical_size = (body_offset - hdr_offset) + body_size;
	return data->physical_size;

}
{'fraction_non_alphanumeric': 0.13202933985330073, 'fraction_numerical': 0.007766431756076514, 'mean_word_length': 4.497233201581028, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 93, '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': '5614294', 'n_tokens_mistral': 2940, 'n_tokens_neox': 2704, 'n_words': 553}