[Midnightbsd-cvs] src [12207] trunk/lib/libmport: revert recursive query logic.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Mon May 27 11:26:23 EDT 2019


Revision: 12207
          http://svnweb.midnightbsd.org/src/?rev=12207
Author:   laffer1
Date:     2019-05-27 11:26:22 -0400 (Mon, 27 May 2019)
Log Message:
-----------
revert recursive query logic. does not play nice with libdispatch

Modified Paths:
--------------
    trunk/lib/libmport/bundle_read_install_pkg.c
    trunk/lib/libmport/db.c
    trunk/lib/libmport/error.c
    trunk/lib/libmport/index.c
    trunk/lib/libmport/index_depends.c
    trunk/lib/libmport/install.c
    trunk/lib/libmport/install_primative.c
    trunk/lib/libmport/mport.h

Modified: trunk/lib/libmport/bundle_read_install_pkg.c
===================================================================
--- trunk/lib/libmport/bundle_read_install_pkg.c	2019-05-27 14:30:48 UTC (rev 12206)
+++ trunk/lib/libmport/bundle_read_install_pkg.c	2019-05-27 15:26:22 UTC (rev 12207)
@@ -136,8 +136,7 @@
 	return MPORT_OK;
 
 	ERROR:
-	if (alist != NULL)
-		mport_assetlist_free(alist);
+	// TODO: asset list free
 	RETURN_CURRENT_ERROR;
 }
 
@@ -947,9 +946,6 @@
 		/* if we couldn't stat the file, we assume there isn't a pkg-msg */
 		return MPORT_OK;
 
-	if (st.st_size < 1)
-		return MPORT_OK;
-
 	if ((file = fopen(filename, "r")) == NULL)
 		RETURN_ERRORX(MPORT_ERR_FATAL, "Couldn't open %s: %s", filename, strerror(errno));
 

Modified: trunk/lib/libmport/db.c
===================================================================
--- trunk/lib/libmport/db.c	2019-05-27 14:30:48 UTC (rev 12206)
+++ trunk/lib/libmport/db.c	2019-05-27 15:26:22 UTC (rev 12207)
@@ -110,19 +110,14 @@
 		RETURN_ERROR(MPORT_ERR_FATAL, "Couldn't allocate memory for sql statement");
 
 	dispatch_sync(mportSQLSerial, ^{
-		int tries = 0;
 		int sqlcode = sqlite3_prepare_v2(db, sql, -1, stmt, NULL);
-
-		while(sqlcode == SQLITE_BUSY || sqlcode == SQLITE_LOCKED) {
-			usleep(200000); // 0.2 second
-			sqlcode = sqlite3_prepare_v2(db, sql, -1, stmt, NULL);
-			if (tries > 10) {
+		if (sqlcode == SQLITE_BUSY || sqlcode == SQLITE_LOCKED) {
+			sleep(1);
+			if (sqlite3_prepare_v2(db, sql, -1, stmt, NULL) != SQLITE_OK) {
 				err = (char *) sqlite3_errmsg(db);
 				result = MPORT_ERR_FATAL;
 			}
-		}
-
-		if (sqlcode != SQLITE_OK) {
+		} else if (sqlcode != SQLITE_OK) {
 			err = (char *) sqlite3_errmsg(db);
 			result = MPORT_ERR_FATAL;
 		}

Modified: trunk/lib/libmport/error.c
===================================================================
--- trunk/lib/libmport/error.c	2019-05-27 14:30:48 UTC (rev 12206)
+++ trunk/lib/libmport/error.c	2019-05-27 15:26:22 UTC (rev 12207)
@@ -37,7 +37,7 @@
 static char err_msg[256];
 
 /* This goes with the error codes in mport.h */
-static char default_error_msg[] = "An error occurred.";
+static char default_error_msg[] = "An error occured.";
 
 
 /* mport_err_code()
@@ -45,9 +45,8 @@
  * Return the current numeric error code. 
  */
 MPORT_PUBLIC_API int
-mport_err_code(void)
-{
-	return mport_err;
+mport_err_code(void) {
+    return mport_err;
 }
 
 /* mport_err_string()
@@ -55,9 +54,8 @@
  * Return the current error string (if any).  Do not free this memory, it is static. 
  */
 MPORT_PUBLIC_API const char *
-mport_err_string(void)
-{
-	return err_msg;
+mport_err_string(void) {
+    return err_msg;
 }
 
 
@@ -69,20 +67,19 @@
  * be used if msg is NULL
  */
 int
-mport_set_err(int code, const char *msg)
-{
-	mport_err = code;
+mport_set_err(int code, const char *msg) {
+    mport_err = code;
 
-	if (code == MPORT_OK) {
-		bzero(err_msg, sizeof(err_msg));
-	} else {
-		if (msg != NULL) {
-			strlcpy(err_msg, msg, sizeof(err_msg));
-		} else {
-			strlcpy(err_msg, default_error_msg, sizeof(err_msg));
-		}
-	}
-	return code;
+    if (code == MPORT_OK) {
+        bzero(err_msg, sizeof(err_msg));
+    } else {
+        if (msg != NULL) {
+            strlcpy(err_msg, msg, sizeof(err_msg));
+        } else {
+            strlcpy(err_msg, default_error_msg, sizeof(err_msg));
+        }
+    }
+    return code;
 }
 
 
@@ -94,19 +91,19 @@
  */
 int
 mport_set_errx(int code, const char *fmt, ...) {
-	va_list args;
-	char *err;
-	int ret;
+    va_list args;
+    char *err;
+    int ret;
 
-	va_start(args, fmt);
-	if (vasprintf(&err, fmt, args) == -1) {
-		fprintf(stderr, "fatal error: mport_set_errx can't format the string.\n");
-		exit(255);
-	}
-	ret = mport_set_err(code, err);
-	free(err);
+    va_start(args, fmt);
+    if (vasprintf(&err, fmt, args) == -1) {
+        fprintf(stderr, "fatal error: mport_set_errx can't format the string.\n");
+        exit(255);
+    }
+    ret = mport_set_err(code, err);
+    free(err);
 
-	va_end(args);
+    va_end(args);
 
-	return ret;
+    return ret;
 }

Modified: trunk/lib/libmport/index.c
===================================================================
--- trunk/lib/libmport/index.c	2019-05-27 14:30:48 UTC (rev 12206)
+++ trunk/lib/libmport/index.c	2019-05-27 15:26:22 UTC (rev 12207)
@@ -35,7 +35,6 @@
 #include <time.h>
 #include <string.h>
 #include <stdlib.h>
-#include <stdint.h>
 #include <errno.h>
 #include <stddef.h>
 

Modified: trunk/lib/libmport/index_depends.c
===================================================================
--- trunk/lib/libmport/index_depends.c	2019-05-27 14:30:48 UTC (rev 12206)
+++ trunk/lib/libmport/index_depends.c	2019-05-27 15:26:22 UTC (rev 12207)
@@ -55,20 +55,38 @@
   
 	MPORT_CHECK_FOR_INDEX(mport, "mport_index_depends_list()")
 
-	if (mport_db_count(mport->db, &count,
-		"with RECURSIVE under_dep (parent, parent_ver, pkg, version, level) as ( values('?', '?',  %Q, %Q, 0) union all select depends.pkg as parent, depends.version as parent_ver, depends.d_pkg as pkg, depends.d_version as version, under_dep.level + 1 from depends JOIN under_dep on depends.pkg = under_dep.pkg) select count(*) FROM under_dep where level > 0;",
-			   pkgname, version) != MPORT_OK)
+	if (mport_db_prepare(mport->db, &stmt,
+	    "SELECT COUNT(*) FROM idx.depends WHERE pkg = %Q and version = %Q",
+	    pkgname, version) != MPORT_OK) {
+		sqlite3_finalize(stmt);
 		RETURN_CURRENT_ERROR;
-
-	if (count == 0)
-		return MPORT_OK;
+	}
+ 
+	switch (sqlite3_step(stmt)) {
+	case SQLITE_ROW:
+		count = sqlite3_column_int(stmt, 0);
+		break;
+	case SQLITE_DONE:
+		ret = SET_ERROR(MPORT_ERR_FATAL,
+		    "No rows returned from a 'SELECT COUNT(*)' query.");
+		goto DONE;
+		break;
+	default:
+		ret = SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(mport->db));
+		goto DONE;
+		break;
+	}
   
+	sqlite3_finalize(stmt);
+  
 	e = (mportDependsEntry **)calloc(count + 1, sizeof(mportDependsEntry *));
 	*entry_vec = e;
   
+	if (count == 0) 
+		return MPORT_OK;
+  
 	if (mport_db_prepare(mport->db, &stmt,
-		 "with RECURSIVE under_dep (parent, parent_ver, pkg, version, level) as ( values('?', '?',  %Q, %Q, 0) union all select depends.pkg as parent, depends.version as parent_ver, depends.d_pkg as pkg, depends.d_version as version, under_dep.level + 1 from depends JOIN under_dep on depends.pkg = under_dep.pkg) select parent, parent_ver, pkg, version, level FROM under_dep where level > 0;"
-		, pkgname, version) != MPORT_OK) {
+	    "SELECT pkg, version, d_pkg, d_version FROM idx.depends WHERE pkg= %Q and version=%Q", pkgname, version) != MPORT_OK) {
 		ret = mport_err_code();
 		goto DONE;
 	}
@@ -81,16 +99,11 @@
 				ret = MPORT_ERR_FATAL;
 				goto DONE;
 			}
-
-			/* parent, parent_ver, pkg, version, level */
+      
 			e[i]->pkgname    = strdup(sqlite3_column_text(stmt, 0));
 			e[i]->version    = strdup(sqlite3_column_text(stmt, 1));
 			e[i]->d_pkgname  = strdup(sqlite3_column_text(stmt, 2));
 			e[i]->d_version  = strdup(sqlite3_column_text(stmt, 3));
-			e[i]->level = sqlite3_column_int(stmt, 4);
-			/* default these to false */
-			e[i]->already_installed = false;
-			e[i]->needs_upgrade = false;
       
 			if (e[i]->pkgname == NULL ||
 			    e[i]->version == NULL ||

Modified: trunk/lib/libmport/install.c
===================================================================
--- trunk/lib/libmport/install.c	2019-05-27 14:30:48 UTC (rev 12206)
+++ trunk/lib/libmport/install.c	2019-05-27 15:26:22 UTC (rev 12207)
@@ -36,82 +36,82 @@
 MPORT_PUBLIC_API int
 mport_install(mportInstance *mport, const char *pkgname, const char *version, const char *prefix)
 {
-	mportIndexEntry **e;
-	char *filename;
-	int ret = MPORT_OK;
-	int e_loc = 0;
+  mportIndexEntry **e;
+  char *filename;
+  int ret = MPORT_OK;
+  int e_loc = 0;
 
-	MPORT_CHECK_FOR_INDEX(mport, "mport_install()");
+  MPORT_CHECK_FOR_INDEX(mport, "mport_install()");
+  
+  if (mport_index_lookup_pkgname(mport, pkgname, &e) != MPORT_OK)
+    RETURN_CURRENT_ERROR;
 
-	if (mport_index_lookup_pkgname(mport, pkgname, &e) != MPORT_OK)
-		RETURN_CURRENT_ERROR;
+  /* we don't support installing more than one top-level package at a time.
+   * Consider a situation like this:
+   *
+   * mport_install(mport, "p5-Class-DBI*");
+   *
+   * Say this matches p5-Class-DBI and p5-Class-DBI-AbstractSearch
+   * and say the order from the index puts p5-Class-DBI-AbstractSearch 
+   * first.
+   * 
+   * p5-Class-DBI-AbstractSearch is installed, and its depends installed.  
+   * However, p5-Class-DBI is a depend of p5-Class-DBI-AbstractSearch, so 
+   * when it comes time to install p5-Class-DBI, we can't - because it is
+   * already installed.
+   *
+   * If a user facing application wants this functionality, it would be
+   * easy to piece together with mport_index_lookup_pkgname(), a
+   * check for already installed packages, and mport_install().
+   */
 
-	/* we don't support installing more than one top-level package at a time.
-	 * Consider a situation like this:
-	 *
-	 * mport_install(mport, "p5-Class-DBI*");
-	 *
-	 * Say this matches p5-Class-DBI and p5-Class-DBI-AbstractSearch
-	 * and say the order from the index puts p5-Class-DBI-AbstractSearch
-	 * first.
-	 *
-	 * p5-Class-DBI-AbstractSearch is installed, and its depends installed.
-	 * However, p5-Class-DBI is a depend of p5-Class-DBI-AbstractSearch, so
-	 * when it comes time to install p5-Class-DBI, we can't - because it is
-	 * already installed.
-	 *
-	 * If a user facing application wants this functionality, it would be
-	 * easy to piece together with mport_index_lookup_pkgname(), a
-	 * check for already installed packages, and mport_install().
-	 */
+  if (e[1] != NULL) {
+    if (version != NULL) {
+        while (e[e_loc] != NULL) {
+          if (strcmp(e[e_loc]->version, version) == 0) {
+            break;
+          }
+          e_loc++;
+        }
+        if (e[e_loc] == NULL) {
+          mport_index_entry_free_vec(e);
+          RETURN_ERRORX(MPORT_ERR_FATAL, "Cound not resolve '%s-%s'.",
+            pkgname, version);
+        }
+    } else {
+      mport_index_entry_free_vec(e);
+      RETURN_ERRORX(MPORT_ERR_FATAL, "Could not resolve '%s' to a single package.", pkgname);
+    }
+  }
+ 
+  asprintf(&filename, "%s/%s", MPORT_FETCH_STAGING_DIR, e[e_loc]->bundlefile);
+  if (filename == NULL) {
+    mport_index_entry_free_vec(e);
+    RETURN_ERROR(MPORT_ERR_FATAL, "Out of memory.");
+  }
 
-	if (e[1] != NULL) {
-		if (version != NULL) {
-			while (e[e_loc] != NULL) {
-				if (strcmp(e[e_loc]->version, version) == 0) {
-					break;
-				}
-				e_loc++;
-			}
-			if (e[e_loc] == NULL) {
-				mport_index_entry_free_vec(e);
-				RETURN_ERRORX(MPORT_ERR_FATAL, "Could not resolve '%s-%s'.",
-					      pkgname, version);
-			}
-		} else {
-			mport_index_entry_free_vec(e);
-			RETURN_ERRORX(MPORT_ERR_FATAL, "Could not resolve '%s' to a single package.", pkgname);
-		}
-	}
+  if (!mport_file_exists(filename)) {
+    if (mport_fetch_bundle(mport, e[e_loc]->bundlefile) != MPORT_OK) {
+      free(filename);
+      mport_index_entry_free_vec(e);
+      RETURN_CURRENT_ERROR;
+    }
+  }
 
-	asprintf(&filename, "%s/%s", MPORT_FETCH_STAGING_DIR, e[e_loc]->bundlefile);
-	if (filename == NULL) {
-		mport_index_entry_free_vec(e);
-		RETURN_ERROR(MPORT_ERR_FATAL, "Out of memory.");
-	}
+  if (mport_verify_hash(filename, e[e_loc]->hash) == 0) {
+    free(filename);
+    mport_index_entry_free_vec(e);
 
-	if (!mport_file_exists(filename)) {
-		if (mport_fetch_bundle(mport, e[e_loc]->bundlefile) != MPORT_OK) {
-			free(filename);
-			mport_index_entry_free_vec(e);
-			RETURN_CURRENT_ERROR;
-		}
-	}
+    if (unlink(filename) == 0)
+      RETURN_ERROR(MPORT_ERR_FATAL, "Package failed hash verification and was removed.\n");
+    else
+      RETURN_ERROR(MPORT_ERR_FATAL, "Package failed hash verification, but could not be removed.\n");
+  }
+ 
+  ret = mport_install_primative(mport, filename, prefix);
 
-	if (mport_verify_hash(filename, e[e_loc]->hash) == 0) {
-		free(filename);
-		mport_index_entry_free_vec(e);
-
-		if (unlink(filename) == 0)
-			RETURN_ERROR(MPORT_ERR_FATAL, "Package failed hash verification and was removed.\n");
-		else
-			RETURN_ERROR(MPORT_ERR_FATAL, "Package failed hash verification, but could not be removed.\n");
-	}
-
-	ret = mport_install_primative(mport, filename, prefix);
-
-	free(filename);
-	mport_index_entry_free_vec(e);
-
-	return ret;
+  free(filename);
+  mport_index_entry_free_vec(e);
+  
+  return ret;
 }

Modified: trunk/lib/libmport/install_primative.c
===================================================================
--- trunk/lib/libmport/install_primative.c	2019-05-27 14:30:48 UTC (rev 12206)
+++ trunk/lib/libmport/install_primative.c	2019-05-27 15:26:22 UTC (rev 12207)
@@ -59,10 +59,8 @@
 		if (prefix != NULL) {
 			/* override the default prefix with the given prefix */
 			free(pkg->prefix);
-			if ((pkg->prefix = strdup(prefix)) == NULL) {
-				/* all hope is lost! bail */
+			if ((pkg->prefix = strdup(prefix)) == NULL) /* all hope is lost! bail */
 				RETURN_ERROR(MPORT_ERR_FATAL, "Out of memory.");
-			}
 		}
 
 		if ((mport_check_preconditions(mport, pkg, MPORT_PRECHECK_INSTALLED | MPORT_PRECHECK_DEPENDS |
@@ -69,12 +67,9 @@
 		                                           MPORT_PRECHECK_CONFLICTS) != MPORT_OK)
 		    ||
 		    (mport_bundle_read_install_pkg(mport, bundle, pkg) != MPORT_OK)) {
-			if (pkg->name != NULL && pkg->version != NULL)
-				mport_call_msg_cb(mport, "Unable to install %s-%s: %s", pkg->name, pkg->version,
-						  mport_err_string());
-			else
-				mport_call_msg_cb(mport, "Unknown error. State is invalid.");
-
+			mport_call_msg_cb(mport, "Unable to install %s-%s: %s", pkg->name, pkg->version,
+			                  mport_err_string());
+			/* TODO: WHY WAS THIS HERE mport_set_err(MPORT_OK, NULL); */
 			error = true;
 			break; /* do not keep going if we have a package failure! */
 		}

Modified: trunk/lib/libmport/mport.h
===================================================================
--- trunk/lib/libmport/mport.h	2019-05-27 14:30:48 UTC (rev 12206)
+++ trunk/lib/libmport/mport.h	2019-05-27 15:26:22 UTC (rev 12207)
@@ -36,7 +36,6 @@
 #include <sqlite3.h>
 #include <sys/queue.h>
 #include <stdio.h>
-#include <stdbool.h>
 
 #include "mport_dispatch.h"
 
@@ -166,9 +165,6 @@
   char *version;
   char *d_pkgname;
   char *d_version;
-  int level;
-  bool already_installed;
-  bool needs_upgrade;
 } mportDependsEntry;
 
 int mport_index_depends_list(mportInstance *, const char *, const char *, mportDependsEntry ***);



More information about the Midnightbsd-cvs mailing list