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

#include "lib.h"
#include "mail-index.h"
#include "mail-index-util.h"
#include "mail-custom-flags.h"
#include "index-storage.h"

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

typedef struct _IndexList IndexList;

struct _IndexList {
	IndexList *next;

	MailIndex *index;
	int refcount;
};

static IndexList *indexes = NULL;

void index_storage_add(MailIndex *index)
{
	IndexList *list;

	list = i_new(IndexList, 1);
	list->refcount = 1;
	list->index = index;

	list->next = indexes;
	indexes = list;
}

MailIndex *index_storage_lookup_ref(const char *path)
{
	IndexList *list;
	struct stat st1, st2;

	if (stat(path, &st1) < 0)
		return NULL;

	/* compare inodes so we don't break even with symlinks */
	for (list = indexes; list != NULL; list = list->next) {
		if (stat(list->index->dir, &st2) == 0) {
			if (st1.st_ino == st2.st_ino &&
			    st1.st_dev == st2.st_dev) {
				list->refcount++;
				return list->index;
			}
		}
	}

	return NULL;
}

void index_storage_unref(MailIndex *index)
{
	IndexList **list, *rec;

	for (list = &indexes; *list != NULL; list = &(*list)->next) {
		rec = *list;

		if (rec->index == index) {
			if (rec->refcount == 0) {
				index->free(index);
				*list = rec->next;
				i_free(rec);
			}
			return;
		}<commit_msg>Indexes were never closed because refcount wasn't updated.<commit_after>/* Copyright (C) 2002 <NAME> */

#include "lib.h"
#include "mail-index.h"
#include "mail-index-util.h"
#include "mail-custom-flags.h"
#include "index-storage.h"

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

typedef struct _IndexList IndexList;

struct _IndexList {
	IndexList *next;

	MailIndex *index;
	int refcount;
};

static IndexList *indexes = NULL;

void index_storage_add(MailIndex *index)
{
	IndexList *list;

	list = i_new(IndexList, 1);
	list->refcount = 1;
	list->index = index;

	list->next = indexes;
	indexes = list;
}

MailIndex *index_storage_lookup_ref(const char *path)
{
	IndexList *list;
	struct stat st1, st2;

	if (stat(path, &st1) < 0)
		return NULL;

	/* compare inodes so we don't break even with symlinks */
	for (list = indexes; list != NULL; list = list->next) {
		if (stat(list->index->dir, &st2) == 0) {
			if (st1.st_ino == st2.st_ino &&
			    st1.st_dev == st2.st_dev) {
				list->refcount++;
				return list->index;
			}
		}
	}

	return NULL;
}

void index_storage_unref(MailIndex *index)
{
	IndexList **list, *rec;

	for (list = &indexes; *list != NULL; list = &(*list)->next) {
		rec = *list;

		if (rec->index == index) {
			if (--rec->refcount == 0) {
				index->free(index);
				*list = rec->next;
				i_free(rec);
			}
			return;
		}
{'fraction_non_alphanumeric': 0.1643316925407043, 'fraction_numerical': 0.012873911397198031, 'mean_word_length': 3.516239316239316, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, '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': '2872794', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1022, 'n_words': 253}