[Midnightbsd-cvs] src: lib/libmport: sync with developement version.

ctriv at midnightbsd.org ctriv at midnightbsd.org
Sat Dec 1 01:21:37 EST 2007


Log Message:
-----------
sync with developement version.

Version comparison now works, but we might change the definition of "works"
in the near future.  As crazy as the FreeBSD semantics are, they might be
useful.

All package files are now bundles.  Typical package files are simply a
special case of a bundle only containting one package.  There is no
performance penalty for many packages in one bundle at install time, and
there is only one code path for all installations.

pkg-deinstall script support has been added (but is not yet tested).

Many new functions for simplifing work.  See mport_db_do(),
mport_db_prepare(), and mport_add_file_to_archive(), et al.

Modified Files:
--------------
    src/lib/libmport:
        Makefile (r1.4 -> r1.5)
        create_pkg.c (r1.7 -> r1.8)
        inst_init.c (r1.1 -> r1.2)
        install_pkg.c (r1.3 -> r1.4)
        mport.h (r1.6 -> r1.7)
        util.c (r1.7 -> r1.8)

Added Files:
-----------
    src/lib/libmport:
        archive.c (r1.1)
        db.c (r1.1)

Removed Files:
-------------
    src/lib/libmport:
        db_schema.c
        db_stub.c
        db_util.c

-------------- next part --------------
--- lib/libmport/db_stub.c
+++ /dev/null
@@ -1,110 +0,0 @@
-/*-
- * Copyright (c) 2007 Chris Reinhardt
- * 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.
- *
- * $MidnightBSD: src/lib/libmport/db_stub.c,v 1.2 2007/11/26 21:41:56 ctriv Exp $
- */
-
-
-
-#include <sqlite3.h>
-#include <stdlib.h>
-#include <string.h>
-#include "mport.h"
-
-__MBSDID("$MidnightBSD: src/lib/libmport/db_stub.c,v 1.2 2007/11/26 21:41:56 ctriv Exp $");
-
-int mport_attach_stub_db(sqlite3 *db, const char *dir)
-{
-  char *file;
-  asprintf(&file, "%s/%s", dir, MPORT_STUB_DB_FILE);
-  
-  if (mport_db_do(db, "ATTACH %Q AS stub", file) != MPORT_OK) { 
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-  }
-  
-  free(file);
-  
-  return MPORT_OK;
-}
-
-int mport_get_meta_from_db(sqlite3 *db, mportPackageMeta **ref)
-{
-  sqlite3_stmt *stmt;
-  const char *tmp = 0;
-  mportPackageMeta *pack;
-  
-  *ref = mport_new_packagemeta();
-  
-  pack = *ref;
-  
-  if (pack == NULL) 
-    return MPORT_ERR_NO_MEM;
-    
-  if (sqlite3_prepare_v2(db, "SELECT pkg, version, origin, lang, prefix FROM stub.package", -1, &stmt, &tmp) != SQLITE_OK)
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-    
-  if (sqlite3_step(stmt) != SQLITE_ROW) 
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-  
-  /* Copy pkg to pack->name */
-  if ((tmp = sqlite3_column_text(stmt, 0)) == NULL) 
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-
-  if ((pack->name = strdup(tmp)) == NULL)
-    return MPORT_ERR_NO_MEM;
-
-  /* Copy version to pack->version */
-  if ((tmp = sqlite3_column_text(stmt, 1)) == NULL) 
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-  
-  if ((pack->version = strdup(tmp)) == NULL)
-    return MPORT_ERR_NO_MEM;
-  
-  /* Copy origin to pack->origin */
-  if ((tmp = sqlite3_column_text(stmt, 2)) == NULL) 
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-  
-  if ((pack->origin = strdup(tmp)) == NULL)
-    return MPORT_ERR_NO_MEM;
-
-  /* Copy lang to pack->lang */
-  if ((tmp = sqlite3_column_text(stmt, 3)) == NULL) 
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-  
-  if ((pack->lang = strdup(tmp)) == NULL)
-    return MPORT_ERR_NO_MEM;
-
-  /* Copy prefix to pack->prefix */
-  if ((tmp = sqlite3_column_text(stmt, 4)) == NULL) 
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-  
-  if ((pack->prefix = strdup(tmp)) == NULL)
-    return MPORT_ERR_NO_MEM;
-
-  sqlite3_finalize(stmt);
-  
-  return MPORT_OK;
-}
-
Index: inst_init.c
===================================================================
RCS file: /home/cvs/src/lib/libmport/inst_init.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -L lib/libmport/inst_init.c -L lib/libmport/inst_init.c -u -r1.1 -r1.2
--- lib/libmport/inst_init.c
+++ lib/libmport/inst_init.c
@@ -43,10 +43,11 @@
 /* set up the master database, and related instance infrastructure. */
 int mport_inst_init(sqlite3 **db)
 {
-  if (mkdir(MPORT_INST_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) != 0) {
-    if (errno != EEXIST) 
-      RETURN_ERROR(MPORT_ERR_SYSCALL_FAILED, strerror(errno));
-  }
+  if (mport_mkdir(MPORT_INST_DIR) != MPORT_OK)
+    RETURN_CURRENT_ERROR;
+  
+  if (mport_mkdir(MPORT_INST_INFRA_DIR) != MPORT_OK)
+    RETURN_CURRENT_ERROR;
   
   return create_master_db(db);
 }
Index: util.c
===================================================================
RCS file: /home/cvs/src/lib/libmport/util.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -L lib/libmport/util.c -L lib/libmport/util.c -u -r1.7 -r1.8
--- lib/libmport/util.c
+++ lib/libmport/util.c
@@ -76,8 +76,6 @@
 }  
 
 
-
-
 /*
  * Copy fromname to toname 
  *
@@ -88,6 +86,22 @@
 }
 
 
+
+/* 
+ * create a directory with mode 755.  Do not fail if the
+ * directory exists already.
+ */
+int mport_mkdir(const char *dir) 
+{
+  if (mkdir(dir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) != 0) {
+    if (errno != EEXIST) 
+      RETURN_ERROR(MPORT_ERR_SYSCALL_FAILED, strerror(errno));
+  }
+  
+  return MPORT_OK;
+}
+  
+
 /*
  * Quick test to see if a file exists.
  */
Index: create_pkg.c
===================================================================
RCS file: /home/cvs/src/lib/libmport/create_pkg.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -L lib/libmport/create_pkg.c -L lib/libmport/create_pkg.c -u -r1.7 -r1.8
--- lib/libmport/create_pkg.c
+++ lib/libmport/create_pkg.c
@@ -30,16 +30,12 @@
 
 #include <sys/cdefs.h>
 #include <sys/time.h>
-#include <sys/stat.h>
 #include <sys/types.h>
-#include <dirent.h>
-#include <fcntl.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 #include <sqlite3.h>
-#include <errno.h>
 #include <md5.h>
 #include <archive.h>
 #include <archive_entry.h>
@@ -49,12 +45,13 @@
 
 
 static int create_package_db(sqlite3 **);
-static int create_plist(sqlite3 *, mportPlist *, mportPackageMeta *);
-static int create_meta(sqlite3 *, mportPackageMeta *);
+static int insert_plist(sqlite3 *, mportPlist *, mportPackageMeta *);
+static int insert_meta(sqlite3 *, mportPackageMeta *);
 static int insert_depends(sqlite3 *, mportPackageMeta *);
 static int insert_conflicts(sqlite3 *, mportPackageMeta *);
-static int copy_metafiles(mportPackageMeta *);
-static int archive_files(mportPlist *, mportPackageMeta *);
+static int archive_files(mportPlist *, mportPackageMeta *, const char *);
+static int archive_metafiles(struct archive *, mportPackageMeta *);
+static int archive_plistfiles(struct archive *, mportPackageMeta *, mportPlist *);
 static int clean_up(const char *);
 
 
@@ -68,32 +65,29 @@
   char *tmpdir = mkdtemp(dirtmpl);
 
   if (tmpdir == NULL) {
-    SET_ERROR(MPORT_ERR_FILEIO, strerror(errno));
+    ret = SET_ERROR(MPORT_ERR_FILEIO, strerror(errno));
     goto CLEANUP;
   }
   if (chdir(tmpdir) != 0)  {
-    SET_ERROR(MPORT_ERR_FILEIO, strerror(errno));
+    ret = SET_ERROR(MPORT_ERR_FILEIO, strerror(errno));
     goto CLEANUP;
   }
 
   if ((ret = create_package_db(&db)) != MPORT_OK)
     goto CLEANUP;
     
-  if ((ret = create_plist(db, plist, pack)) != MPORT_OK)
+  if ((ret = insert_plist(db, plist, pack)) != MPORT_OK)
     goto CLEANUP;
   
-  if ((ret = create_meta(db, pack)) != MPORT_OK)
+  if ((ret = insert_meta(db, pack)) != MPORT_OK)
     goto CLEANUP;
     
   if (sqlite3_close(db) != SQLITE_OK) {
-    SET_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+    ret = SET_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
     goto CLEANUP;
   }
     
-  if ((ret = copy_metafiles(pack)) != MPORT_OK) 
-    goto CLEANUP;
-    
-  if ((ret = archive_files(plist, pack)) != MPORT_OK)
+  if ((ret = archive_files(plist, pack, tmpdir)) != MPORT_OK)
     goto CLEANUP;
   
   CLEANUP:  
@@ -113,12 +107,10 @@
   return mport_generate_stub_schema(*db);
 }
 
-static int create_plist(sqlite3 *db, mportPlist *plist, mportPackageMeta *pack)
+static int insert_plist(sqlite3 *db, mportPlist *plist, mportPackageMeta *pack)
 {
   mportPlistEntry *e;
   sqlite3_stmt *stmnt;
-  const char *rest  = 0;
-  int ret;
   char sql[]  = "INSERT INTO assets (pkg, type, data, checksum) VALUES (?,?,?,?)";
   char md5[33];
   char file[FILENAME_MAX];
@@ -127,9 +119,8 @@
   strlcpy(cwd, pack->sourcedir, FILENAME_MAX);
   strlcat(cwd, pack->prefix, FILENAME_MAX);
   
-  if (sqlite3_prepare_v2(db, sql, -1, &stmnt, &rest) != SQLITE_OK) {
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-  }
+  if (mport_db_prepare(db, &stmnt, sql) != MPORT_OK) 
+    RETURN_CURRENT_ERROR;
   
   STAILQ_FOREACH(e, plist, next) {
     if (e->type == PLIST_CWD) {
@@ -152,11 +143,11 @@
     }
     
     if (e->type == PLIST_FILE) {
-      snprintf(file, FILENAME_MAX, "%s/%s", cwd, e->data);
+      (void)snprintf(file, FILENAME_MAX, "%s/%s", cwd, e->data);
       
       if (MD5File(file, md5) == NULL) {
         char *error;
-        asprintf(&error, "File not found: %s", file);
+        (void)asprintf(&error, "File not found: %s", file);
         RETURN_ERROR(MPORT_ERR_FILE_NOT_FOUND, error);
       }
       
@@ -168,7 +159,7 @@
         RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
       }
     }
-    if ((ret = sqlite3_step(stmnt)) != SQLITE_DONE) {
+    if (sqlite3_step(stmnt) != SQLITE_DONE) {
       RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
     }
         
@@ -180,14 +171,14 @@
   return MPORT_OK;
 }     
 
-static int create_meta(sqlite3 *db, mportPackageMeta *pack)
+static int insert_meta(sqlite3 *db, mportPackageMeta *pack)
 {
   sqlite3_stmt *stmnt;
   const char *rest  = 0;
   struct timespec now;
   int ret;
   
-  char sql[]  = "INSERT INTO package (pkg, version, origin, lang, prefix, date) VALUES (?,?,?,?,?,?)";
+  char sql[]  = "INSERT INTO packages (pkg, version, origin, lang, prefix, date) VALUES (?,?,?,?,?,?)";
   
   if (sqlite3_prepare_v2(db, sql, -1, &stmnt, &rest) != SQLITE_OK) {
     RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
@@ -332,46 +323,11 @@
 
 
 
-/* this is just to save a lot of typing.  It will only work in the
-   copy_metafiles() function.
- */ 
-#define COPY_PKG_METAFILE(field, tofile) \
-  if (pack->field != NULL && mport_file_exists(pack->field)) { \
-    if ((ret = mport_copy_file(pack->field, tofile)) != MPORT_OK) { \
-      return ret; \
-    } \
-  } \
-
-
-static int copy_metafiles(mportPackageMeta *pack) 
-{
-  int ret;
-
-  COPY_PKG_METAFILE(mtree, MPORT_MTREE_FILE);
-  COPY_PKG_METAFILE(pkginstall, MPORT_INSTALL_FILE);
-  COPY_PKG_METAFILE(pkgdeinstall, MPORT_DEINSTALL_FILE);
-  COPY_PKG_METAFILE(pkgmessage, MPORT_MESSAGE_FILE);
-  
-  return ret;
-}
-
-
-static int archive_files(mportPlist *plist, mportPackageMeta *pack)
+static int archive_files(mportPlist *plist, mportPackageMeta *pack, const char *tmpdir)
 {
   struct archive *a;
-  struct archive_entry *entry;
-  struct stat st;
-  DIR *dir;
-  struct dirent *diren;
-  mportPlistEntry *e;
   char filename[FILENAME_MAX];
-  char buff[8192];
-  char *cwd;
-  int len;
-  int fd;
-   
-  cwd = pack->prefix; 
-    
+
   a = archive_write_new();
   archive_write_set_compression_bzip2(a);
   archive_write_set_format_pax(a);
@@ -380,40 +336,64 @@
     RETURN_ERROR(MPORT_ERR_ARCHIVE, archive_error_string(a)); 
   }
   
-  /* First step - add the files in the tmpdir to the archive. */    
-  if ((dir = opendir(".")) == NULL) {
-    RETURN_ERROR(MPORT_ERR_SYSCALL_FAILED, strerror(errno));
+  /* First step - +CONTENTS.db ALWAYS GOES FIRST!!! */        
+  (void)snprintf(filename, FILENAME_MAX, "%s/%s", tmpdir, MPORT_STUB_DB_FILE);
+  if (mport_add_file_to_archive(a, filename, MPORT_STUB_DB_FILE)) 
+    RETURN_CURRENT_ERROR;
+    
+  /* second step - the meta files */
+  if (archive_metafiles(a, pack) != MPORT_OK)
+    RETURN_CURRENT_ERROR;
+  
+  /* last step - the real files from the plist */
+  if (archive_plistfiles(a, pack, plist) != MPORT_OK)
+    RETURN_CURRENT_ERROR;
+    
+  archive_write_finish(a);
+  
+  return MPORT_OK;    
+}
+
+
+static int archive_metafiles(struct archive *a, mportPackageMeta *pack)
+{
+  char filename[FILENAME_MAX], dir[FILENAME_MAX];
+  
+  (void)snprintf(dir, FILENAME_MAX, "%s/%s-%s", MPORT_STUB_INFRA_DIR, pack->name, pack->version);
+
+  if (pack->mtree != NULL && mport_file_exists(pack->mtree)) {
+    (void)snprintf(filename, FILENAME_MAX, "%s/%s", dir, MPORT_MTREE_FILE);
+    if (mport_add_file_to_archive(a, pack->mtree, filename) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
   }
   
-  while ((diren = readdir(dir)) != NULL) {
-    if (strcmp(diren->d_name, ".") == 0 || strcmp(diren->d_name, "..") == 0) 
-      continue;
-    
-    if (lstat(diren->d_name, &st) != 0) {
-      RETURN_ERROR(MPORT_ERR_SYSCALL_FAILED, strerror(errno));
-    }
-    
-    entry = archive_entry_new();
-    archive_entry_copy_stat(entry, &st);
-    archive_entry_set_pathname(entry, diren->d_name);
-    archive_write_header(a, entry);
-    if ((fd = open(diren->d_name, O_RDONLY)) == -1) {
-      RETURN_ERROR(MPORT_ERR_SYSCALL_FAILED, strerror(errno));
-    }
-    
-    len = read(fd, buff, sizeof(buff));
-    while (len > 0) {
-      archive_write_data(a, buff, len);
-      len = read(fd, buff, sizeof(buff));
-    }
-    
-    archive_entry_free(entry);
-    close(fd);
+  if (pack->pkginstall != NULL && mport_file_exists(pack->pkginstall)) {
+    (void)snprintf(filename, FILENAME_MAX, "%s/%s", dir, MPORT_INSTALL_FILE);
+    if (mport_add_file_to_archive(a, pack->pkginstall, filename) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
+  }
+  
+  if (pack->pkgdeinstall != NULL && mport_file_exists(pack->pkgdeinstall)) {
+    (void)snprintf(filename, FILENAME_MAX, "%s/%s", dir, MPORT_DEINSTALL_FILE);
+    if (mport_add_file_to_archive(a, pack->pkgdeinstall, filename) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
   }
   
-  closedir(dir);
+  if (pack->pkgmessage != NULL && mport_file_exists(pack->pkgmessage)) {
+    (void)snprintf(filename, FILENAME_MAX, "%s/%s", dir, MPORT_MESSAGE_FILE);
+    if (mport_add_file_to_archive(a, pack->pkgmessage, filename) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
+  }
+  
+  return MPORT_OK;
+}
+
+static int archive_plistfiles(struct archive *a, mportPackageMeta *pack, mportPlist *plist)
+{
+  mportPlistEntry *e;
+  char filename[FILENAME_MAX];
+  char *cwd = pack->prefix;
   
-  /* second step - all the files in the plist */  
   STAILQ_FOREACH(e, plist, next) {
     if (e->type == PLIST_CWD) {
       if (e->data == NULL) {
@@ -427,31 +407,13 @@
       continue;
     }
     
-    snprintf(filename, FILENAME_MAX, "%s/%s/%s", pack->sourcedir, cwd, e->data);
-    
-    if (lstat(filename, &st) != 0) {
-      RETURN_ERROR(MPORT_ERR_SYSCALL_FAILED, strerror(errno));
-    }
+    (void)snprintf(filename, FILENAME_MAX, "%s/%s/%s", pack->sourcedir, cwd, e->data);
     
-    entry = archive_entry_new();
-    archive_entry_copy_stat(entry, &st);
-    archive_entry_set_pathname(entry, e->data);
-    archive_write_header(a, entry);
-    if ((fd = open(filename, O_RDONLY)) == -1) {
-      RETURN_ERROR(MPORT_ERR_SYSCALL_FAILED, strerror(errno));
-    }
-    
-    len = read(fd, buff, sizeof(buff));
-    while (len > 0) {
-      archive_write_data(a, buff, len);
-      len = read(fd, buff, sizeof(buff));
-    }
-    
-    archive_entry_free(entry);
-    close(fd);
-  }
-  
-  archive_write_finish(a);
+    if (mport_add_file_to_archive(a, filename, e->data) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
+  }    
+ 
+  return MPORT_OK;
 }
 
 #ifdef DEBUG
--- lib/libmport/db_util.c
+++ /dev/null
@@ -1,67 +0,0 @@
-/*-
- * Copyright (c) 2007 Chris Reinhardt
- * 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.
- *
- * $MidnightBSD: src/lib/libmport/db_util.c,v 1.2 2007/11/26 21:41:56 ctriv Exp $
- */
-
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#include "mport.h"
-
-__MBSDID("$MidnightBSD: src/lib/libmport/db_util.c,v 1.2 2007/11/26 21:41:56 ctriv Exp $");
-
-/* mport_db_do(sqlite3 *db, const char *sql, ...)
- * 
- * A wrapper for doing executing a single sql query.  Takes a sqlite3 struct
- * pointer, a format string and a list of args.  See the documentation for 
- * sqlite3_vmprintf() for format information.
- */
-int mport_db_do(sqlite3 *db, const char *fmt, ...) 
-{
-  va_list args;
-  char *sql;
-  
-  va_start(args, fmt);
-  
-  if ((sql = sqlite3_vmprintf(fmt, args)) == NULL) {
-    return MPORT_ERR_NO_MEM;
-  }
-
-  if (sqlite3_exec(db, sql, 0, 0, 0) != SQLITE_OK) {
-    sqlite3_free(sql);
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-  }
-  
-  sqlite3_free(sql);
-  
-  return MPORT_OK;
-}
-  
-
-
-
--- lib/libmport/db_schema.c
+++ /dev/null
@@ -1,80 +0,0 @@
-/*-
- * Copyright (c) 2007 Chris Reinhardt
- * 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.
- *
- * $MidnightBSD: src/lib/libmport/db_schema.c,v 1.4 2007/11/22 08:00:32 ctriv Exp $
- */
-
-
-
-#include <sqlite3.h>
-#include <stdlib.h>
-#include "mport.h"
-
-__MBSDID("$MidnightBSD: src/lib/libmport/db_schema.c,v 1.4 2007/11/22 08:00:32 ctriv Exp $");
-
-static int run_sql(sqlite3 *db, const char *sql);
-
-#define RUN_SQL(db, sql) \
-  if (run_sql(db, sql) != SQLITE_OK) \
-    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db))
-
-
-int mport_generate_stub_schema(sqlite3 *db) 
-{
-  RUN_SQL(db, "CREATE TABLE assets    (pkg text not NULL, type int NOT NULL, data text, checksum text)");
-  RUN_SQL(db, "CREATE TABLE package   (pkg text NOT NULL, version text NOT NULL, origin text NOT NULL, lang text, options text, date int NOT NULL, prefix text NOT NULL)");
-  RUN_SQL(db, "CREATE TABLE conflicts (pkg text NOT NULL, conflict_pkg text NOT NULL, conflict_version text NOT NULL)");
-  RUN_SQL(db, "CREATE TABLE depends   (pkg text NOT NULL, depend_pkgname text NOT NULL, depend_pkgversion text NOT NULL, depend_port text NOT NULL)");
-    
-  return MPORT_OK;  
-}
-
-int mport_generate_master_schema(sqlite3 *db) 
-{
-  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS packages (pkg text NOT NULL, version text NOT NULL, origin text NOT NULL, prefix text NOT NULL, lang text, options text, date int)");
-  RUN_SQL(db, "CREATE UNIQUE INDEX IF NOT EXISTS packages_pkg ON packages (pkg)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS packages_origin ON packages (origin)");
-  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS depends (pkg text NOT NULL, depend_pkgname text NOT NULL, depend_pkgversion text NOT NULL, depend_port text NOT NULL)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS depends_pkg ON depends (pkg)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS depends_dependpkgname ON depends (depend_pkgname)");
-  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS assets (pkg text NOT NULL, type int NOT NULL, data text, checksum text)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS assets_pkg ON assets (pkg)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS assets_data ON assets (data)");
-  
-  return MPORT_OK;
-}
-
-static int run_sql(sqlite3 *db, const char *sql)
-{
-  char *error = 0;
-  
-  return sqlite3_exec(
-    db,
-    sql,
-    NULL,
-    NULL, 
-    &error
-  );
-}
--- /dev/null
+++ lib/libmport/db.c
@@ -0,0 +1,289 @@
+/*-
+ * Copyright (c) 2007 Chris Reinhardt
+ * 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.
+ *
+ * $MidnightBSD: src/lib/libmport/db.c,v 1.1 2007/12/01 06:21:37 ctriv Exp $
+ */
+
+
+
+#include <sqlite3.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include "mport.h"
+
+__MBSDID("$MidnightBSD: src/lib/libmport/db.c,v 1.1 2007/12/01 06:21:37 ctriv Exp $");
+
+
+static int populate_meta_from_stmt(mportPackageMeta *, sqlite3 *, sqlite3_stmt *);
+
+
+/* mport_db_do(sqlite3 *db, const char *sql, ...)
+ * 
+ * A wrapper for doing executing a single sql query.  Takes a sqlite3 struct
+ * pointer, a format string and a list of args.  See the documentation for 
+ * sqlite3_vmprintf() for format information.
+ */
+int mport_db_do(sqlite3 *db, const char *fmt, ...) 
+{
+  va_list args;
+  char *sql;
+  
+  va_start(args, fmt);
+  
+  if ((sql = sqlite3_vmprintf(fmt, args)) == NULL) {
+    return MPORT_ERR_NO_MEM;
+  }
+
+  if (sqlite3_exec(db, sql, 0, 0, 0) != SQLITE_OK) {
+    sqlite3_free(sql);
+    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+  }
+  
+  sqlite3_free(sql);
+  
+  return MPORT_OK;
+}
+
+
+
+/* mport_db_prepare(sqlite3 *, sqlite3_stmt **, const char *, ...)
+ * 
+ * A wrapper for preparing sqlite statements into statement structs.
+ * This function returns MPORT_OK on success.  The sqlite3_stmt pointer 
+ * may be null if this function does not return MPORT_OK.
+ */
+int mport_db_prepare(sqlite3 *db, sqlite3_stmt **stmt, const char * fmt, ...)
+{
+  va_list args;
+  char *sql;
+  
+  va_start(args, fmt);
+  
+  if ((sql = sqlite3_vmprintf(fmt, args)) == NULL) {
+    return MPORT_ERR_NO_MEM;
+  }
+  
+  if (sqlite3_prepare_v2(db, sql, -1, stmt, NULL) != SQLITE_OK) {
+    sqlite3_free(sql);
+    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+  }
+  
+  sqlite3_free(sql);
+  
+  return MPORT_OK;
+}
+
+  
+
+/* mport_attach_stub_db(sqlite *db, const char *tmpdir) 
+ *
+ * Attaches tmpdir/MPORT_STUB_DB_FILE to the given database handle as 
+ * 'stub'.  (stub.table to access a table in the stub db)
+ *
+ * Returns MPORT_OK on success.
+ */
+int mport_attach_stub_db(sqlite3 *db, const char *dir)
+{
+  char *file;
+  asprintf(&file, "%s/%s", dir, MPORT_STUB_DB_FILE);
+  
+  if (mport_db_do(db, "ATTACH %Q AS stub", file) != MPORT_OK) { 
+    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+  }
+  
+  free(file);
+  
+  return MPORT_OK;
+}
+
+
+/* mport_get_meta_from_stub(sqlite *db, mportPackageMeta ***pack)
+ *
+ * Allocates and populates a vector of mportPackageMeta structs from the stub database
+ * connected to db. These structs represent all the packages in the stub database.
+ * This does not populate the conflicts and depends fields.
+ */
+int mport_get_meta_from_stub(sqlite3 *db, mportPackageMeta ***ref)
+{
+  sqlite3_stmt *stmt;
+  int len;
+  mportPackageMeta **vec;
+  
+  if (mport_db_prepare(db, &stmt, "SELECT COUNT(*) FROM stub.packages") != MPORT_OK)
+    RETURN_CURRENT_ERROR;
+  
+  len = sqlite3_column_int(stmt, 0);
+  sqlite3_finalize(stmt);
+  
+  vec = (mportPackageMeta**)malloc((1+len) * sizeof(mportPackageMeta *));
+  *ref = vec;
+  
+  if (vec == NULL) 
+    return MPORT_ERR_NO_MEM;
+    
+  if (mport_db_prepare(db, &stmt, "SELECT pkg, version, origin, lang, prefix FROM stub.packages") != MPORT_OK)
+    RETURN_CURRENT_ERROR;
+  
+  while (*vec != NULL) { 
+    switch (sqlite3_step(stmt)) {
+      case SQLITE_ROW:
+        *vec = mport_new_packagemeta();
+        if (*vec == NULL)
+          return MPORT_ERR_NO_MEM;
+        if (populate_meta_from_stmt(*vec, db, stmt) != MPORT_OK)
+          RETURN_CURRENT_ERROR;
+        vec++;
+        break;
+      case SQLITE_DONE:
+        /* set the last cell in the array to null */
+        *vec = NULL;
+        break;
+      default:
+        sqlite3_finalize(stmt);
+        RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+        break; /* not reached */
+    }
+  }
+  
+  sqlite3_finalize(stmt);
+  
+  return MPORT_OK;
+}
+
+
+/* mport_get_meta_from_master(sqlite3 *db, mportPacakgeMeta **pack, const char *name)
+ *
+ * Allocate and populate the package meta for the given package from the
+ * master database.
+ * 
+ * pack is set to NULL if package 'name' is not installed
+ */
+int mport_get_meta_from_master(sqlite3 *db, mportPackageMeta **pack, const char *name)
+{
+  sqlite3_stmt *stmt;
+
+  if (mport_db_prepare(db, &stmt, "SELECT pkg, version, lang, prefix FROM packages WHERE pkg=%Q", name) != MPORT_OK)
+    RETURN_CURRENT_ERROR;
+  
+  switch (sqlite3_step(stmt)) {
+    case SQLITE_ROW:
+      *pack = mport_new_packagemeta();
+      if (*pack == NULL)
+        return MPORT_ERR_NO_MEM;
+      return populate_meta_from_stmt(*pack, db, stmt);
+      break;
+    case SQLITE_DONE:
+      *pack = NULL;
+      sqlite3_finalize(stmt);
+      return MPORT_OK;
+      break;
+    default:
+      sqlite3_finalize(stmt);
+      RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+      break;
+  }
+  
+  /* no reached */
+  return MPORT_OK;
+}
+  
+ 
+ 
+static int populate_meta_from_stmt(mportPackageMeta *pack, sqlite3 *db, sqlite3_stmt *stmt) 
+{  
+  const char *tmp = 0;
+
+  /* Copy pkg to pack->name */
+  if ((tmp = sqlite3_column_text(stmt, 0)) == NULL) 
+    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+
+  if ((pack->name = strdup(tmp)) == NULL)
+    return MPORT_ERR_NO_MEM;
+
+  /* Copy version to pack->version */
+  if ((tmp = sqlite3_column_text(stmt, 1)) == NULL) 
+    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+  
+  if ((pack->version = strdup(tmp)) == NULL)
+    return MPORT_ERR_NO_MEM;
+  
+  /* Copy origin to pack->origin */
+  if ((tmp = sqlite3_column_text(stmt, 2)) == NULL) 
+    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+  
+  if ((pack->origin = strdup(tmp)) == NULL)
+    return MPORT_ERR_NO_MEM;
+
+  /* Copy lang to pack->lang */
+  if ((tmp = sqlite3_column_text(stmt, 3)) == NULL) 
+    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+  
+  if ((pack->lang = strdup(tmp)) == NULL)
+    return MPORT_ERR_NO_MEM;
+
+  /* Copy prefix to pack->prefix */
+  if ((tmp = sqlite3_column_text(stmt, 4)) == NULL) 
+    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+  
+  if ((pack->prefix = strdup(tmp)) == NULL)
+    return MPORT_ERR_NO_MEM;
+
+  
+  return MPORT_OK;
+}
+
+
+
+#define RUN_SQL(db, sql) \
+  if (mport_db_do(db, sql) != MPORT_OK) \
+    RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db))
+
+
+int mport_generate_stub_schema(sqlite3 *db) 
+{
+  RUN_SQL(db, "CREATE TABLE assets    (pkg text not NULL, type int NOT NULL, data text, checksum text)");
+  RUN_SQL(db, "CREATE TABLE packages  (pkg text NOT NULL, version text NOT NULL, origin text NOT NULL, lang text, options text, date int NOT NULL, prefix text NOT NULL)");
+  RUN_SQL(db, "CREATE TABLE conflicts (pkg text NOT NULL, conflict_pkg text NOT NULL, conflict_version text NOT NULL)");
+  RUN_SQL(db, "CREATE TABLE depends   (pkg text NOT NULL, depend_pkgname text NOT NULL, depend_pkgversion text NOT NULL, depend_port text NOT NULL)");
+    
+  return MPORT_OK;  
+}
+
+int mport_generate_master_schema(sqlite3 *db) 
+{
+  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS packages (pkg text NOT NULL, version text NOT NULL, origin text NOT NULL, prefix text NOT NULL, lang text, options text, date int)");
+  RUN_SQL(db, "CREATE UNIQUE INDEX IF NOT EXISTS packages_pkg ON packages (pkg)");
+  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS packages_origin ON packages (origin)");
+  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS depends (pkg text NOT NULL, depend_pkgname text NOT NULL, depend_pkgversion text NOT NULL, depend_port text NOT NULL)");
+  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS depends_pkg ON depends (pkg)");
+  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS depends_dependpkgname ON depends (depend_pkgname)");
+  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS assets (pkg text NOT NULL, type int NOT NULL, data text, checksum text)");
+  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS assets_pkg ON assets (pkg)");
+  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS assets_data ON assets (data)");
+  
+  return MPORT_OK;
+}
Index: install_pkg.c
===================================================================
RCS file: /home/cvs/src/lib/libmport/install_pkg.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -L lib/libmport/install_pkg.c -L lib/libmport/install_pkg.c -u -r1.3 -r1.4
--- lib/libmport/install_pkg.c
+++ lib/libmport/install_pkg.c
@@ -30,6 +30,7 @@
 
 #include "mport.h"
 #include <sys/cdefs.h>
+#include <libgen.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -60,12 +61,12 @@
   char filepath[FILENAME_MAX];
   const char *file;
   sqlite3 *db;
+  mportPackageMeta **packs;
   mportPackageMeta *pack;
-  int ret = MPORT_OK;
   
   /* initialize our local mport instance */
-  if ((ret = mport_inst_init(&db)) != MPORT_OK) 
-    return ret;
+  if (mport_inst_init(&db) != MPORT_OK) 
+    RETURN_CURRENT_ERROR;
 
   /* extract the meta-files into the a temp dir */  
   char dirtmpl[] = "/tmp/mport.XXXXXXXX"; 
@@ -93,38 +94,42 @@
   }
   
   /* Attach the stub db */
-  if ((ret = mport_attach_stub_db(db, tmpdir)) != MPORT_OK) 
-    return ret;
+  if (mport_attach_stub_db(db, tmpdir) != MPORT_OK) 
+    RETURN_CURRENT_ERROR;
 
-  /* get the meta object from the stub database */  
-  if ((ret = mport_get_meta_from_db(db, &pack)) != MPORT_OK)
-    return ret;
-
-  if (prefix != NULL) {
-    free(pack->prefix);
-    pack->prefix = strdup(prefix);
-  }
-  
-  /* check if this is installed already, depends, and conflicts */
-  if ((ret = check_preconditions(db, pack)) != MPORT_OK)
-    return ret;
-
-  /* Run mtree.  Run pkg-install. Etc... */
-  if ((ret = do_pre_install(db, pack, tmpdir)) != MPORT_OK)
-    return ret;
+  /* get the meta objects from the stub database */  
+  if (mport_get_meta_from_stub(db, &packs) != MPORT_OK)
+    RETURN_CURRENT_ERROR;
+
+  for (;*packs; packs++) {
+    pack  = *packs;    
+
+    if (prefix != NULL) {
+      free(pack->prefix);
+      pack->prefix = strdup(prefix);
+    }
+    
+    /* check if this is installed already, depends, and conflicts */
+    if (check_preconditions(db, pack) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
+
+    /* Run mtree.  Run pkg-install. Etc... */
+    if (do_pre_install(db, pack, tmpdir) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
 
-  if ((ret = do_actual_install(a, entry, db, pack, tmpdir)) != MPORT_OK)
-    return ret;
+    if (do_actual_install(a, entry, db, pack, tmpdir) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
+    
+    archive_read_finish(a);
+    
+    if (do_post_install(db, pack, tmpdir) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
+  } 
   
-  archive_read_finish(a);
+  if (clean_up(tmpdir) != MPORT_OK)
+    RETURN_CURRENT_ERROR;
   
-  if ((ret = do_post_install(db, pack, tmpdir)) != MPORT_OK)
-    return ret;
- 
-  if ((ret = clean_up(tmpdir)) != MPORT_OK)
-    return ret;
-  
-  return ret;
+  return MPORT_OK;
 }
 
 /* This does everything that has to happen before we start installing files.
@@ -154,7 +159,6 @@
       const char *tmpdir
     )
 {
-  const char *err;
   int ret;
   mportPlistEntryType type;
   char *data, *cwd;
@@ -169,13 +173,13 @@
   if ((ret = mport_db_do(db, "INSERT INTO packages (pkg, version, origin, prefix, lang, options) VALUES (%Q,%Q,%Q,%Q,%Q,%Q)", pack->name, pack->version, pack->origin, pack->prefix, pack->lang, pack->options)) != MPORT_OK)
     goto ERROR;
   /* Insert the assets into the master table */
-  if ((ret = mport_db_do(db, "INSERT INTO assets (pkg, type, data, checksum) SELECT pkg,type,data,checksum FROM stub.assets")) != MPORT_OK)
+  if ((ret = mport_db_do(db, "INSERT INTO assets (pkg, type, data, checksum) SELECT pkg,type,data,checksum FROM stub.assets WHERE pkg=%Q", pack->name)) != MPORT_OK)
     goto ERROR;  
   /* Insert the depends into the master table */
-  if ((ret = mport_db_do(db, "INSERT INTO depends (pkg, depend_pkgname, depend_pkgversion, depend_port) SELECT pkg,depend_pkgname,depend_pkgversion,depend_port FROM stub.depends")) != MPORT_OK) 
+  if ((ret = mport_db_do(db, "INSERT INTO depends (pkg, depend_pkgname, depend_pkgversion, depend_port) SELECT pkg,depend_pkgname,depend_pkgversion,depend_port FROM stub.depends WHERE pkg=%Q", pack->name)) != MPORT_OK) 
     goto ERROR;
   
-  if ((ret = sqlite3_prepare_v2(db, "SELECT type,data FROM stub.assets", -1, &assets, &err)) != SQLITE_OK) {
+  if ((ret = mport_db_prepare(db, &assets, "SELECT type,data FROM stub.assets WHERE pkg=%Q", pack->name)) != MPORT_OK) {
     mport_set_err(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
     goto ERROR;
   }
@@ -203,13 +207,13 @@
          * in the archive was read in the loop in mport_install_pkg.  we
          * use the current entry and then update it. */
         if (entry == NULL) {
-          mport_set_err(MPORT_ERR_INTERNAL, "Plist to arhive mismatch!");
+          ret = SET_ERROR(MPORT_ERR_INTERNAL, "Plist to arhive mismatch!");
           goto ERROR; 
         } 
-        snprintf(file, FILENAME_MAX, "%s/%s", cwd, data);
+        (void)snprintf(file, FILENAME_MAX, "%s/%s", cwd, data);
         archive_entry_set_pathname(entry, file);
         if ((ret = archive_read_extract(a, entry, ARCHIVE_EXTRACT_OWNER|ARCHIVE_EXTRACT_PERM)) != ARCHIVE_OK) {
-          mport_set_err(MPORT_ERR_ARCHIVE, archive_error_string(a));
+          ret = SET_ERROR(MPORT_ERR_ARCHIVE, archive_error_string(a));
           goto ERROR;
         }
         /* we only look for fatal, because EOF is only an error if we come
@@ -239,6 +243,19 @@
 
 static int do_post_install(sqlite3 *db, mportPackageMeta *pack, const char *tmpdir)
 {
+  char to[FILENAME_MAX], from[FILENAME_MAX];
+  (void)snprintf(from, FILENAME_MAX, "%s/%s/%s-%s/%s", tmpdir, MPORT_STUB_INFRA_DIR, pack->name, pack->version, MPORT_DEINSTALL_FILE);
+  
+  if (mport_file_exists(from)) {
+    (void)snprintf(to, FILENAME_MAX, "%s/%s-%s/%s", MPORT_INST_INFRA_DIR, pack->name, pack->version, MPORT_DEINSTALL_FILE);
+    
+    if (mport_mkdir(dirname(to)) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
+  
+    if (mport_copy_file(from, to) != MPORT_OK)
+      RETURN_CURRENT_ERROR;
+  }
+  
   return run_pkg_install(tmpdir, pack, "POST-INSTALL");
 }
 
@@ -385,7 +402,7 @@
   char file[FILENAME_MAX];
   int ret;
   
-  snprintf(file, FILENAME_MAX, "%s/%s", tmpdir, MPORT_MTREE_FILE);
+  (void)snprintf(file, FILENAME_MAX, "%s/%s/%s-%s/%s", tmpdir, MPORT_STUB_INFRA_DIR, pack->name, pack->version, MPORT_MTREE_FILE);
   
   if (mport_file_exists(file)) {
     if ((ret = mport_xsystem("%s -U -f %s -d -e -p %s >/dev/null", MPORT_MTREE_BIN, file, pack->prefix)) != 0) 
@@ -395,12 +412,13 @@
   return MPORT_OK;
 }
 
+
 static int run_pkg_install(const char *tmpdir, mportPackageMeta *pack, const char *mode)
 {
   char file[FILENAME_MAX];
   int ret;
   
-  snprintf(file, FILENAME_MAX, "%s/%s", tmpdir, MPORT_INSTALL_FILE);    
+  snprintf(file, FILENAME_MAX, "%s/%s/%s-%s/%s", tmpdir, MPORT_STUB_INFRA_DIR, pack->name, pack->version, MPORT_INSTALL_FILE);    
   if (mport_file_exists(file)) {
     if ((ret = mport_xsystem("PKG_PREFIX=%s %s %s %s", pack->prefix, MPORT_SH_BIN, file, mode)) != 0)
       return mport_set_errx(MPORT_ERR_SYSCALL_FAILED, "%s %s returned non-zero: %i" MPORT_INSTALL_FILE, mode, ret);
@@ -412,8 +430,11 @@
 
 static int clean_up(const char *tmpdir) 
 {
-  // return mport_rmtree(tmpdir);
+#ifdef DEBUG
   return MPORT_OK;
+#else
+  return mport_rmtree(tmpdir);
+#endif
 }
 
 
Index: mport.h
===================================================================
RCS file: /home/cvs/src/lib/libmport/mport.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -L lib/libmport/mport.h -L lib/libmport/mport.h -u -r1.6 -r1.7
--- lib/libmport/mport.h
+++ lib/libmport/mport.h
@@ -90,7 +90,7 @@
 
 mportPackageMeta * mport_new_packagemeta(void);
 void mport_free_packagemeta(mportPackageMeta *);
-
+void mport_free_packagemeta_vec(mportPackageMeta **);
 
 /* Package creation */
 int mport_create_pkg(mportPlist *, mportPackageMeta *);
@@ -108,9 +108,10 @@
 
 /* Various database convience functions */
 int mport_attach_stub_db(sqlite3 *, const char *);
-int mport_get_meta_from_db(sqlite3 *, mportPackageMeta **);
+int mport_get_meta_from_stub(sqlite3 *, mportPackageMeta ***);
+int mport_get_meta_from_master(sqlite3 *, mportPackageMeta**, const char *);
 int mport_db_do(sqlite3 *, const char *, ...);
-
+int mport_db_prepare(sqlite3 *, sqlite3_stmt **, const char *, ...);
 
 /* instance init */
 int mport_inst_init(sqlite3 **);
@@ -142,6 +143,7 @@
 #define MPORT_ERR_MALFORMED_VERSION	12
 
 
+#define RETURN_CURRENT_ERROR return mport_err_code()
 #define RETURN_ERROR(code, msg) return mport_set_errx((code), "Error at %s:(%d): %s", __FILE__, __LINE__, (msg))
 #define SET_ERROR(code,msg) mport_set_errx((code), "Error at %s:(%d): %s", __FILE__, __LINE__, (msg))
 
@@ -149,22 +151,30 @@
 /* Utils */
 int mport_copy_file(const char *, const char *);
 int mport_rmtree(const char *);
+int mport_mkdir(const char *);
 int mport_file_exists(const char *);
 int mport_xsystem(const char *, ...);
 void mport_parselist(char *, char ***);
 int mport_run_plist_exec(const char *, const char *, const char *);
 
-/* Meta files */
+
+/* archive helpers */
+#include <archive.h>
+int mport_add_file_to_archive(struct archive *, const char *, const char *);
+
+/* Infrastructure files */
 #define MPORT_STUB_DB_FILE 	"+CONTENTS.db"
-#define MPORT_MTREE_FILE   	"+MTREE"
-#define MPORT_INSTALL_FILE 	"+INSTALL"
-#define MPORT_DEINSTALL_FILE	"+DEINSTALL"
-#define MPORT_MESSAGE_FILE	"+MESSAGE"
+#define MPORT_STUB_INFRA_DIR	"+INFRASTRUCTURE"
+#define MPORT_MTREE_FILE   	"mtree"
+#define MPORT_INSTALL_FILE 	"pkg-install"
+#define MPORT_DEINSTALL_FILE	"pkg-deinstall"
+#define MPORT_MESSAGE_FILE	"pkg-message"
 
 
 /* Instance files */
 #define MPORT_INST_DIR 		"/var/db/mport"
 #define MPORT_MASTER_DB_FILE	"/var/db/mport/master.db"
+#define MPORT_INST_INFRA_DIR	"/var/db/mport/infrastructure"
 
 /* Binaries we use */
 #define MPORT_MTREE_BIN		"/usr/sbin/mtree"
--- /dev/null
+++ lib/libmport/archive.c
@@ -0,0 +1,74 @@
+/*-
+ * Copyright (c) 2007 Chris Reinhardt
+ * 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.
+ *
+ * $MidnightBSD: src/lib/libmport/archive.c,v 1.1 2007/12/01 06:21:37 ctriv Exp $
+ */
+
+
+#include <sys/stat.h>
+#include <string.h>
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <archive.h>
+#include <archive_entry.h>
+#include "mport.h"
+
+__MBSDID("$MidnightBSD: src/lib/libmport/archive.c,v 1.1 2007/12/01 06:21:37 ctriv Exp $");
+
+
+int mport_add_file_to_archive(struct archive *a, const char *filename, const char *path) 
+{
+  struct archive_entry *entry;
+  struct stat st;
+  int fd, len;
+  char buff[1024*8];
+  
+  if (lstat(filename, &st) != 0) {
+    RETURN_ERROR(MPORT_ERR_SYSCALL_FAILED, strerror(errno));
+  }
+  
+  entry = archive_entry_new();
+  archive_entry_copy_stat(entry, &st);
+  archive_entry_set_pathname(entry, path);
+  archive_write_header(a, entry);
+  
+  if ((fd = open(filename, O_RDONLY)) == -1) {
+    RETURN_ERROR(MPORT_ERR_SYSCALL_FAILED, strerror(errno));
+  }
+    
+  len = read(fd, buff, sizeof(buff));
+  while (len > 0) {
+    archive_write_data(a, buff, len);
+    len = read(fd, buff, sizeof(buff));
+  }
+    
+  archive_entry_free(entry);
+  close(fd);
+
+  return MPORT_OK;  
+}
+
Index: Makefile
===================================================================
RCS file: /home/cvs/src/lib/libmport/Makefile,v
retrieving revision 1.4
retrieving revision 1.5
diff -L lib/libmport/Makefile -L lib/libmport/Makefile -u -r1.4 -r1.5
--- lib/libmport/Makefile
+++ lib/libmport/Makefile
@@ -1,7 +1,8 @@
 # $MidnightBSD$
 
 LIB=		mport
-SRCS=		plist.c	create_pkg.c db_schema.c db_stub.c db_util.c util.c error.c install_pkg.c inst_init.c version_cmp.c
+SRCS=		archive.c plist.c create_pkg.c db.c util.c error.c \
+		install_pkg.c inst_init.c version_cmp.c
 
 INCS=		mport.h
 


More information about the Midnightbsd-cvs mailing list