Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
WIP for ./pkg create
jlaffaye committed 15 years ago
commit 4fde78b38045092df426de0608f126c846470ff7
parent d4ac798
8 files changed +113 -34
modified libpkg/pkg.c
@@ -39,6 +39,13 @@ pkg_origin(struct pkg *pkg)
}

int
+
pkg_dep(struct pkg *pkg, struct pkg *dep)
+
{
+
	pkg_reset(dep);
+
	return (pkgdb_query_dep(pkg, dep));
+
}
+

+
int
pkg_rdep(struct pkg *pkg, struct pkg *rdep)
{
	pkg_reset(rdep);
@@ -46,16 +53,16 @@ pkg_rdep(struct pkg *pkg, struct pkg *rdep)
}

int
-
pkg_files(struct pkg *pkg, const char **path)
+
pkg_conflicts(struct pkg *pkg, struct pkg *conflict)
{
-
	return (pkgdb_query_files(pkg, path));
+
	pkg_reset(conflict);
+
	return (pkgdb_query_rdep(pkg, conflict));
}

int
-
pkg_dep(struct pkg *pkg, struct pkg *dep)
+
pkg_files(struct pkg *pkg, const char **path, const char **md5)
{
-
	pkg_reset(dep);
-
	return (pkgdb_query_dep(pkg, dep));
+
	return (pkgdb_query_files(pkg, path, md5));
}

int
modified libpkg/pkg.h
@@ -25,7 +25,8 @@ const char *pkg_desc(struct pkg *);
const char *pkg_origin(struct pkg *);
int pkg_dep(struct pkg *, struct pkg *);
int pkg_rdep(struct pkg *, struct pkg *);
-
int pkg_files(struct pkg *, const char **);
+
int pkg_conflicts(struct pkg*, struct pkg*);
+
int pkg_files(struct pkg *, const char **, const char **);

/* pkgdb */
int pkgdb_open(struct pkgdb **);
modified libpkg/pkg_create.c
@@ -14,33 +14,34 @@

#define METADATA_GLOB "+{DEINSTALL,INSTALL,MTREE_DIRS}"

-
static int pkg_create_from_dir(const char *, const char *, struct archive *, struct pkg *);
+
static int pkg_create_from_dir(struct pkg_manifest *, const char *, struct archive *);
static const char * pkg_create_set_format(struct archive *, pkg_formats);

static int
-
pkg_create_from_dir(const char *mpath, const char *root, struct archive *pkg_archive, struct pkg *pkg)
+
pkg_create_from_dir(struct pkg_manifest *m, const char *root, struct archive *pkg_archive)
{
	struct archive_entry *entry;
+
#if 0
	char *path;
	char glob_pattern[MAXPATHLEN + sizeof(METADATA_GLOB)];
	glob_t g;
+
#endif
	int fd;
-
	size_t len, i = 0;
+
	size_t len/*, i = 0*/;
	char buf[BUFSIZ];
	char *buffer;
	size_t buffer_len;
	char fpath[MAXPATHLEN];
	const char *filepath;
	struct archive *ar;
-
	struct pkg_manifest *m;

	ar = archive_read_disk_new();
	archive_read_disk_set_standard_lookup(ar);

	entry = archive_entry_new();

-
	pkg_manifest_from_pkg(pkg, &m);
-

+
/* TODO: Add metadatas */
+
#if 0	
	path = strdup(mpath);
	buffer = strrchr(path, '/');
	buffer[0] = '\0';
@@ -67,15 +68,11 @@ pkg_create_from_dir(const char *mpath, const char *root, struct archive *pkg_arc
		}
	}
	globfree(&g);
-

-
	/* TODO: remove automatic */
+
#endif
	buffer = pkg_manifest_dump_buffer(m);
	buffer_len = strlen(buffer);

-
	archive_entry_copy_sourcepath(entry, mpath);
-
	if (archive_read_disk_entry_from_file(ar, entry, -1, 0) != ARCHIVE_OK)
-
		warnx("archive_read_disk_entry_form_file(%s): %s", mpath, archive_error_string(ar));
-

+
	archive_entry_set_filetype(entry, AE_IFREG);
	archive_entry_set_pathname(entry, "+MANIFEST");
	archive_entry_set_size(entry, buffer_len);
	archive_write_header(pkg_archive, entry);
@@ -106,7 +103,6 @@ pkg_create_from_dir(const char *mpath, const char *root, struct archive *pkg_arc
		archive_entry_clear(entry);
	}

-
	pkg_manifest_free(m);
	archive_entry_free(entry);
	archive_read_finish(ar);

@@ -114,16 +110,14 @@ pkg_create_from_dir(const char *mpath, const char *root, struct archive *pkg_arc
}

int
-
pkg_create(const char *mpath, pkg_formats format, const char *outdir, const char *rootdir, struct pkg *lpkg)
+
pkg_create(const char *mpath, pkg_formats format, const char *outdir, const char *rootdir, struct pkg *pkg)
{
	char namever[FILENAME_MAX];
	struct archive *pkg_archive;
	char archive_path[MAXPATHLEN];
-
	struct pkg *pkg;
	const char *ext;
	struct pkg_manifest *m;

-
	pkg = lpkg;
	pkg_archive = archive_write_new();

	if ((ext = pkg_create_set_format(pkg_archive, format)) == NULL) {
@@ -132,9 +126,12 @@ pkg_create(const char *mpath, pkg_formats format, const char *outdir, const char
		return (-1);
	}

-
	if (pkg == NULL) {
+
	if (pkg == NULL && mpath != NULL) {
		m = pkg_manifest_load_file(mpath);
-
		pkg_from_manifest(pkg, m);
+
	} else if (pkg != NULL) {
+
		pkg_manifest_from_pkg(pkg, &m);
+
	} else {
+
		err(1, "mpath or pkg required");
	}

	snprintf(namever, sizeof(namever), "%s-%s", pkg_name(pkg), pkg_version(pkg));
@@ -144,8 +141,9 @@ pkg_create(const char *mpath, pkg_formats format, const char *outdir, const char
	archive_write_set_format_pax_restricted(pkg_archive);
	archive_write_open_filename(pkg_archive, archive_path);

-
	pkg_create_from_dir(mpath, rootdir, pkg_archive, pkg);
+
	pkg_create_from_dir(m, rootdir, pkg_archive);

+
	pkg_manifest_free(m);
	archive_write_close(pkg_archive);
	archive_write_finish(pkg_archive);

modified libpkg/pkg_manifest.c
@@ -391,9 +391,29 @@ pkg_manifest_load_file(const char *path)
int
pkg_manifest_from_pkg(struct pkg *pkg, struct pkg_manifest **m)
{
-
	/* TODO */
-
	(void)pkg;
-
	(void)m;
+
	struct pkg *dep;
+
	const char *path;
+
	const char *md5;
+

+
	*m = pkg_manifest_new();
+

+
	pkg_manifest_add_value(*m, "name", pkg_name(pkg));
+
	pkg_manifest_add_value(*m, "version", pkg_version(pkg));
+
	pkg_manifest_add_value(*m, "origin", pkg_version(pkg));
+
	pkg_manifest_add_value(*m, "comment", pkg_comment(pkg));
+
	pkg_manifest_add_value(*m, "desc", pkg_desc(pkg));
+

+
	pkg_new(&dep);
+
	while (pkg_dep(pkg, dep) == 0) {
+
		pkg_manifest_add_dep(*m, pkg_name(dep), pkg_origin(dep), pkg_version(dep));
+
	}
+
	pkg_free(dep);
+

+
	while (pkg_files(pkg, &path, &md5) == 0) {
+
		pkg_manifest_add_file(*m, path, md5);
+
	}
+

+
	/* TODO: conflicts, exec, unexec */
	return (0);
}

modified libpkg/pkg_private.h
@@ -12,6 +12,7 @@ struct pkg {
	struct pkgdb *pdb;
	sqlite3_stmt *deps_stmt;
	sqlite3_stmt *rdeps_stmt;
+
	sqlite3_stmt *conflicts_stmt;
	sqlite3_stmt *which_stmt;
	sqlite3_stmt *files_stmt;
	struct pkg_manifest *m;
modified libpkg/pkgdb.c
@@ -149,7 +149,13 @@ pkgdb_init(sqlite3 *sdb)
		"md5 TEXT,"
		"package_id TEXT"
	");"
-
	"CREATE INDEX files_package ON files (package_id);";
+
	"CREATE INDEX files_package ON files (package_id);"
+
	"CREATE TABLE conflicts ("
+
		"name TEXT,"
+
		"package_id TEXT,"
+
		"PRIMARY KEY (package_id,name)"
+
	");"
+
	"CREATE INDEX conflicts_package ON conflicts (package_id);";

	if (sqlite3_exec(sdb, sql, NULL, NULL, &errmsg) != SQLITE_OK)
		errx(EXIT_FAILURE, "sqlite3_exec(): %s", errmsg);
@@ -159,8 +165,10 @@ pkgdb_init(sqlite3 *sdb)
	struct pkg_manifest *m;
	sqlite3_stmt *stmt_pkg;
	sqlite3_stmt *stmt_dep;
+
	sqlite3_stmt *stmt_conflicts;
	sqlite3_stmt *stmt_file;
	const char *dbdir;
+
	const char *conflict;
	char mpath[MAXPATHLEN];
	int nb_pkg;
	int i;
@@ -178,6 +186,10 @@ pkgdb_init(sqlite3 *sdb)
			"VALUES (?1, ?2, ?3, ?4);",
			-1, &stmt_dep, NULL);

+
	sqlite3_prepare(sdb, "INSERT INTO conflicts (name, package_id)"
+
			"VALUES (?1, ?2, ?3, ?4);",
+
			-1, &stmt_conflicts, NULL);
+

	sqlite3_prepare(sdb, "INSERT INTO files (path, md5, package_id)"
			"VALUES (?1, ?2, ?3);",
			-1, &stmt_file, NULL);
@@ -207,6 +219,16 @@ pkgdb_init(sqlite3 *sdb)
			sqlite3_reset(stmt_dep);
		}

+
		pkg_manifest_conflict_init(m);
+
		while ((conflict = pkg_manifest_conflict_next(m)) != NULL) {
+
			sqlite3_bind_text(stmt_conflicts, 1, conflict, -1, SQLITE_STATIC);
+
			sqlite3_bind_text(stmt_conflicts, 2, pkg_manifest_value(m, "origin"), -1,
+
							  SQLITE_STATIC);
+

+
			sqlite3_step(stmt_conflicts);
+
			sqlite3_reset(stmt_conflicts);
+
		}
+

		pkg_manifest_file_init(m);
		while (pkg_manifest_file_next(m) == 0) {
			sqlite3_bind_text(stmt_file, 1, pkg_manifest_file_path(m), -1, SQLITE_STATIC);
@@ -224,6 +246,7 @@ pkgdb_init(sqlite3 *sdb)

	sqlite3_finalize(stmt_pkg);
	sqlite3_finalize(stmt_dep);
+
	sqlite3_finalize(stmt_conflicts);
	sqlite3_finalize(stmt_file);

	sqlite3_exec(sdb, "COMMIT;", NULL, NULL, NULL);
@@ -412,18 +435,44 @@ pkgdb_query_rdep(struct pkg *pkg, struct pkg *rdep) {
}

int
-
pkgdb_query_files(struct pkg *pkg, const char **path) {
+
pkgdb_query_conflicts(struct pkg *pkg, struct pkg *conflict) {
+
	int retcode;
+

+
	if (pkg->conflicts_stmt == NULL) {
+
		sqlite3_prepare(pkg->pdb->sqlite,
+
						"SELECT name, origin, version FROM conflicts "
+
						"WHERE package_id = ?1;", -1, &pkg->files_stmt, NULL);
+
		sqlite3_bind_text(pkg->files_stmt, 1, pkg->origin, -1, SQLITE_STATIC);
+
	}
+

+
	retcode = sqlite3_step(pkg->files_stmt);
+
	if (retcode == SQLITE_ROW) {
+
		conflict->name = sqlite3_column_text(pkg->files_stmt, 0);
+
		conflict->origin = sqlite3_column_text(pkg->files_stmt, 1);
+
		conflict->version = sqlite3_column_text(pkg->files_stmt, 2);
+
		return (0);
+
	} else if (retcode == SQLITE_DONE) {
+
		sqlite3_reset(pkg->files_stmt);
+
		return (1);
+
	} else {
+
		return (-1);
+
	}
+
}
+

+
int
+
pkgdb_query_files(struct pkg *pkg, const char **path, const char **md5) {
	int retcode;

	if (pkg->files_stmt == NULL) {
		sqlite3_prepare(pkg->pdb->sqlite,
-
						"SELECT path from files where package_id = ?1;", -1, &pkg->files_stmt, NULL);
+
						"SELECT path, md5 FROM files WHERE package_id = ?1;", -1, &pkg->files_stmt, NULL);
		sqlite3_bind_text(pkg->files_stmt, 1, pkg->origin, -1, SQLITE_STATIC);
	}

	retcode = sqlite3_step(pkg->files_stmt);
	if (retcode == SQLITE_ROW) {
		*path = sqlite3_column_text(pkg->files_stmt, 0);
+
		*md5 = sqlite3_column_text(pkg->files_stmt, 1);
		return (0);
	} else if (retcode == SQLITE_DONE) {
		sqlite3_reset(pkg->files_stmt);
modified libpkg/pkgdb.h
@@ -10,7 +10,8 @@ struct pkgdb {

int pkgdb_query_dep(struct pkg *, struct pkg *);
int pkgdb_query_rdep(struct pkg *, struct pkg *);
-
int pkgdb_query_files(struct pkg *, const char **);
+
int pkgdb_query_conflicts(struct pkg *, struct pkg *);
+
int pkgdb_query_files(struct pkg *, const char **, const char **);
void pkgdb_set_error(struct pkgdb *, int, const char *, ...);

#endif
modified pkg/info.c
@@ -17,9 +17,10 @@ pkg_size(struct pkg *pkg)
{
	struct stat st;
	const char *path;
+
	const char *md5;
	int64_t size = 0;

-
	while(pkg_files(pkg, &path) == 0) {
+
	while(pkg_files(pkg, &path, &md5) == 0) {
		if (stat(path, &st) != 0) {
			warn("stat(%s)", path);
			continue;
@@ -41,6 +42,7 @@ cmd_info(int argc, char **argv)
	struct pkg *pkg;
	struct pkg *dep;
	const char *path;
+
	const char *md5;
	unsigned char opt = 0;
	char size[7];
	match_t match = MATCH_EXACT;
@@ -115,7 +117,7 @@ cmd_info(int argc, char **argv)
			printf("\n");
		} else if (opt & INFO_LIST_FILES) {
			printf("%s-%s owns the following files:\n", pkg_name(pkg), pkg_version(pkg));
-
			while (pkg_files(pkg, &path) == 0) {
+
			while (pkg_files(pkg, &path, &md5) == 0) {
				printf("%s\n", path);
			}
		} else if (opt & INFO_SIZE) {