[Midnightbsd-cvs] src [9828] trunk/lib/libmport: Migrate the asset list routine to it's own file and include extra fields so it can be used elsewhere.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Fri May 11 15:21:53 EDT 2018


Revision: 9828
          http://svnweb.midnightbsd.org/src/?rev=9828
Author:   laffer1
Date:     2018-05-11 15:21:52 -0400 (Fri, 11 May 2018)
Log Message:
-----------
Migrate the asset list routine to it's own file and include extra fields so it can be used elsewhere.

Introduce a new function to get a package metadata from a file path.  This allows us to find out what installed a file.

Modified Paths:
--------------
    trunk/lib/libmport/bundle_read_update_pkg.c
    trunk/lib/libmport/mport.h
    trunk/lib/libmport/mport_private.h
    trunk/lib/libmport/pkgmeta.c
    trunk/lib/libmport/verify.c

Added Paths:
-----------
    trunk/lib/libmport/asset.c

Added: trunk/lib/libmport/asset.c
===================================================================
--- trunk/lib/libmport/asset.c	                        (rev 0)
+++ trunk/lib/libmport/asset.c	2018-05-11 19:21:52 UTC (rev 9828)
@@ -0,0 +1,185 @@
+/*-
+ * Copyright (c) 2018 Lucas Holt
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in the
+ *  documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+
+#include <sys/cdefs.h>
+__MBSDID("$MidnightBSD$");
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <string.h>
+#include <sqlite3.h>
+#include <stdlib.h>
+#include "mport.h"
+#include "mport_private.h"
+
+MPORT_PUBLIC_API int
+mport_asset_get_package_from_file_path(mportInstance *mport, char *filePath, __block mportPackageMeta *pack)
+{
+	__block sqlite3_stmt *stmt = NULL;
+	__block int result = MPORT_OK;
+	__block char *err;
+
+	if (mport_db_prepare(mport->db, &stmt, "SELECT pkg FROM assets WHERE data=%Q", filePath) != MPORT_OK) {
+		sqlite3_finalize(stmt);
+		RETURN_CURRENT_ERROR;
+	}
+
+	if (stmt == NULL) {
+		RETURN_ERROR(MPORT_ERR_FATAL, "Statement was null");
+	}
+
+	dispatch_sync(mportSQLSerial, ^{
+	    while (1) {
+		    mportAssetListEntry *e;
+
+		    int ret = sqlite3_step(stmt);
+
+		    if (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) {
+			    sleep(1);
+			    ret = sqlite3_step(stmt);
+		    }
+
+		    if (ret == SQLITE_DONE)
+			    break;
+
+		    if (ret != SQLITE_ROW) {
+			    err = (char *) sqlite3_errmsg(mport->db);
+			    result = MPORT_ERR_FATAL;
+			    break; // we finalize below
+		    }
+
+		    e = (mportAssetListEntry *) calloc(1, sizeof(mportAssetListEntry));
+
+		    if (e == NULL) {
+			    err = "Out of memory";
+			    result = MPORT_ERR_FATAL;
+			    break; // we finalize below
+		    }
+
+		    const unsigned char *pkgName = sqlite3_column_text(stmt, 0);
+
+		    if (pkgName != NULL) {
+			    mportPackageMeta **packs;
+			    if (mport_pkgmeta_search_master(mport, &packs, "pkg=%Q", pkgName) != MPORT_OK) {
+				    err = "Package does not exist despite having assets";
+				    result = MPORT_ERR_FATAL;
+				    break; // we finalize below
+			    } else {
+				    pack = packs[0];
+				    result = MPORT_OK;
+			    }
+		    }
+	    }
+
+	    sqlite3_finalize(stmt);
+	});
+
+	if (result == MPORT_ERR_FATAL)
+		SET_ERRORX(result, "Error reading assets %s", err);
+	return result;
+}
+
+MPORT_PUBLIC_API int
+mport_asset_get_assetlist(mportInstance *mport, mportPackageMeta *pack, mportAssetList **alist_p)
+{
+	__block mportAssetList *alist;
+	__block sqlite3_stmt *stmt = NULL;
+	__block int result = MPORT_OK;
+	__block char *err;
+
+	if ((alist = mport_assetlist_new()) == NULL)
+		RETURN_ERROR(MPORT_ERR_FATAL, "Out of memory.");
+
+	*alist_p = alist;
+
+	// pkg text NOT NULL, type int NOT NULL, data text, checksum text, owner text, grp text, mode text)
+
+	if (mport_db_prepare(mport->db, &stmt, "SELECT type,data,checksum,owner,grp,mode FROM assets WHERE pkg=%Q", pack->name) != MPORT_OK) {
+		sqlite3_finalize(stmt);
+		RETURN_CURRENT_ERROR;
+	}
+
+	if (stmt == NULL) {
+		RETURN_ERROR(MPORT_ERR_FATAL, "Statement was null");
+	}
+
+	dispatch_sync(mportSQLSerial, ^{
+	    while (1) {
+		    mportAssetListEntry *e;
+
+		    int ret = sqlite3_step(stmt);
+
+		    if (ret == SQLITE_BUSY || ret == SQLITE_LOCKED) {
+			    sleep(1);
+			    ret = sqlite3_step(stmt);
+		    }
+
+		    if (ret == SQLITE_DONE)
+			    break;
+
+		    if (ret != SQLITE_ROW) {
+			    err = (char *) sqlite3_errmsg(mport->db);
+			    result = MPORT_ERR_FATAL;
+			    break; // we finalize below
+		    }
+
+		    e = (mportAssetListEntry *) calloc(1, sizeof(mportAssetListEntry));
+
+		    if (e == NULL) {
+			    err = "Out of memory";
+			    result = MPORT_ERR_FATAL;
+			    break; // we finalize below
+		    }
+
+		    e->type = (mportAssetListEntryType) sqlite3_column_int(stmt, 0);
+		    const unsigned char *data = sqlite3_column_text(stmt, 1);
+		    const unsigned char *checksum = sqlite3_column_text(stmt, 2);
+		    const unsigned char *owner = sqlite3_column_text(stmt, 3);
+		    const unsigned char *group = sqlite3_column_text(stmt, 4);
+		    const unsigned char *mode = sqlite3_column_text(stmt, 5);
+
+		    e->data = data == NULL ? NULL : strdup((char *) data);
+		    if (checksum != NULL)
+			    e->checksum = strdup((char *) checksum);
+		    if (owner != NULL)
+			    e->owner = strdup((char *) owner);
+		    if (group != NULL)
+			    e->group = strdup((char *) group);
+		    if (mode != NULL)
+			    e->mode = strdup((char *) mode);
+
+		    STAILQ_INSERT_TAIL(alist, e, next);
+	    }
+
+	    sqlite3_finalize(stmt);
+	});
+
+	if (result == MPORT_ERR_FATAL)
+		SET_ERRORX(result, "Error reading assets %s", err);
+	return result;
+}
\ No newline at end of file


Property changes on: trunk/lib/libmport/asset.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Modified: trunk/lib/libmport/bundle_read_update_pkg.c
===================================================================
--- trunk/lib/libmport/bundle_read_update_pkg.c	2018-04-30 12:43:04 UTC (rev 9827)
+++ trunk/lib/libmport/bundle_read_update_pkg.c	2018-05-11 19:21:52 UTC (rev 9828)
@@ -80,7 +80,7 @@
   mportCreateExtras *extra;
   int ret;
  
-  if (mport_pkgmeta_get_assetlist(mport, pkg, &alist) != MPORT_OK)
+  if (mport_asset_get_assetlist(mport, pkg, &alist) != MPORT_OK)
     RETURN_CURRENT_ERROR;
 
   if (build_create_extras(mport, pkg, tempfile, &extra) != MPORT_OK) 

Modified: trunk/lib/libmport/mport.h
===================================================================
--- trunk/lib/libmport/mport.h	2018-04-30 12:43:04 UTC (rev 9827)
+++ trunk/lib/libmport/mport.h	2018-05-11 19:21:52 UTC (rev 9828)
@@ -123,8 +123,10 @@
   char *os_release;
   char *cpe;
   int locked;
-} mportPackageMeta;  
+} mportPackageMeta;
 
+int mport_asset_get_assetlist(mportInstance *, mportPackageMeta *, mportAssetList **);
+int mport_asset_get_package_from_file_path(mportInstance *, char *, __block mportPackageMeta *);
 
 mportPackageMeta * mport_pkgmeta_new(void);
 void mport_pkgmeta_free(mportPackageMeta *);

Modified: trunk/lib/libmport/mport_private.h
===================================================================
--- trunk/lib/libmport/mport_private.h	2018-04-30 12:43:04 UTC (rev 9827)
+++ trunk/lib/libmport/mport_private.h	2018-05-11 19:21:52 UTC (rev 9828)
@@ -77,7 +77,6 @@
 
 /* pkgmeta */
 int mport_pkgmeta_read_stub(mportInstance *, mportPackageMeta ***);
-int mport_pkgmeta_get_assetlist(mportInstance *, mportPackageMeta *, mportAssetList **);
 int mport_pkgmeta_logevent(mportInstance *, mportPackageMeta *, const char *);
 
 /* Utils */

Modified: trunk/lib/libmport/pkgmeta.c
===================================================================
--- trunk/lib/libmport/pkgmeta.c	2018-04-30 12:43:04 UTC (rev 9827)
+++ trunk/lib/libmport/pkgmeta.c	2018-05-11 19:21:52 UTC (rev 9828)
@@ -346,83 +346,6 @@
  
   sqlite3_finalize(stmt);
   return ret; 
-}  
-
-
-int
-mport_pkgmeta_get_assetlist(mportInstance *mport, mportPackageMeta *pkg, mportAssetList **alist_p)
-{
-  mportAssetList *alist;
-  sqlite3_stmt *stmt;
-  int ret;
-  mportAssetListEntry *e;
-  
-  if ((alist = mport_assetlist_new()) == NULL)
-    RETURN_ERROR(MPORT_ERR_FATAL, "Out of memory.");
-
-  *alist_p = alist;
-  
-  if (mport_db_prepare(mport->db, &stmt, "SELECT type, data, owner, grp, mode FROM assets WHERE pkg=%Q", pkg->name) != MPORT_OK) {
-    sqlite3_finalize(stmt);
-    RETURN_CURRENT_ERROR;
-  }
-
-  if (stmt == NULL)
-	RETURN_CURRENT_ERROR;
-    
-  while (1) {
-    ret = sqlite3_step(stmt);
-    
-    if (ret == SQLITE_DONE)
-      break;
-      
-    if (ret != SQLITE_ROW) {
-      sqlite3_finalize(stmt);
-      RETURN_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(mport->db));
-    }
-    
-    e = (mportAssetListEntry *)calloc(1, sizeof(mportAssetListEntry));
-    
-    if (e == NULL) {
-      sqlite3_finalize(stmt);
-      RETURN_ERROR(MPORT_ERR_FATAL, "Out of memory.");
-    }
-
-    const unsigned char *data;
-    const unsigned char *owner;
-    const unsigned char *group;
-    const unsigned char *mode;
-    
-    e->type = sqlite3_column_int(stmt, 0);
-    data = sqlite3_column_text(stmt, 1);
-    owner = sqlite3_column_text(stmt, 2);
-    group = sqlite3_column_text(stmt, 3);
-    mode = sqlite3_column_text(stmt, 4);
-     
-    if (data == NULL) {
-      sqlite3_finalize(stmt);
-      RETURN_ERROR(MPORT_ERR_FATAL, "Out of memory.");
-    }
-
-    e->data = strdup(data);
-    if (owner != NULL)
-        e->owner = strdup(owner);
-    if (group != NULL)
-        e->group = strdup(group);
-    if (mode != NULL)
-        e->mode = strdup(mode);
-
-    if (e->data == NULL) {
-      sqlite3_finalize(stmt);
-      RETURN_ERROR(MPORT_ERR_FATAL, "Out of memory.");
-    }
-    
-    STAILQ_INSERT_TAIL(alist, e, next);
-  }
-  
-  sqlite3_finalize(stmt);
-
-  return MPORT_OK;
 }
 
 

Modified: trunk/lib/libmport/verify.c
===================================================================
--- trunk/lib/libmport/verify.c	2018-04-30 12:43:04 UTC (rev 9827)
+++ trunk/lib/libmport/verify.c	2018-05-11 19:21:52 UTC (rev 9828)
@@ -53,64 +53,67 @@
 		sqlite3_finalize(stmt);
 		RETURN_CURRENT_ERROR;
 	}
-	
+
 	while (1) {
-        ret = sqlite3_step(stmt);
+		ret = sqlite3_step(stmt);
 
-        if (ret == SQLITE_DONE)
-            break;
+		if (ret == SQLITE_DONE)
+			break;
 
-        if (ret != SQLITE_ROW) {
-            /* some error occured */
-            SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(mport->db));
-            sqlite3_finalize(stmt);
-            RETURN_CURRENT_ERROR;
-        }
+		if (ret != SQLITE_ROW) {
+			/* some error occured */
+			SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(mport->db));
+			sqlite3_finalize(stmt);
+			RETURN_CURRENT_ERROR;
+		}
 
-        type = (mportAssetListEntryType) sqlite3_column_int(stmt, 0);
-        data = sqlite3_column_text(stmt, 1);
-        checksum = sqlite3_column_text(stmt, 2);
+		type = (mportAssetListEntryType) sqlite3_column_int(stmt, 0);
+		data = sqlite3_column_text(stmt, 1);
+		checksum = sqlite3_column_text(stmt, 2);
 
-        char file[FILENAME_MAX];
-        /* XXX TMP */
-        if (data == NULL) {
-            /* XXX data is null when ASSET_CHMOD (mode) or similar commands are in plist */
-            snprintf(file, sizeof(file), "%s", mport->root);
-        } else if (*data == '/') {
-	    /* we don't use mport->root because it's an absolute path like /var */
-            snprintf(file, sizeof(file), "%s", data);
-        } else {
-            snprintf(file, sizeof(file), "%s%s/%s", mport->root, pack->prefix, data);
-        }
+		char file[FILENAME_MAX];
+		/* XXX TMP */
+		if (data == NULL) {
+			/* XXX data is null when ASSET_CHMOD (mode) or similar commands are in plist */
+			snprintf(file, sizeof(file), "%s", mport->root);
+		} else if (*data == '/') {
+			/* we don't use mport->root because it's an absolute path like /var */
+			snprintf(file, sizeof(file), "%s", data);
+		} else {
+			snprintf(file, sizeof(file), "%s%s/%s", mport->root, pack->prefix, data);
+		}
 
-        switch (type) {
-	    case ASSET_FILE_OWNER_MODE:  
-		/* FALLS THROUGH */
-            case ASSET_FILE:
-		/* FALLS THROUGH */
-            case ASSET_SAMPLE:
-                if (lstat(file, &st) != 0) {
-                    mport_call_msg_cb(mport, "Can't stat %s: %s", file, strerror(errno));
-                    break; /* next asset */
-                }
+		switch (type) {
+			case ASSET_FILE_OWNER_MODE:
+				/* FALLS THROUGH */
+			case ASSET_FILE:
+				/* FALLS THROUGH */
+			case ASSET_SAMPLE:
+				if (lstat(file, &st) != 0) {
+					mport_call_msg_cb(mport, "Can't stat %s: %s", file, strerror(errno));
+					break; /* next asset */
+				}
 
-                if (S_ISREG(st.st_mode)) {
-                    if (MD5File(file, md5) == NULL)
-                        mport_call_msg_cb(mport, "Can't md5 %s: %s", file, strerror(errno));
+				if (S_ISREG(st.st_mode)) {
+					if (MD5File(file, md5) == NULL)
+						mport_call_msg_cb(mport, "Can't md5 %s: %s", file, strerror(errno));
 
-                    if (md5 == NULL)
-                        mport_call_msg_cb(mport, "Destination checksum could not be computed %s", file);
-                    else if (checksum == NULL)
-                        mport_call_msg_cb(mport, "Source checksum missing %s", file);
-                    else if (strcmp(md5, checksum) != 0)
-                        mport_call_msg_cb(mport, "Checksum mismatch: %s %s %s", file, md5, checksum);
-                }
+					if (md5 == NULL)
+						mport_call_msg_cb(mport,
+						                  "Destination checksum could not be computed %s",
+						                  file);
+					else if (checksum == NULL)
+						mport_call_msg_cb(mport, "Source checksum missing %s", file);
+					else if (strcmp(md5, checksum) != 0)
+						mport_call_msg_cb(mport, "Checksum mismatch: %s %s %s", file, md5,
+						                  checksum);
+				}
 
-                break;
-            default:
-                /* do nothing */
-                break;
-        }
+				break;
+			default:
+				/* do nothing */
+				break;
+		}
 	}
 
 	sqlite3_finalize(stmt);



More information about the Midnightbsd-cvs mailing list