Skip to content

Commit

Permalink
refactor version checks
Browse files Browse the repository at this point in the history
  • Loading branch information
laffer1 committed Oct 17, 2021
1 parent d0a3c6a commit cb2b1dd
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 63 deletions.
15 changes: 11 additions & 4 deletions libexec/mport.create/mport.create.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@ int main(int argc, char *argv[])
{
int ch;
int plist_seen = 0;
mportInstance *mport = mport_instance_new();
mportPackageMeta *pack = mport_pkgmeta_new();
mportCreateExtras *extra = mport_createextras_new();
mportAssetList *assetlist = mport_assetlist_new();
FILE *fp;
struct tm expDate;

if (pack == NULL || extra == NULL || assetlist == NULL) {
errx(1, "Failed to allocate memory");
if (mport == NULL || pack == NULL || extra == NULL || assetlist == NULL) {
errx(EXIT_FAILURE, "Failed to allocate memory");
}

// we need this to know if the user customized the "target_os" configuration.
// the caveat is that the userland it was built against could be wrong.
if (mport_instance_init(mport, NULL) != MPORT_OK) {
errx(EXIT_FAILURE, mport_err_string());
}

while ((ch = getopt(argc, argv, "C:D:E:M:O:P:S:c:d:e:f:i:j:l:m:n:o:p:r:s:t:v:x:")) != -1) {
Expand Down Expand Up @@ -170,9 +177,9 @@ int main(int argc, char *argv[])
usage();
}

if (mport_create_primative(assetlist, pack, extra) != MPORT_OK) {
if (mport_create_primative(mport, assetlist, pack, extra) != MPORT_OK) {
warnx("%s", mport_err_string());
exit(1);
exit(EXIT_FAILURE);
}

return 0;
Expand Down
11 changes: 4 additions & 7 deletions libexec/mport.list/mport.list.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,13 @@ main(int argc, char *argv[])
}

mport = mport_instance_new();
os_release = mport_get_osrelease_setting(mport);
if (os_release == NULL) {
os_release = mport_get_osrelease();
}

if (mport_instance_init(mport, NULL) != MPORT_OK) {
warnx("%s", mport_err_string());
exit(1);
exit(EXIT_FAILURE);
}

os_release = mport_get_osrelease(mport);

if (update && mport_index_load(mport) != MPORT_OK) {
warnx("Unable to load updates index, %s", mport_err_string());
exit(8);
Expand All @@ -105,7 +102,7 @@ main(int argc, char *argv[])
if (mport_pkgmeta_list(mport, &packs) != MPORT_OK) {
warnx("%s", mport_err_string());
mport_instance_free(mport);
exit(1);
exit(EXIT_FAILURE);
}

if (packs == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion libmport/bundle_read_update_pkg.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static int make_backup_bundle(mportInstance *mport, mportPackageMeta *pkg, char

extra->is_backup = true;

ret = mport_create_primative(alist, pkg, extra);
ret = mport_create_primative(mport, alist, pkg, extra);

mport_assetlist_free(alist);
mport_createextras_free(extra);
Expand Down
20 changes: 4 additions & 16 deletions libmport/check_preconditions.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ static int check_if_installed(mportInstance *mport, mportPackageMeta *pack)
/* Row was found */
inst_version = sqlite3_column_text(stmt, 0);
os_release = sqlite3_column_text(stmt, 1);
system_os_release = (char *) mport_get_osrelease_setting(mport);
if (system_os_release == NULL) {
system_os_release = (char *) mport_get_osrelease();
}
system_os_release = (char *) mport_get_osrelease(mport);

/* Different os release version should not be considered the same package */
if (strcmp(os_release, system_os_release) != 0) {
Expand Down Expand Up @@ -198,10 +195,7 @@ static int check_depends(mportInstance *mport, mportPackageMeta *pack)
RETURN_CURRENT_ERROR;
}

system_os_release = (char *) mport_get_osrelease_setting(mport);
if (system_os_release == NULL) {
system_os_release = (char *) mport_get_osrelease();
}
system_os_release = (char *) mport_get_osrelease(mport);

while (1) {
ret = sqlite3_step(stmt);
Expand Down Expand Up @@ -302,10 +296,7 @@ check_if_older_installed(mportInstance *mport, mportPackageMeta *pkg)
int ret;
const char *os_release;

os_release = mport_get_osrelease_setting(mport);
if (os_release == NULL) {
os_release = mport_get_osrelease();
}
os_release = mport_get_osrelease(mport);

if (mport_db_prepare(mport->db, &stmt,
"SELECT os_release FROM packages WHERE pkg=%Q and ((mport_version_cmp(version, %Q) < 0 and os_release=%Q) or os_release != %Q)",
Expand Down Expand Up @@ -337,10 +328,7 @@ check_if_older_os(mportInstance *mport, mportPackageMeta *pkg)
int ret;
const char *os_release;

os_release = mport_get_osrelease_setting(mport);
if (os_release == NULL) {
os_release = mport_get_osrelease();
}
os_release = mport_get_osrelease(mport);

if (mport_db_prepare(mport->db, &stmt,
"SELECT os_release FROM packages WHERE pkg=%Q and mport_version_cmp(os_release, %Q) < 0",
Expand Down
11 changes: 5 additions & 6 deletions libmport/create_primative.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static int create_stub_db(sqlite3 **, const char *);

static int insert_assetlist(sqlite3 *, mportAssetList *, mportPackageMeta *, mportCreateExtras *);

static int insert_meta(sqlite3 *, mportPackageMeta *, mportCreateExtras *);
static int insert_meta(mportInstance *, sqlite3 *, mportPackageMeta *, mportCreateExtras *);

static int insert_depends(sqlite3 *, mportPackageMeta *, mportCreateExtras *);

Expand All @@ -61,9 +61,8 @@ static int archive_assetlistfiles(mportBundleWrite *, mportPackageMeta *, mportC

static int clean_up(const char *);


MPORT_PUBLIC_API int
mport_create_primative(mportAssetList *assetlist, mportPackageMeta *pack, mportCreateExtras *extra)
mport_create_primative(mportInstance *mport, mportAssetList *assetlist, mportPackageMeta *pack, mportCreateExtras *extra)
{
int error_code = MPORT_OK;

Expand All @@ -83,7 +82,7 @@ mport_create_primative(mportAssetList *assetlist, mportPackageMeta *pack, mportC
if ((error_code = insert_assetlist(db, assetlist, pack, extra)) != MPORT_OK)
goto CLEANUP;

if ((error_code = insert_meta(db, pack, extra)) != MPORT_OK)
if ((error_code = insert_meta(mport, db, pack, extra)) != MPORT_OK)
goto CLEANUP;

if (sqlite3_close(db) != SQLITE_OK) {
Expand Down Expand Up @@ -229,15 +228,15 @@ insert_assetlist(sqlite3 *db, mportAssetList *assetlist, mportPackageMeta *pack,
}

static int
insert_meta(sqlite3 *db, mportPackageMeta *pack, mportCreateExtras *extra)
insert_meta(mportInstance *mport, sqlite3 *db, mportPackageMeta *pack, mportCreateExtras *extra)
{
int error_code = MPORT_OK;

sqlite3_stmt *stmnt = NULL;
const char *rest = 0;
char sql[] = "INSERT INTO packages (pkg, version, origin, lang, prefix, comment, os_release, cpe, deprecated, expiration_date, no_provide_shlib, flavor) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";

char *os_release = mport_get_osrelease();
char *os_release = mport_get_osrelease(mport);
if (pack->cpe == NULL) {
pack->cpe = malloc(1 * sizeof(char));
pack->cpe[0] = '\0';
Expand Down
2 changes: 1 addition & 1 deletion libmport/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ mport_generate_stub_schema(sqlite3 *db)
char *ptr;
char *sql;

ptr = mport_get_osrelease();
ptr = mport_get_osrelease(mport);
if (ptr == NULL)
RETURN_ERROR(MPORT_ERR_FATAL, "OS Release could not be determined");
asprintf(&sql, "INSERT INTO meta VALUES (\"os_release\", \"%s\")", ptr);
Expand Down
7 changes: 3 additions & 4 deletions libmport/mport.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ typedef struct {
mportCreateExtras * mport_createextras_new(void);
void mport_createextras_free(mportCreateExtras *);

int mport_create_primative(mportAssetList *, mportPackageMeta *, mportCreateExtras *);
int mport_create_primative(mportInstance *, mportAssetList *, mportPackageMeta *, mportCreateExtras *);

/* Merge primative */
int mport_merge_primative(const char **, const char *);
Expand Down Expand Up @@ -269,9 +269,8 @@ int mport_setting_set(mportInstance *, const char *, const char *);
void mport_parselist(char *, char ***);
int mport_verify_hash(const char *, const char *);
int mport_file_exists(const char *);
char * mport_version(void);
char * mport_get_osrelease(void);
char * mport_get_osrelease_setting(mportInstance *);
char * mport_version(mportInstance *);
char * mport_get_osrelease(mportInstance *);

/* Locks */
enum _LockState {
Expand Down
24 changes: 9 additions & 15 deletions libmport/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,14 @@ mport_decompress_bzip2(const char *input, const char *output)
}

MPORT_PUBLIC_API char *
mport_get_osrelease(void)
mport_get_osrelease(mportInstance *mport)
{
char *version;
char *version = NULL;

// honor settings first
if (mport != NULL) {
version = mport_setting_get(mport, MPORT_SETTING_TARGET_OS);
}

// try midnightbsd-version
if (version == NULL) {
Expand All @@ -598,17 +603,6 @@ mport_get_osrelease(void)
return version;
}

MPORT_PUBLIC_API char *
mport_get_osrelease_setting(mportInstance *mport)
{
char *version;

// honor settings first
version = mport_setting_get(mport, MPORT_SETTING_TARGET_OS);

return version;
}

static char *
mport_get_osrelease_kern(void)
{
Expand Down Expand Up @@ -709,10 +703,10 @@ mport_get_osrelease_userland(void) {


MPORT_PUBLIC_API char *
mport_version(void)
mport_version(mportInstance *mport)
{
char *version;
char *osrel = mport_get_osrelease();
char *osrel = mport_get_osrelease(mport);
asprintf(&version, "mport %s for MidnightBSD %s, Bundle Version %s\n",
MPORT_VERSION, osrel, MPORT_BUNDLE_VERSION_STR);
free(osrel);
Expand Down
20 changes: 11 additions & 9 deletions mport/mport.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

static void usage(void);

static void show_version(void);
static void show_version(mportInstance *);

static void loadIndex(mportInstance *);

Expand Down Expand Up @@ -119,11 +119,6 @@ main(int argc, char *argv[]) {
argc -= optind;
argv += optind;

if (version == 1) {
show_version();
exit(EXIT_SUCCESS);
}

if (chroot_path != NULL) {
if (chroot(chroot_path) == -1) {
err(EXIT_FAILURE, "chroot failed");
Expand All @@ -136,6 +131,12 @@ main(int argc, char *argv[]) {
errx(1, "%s", mport_err_string());
}

if (version == 1) {
show_version(mport);
mport_instance_free(mport);
exit(EXIT_SUCCESS);
}

if (!strcmp(argv[1], "install")) {
if (argc == 2) {
mport_instance_free(mport);
Expand Down Expand Up @@ -303,7 +304,8 @@ main(int argc, char *argv[]) {

void
usage(void) {
show_version();
show_version(NULL);
fprintf(stderr, "OS version is the installed system version and does not reflect config customization.\n")

fprintf(stderr,
"usage: mport <command> args:\n"
Expand Down Expand Up @@ -335,8 +337,8 @@ usage(void) {
}

void
show_version(void) {
char *version = mport_version();
show_version(mportInstance *mport) {
char *version = mport_version(mport);
fprintf(stderr, "%s", version);
free(version);
}
Expand Down

0 comments on commit cb2b1dd

Please sign in to comment.