>From bf5054d2207eaf66640e1a132209a869f1b92124 Mon Sep 17 00:00:00 2001
From: Allan McRae <mcrae_allan@hotmail.com>
Date: Sat, 22 Dec 2007 21:28:08 +1000
Subject: [PATCH] Add remove counterparts to alpm_option_add_* functions

Fixes FS#7428. Added functions to remove cachedir, noupgrade, noextract,
ignorepkg, holdpkg and ignoregrp.  Also fixes memory leak in
alpm_option_add_cachedir().

Signed-off-by: Allan McRae <mcrae_allan@hotmail.com>
---
 lib/libalpm/alpm.h   |    6 +++
 lib/libalpm/handle.c |   84 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 0 deletions(-)

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 3a484be..4faeedd 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -104,6 +104,7 @@ int alpm_option_set_dbpath(const char *dbpath);
 alpm_list_t *alpm_option_get_cachedirs();
 int alpm_option_add_cachedir(const char *cachedir);
 void alpm_option_set_cachedirs(alpm_list_t *cachedirs);
+int alpm_option_remove_cachedir(const char *cachedir);
 
 const char *alpm_option_get_logfile();
 int alpm_option_set_logfile(const char *logfile);
@@ -117,22 +118,27 @@ void alpm_option_set_usesyslog(unsigned short usesyslog);
 alpm_list_t *alpm_option_get_noupgrades();
 void alpm_option_add_noupgrade(const char *pkg);
 void alpm_option_set_noupgrades(alpm_list_t *noupgrade);
+int alpm_option_remove_noupgrade(const char *pkg);
 
 alpm_list_t *alpm_option_get_noextracts();
 void alpm_option_add_noextract(const char *pkg);
 void alpm_option_set_noextracts(alpm_list_t *noextract);
+int alpm_option_remove_noextract(const char *pkg);
 
 alpm_list_t *alpm_option_get_ignorepkgs();
 void alpm_option_add_ignorepkg(const char *pkg);
 void alpm_option_set_ignorepkgs(alpm_list_t *ignorepkgs);
+int alpm_option_remove_ignorepkg(const char *pkg);
 
 alpm_list_t *alpm_option_get_holdpkgs();
 void alpm_option_add_holdpkg(const char *pkg);
 void alpm_option_set_holdpkgs(alpm_list_t *holdpkgs);
+int alpm_option_remove_holdpkg(const char *pkg);
 
 alpm_list_t *alpm_option_get_ignoregrps();
 void alpm_option_add_ignoregrp(const char *grp);
 void alpm_option_set_ignoregrps(alpm_list_t *ignoregrps);
+int alpm_option_remove_ignoregrp(const char *grp);
 
 time_t alpm_option_get_upgradedelay();
 void alpm_option_set_upgradedelay(time_t delay);
diff --git a/lib/libalpm/handle.c b/lib/libalpm/handle.c
index ab6fc7e..0bbfa89 100644
--- a/lib/libalpm/handle.c
+++ b/lib/libalpm/handle.c
@@ -386,6 +386,7 @@ int SYMEXPORT alpm_option_add_cachedir(const char *cachedir)
 	newcachedir[cachedirlen-1] = '/';
 	handle->cachedirs = alpm_list_add(handle->cachedirs, newcachedir);
 	_alpm_log(PM_LOG_DEBUG, "option 'cachedir' = %s\n", newcachedir);
+	FREE(newcachedir);
 	return(0);
 }
 
@@ -395,6 +396,29 @@ void SYMEXPORT alpm_option_set_cachedirs(alpm_list_t *cachedirs)
 	if(cachedirs) handle->cachedirs = cachedirs;
 }
 
+int SYMEXPORT alpm_option_remove_cachedir(const char *cachedir)
+{
+	void *vdata = NULL;
+	char *newcachedir;
+	size_t cachedirlen;
+	/* verify cachedir ends in a '/' */
+	cachedirlen = strlen(cachedir);
+	if(cachedir[cachedirlen-1] != '/') {
+		cachedirlen += 1;
+	}
+	newcachedir = calloc(cachedirlen + 1, sizeof(char));
+	strncpy(newcachedir, cachedir, cachedirlen);
+	newcachedir[cachedirlen-1] = '/';
+	handle->cachedirs = alpm_list_remove(handle->cachedirs, newcachedir,
+		_alpm_str_cmp, &vdata);
+	FREE(newcachedir);
+	if(vdata != NULL) {
+		FREE(vdata);
+		return(1);
+	}
+	return(0);
+}
+
 int SYMEXPORT alpm_option_set_logfile(const char *logfile)
 {
 	char *oldlogfile = handle->logfile;
@@ -436,6 +460,18 @@ void SYMEXPORT alpm_option_set_noupgrades(alpm_list_t *noupgrade)
 	if(noupgrade) handle->noupgrade = noupgrade;
 }
 
+int SYMEXPORT alpm_option_remove_noupgrade(const char *pkg)
+{
+	void *vdata = NULL;
+	handle->noupgrade = alpm_list_remove(handle->noupgrade, pkg, 
+		_alpm_str_cmp, &vdata);
+	if(vdata != NULL) {
+		FREE(vdata);
+		return(1);
+	}
+	return(0);
+}
+
 void SYMEXPORT alpm_option_add_noextract(const char *pkg)
 {
 	handle->noextract = alpm_list_add(handle->noextract, strdup(pkg));
@@ -447,6 +483,18 @@ void SYMEXPORT alpm_option_set_noextracts(alpm_list_t *noextract)
 	if(noextract) handle->noextract = noextract;
 }
 
+int SYMEXPORT alpm_option_remove_noextract(const char *pkg)
+{
+	void *vdata = NULL;
+	handle->noextract = alpm_list_remove(handle->noextract, strdup(pkg), 
+		_alpm_str_cmp, &vdata);
+	if(vdata != NULL) {
+		FREE(vdata);
+		return(1);
+	}
+	return(0);
+}
+
 void SYMEXPORT alpm_option_add_ignorepkg(const char *pkg)
 {
 	handle->ignorepkg = alpm_list_add(handle->ignorepkg, strdup(pkg));
@@ -458,6 +506,18 @@ void alpm_option_set_ignorepkgs(alpm_list_t *ignorepkgs)
 	if(ignorepkgs) handle->ignorepkg = ignorepkgs;
 }
 
+int SYMEXPORT alpm_option_remove_ignorepkg(const char *pkg)
+{
+	void *vdata = NULL;
+	handle->ignorepkg = alpm_list_remove(handle->ignorepkg, strdup(pkg), 
+		_alpm_str_cmp, &vdata);
+	if(vdata != NULL) {
+		FREE(vdata);
+		return(1);
+	}
+	return(0);
+}
+
 void SYMEXPORT alpm_option_add_holdpkg(const char *pkg)
 {
 	handle->holdpkg = alpm_list_add(handle->holdpkg, strdup(pkg));
@@ -469,6 +529,18 @@ void SYMEXPORT alpm_option_set_holdpkgs(alpm_list_t *holdpkgs)
 	if(holdpkgs) handle->holdpkg = holdpkgs;
 }
 
+int SYMEXPORT alpm_option_remove_holdpkg(const char *pkg)
+{
+	void *vdata = NULL;
+	handle->holdpkg = alpm_list_remove(handle->holdpkg, strdup(pkg), 
+		_alpm_str_cmp, &vdata);
+	if(vdata != NULL) {
+		FREE(vdata);
+		return(1);
+	}
+	return(0);
+}
+
 void SYMEXPORT alpm_option_add_ignoregrp(const char *grp)
 {
 	handle->ignoregrp = alpm_list_add(handle->ignoregrp, strdup(grp));
@@ -480,6 +552,18 @@ void alpm_option_set_ignoregrps(alpm_list_t *ignoregrps)
 	if(ignoregrps) handle->ignoregrp = ignoregrps;
 }
 
+int SYMEXPORT alpm_option_remove_ignoregrp(const char *grp)
+{
+	void *vdata = NULL;
+	handle->ignoregrp = alpm_list_remove(handle->ignoregrp, strdup(grp), 
+		_alpm_str_cmp, &vdata);
+	if(vdata != NULL) {
+		FREE(vdata);
+		return(1);
+	}
+	return(0);
+}
+
 void SYMEXPORT alpm_option_set_upgradedelay(time_t delay)
 {
 	handle->upgradedelay = delay;
-- 
1.5.3.7

