Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add a quick and dirty pkg convert able to convert the database from/to pkg_install
Baptiste Daroussin committed 13 years ago
commit e67fbd2e13235f17c8548b1088e4d7870d032248
parent 8627cf7
8 files changed +216 -9
modified libpkg/pkg.h.in
@@ -1171,4 +1171,6 @@ int pkg_get_myarch(char *pkgarch, size_t sz);
void pkgdb_cmd(int argc, char **argv);
int pkg_old_load_from_path(struct pkg *pkg, const char *path);
int pkg_old_emit_content(struct pkg *pkg, char **dest);
+
int pkg_from_old(struct pkg *pkg);
+
int pkg_to_old(struct pkg *pkg);
#endif
modified libpkg/pkg_create.c
@@ -72,8 +72,13 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,

		if ((pkg_sum == NULL || pkg_sum[0] == '\0') &&
		    lstat(fpath, &st) == 0 && !S_ISLNK(st.st_mode)) {
-
			if (sha256_file(fpath, sha256) != EPKG_OK)
-
				return (EPKG_FATAL);
+
			if (pkg->type == PKG_OLD_FILE) {
+
				if (md5_file(fpath, sha256) != EPKG_OK)
+
					return (EPKG_FATAL);
+
			} else {
+
				if (sha256_file(fpath, sha256) != EPKG_OK)
+
					return (EPKG_FATAL);
+
			}
			strlcpy(file->sum, sha256, sizeof(file->sum));
		}
	}
@@ -82,11 +87,24 @@ pkg_create_from_dir(struct pkg *pkg, const char *root,
	 * Register shared libraries used by the package if SHLIBS
	 * enabled in conf.  Deletes shlib info if not.
	 */
-
	pkg_register_shlibs(pkg);
+
	if (pkg->type == PKG_OLD_FILE) {
+
		const char *desc, *display, *comment;

-
	pkg_emit_manifest(pkg, &m);
-
	packing_append_buffer(pkg_archive, m, "+MANIFEST", strlen(m));
-
	free(m);
+
		pkg_old_emit_content(pkg, &m);
+
		packing_append_buffer(pkg_archive, m, "+CONTENTS", strlen(m));
+
		free(m);
+

+
		pkg_get(pkg, PKG_DESC, &desc, PKG_MESSAGE, &display, PKG_COMMENT, &comment);
+
		packing_append_buffer(pkg_archive, desc, "+DESC", strlen(desc));
+
		packing_append_buffer(pkg_archive, display, "+DISPLAY", strlen(display));
+
		packing_append_buffer(pkg_archive, comment, "+COMMENT", strlen(comment));
+
	} else {
+
		pkg_register_shlibs(pkg);
+

+
		pkg_emit_manifest(pkg, &m);
+
		packing_append_buffer(pkg_archive, m, "+MANIFEST", strlen(m));
+
		free(m);
+
	}

	pkg_get(pkg, PKG_MTREE, &mtree);
	if (mtree != NULL)
@@ -136,7 +154,8 @@ pkg_create_archive(const char *outdir, struct pkg *pkg, pkg_formats format,
	/*
	 * Ensure that we have all the information we need
	 */
-
	assert((pkg->flags & required_flags) == required_flags);
+
	if (pkg->type != PKG_OLD_FILE)
+
		assert((pkg->flags & required_flags) == required_flags);

	if (mkdirs(outdir) != EPKG_OK)
		return NULL;
modified libpkg/pkg_old.c
@@ -148,7 +148,7 @@ pkg_old_emit_content(struct pkg *pkg, char **dest)
		     pkg_file_cksum(file));
	}

-
	while (pkg_dirs(pkg, &dir)) {
+
	while (pkg_dirs(pkg, &dir) == EPKG_OK) {
		if (pkg_dir_try(dir)) {
			sbuf_printf(content,
			    "@dirrm %s\n",
@@ -166,3 +166,31 @@ pkg_old_emit_content(struct pkg *pkg, char **dest)

	return (EPKG_OK);
}
+

+
int
+
pkg_to_old(struct pkg *p)
+
{
+
	struct pkg_file *f = NULL;
+
	char md5[MD5_DIGEST_LENGTH * 2 + 1];
+

+
	p->type = PKG_OLD_FILE;
+
	while (pkg_files(p, &f) == EPKG_OK)
+
		if (md5_file(pkg_file_path(f), md5) == EPKG_OK)
+
			strlcpy(f->sum, md5, sizeof(f->sum));
+

+
	return (EPKG_OK);
+
}
+

+
int
+
pkg_from_old(struct pkg *p __unused)
+
{
+
	struct pkg_file *f = NULL;
+
	char sha256[SHA256_DIGEST_LENGTH * 2 + 1];
+

+
	p->type = PKG_INSTALLED;
+
	while (pkg_files(p, &f) == EPKG_OK)
+
		if (sha256_file(pkg_file_path(f), sha256) == EPKG_OK)
+
			strlcpy(f->sum, sha256, sizeof(f->sum));
+

+
	return (EPKG_OK);
+
}
modified pkg/Makefile
@@ -5,6 +5,7 @@ SRCS= add.c \
		backup.c \
		check.c \
		clean.c \
+
		convert.c \
		create.c \
		delete.c \
		event.c \
added pkg/convert.c
@@ -0,0 +1,152 @@
+
/*-
+
 * Copyright (c) 2012 Baptiste Daroussin <bapt@FreeBSD.org>
+
 * 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
+
 *    in this position and unchanged.
+
 * 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(S) ``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(S) 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/param.h>
+
#include <sys/stat.h>
+

+
#include <string.h>
+
#include <sysexits.h>
+

+
#include <pkg.h>
+

+
#include "pkgcli.h"
+

+
void
+
usage_convert(void)
+
{
+
	fprintf(stderr, "usage pkg convert [-r]\n\n");
+
	fprintf(stderr, "For more informations see 'pkg help convert'.\n");
+
}
+

+
static int
+
convert_to_old(void)
+
{
+
	struct pkgdb *db = NULL;
+
	struct pkg *pkg = NULL;
+
	struct pkgdb_it *it = NULL;
+
	char *content, *name, *version, *buf;
+
	int ret = EX_OK;
+
	char path[MAXPATHLEN];
+
	int query_flags = PKG_LOAD_DEPS | PKG_LOAD_FILES |
+
	    PKG_LOAD_DIRS | PKG_LOAD_SCRIPTS |
+
	    PKG_LOAD_OPTIONS | PKG_LOAD_MTREE |
+
	    PKG_LOAD_USERS | PKG_LOAD_GROUPS;
+
	FILE *fp;
+

+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
		pkgdb_close(db);
+
		return (EX_IOERR);
+
	}
+

+
	if ((it = pkgdb_query(db, NULL, MATCH_ALL)) == NULL) {
+
		ret = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	while (pkgdb_it_next(it, &pkg, query_flags) == EPKG_OK) {
+
		pkg_to_old(pkg);
+
		pkg_old_emit_content(pkg, &content);
+
		pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version);
+
		printf("Converting %s-%s...", name, version);
+
		snprintf(path, MAXPATHLEN, "/var/db/pkg/%s-%s", name, version);
+
		mkdir(path, 0755);
+

+
		snprintf(path, MAXPATHLEN, "/var/db/pkg/%s-%s/+CONTENTS", name, version);
+
		fp = fopen(path, "w");
+
		fputs(content, fp);
+
		fclose(fp);
+

+
		pkg_get(pkg, PKG_DESC, &buf);
+
		snprintf(path, MAXPATHLEN, "/var/db/pkg/%s-%s/+DESC", name, version);
+
		fp = fopen(path, "w");
+
		fputs(buf, fp);
+
		fclose(fp);
+

+
		pkg_get(pkg, PKG_COMMENT, &buf);
+
		snprintf(path, MAXPATHLEN, "/var/db/pkg/%s-%s/+COMMENT", name, version);
+
		fp = fopen(path, "w");
+
		fputs(buf, fp);
+
		fclose(fp);
+

+
		pkg_get(pkg, PKG_MESSAGE, &buf);
+
		if (buf != NULL) {
+
			snprintf(path, MAXPATHLEN, "/var/db/pkg/%s-%s/+DISPLAY", name, version);
+
			fp = fopen(path, "w");
+
			fputs(buf, fp);
+
			fclose(fp);
+
		}
+

+
		pkg_get(pkg, PKG_MTREE, &buf);
+
		if (buf != NULL) {
+
			snprintf(path, MAXPATHLEN, "/var/db/pkg/%s-%s/+MTREE_DIRS", name, version);
+
			fp = fopen(path, "w");
+
			fputs(buf, fp);
+
			fclose(fp);
+
		}
+
		/* TODO scripts + required_by */
+
		printf("done.\n");
+

+
		free(content);
+
	}
+

+
cleanup:
+
	pkg_free(pkg);
+
	pkgdb_it_free(it);
+
	pkgdb_close(db);
+

+
	return (ret);
+
}
+

+
static int
+
convert_from_old(void)
+
{
+
	return (EX_OK);
+
}
+

+
int
+
exec_convert(int argc, char **argv)
+
{
+
	bool revert = false;
+

+
	if (argc > 2) {
+
		usage_convert();
+
		return (EX_USAGE);
+
	}
+

+
	if (argc == 2) {
+
		if (strcmp(argv[1], "-r") == 0)
+
			revert = true;
+
		else {
+
			usage_convert();
+
			return (EX_USAGE);
+
		}
+
	}
+

+
	if (revert)
+
		return (convert_from_old());
+
	else
+
		return (convert_to_old());
+
}
modified pkg/create.c
@@ -73,7 +73,7 @@ pkg_create_matches(int argc, char **argv, match_t match, pkg_formats fmt,
	struct pkg *pkg = NULL;
	struct pkgdb *db = NULL;
	struct pkgdb_it *it = NULL;
-
	int query_flags = PKG_LOAD_DEPS | PKG_LOAD_FILES | 
+
	int query_flags = PKG_LOAD_DEPS | PKG_LOAD_FILES |
	    PKG_LOAD_CATEGORIES | PKG_LOAD_DIRS | PKG_LOAD_SCRIPTS |
	    PKG_LOAD_OPTIONS | PKG_LOAD_MTREE | PKG_LOAD_LICENSES |
	    PKG_LOAD_USERS | PKG_LOAD_GROUPS | PKG_LOAD_SHLIBS;
modified pkg/main.c
@@ -69,6 +69,7 @@ static struct commands {
	{ "backup", "Backs-up and restores the local package database", exec_backup, usage_backup},
	{ "check", "Checks for missing dependencies and database consistency", exec_check, usage_check},
	{ "clean", "Cleans old packages from the cache", exec_clean, usage_clean},
+
	{ "convert", "Convert database from/to pkgng", exec_convert, usage_convert},
	{ "create", "Creates software package distributions", exec_create, usage_create},
	{ "delete", "Deletes packages from the database and the system", exec_delete, usage_delete},
	{ "fetch", "Fetches packages from a remote repository", exec_fetch, usage_fetch},
modified pkg/pkgcli.h
@@ -158,6 +158,10 @@ void usage_version(void);
int exec_which(int, char **);
void usage_which(void);

+
/* pkg convert */
+
int exec_convert(int, char **);
+
void usage_convert(void);
+

/* utils */

/* These are the fields of the Full output, in order */