/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-uid-cache.c: UID caching code. */

/* 
 * Authors:
 *  Dan Winship <danw@ximian.com>
 *
 * Copyright 2000 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of version 2 of the GNU General Public 
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

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

#include "camel-uid-cache.h"

struct _uid_state {
	int level;
	gboolean save;
};

static void free_uid (gpointer key, gpointer value, gpointer data);
static void maybe_write_uid (gpointer key, gpointer value, gpointer data);


static int
mkdir_heir (const char *path, mode_t mode)
{
	char *copy, *p;
	
	p = copy = g_strdup (path);
	do {
		p = strchr (p + 1, '/');
		if (p)
			*p = '\0';
		if (access (copy, F_OK) == -1) {
			if (mkdir (copy, mode) == -1) {
				g_free (copy);
				return -1;
			}
		}
		if (p)
			*p = '/';
	} while (p);
	
	g_free (copy);
	return 0;
}

/**
 * camel_uid_cache_new:
 * @filename: path to load the cache from
 *
 * Creates a new UID cache, initialized from @filename. If @filename
 * doesn't already exist, the UID cache will be empty. Otherwise, if
 * it does exist but can't be read, the function will return %NULL.
 *
 * Return value: a new UID cache, or %NULL
 **/
CamelUIDCache *
camel_uid_cache_new (const char *filename)
{
	CamelUIDCache *cache;
	struct stat st;
	char *dirname, *buf, **uids;
	int fd, i;
	
	dirname = g_path_get_dirname (filename);
	mkdir_heir (dirname, 0700);
	g_free (dirname);
	
	fd = open (filename, O_RDWR | O_CREAT, 0700);
	if (fd == -1)
		return NULL;
	
	if (fstat (fd, &st) != 0) {
		close (fd);
		return NULL;
	}
	buf = g_malloc (st.st_size + 1);
	
	if (read (fd, buf, st.st_size) == -1) {
		close (fd);
		g_free (buf);
		return NULL;
	}
	buf[st.st_size] = '\0';
	
	cache = g_new (CamelUIDCache, 1);
	cache->fd = fd;
	cache->level = 1;
	cache->uids = g_hash_table_new (g_str_hash, g_str_equal);
	
	uids = g_strsplit (buf, "\n", 0);
	g_free (buf);
	for (i = 0; uids[i]; i++) {
		struct _uid_state *state;
		
		state = g_new (struct _uid_state, 1);
		state->level = cache->level;
		state->save = TRUE;
		
		g_hash_table_insert (cache->uids, uids[i], state);
	}
	g_free (uids);
	
	return cache;
}

/**
 * camel_uid_cache_save:
 * @cache: a CamelUIDCache
 *
 * Attempts to save @cache back to disk.
 *
 * Return value: success or failure
 **/
gboolean
camel_uid_cache_save (CamelUIDCache *cache)
{
	if (lseek (cache->fd, 0, SEEK_SET) != 0)
		return FALSE;
	g_hash_table_foreach (cache->uids, maybe_write_uid, cache);
	return ftruncate (cache->fd, lseek (cache->fd, 0, SEEK_CUR)) == 0;
}

static void
maybe_write_uid (gpointer key, gpointer value, gpointer data)
{
	CamelUIDCache *cache = data;
	struct _uid_state *state = value;
	
	if (state && state->level == cache->level && state->save) {
		write (cache->fd, key, strlen (key));
		write (cache->fd, "\n", 1);
	}
}


/**
 * camel_uid_cache_destroy:
 * @cache: a CamelUIDCache
 *
 * Destroys @cache and frees its data.
 **/
void
camel_uid_cache_destroy (CamelUIDCache *cache)
{
	g_hash_table_foreach (cache->uids, free_uid, NULL);
	g_hash_table_destroy (cache->uids);
	close (cache->fd);
	g_free (cache);
}

static void
free_uid (gpointer key, gpointer value, gpointer data)
{
	g_free (key);
	g_free (value);
}


/**
 * camel_uid_cache_get_new_uids:
 * @cache: a CamelUIDCache
 * @uids: an array of UIDs
 *
 * Returns an array of UIDs from @uids that are not in @cache, and
 * removes UIDs from @cache that aren't in @uids.
 *
 * Return value: an array of new UIDs, which must be freed with
 * camel_uid_cache_free_uids().
 **/
GPtrArray *
camel_uid_cache_get_new_uids (CamelUIDCache *cache, GPtrArray *uids)
{
	GPtrArray *new_uids;
	gpointer old_uid;
	char *uid;
	int i;
	
	new_uids = g_ptr_array_new ();
	cache->level++;
	
	for (i = 0; i < uids->len; i++) {
		struct _uid_state *state;
		
		uid = uids->pdata[i];
		if (g_hash_table_lookup_extended (cache->uids, uid, (void **)&old_uid, (void **)&state)) {
			g_hash_table_remove (cache->uids, uid);
			g_free (old_uid);
		} else {
			g_ptr_array_add (new_uids, g_strdup (uid));
			state = g_new (struct _uid_state, 1);
			state->save = FALSE;
		}
		
		state->level = cache->level;
		g_hash_table_insert (cache->uids, g_strdup (uid), state);
	}
	
	return new_uids;
}


/**
 * camel_uid_cache_save_uid:
 * @cache: a CamelUIDCache
 * @uid: a uid to save
 *
 * Marks a uid for saving.
 **/
void
camel_uid_cache_save_uid (CamelUIDCache *cache, const char *uid)
{
	struct _uid_state *state;
	gpointer old_uid;
	
	g_return_if_fail (uid != NULL);
	
	if (g_hash_table_lookup_extended (cache->uids, uid, (void **)&old_uid, (void **)&state)) {
		state->save = TRUE;
		state->level = cache->level;
	} else {
		state = g_new (struct _uid_state, 1);
		state->save = TRUE;
		state->level = cache->level;
		
		g_hash_table_insert (cache->uids, g_strdup (uid), state);
	}
}


/**
 * camel_uid_cache_free_uids:
 * @uids: an array returned from camel_uid_cache_get_new_uids()
 *
 * Frees the array of UIDs.
 **/
void
camel_uid_cache_free_uids (GPtrArray *uids)
{
	int i;
	
	for (i = 0; i < uids->len; i++)
		g_free (uids->pdata[i]);
	g_ptr_array_free (uids, TRUE);
}
ats/databases/libgda'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases/libgda'>
<input type='hidden' name='id' value='d09ae746fd279eefe320a150670bcbfa61a66209'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/?id=d09ae746fd279eefe320a150670bcbfa61a66209'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases?id=d09ae746fd279eefe320a150670bcbfa61a66209'>databases</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases/libgda?id=d09ae746fd279eefe320a150670bcbfa61a66209'>libgda</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/log/databases/libgda?id=d09ae746fd279eefe320a150670bcbfa61a66209&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=87f4d471fb27b9547c1a0de221b51f4c31684157'>- Welcome X.org 7.2 \o/.</a></td><td>flz</td><td><span title='2007-05-20 04:36:56 +0800'>2007-05-20</span></td><td>2</td><td><span class='deletions'>-23</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=a0fa03b7f06fbd2b742d0f43e58bdf6980346c41'>Fix the build with objformat removal on -CURRENT.</a></td><td>mezz</td><td><span title='2007-03-03 02:59:29 +0800'>2007-03-03</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=2e49f22cb95fce3840ccd20087cdc01389b7bcab'>Add conflicts with libgda2.</a></td><td>netchild</td><td><span title='2007-01-07 03:34:50 +0800'>2007-01-07</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+3</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=5a13dc799fc97432b6ccef8591950142a12e69fa'>Add a patch which lets this compile on my -current system.</a></td><td>netchild</td><td><span title='2007-01-07 03:31:00 +0800'>2007-01-07</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+18</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=0deb7538cc6a5ae90f4e3e4bdbfe6d0ab974aed8'>Chase the GNOME X11BASE to LOCALBASE move, and fix the build with the</a></td><td>marcus</td><td><span title='2006-10-14 16:54:54 +0800'>2006-10-14</span></td><td>2</td><td><span class='deletions'>-3</span>/<span class='insertions'>+26</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=1a0d3c0ca1adc625e4369c026975d83e678e8ce3'>portlint:</a></td><td>mezz</td><td><span title='2006-05-16 12:02:04 +0800'>2006-05-16</span></td><td>2</td><td><span class='deletions'>-13</span>/<span class='insertions'>+14</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=adf42e5d3b9afd4f472a3e8b07f76dbc6e411ff6'>Add USE_GETTEXT to appease portlint.</a></td><td>mezz</td><td><span title='2006-05-16 06:23:04 +0800'>2006-05-16</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=f07e7ab26ee96c06478847d07ac072b4b2c64e23'>Remove USE_REINPLACE from all categories starting with D</a></td><td>edwin</td><td><span title='2006-05-05 05:41:12 +0800'>2006-05-05</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+0</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=da880765886f48c0ac8fd253e5601d04ea853215'>Conversion to a single libtool environment.</a></td><td>ade</td><td><span title='2006-02-23 18:40:44 +0800'>2006-02-23</span></td><td>2</td><td><span class='deletions'>-2</span>/<span class='insertions'>+6</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=6aa335e557af790cf39cf78dc626b08374709484'>- Add SHA256 checksums</a></td><td>pav</td><td><span title='2005-11-24 06:41:05 +0800'>2005-11-24</span></td><td>1</td><td><span class='deletions'>-0</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=8d9d5197561118544281733430b48b0b60884450'>Mass-conversion to the USE_AUTOTOOLS New World Order.  The code present</a></td><td>ade</td><td><span title='2005-11-15 14:52:12 +0800'>2005-11-15</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>
<tr><td class='commitgraph'>* </td><td><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports-gnome/commit/databases/libgda?id=3f8673b5304491d815979cbca9f5698f2e27e181'>At Kris's request, back out the MACHINE_ARCH spelling correction until</a></td><td>obrien</td><td><span title='2005-04-12 11:26:56 +0800'>2005-04-12</span></td><td>1</td><td><span class='deletions'>-1</span>/<span class='insertions'>+1</span></td></tr>