Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Think again. Add a global case_sensitive flag which defaults to true (case sensitive), plus accessor and mutator functions.
Matthew Seaman committed 13 years ago
commit b958fabfb72a2ec22f13be6f3743c52489adb78a
parent ee66c21
5 files changed +70 -43
modified libpkg/pkg.c
@@ -1144,19 +1144,13 @@ pkg_recompute(struct pkgdb *db, struct pkg *pkg)
}

int
-
pkg_is_installed(struct pkgdb *db, const char *origin, bool case_insensitive)
+
pkg_is_installed(struct pkgdb *db, const char *origin)
{
	struct pkg *pkg = NULL;
	struct pkgdb_it *it = NULL;
	int ret = EPKG_FATAL;
-
	match_t	match_type;

-
	if (case_insensitive)
-
		match_type = MATCH_EXACT;
-
	else
-
		match_type = MATCH_CASE_INSENSITIVE;
-

-
	if ((it = pkgdb_query(db, origin, match_type)) == NULL)
+
	if ((it = pkgdb_query(db, origin, MATCH_EXACT)) == NULL)
		return (EPKG_FATAL);

	ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC);
modified libpkg/pkg.h.in
@@ -125,27 +125,26 @@ typedef enum {
	 */
	MATCH_ALL,
	/**
-
	 * The argument is the exact pattern.
+
	 * The argument is the exact pattern.  Match will be case
+
	 * sensitive or case insensitive according to
+
	 * pkgdb_case_sensitive()
	 */
	MATCH_EXACT,
	/**
-
	 * The argument is an exact pattern except that matches
-
	 * will be made case insensitively
-
	 */
-
	MATCH_CASE_INSENSITIVE,
-
	/**
-
	 * The argument is a globbing expression.
+
	 * The argument is an exact pattern except that matches will
+
	 * be made case insensitively.  Match is always case sensitive
	 */
	MATCH_GLOB,
	/**
	 * The argument is a regular expression ('modern' style
-
	 * according to re_format(7).
+
	 * according to re_format(7).  Match will be case sensitive or
+
	 * case insensitive according to pkgdb_case_sensitive()
	 */
	MATCH_REGEX,
	/**
	 * The argument is a WHERE clause to use as condition
	 */
-
	MATCH_CONDITION
+
	MATCH_CONDITION,
} match_t;

/**
@@ -791,12 +790,12 @@ const char *pkg_shlib_name(struct pkg_shlib const * const);
/**
 * @param db A pointer to a struct pkgdb object
 * @param origin Package origin
-
 * @param case_insensitive Match the origin name case insensitively
 * @return EPKG_OK if the package is installed,
 * and != EPKG_OK if the package is not installed or an error occurred
+
 * Match will be case sensitive or insensitive depending on
+
 * pkgdb_case_sensitive()
 */
-
int pkg_is_installed(struct pkgdb *db, const char *origin,
-
		     bool case_insensitive);
+
int pkg_is_installed(struct pkgdb *db, const char *origin);

/**
 * Create a repository database.
@@ -862,6 +861,17 @@ int pkgdb_register_ports(struct pkgdb *db, struct pkg *pkg);
int pkgdb_unregister_pkg(struct pkgdb *pkg, const char *origin);

/**
+
 * Set the case sensitivity flag on or off.  Defaults to 
+
 * true (case_sensitive)
+
 */
+
void pkgdb_set_case_sensitivity(bool);
+

+
/**
+
 * Query the state of the case sensitity setting.
+
 */
+
bool pkgdb_case_sensitive(void);
+

+
/**
 * Query the local package database.
 * @param type Describe how pattern should be used.
 * @warning Returns NULL on failure.
modified libpkg/pkg_add.c
@@ -111,7 +111,6 @@ pkg_add(struct pkgdb *db, const char *path, unsigned flags)
	struct pkg	*pkg = NULL;
	struct pkg_dep	*dep = NULL;
	struct pkg      *pkg_inst = NULL;
-
	struct pkgdb_it *it = NULL;
	bool		 extract = true;
	bool		 handle_rc = false;
	char		 dpath[MAXPATHLEN + 1];
@@ -166,7 +165,7 @@ pkg_add(struct pkgdb *db, const char *path, unsigned flags)
	 * Check if the package is already installed
	 */

-
	ret = pkg_is_installed(db, origin, false);
+
	ret = pkg_is_installed(db, origin);
	if (ret == EPKG_OK) {
		pkg_emit_already_installed(pkg_inst);
		pkg_free(pkg_inst);
@@ -189,7 +188,7 @@ pkg_add(struct pkgdb *db, const char *path, unsigned flags)
	}

	while (pkg_deps(pkg, &dep) == EPKG_OK) {
-
		if (pkg_is_installed(db, pkg_dep_origin(dep), false) != EPKG_OK) {
+
		if (pkg_is_installed(db, pkg_dep_origin(dep)) != EPKG_OK) {
			const char *dep_name = pkg_dep_name(dep);
			const char *dep_ver = pkg_dep_version(dep);

modified libpkg/pkgdb.c
@@ -251,8 +251,15 @@ pkgdb_regex(sqlite3_context *ctx, int argc, sqlite3_value **argv)

	re = (regex_t *)sqlite3_get_auxdata(ctx, 0);
	if (re == NULL) {
+
		int cflags;
+

+
		if (pkgdb_case_sensitive())
+
			cflags = REG_EXTENDED | REG_NOSUB;
+
		else
+
			cflags = REG_EXTENDED | REG_NOSUB | REG_ICASE;
+

		re = malloc(sizeof(regex_t));
-
		if (regcomp(re, regex, REG_EXTENDED | REG_NOSUB) != 0) {
+
		if (regcomp(re, regex, cflags) != 0) {
			sqlite3_result_error(ctx, "Invalid regex\n", -1);
			free(re);
			return;
@@ -1260,6 +1267,23 @@ pkgdb_it_free(struct pkgdb_it *it)
	free(it);
}

+
/* By default, MATCH_EXACT and MATCH_REGEX are case sensitive. */
+

+
static bool _case_sensitive_flag = true;
+

+
void
+
pkgdb_set_case_sensitivity(bool case_sensitive)
+
{
+
	_case_sensitive_flag = case_sensitive;
+
	return;
+
}
+

+
bool
+
pkgdb_case_sensitive(void)
+
{
+
	return (_case_sensitive_flag);
+
}
+

static const char *
pkgdb_get_pattern_query(const char *pattern, match_t match)
{
@@ -1274,19 +1298,20 @@ pkgdb_get_pattern_query(const char *pattern, match_t match)
		comp = "";
		break;
	case MATCH_EXACT:
-
		if (checkorigin == NULL)
-
			comp = " WHERE name = ?1 "
-
				"OR name || \"-\" || version = ?1";
-
		else
-
			comp = " WHERE origin = ?1";
-
		break;
-
	case MATCH_CASE_INSENSITIVE:
-
		if (checkorigin == NULL)
-
			comp = " WHERE name = ?1 COLLATE NOCASE"
-
				"OR name || \"-\" || version = ?1"
-
				"COLLATE NOCASE";
-
		else
-
			comp = " WHERE origin = ?1 COLLATE NOCASE";
+
		if (pkgdb_case_sensitive()) {
+
			if (checkorigin == NULL)
+
				comp = " WHERE name = ?1 "
+
					"OR name || \"-\" || version = ?1";
+
			else
+
				comp = " WHERE origin = ?1";
+
		} else {
+
			if (checkorigin == NULL)
+
				comp = " WHERE name = ?1 COLLATE NOCASE"
+
					"OR name || \"-\" || version = ?1"
+
					"COLLATE NOCASE";
+
			else
+
				comp = " WHERE origin = ?1 COLLATE NOCASE";
+
		}
		break;
	case MATCH_GLOB:
		if (checkorigin == NULL)
@@ -1320,10 +1345,10 @@ pkgdb_get_match_how(match_t match)
		how = NULL;
		break;
	case MATCH_EXACT:
-
		how = "%s = ?1";
-
		break;
-
	case MATCH_CASE_INSENSITIVE:
-
		how = "%s = ?1 COLLATE NOCASE";
+
		if (pkgdb_case_sensitive())
+
			how = "%s = ?1";
+
		else
+
			how = "%s = ?1 COLLATE NOCASE";			
		break;
	case MATCH_GLOB:
		how = "%s GLOB ?1";
modified pkg/check.c
@@ -71,8 +71,7 @@ check_deps(struct pkgdb *db, struct pkg *p, struct deps_head *dh)

	while (pkg_deps(p, &dep) == EPKG_OK) {
		/* do we have a missing dependency? */
-
		if (pkg_is_installed(db, pkg_dep_origin(dep), false)
-
		    != EPKG_OK) {
+
		if (pkg_is_installed(db, pkg_dep_origin(dep)) != EPKG_OK) {
			printf("%s has a missing dependency: %s\n", origin,
			       pkg_dep_origin(dep)),
			add_missing_dep(dep, dh, &nbpkgs);