[Midnightbsd-cvs] src: lib/libmport: First working version with installation.
ctriv at midnightbsd.org
ctriv at midnightbsd.org
Mon Nov 26 16:41:56 EST 2007
Log Message:
-----------
First working version with installation. Many code paths still untested.
Modified Files:
--------------
src/lib/libmport:
db_stub.c (r1.1 -> r1.2)
db_util.c (r1.1 -> r1.2)
install_pkg.c (r1.2 -> r1.3)
version_cmp.c (r1.1 -> r1.2)
-------------- next part --------------
Index: db_stub.c
===================================================================
RCS file: /home/cvs/src/lib/libmport/db_stub.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -L lib/libmport/db_stub.c -L lib/libmport/db_stub.c -u -r1.1 -r1.2
--- lib/libmport/db_stub.c
+++ lib/libmport/db_stub.c
@@ -40,7 +40,7 @@
char *file;
asprintf(&file, "%s/%s", dir, MPORT_STUB_DB_FILE);
- if (mport_db_do(db, "ATTACH %s AS stub", file) != MPORT_OK) {
+ if (mport_db_do(db, "ATTACH %Q AS stub", file) != MPORT_OK) {
RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
}
@@ -51,28 +51,30 @@
int mport_get_meta_from_db(sqlite3 *db, mportPackageMeta **ref)
{
- mportPackageMeta *pack = *ref;
sqlite3_stmt *stmt;
const char *tmp = 0;
+ mportPackageMeta *pack;
- pack = mport_new_packagemeta();
+ *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)
+ 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_DONE)
+ 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));
@@ -88,14 +90,14 @@
return MPORT_ERR_NO_MEM;
/* Copy lang to pack->lang */
- if ((tmp = sqlite3_column_text(stmt, 4)) == NULL)
+ 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, 5)) == NULL)
+ if ((tmp = sqlite3_column_text(stmt, 4)) == NULL)
RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
if ((pack->prefix = strdup(tmp)) == NULL)
Index: install_pkg.c
===================================================================
RCS file: /home/cvs/src/lib/libmport/install_pkg.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -L lib/libmport/install_pkg.c -L lib/libmport/install_pkg.c -u -r1.2 -r1.3
--- lib/libmport/install_pkg.c
+++ lib/libmport/install_pkg.c
@@ -31,6 +31,7 @@
#include "mport.h"
#include <sys/cdefs.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <archive.h>
@@ -98,14 +99,16 @@
/* 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);
+ }
- if (prefix != NULL)
- pack->prefix = (char *)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;
@@ -157,18 +160,19 @@
char *data, *cwd;
char file[FILENAME_MAX];
sqlite3_stmt *assets;
+
- if (mport_db_do(db, "START TRANSACTION") != MPORT_OK)
+ if (mport_db_do(db, "BEGIN TRANSACTION") != MPORT_OK)
goto ERROR;
/* Insert the package meta row into the packages table - XXX Does not honor pack->prefix! */
- if (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)
+ 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 (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")) != MPORT_OK)
goto ERROR;
/* Insert the depends into the master table */
- if (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")) != MPORT_OK)
goto ERROR;
if ((ret = sqlite3_prepare_v2(db, "SELECT type,data FROM stub.assets", -1, &assets, &err)) != SQLITE_OK) {
@@ -220,7 +224,7 @@
break;
}
}
-
+
sqlite3_finalize(assets);
if (mport_db_do(db, "COMMIT TRANSACTION") != MPORT_OK)
@@ -249,15 +253,16 @@
const char *inst_version;
ret = MPORT_OK;
-
+
/* check if the package is already installed */
if (sqlite3_prepare_v2(db, "SELECT version FROM packages WHERE pkg=?", -1, &lookup, NULL) != SQLITE_OK)
RETURN_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
-
+
if (sqlite3_bind_text(lookup, 1, pack->name, -1, SQLITE_STATIC) != SQLITE_OK) {
ret = SET_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
goto DONE;
}
+
sret = sqlite3_step(lookup);
switch (sret) {
@@ -276,6 +281,7 @@
goto DONE;
}
sqlite3_finalize(lookup);
+
/* Check for conflicts */
if (sqlite3_prepare_v2(db, "SELECT conflict_pkg, conflict_version FROM stub.conflicts", -1, &stmt, NULL) != SQLITE_OK) {
@@ -322,7 +328,7 @@
}
/* check for depends */
- if (sqlite3_prepare_v2(db, "SELECT depend_pkgname, depend_pkgersion FROM stub.depends", -1, &stmt, NULL) != SQLITE_OK) {
+ if (sqlite3_prepare_v2(db, "SELECT depend_pkgname, depend_pkgversion FROM stub.depends", -1, &stmt, NULL) != SQLITE_OK) {
ret = SET_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
goto DONE;
}
@@ -352,12 +358,19 @@
goto DONE;
}
} else if (lret == SQLITE_DONE) {
- /* No more depends to check. */
- break;
+ /* this depend isn't installed. */
+ ret = mport_set_errx(MPORT_ERR_MISSING_DEPEND, "%s depends on %s, which is not installed.", pack->name, depend_pkg);
+ goto DONE;
} else {
ret = SET_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
goto DONE;
}
+ } else if (sret == SQLITE_DONE) {
+ /* No more depends to check. */
+ break;
+ } else {
+ ret = SET_ERROR(MPORT_ERR_SQLITE, sqlite3_errmsg(db));
+ goto DONE;
}
}
Index: version_cmp.c
===================================================================
RCS file: /home/cvs/src/lib/libmport/version_cmp.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -L lib/libmport/version_cmp.c -L lib/libmport/version_cmp.c -u -r1.1 -r1.2
--- lib/libmport/version_cmp.c
+++ lib/libmport/version_cmp.c
@@ -28,6 +28,7 @@
#include <string.h>
#include <stdlib.h>
+#include <ctype.h>
#include "mport.h"
__MBSDID("$MidnightBSD$");
@@ -40,6 +41,7 @@
static void parse_version(const char *, struct version *);
static int cmp_versions(char *, char *);
+static int cmp_ints(int, int);
int mport_version_cmp(const char *astr, const char *bstr)
{
@@ -52,9 +54,11 @@
/* remember that a.version/b.version are useless after calling
cmp_versions (but astr and bstr are unchanged.) */
- result = (a.epoch < b.epoch)
- || cmp_versions(a.version, b.version)
- || (a.revision < b.revision);
+ if ((result = cmp_ints(a.epoch, b.epoch)) == 0) {
+ if ((result = cmp_versions(a.version, b.version)) == 0) {
+ result = cmp_ints(a.revision, b.revision);
+ }
+ }
free(a.version);
free(b.version);
@@ -88,43 +92,55 @@
v->version = s;
}
-static int cmp_versions(char *a, char *b)
+static int cmp_ints(int a, int b)
{
- char *a_dot, *b_dot;
- int a_sub, b_sub;
-
- while (1) {
- a_dot = index(a, '.');
- b_dot = index(b, '.');
+ if (a == b)
+ return 0;
+ if (a < b)
+ return -1;
- if (a_dot == NULL && b_dot == NULL) {
- a_sub = (int)strtol(a, NULL, 10);
- b_sub = (int)strtol(b, NULL, 10);
-
- return a_sub < b_sub;
- } else if (a_dot == NULL) {
- /* b has more sub versions, must be more recent */
- return -1;
- } else if (b_dot == NULL) {
- /* a has more sub versions, must be more recent */
- return 1;
+ return 1;
+}
+
+static int cmp_versions(char *a, char *b)
+{
+ int a_sub, b_sub, result;
+
+ while (*a || *b) {
+ if (*a) {
+ while (*a == '.' || *a == '+')
+ a++;
+
+ if (isdigit(*a)) {
+ a_sub = (int)strtol(a, &a, 10);
+ } else {
+ a_sub = (int)*a;
+ a++;
+ }
} else {
- *a_dot = *b_dot = '\0';
-
- a_sub = (int)strtol(a, NULL, 10);
- b_sub = (int)strtol(b, NULL, 10);
+ a_sub = 0;
+ }
- if (a_sub != b_sub) {
- return a_sub < b_sub;
+ if (*b) {
+ while (*b == '.' || *b == '+')
+ b++;
+
+ if (isdigit(*b)) {
+ b_sub = (int)strtol(b, &b, 10);
+ } else {
+ b_sub = (int)*b;
+ b++;
}
-
- /* this pair of sub versions matched. Go to the next pair. */
- a = a_dot + 1;
- b = b_dot + 1;
+ } else {
+ b_sub = 0;
}
+
+ result = cmp_ints(a_sub, b_sub);
+
+ if (result != 0)
+ break;
}
+
- /* not reached */
- return 0;
-}
-
+ return result;
+}
Index: db_util.c
===================================================================
RCS file: /home/cvs/src/lib/libmport/db_util.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -L lib/libmport/db_util.c -L lib/libmport/db_util.c -u -r1.1 -r1.2
--- lib/libmport/db_util.c
+++ lib/libmport/db_util.c
@@ -51,7 +51,7 @@
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));
More information about the Midnightbsd-cvs
mailing list