[Midnightbsd-cvs] src [7679] trunk/lib/libmport/bundle_read_install_pkg.c: convert pre and post to use shared code.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun Aug 7 02:53:47 EDT 2016


Revision: 7679
          http://svnweb.midnightbsd.org/src/?rev=7679
Author:   laffer1
Date:     2016-08-07 02:53:47 -0400 (Sun, 07 Aug 2016)
Log Message:
-----------
convert pre and post to use shared code. This allows us to use the serial queue for sql access and reuse code

Modified Paths:
--------------
    trunk/lib/libmport/bundle_read_install_pkg.c

Modified: trunk/lib/libmport/bundle_read_install_pkg.c
===================================================================
--- trunk/lib/libmport/bundle_read_install_pkg.c	2016-08-07 06:17:10 UTC (rev 7678)
+++ trunk/lib/libmport/bundle_read_install_pkg.c	2016-08-07 06:53:47 UTC (rev 7679)
@@ -55,8 +55,10 @@
 static int create_depends(mportInstance *mport, mportPackageMeta *pkg);
 static int create_sample_file(const char *file);
 static int mark_complete(mportInstance *, mportPackageMeta *);
-static int mport_bundle_read_get_assetlist(mportInstance *mport, mportPackageMeta *pkg, mportAssetList **alist_p);
+static int mport_bundle_read_get_assetlist(mportInstance *mport, mportPackageMeta *pkg, mportAssetList **alist_p, enum phase);
 
+enum phase { PREINSTALL, ACTUALINSTALL, POSTINSTALL};
+
 /**
  * This is a wrapper for all bund read install operations
  */
@@ -87,71 +89,54 @@
 static int
 do_pre_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMeta *pkg)
 {
-    int ret;
-    char cwd[FILENAME_MAX];
-    char file[FILENAME_MAX];
-    sqlite3_stmt *assets = NULL;
-    sqlite3 *db = mport->db;
-    mportAssetListEntryType type;
-    const char *data;
+	char cwd[FILENAME_MAX];
+	char file[FILENAME_MAX];
+	mportAssetList *alist;
+	mportAssetListEntry *e = NULL;
 
-    /* run mtree */
-    if (run_mtree(mport, bundle, pkg) != MPORT_OK)
-        RETURN_CURRENT_ERROR;
+	/* run mtree */
+	if (run_mtree(mport, bundle, pkg) != MPORT_OK)
+		RETURN_CURRENT_ERROR;
 
-    /* run pkg-install PRE-INSTALL */
-    if (run_pkg_install(mport, bundle, pkg, "PRE-INSTALL") != MPORT_OK)
-        RETURN_CURRENT_ERROR;
+	/* run pkg-install PRE-INSTALL */
+	if (run_pkg_install(mport, bundle, pkg, "PRE-INSTALL") != MPORT_OK)
+		RETURN_CURRENT_ERROR;
 
-    /* Process @preexec steps */
-    if (mport_db_prepare(db, &assets, "SELECT type, data FROM stub.assets WHERE pkg=%Q and type in (%d, %d)", pkg->name, ASSET_CWD, ASSET_PREEXEC) != MPORT_OK) {
-		sqlite3_finalize(assets);
+	/* Process @preexec steps */
+	if (mport_bundle_read_get_assetlist(mport, pkg, &alist, PREINSTALL) != MPORT_OK)
 		goto ERROR;
-	}
 
-    (void) strlcpy(cwd, pkg->prefix, sizeof(cwd));
+	(void) strlcpy(cwd, pkg->prefix, sizeof(cwd));
 
-    if (mport_chdir(mport, cwd) != MPORT_OK)
-        goto ERROR;
+	if (mport_chdir(mport, cwd) != MPORT_OK)
+		goto ERROR;
 
-    while (1) {
-        ret = sqlite3_step(assets);
+	STAILQ_FOREACH(e, alist, next) {
+		switch (e->type) {
+			case ASSET_CWD:
+				(void) strlcpy(cwd, e->data == NULL ? pkg->prefix : e->data, sizeof(cwd));
+				if (mport_chdir(mport, cwd) != MPORT_OK)
+					goto ERROR;
 
-        if (ret == SQLITE_DONE)
-            break;
+				break;
+			case ASSET_PREEXEC:
+				if (mport_run_asset_exec(mport, e->data, cwd, file) != MPORT_OK)
+					goto ERROR;
+				break;
+			default:
+				/* do nothing */
+				break;
+		}
+	}
 
-        if (ret != SQLITE_ROW) {
-            SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
-            goto ERROR;
-        }
+	mport_assetlist_free(alist);
+	mport_pkgmeta_logevent(mport, pkg, "preexec");
 
-        type = (mportAssetListEntryType) sqlite3_column_int(assets, 0);
-        data = (const char *) sqlite3_column_text(assets, 1);
+	return MPORT_OK;
 
-        switch (type) {
-            case ASSET_CWD:
-                (void) strlcpy(cwd, data == NULL ? pkg->prefix : data, sizeof(cwd));
-                if (mport_chdir(mport, cwd) != MPORT_OK)
-                    goto ERROR;
-
-                break;
-            case ASSET_PREEXEC:
-                if (mport_run_asset_exec(mport, data, cwd, file) != MPORT_OK)
-                    goto ERROR;
-                break;
-            default:
-                /* do nothing */
-                break;
-        }
-    }
-    sqlite3_finalize(assets);
-    mport_pkgmeta_logevent(mport, pkg, "preexec");
-
-    return MPORT_OK;
-
-    ERROR:
-        sqlite3_finalize(assets);
-        RETURN_CURRENT_ERROR;
+	ERROR:
+		// TODO: asset list free
+	RETURN_CURRENT_ERROR;
 }
 
 /* get the file count for the progress meter */
@@ -158,7 +143,9 @@
 static int
 get_file_count(mportInstance *mport, char *pkg_name, int *file_total)
 {
-	sqlite3_stmt *count;
+	__block sqlite3_stmt *count;
+	__block int result = MPORT_OK;
+	__block char *err;
 
 	if (mport_db_prepare(mport->db, &count,
 						 "SELECT COUNT(*) FROM stub.assets WHERE (type=%i or type=%i or type=%i or type=%i) AND pkg=%Q",
@@ -167,18 +154,22 @@
 		RETURN_CURRENT_ERROR;
 	}
 
-	switch (sqlite3_step(count)) {
-		case SQLITE_ROW:
-			*file_total = sqlite3_column_int(count, 0);
-			sqlite3_finalize(count);
-			break;
-		default:
-			SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(mport->db));
-			sqlite3_finalize(count);
-			RETURN_CURRENT_ERROR;
-	}
+	dispatch_sync(mportSQLSerial, ^{
+		switch (sqlite3_step(count)) {
+			case SQLITE_ROW:
+				*file_total = sqlite3_column_int(count, 0);
+				sqlite3_finalize(count);
+				break;
+			default:
+				err = (char *) sqlite3_errmsg(mport->db);
+				result = MPORT_ERR_FATAL;
+				sqlite3_finalize(count);
+		}
+	});
 
-	return MPORT_OK;
+	if (result == MPORT_ERR_FATAL)
+		SET_ERRORX(result, "Error reading file count %s", err);
+	return result;
 }
 
 static int
@@ -240,10 +231,10 @@
  * filtered on entries that are not pre/post exec.
  */
 static int
-mport_bundle_read_get_assetlist(mportInstance *mport, mportPackageMeta *pkg, mportAssetList **alist_p)
+mport_bundle_read_get_assetlist(mportInstance *mport, mportPackageMeta *pkg, mportAssetList **alist_p, enum phase state)
 {
 	__block mportAssetList *alist;
-	__block sqlite3_stmt *stmt;
+	__block sqlite3_stmt *stmt = NULL;
 	__block int result = MPORT_OK;
 	__block char *err;
 
@@ -252,9 +243,23 @@
 
 	*alist_p = alist;
 
-	if (mport_db_prepare(mport->db, &stmt, "SELECT type,data,checksum,owner,grp,mode FROM stub.assets WHERE pkg=%Q and type not in (%d, %d)", pkg->name, ASSET_PREEXEC, ASSET_POSTEXEC) != MPORT_OK) {
-		sqlite3_finalize(stmt);
-		RETURN_CURRENT_ERROR;
+	if (state == PREINSTALL) {
+		if (mport_db_prepare(mport->db, &stmt, "SELECT type,data,checksum,owner,grp,mode FROM stub.assets WHERE pkg=%Q and type in (%d, %d)", pkg->name, ASSET_CWD, ASSET_PREEXEC) != MPORT_OK) {
+			sqlite3_finalize(stmt);
+			RETURN_CURRENT_ERROR;
+		}
+	} else if (state == ACTUALINSTALL) {
+		if (mport_db_prepare(mport->db, &stmt,
+							 "SELECT type,data,checksum,owner,grp,mode FROM stub.assets WHERE pkg=%Q and type not in (%d, %d)",
+							 pkg->name, ASSET_PREEXEC, ASSET_POSTEXEC) != MPORT_OK) {
+			sqlite3_finalize(stmt);
+			RETURN_CURRENT_ERROR;
+		}
+	} else if (state == POSTINSTALL) {
+		if (mport_db_prepare(mport->db, &stmt, "SELECT type,data,checksum,owner,grp,mode FROM stub.assets WHERE pkg=%Q and type in (%d, %d)", pkg->name, ASSET_CWD, ASSET_POSTEXEC) != MPORT_OK) {
+			sqlite3_finalize(stmt);
+			RETURN_CURRENT_ERROR;
+		}
 	}
 
 	if (stmt == NULL) {
@@ -371,7 +376,7 @@
         MPORT_OK)
         goto ERROR;
 
-	if (mport_bundle_read_get_assetlist(mport, pkg, &alist) != MPORT_OK)
+	if (mport_bundle_read_get_assetlist(mport, pkg, &alist, ACTUALINSTALL) != MPORT_OK)
 		goto ERROR;
 
     (void) strlcpy(cwd, pkg->prefix, sizeof(cwd));
@@ -713,23 +718,17 @@
 	return mark_complete(mport, pkg);
 }
 
+
 static int
 run_postexec(mportInstance *mport, mportPackageMeta *pkg)
 {
-    int ret;
-    char cwd[FILENAME_MAX];
-    sqlite3_stmt *assets = NULL;
-    sqlite3 *db;
-    mportAssetListEntryType type;
-    const char *data;
+	mportAssetList *alist;
+	mportAssetListEntry *e = NULL;
+	char cwd[FILENAME_MAX];
 
-    db = mport->db;
-
     /* Process @postexec steps */
-    if (mport_db_prepare(db, &assets, "SELECT type, data FROM stub.assets WHERE pkg=%Q and type in (%d, %d)", pkg->name, ASSET_CWD, ASSET_POSTEXEC) != MPORT_OK) {
-		sqlite3_finalize(assets);
+	if (mport_bundle_read_get_assetlist(mport, pkg, &alist, POSTINSTALL) != MPORT_OK)
 		goto ERROR;
-	}
 
     (void) strlcpy(cwd, pkg->prefix, sizeof(cwd));
 
@@ -736,37 +735,24 @@
     if (mport_chdir(mport, cwd) != MPORT_OK)
         goto ERROR;
 
-    while (1) {
-        ret = sqlite3_step(assets);
-
-        if (ret == SQLITE_DONE)
-            break;
-
-        if (ret != SQLITE_ROW) {
-            SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
-            goto ERROR;
-        }
-
-        type = (mportAssetListEntryType) sqlite3_column_int(assets, 0);
-        data = (char *) sqlite3_column_text(assets, 1);
-
+	STAILQ_FOREACH(e, alist, next) {
         char file[FILENAME_MAX];
-        if (data == NULL) {
+        if (e->data == NULL) {
             snprintf(file, sizeof(file), "%s", mport->root);
-        } else if (*data == '/') {
-            snprintf(file, sizeof(file), "%s%s", mport->root, data);
+        } else if (e->data[0] == '/') {
+            snprintf(file, sizeof(file), "%s%s", mport->root, e->data);
         } else {
-            snprintf(file, sizeof(file), "%s%s/%s", mport->root, pkg->prefix, data);
+            snprintf(file, sizeof(file), "%s%s/%s", mport->root, pkg->prefix, e->data);
         }
 
-        switch (type) {
+        switch (e->type) {
             case ASSET_CWD:
-                (void) strlcpy(cwd, data == NULL ? pkg->prefix : data, sizeof(cwd));
+                (void) strlcpy(cwd, e->data == NULL ? pkg->prefix : e->data, sizeof(cwd));
                 if (mport_chdir(mport, cwd) != MPORT_OK)
                     goto ERROR;
                 break;
             case ASSET_POSTEXEC:
-                if (mport_run_asset_exec(mport, data, cwd, file) != MPORT_OK)
+                if (mport_run_asset_exec(mport, e->data, cwd, file) != MPORT_OK)
                     goto ERROR;
                 break;
             default:
@@ -774,13 +760,14 @@
                 break;
         }
     }
-    sqlite3_finalize(assets);
+
+	mport_assetlist_free(alist);
     mport_pkgmeta_logevent(mport, pkg, "postexec");
 
     return MPORT_OK;
 
     ERROR:
-    sqlite3_finalize(assets);
+	// TODO: asset list free
     RETURN_CURRENT_ERROR;
 }
 



More information about the Midnightbsd-cvs mailing list