Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Moved getters functions from pkgdb.c to pkg.c
jlaffaye committed 15 years ago
commit 62437a234c945192584b89d7066832714ec5dd0f
parent 1bb05a0
6 files changed +251 -250
modified libpkg/Makefile
@@ -10,6 +10,7 @@ SRCS= pkg.c \
		pkgdb.c \
		pkgdb_cache.c \
		pkg_compat.c \
+
		pkg_create.c \
		pkg_manifest.c \
		util.c

modified libpkg/pkg.c
@@ -1,189 +1,69 @@
-
#include <sys/stat.h>
-
#include <sys/param.h>
-

-
#include <archive.h>
-
#include <archive_entry.h>
-
#include <err.h>
-
#include <stdlib.h>
-
#include <glob.h>
-
#include <libgen.h>
-
#include <string.h>
-
#include <fcntl.h>
-

-
#include "pkg_manifest.h"
#include "pkgdb.h"
-
#include "util.h"
-

-
#define METADATA_GLOB "+{DEINSTALL,INSTALL,MTREE_DIRS}"
+
#include "pkgdb_cache.h"

-
static int pkg_create_from_dir(char *, const char *, struct archive *);
-

-
static int
-
pkg_create_from_dir(char *path, const char *root, struct archive *pkg_archive)
+
static const char *
+
pkg_getattr(struct pkg *pkg, const char **val, const char *attr)
{
-
	struct archive_entry *entry;
-
	char glob_pattern[MAXPATHLEN + sizeof(METADATA_GLOB)];
-
	glob_t g;
-
	int fd;
-
	size_t len, i = 0;
-
	char buf[BUFSIZ];
-
	char *buffer;
-
	size_t buffer_len;
-
	char mpath[MAXPATHLEN];
-
	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);
-

-
	snprintf(mpath, sizeof(mpath), "%s+MANIFEST", path);
-
	entry = archive_entry_new();
-

-
	if ((m = pkg_manifest_load_file(mpath)) == NULL)
-
		errx(EXIT_FAILURE, "Can not continue without manifest");
-

-
	/* Add the metadatas */
-
	snprintf(glob_pattern, sizeof(glob_pattern), "%s"METADATA_GLOB, path);
-
	
-
	if (glob(glob_pattern, GLOB_NOSORT|GLOB_BRACE, NULL, &g) == 0) {
-
		for ( i = 0; i < g.gl_pathc; i++) {
-
			fd = open(g.gl_pathv[i], O_RDONLY);
-
			archive_entry_copy_sourcepath(entry, g.gl_pathv[i]);
-

-
			if (archive_read_disk_entry_from_file(ar, entry, -1, 0) != ARCHIVE_OK)
-
				warnx("archive_read_disk_entry_from_file(): %s", archive_error_string(ar));
+
	if (*val == NULL)
+
		*val = pkgdb_cache_getattr(pkg, attr);

-
			archive_entry_set_pathname(entry, basename(g.gl_pathv[i]));
-
			archive_write_header(pkg_archive, entry);
-
			while ( (len = read(fd, buf, sizeof(buf))) > 0 )
-
				archive_write_data(pkg_archive, buf, len);
-

-
			close(fd);
-
			archive_entry_clear(entry);
-
		}
-
	}
-
	globfree(&g);
-

-
	/* TODO: remove automatic */
-
	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", archive_error_string(ar));
-

-
	archive_entry_set_pathname(entry, "+MANIFEST");
-
	archive_entry_set_size(entry, buffer_len);
-
	archive_write_header(pkg_archive, entry);
-
	archive_write_data(pkg_archive, buffer, buffer_len);
-
	archive_entry_clear(entry);
-

-
	free(buffer);
+
	return (*val);
+
}

-
	pkg_manifest_file_init(m);
-
	while (pkg_manifest_file_next(m) == 0) {
-
		filepath = pkg_manifest_file_path(m);
-
		snprintf(fpath, sizeof(MAXPATHLEN), "%s/%s", root, filepath);
-
		archive_entry_copy_sourcepath(entry, filepath);
+
const char *
+
pkg_namever(struct pkg *pkg)
+
{
+
	return (pkg_getattr(pkg, &pkg->namever, "namever"));
+
}

-
		if (archive_read_disk_entry_from_file(ar, entry, -1, 0) != ARCHIVE_OK)
-
			warnx("archive_read_disk_entry_from_file(): %s", archive_error_string(ar));
+
const char *
+
pkg_name(struct pkg *pkg)
+
{
+
	return (pkg_getattr(pkg, &pkg->name, "name"));
+
}

-
		archive_entry_set_pathname(entry, filepath);
-
		archive_write_header(pkg_archive, entry);
-
		fd = open(filepath, O_RDONLY);
-
		if (fd != -1) {
-
			while ( (len = read(fd, buf, sizeof(buf))) > 0 )
-
				archive_write_data(pkg_archive, buf, len);
+
const char *
+
pkg_version(struct pkg *pkg)
+
{
+
	return (pkg_getattr(pkg, &pkg->version, "version"));
+
}

-
			close(fd);
-
		}
-
		archive_entry_clear(entry);
-
	}
+
const char *
+
pkg_comment(struct pkg *pkg)
+
{
+
	return (pkg_getattr(pkg, &pkg->comment, "comment"));
+
}

-
	pkg_manifest_free(m);
-
	archive_entry_free(entry);
-
	archive_read_finish(ar);
+
const char *
+
pkg_desc(struct pkg *pkg)
+
{
+
	return (pkg_getattr(pkg, &pkg->comment, "comment"));
+
}

-
	return (0);
+
const char *
+
pkg_origin(struct pkg *pkg)
+
{
+
	return (pkg_getattr(pkg, &pkg->origin, "origin"));
}

int
-
pkg_create(char *pkgname, pkg_formats format, const char *outdir, const char *rootdir)
+
pkg_dep(struct pkg *pkg, struct pkg *dep)
{
-
	struct pkgdb db;
-
	struct pkg pkg;
-
	const char *pkg_dbdir;
-
	char pkgpath[MAXPATHLEN];
-
	struct archive *pkg_archive;
-
	char archive_path[MAXPATHLEN];
-
	const char *ext;
-

-
	switch (format) {
-
		case TAR:
-
			ext = "tar";
-
			break;
-
		case TGZ:
-
			ext = "tgz";
-
			break;
-
		case TBZ:
-
			ext = "tbz";
-
			break;
-
		case TXZ:
-
			ext = "txz";
-
			break;
-
		default:
-
			warnx("Unsupport format");
-
			return (-1);
-
			break; /* NOT REACHED */
-
	}
-

-
	pkg_dbdir = pkgdb_get_dir();
-

-
	if (pkgdb_init(&db, pkgname, MATCH_EXACT) == -1) {
-
		pkgdb_warn(&db);
-
		return (-1);
-
	}
-

-
	if (pkgdb_query(&db, &pkg) != 0) {
-
		if (db.errnum > -1)
-
			pkgdb_warn(&db);
-
		warnx("%s: no such package", pkgname);
-
		pkgdb_free(&db);
-
		return (-1);
-
	}
-

-
	printf("Creating package %s/%s.%s\n", outdir, pkg_namever(&pkg), ext);
-
	snprintf(pkgpath, sizeof(pkgpath), "%s/%s/", pkg_dbdir, pkg_namever(&pkg));
-
	snprintf(archive_path, sizeof(archive_path), "%s/%s.%s", outdir, pkg_namever(&pkg), ext);
-

-
	pkg_archive = archive_write_new();
-

-
	switch (format) {
-
		case TAR:
-
			archive_write_set_compression_none(pkg_archive);
-
			break;
-
		case TGZ:
-
			archive_write_set_compression_gzip(pkg_archive);
-
			break;
-
		case TBZ:
-
			archive_write_set_compression_bzip2(pkg_archive);
-
			break;
-
		case TXZ:
-
			if (archive_write_set_compression_lzma(pkg_archive) != ARCHIVE_OK) {
-
				warnx("%s", archive_error_string(pkg_archive));
-
			}
-
			break;
-
	}
-

-
	archive_write_set_format_pax_restricted(pkg_archive);
-
	archive_write_open_filename(pkg_archive, archive_path);
-
	pkg_create_from_dir(pkgpath, rootdir, pkg_archive);
-
	archive_write_close(pkg_archive);
-
	archive_write_finish(pkg_archive);
+
	/* call backend dep query */
+
	return (pkgdb_cache_dep(pkg, dep));
+
}

-
	pkgdb_free(&db);
-
	return (0);
+
void
+
pkg_reset(struct pkg *pkg)
+
{
+
	pkg->namever = NULL;
+
	pkg->name = NULL;
+
	pkg->version = NULL;
+
	pkg->origin = NULL;
+
	pkg->comment = NULL;
+
	pkg->desc = NULL;
+
	pkg->idx = -1;
+
	pkg->idep = 0;
+
	pkg->irdep = 0;
+
	pkg->pdb = NULL;
}
modified libpkg/pkg.h
@@ -40,6 +40,14 @@ struct pkg {
	struct pkgdb *pdb;
};

+
void pkg_reset(struct pkg *);
+
const char *pkg_namever(struct pkg *);
+
const char *pkg_name(struct pkg *);
+
const char *pkg_version(struct pkg *);
+
const char *pkg_comment(struct pkg *);
+
const char *pkg_desc(struct pkg *);
+
const char *pkg_origin(struct pkg *);
+
int pkg_dep(struct pkg *, struct pkg *);

typedef enum pkg_formats { TAR, TGZ, TBZ, TXZ } pkg_formats;
int pkg_create(char *, pkg_formats, const char *, const char *);
added libpkg/pkg_create.c
@@ -0,0 +1,189 @@
+
#include <sys/stat.h>
+
#include <sys/param.h>
+

+
#include <archive.h>
+
#include <archive_entry.h>
+
#include <err.h>
+
#include <stdlib.h>
+
#include <glob.h>
+
#include <libgen.h>
+
#include <string.h>
+
#include <fcntl.h>
+

+
#include "pkg_manifest.h"
+
#include "pkgdb.h"
+
#include "util.h"
+

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

+
static int pkg_create_from_dir(char *, const char *, struct archive *);
+

+
static int
+
pkg_create_from_dir(char *path, const char *root, struct archive *pkg_archive)
+
{
+
	struct archive_entry *entry;
+
	char glob_pattern[MAXPATHLEN + sizeof(METADATA_GLOB)];
+
	glob_t g;
+
	int fd;
+
	size_t len, i = 0;
+
	char buf[BUFSIZ];
+
	char *buffer;
+
	size_t buffer_len;
+
	char mpath[MAXPATHLEN];
+
	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);
+

+
	snprintf(mpath, sizeof(mpath), "%s+MANIFEST", path);
+
	entry = archive_entry_new();
+

+
	if ((m = pkg_manifest_load_file(mpath)) == NULL)
+
		errx(EXIT_FAILURE, "Can not continue without manifest");
+

+
	/* Add the metadatas */
+
	snprintf(glob_pattern, sizeof(glob_pattern), "%s"METADATA_GLOB, path);
+
	
+
	if (glob(glob_pattern, GLOB_NOSORT|GLOB_BRACE, NULL, &g) == 0) {
+
		for ( i = 0; i < g.gl_pathc; i++) {
+
			fd = open(g.gl_pathv[i], O_RDONLY);
+
			archive_entry_copy_sourcepath(entry, g.gl_pathv[i]);
+

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

+
			archive_entry_set_pathname(entry, basename(g.gl_pathv[i]));
+
			archive_write_header(pkg_archive, entry);
+
			while ( (len = read(fd, buf, sizeof(buf))) > 0 )
+
				archive_write_data(pkg_archive, buf, len);
+

+
			close(fd);
+
			archive_entry_clear(entry);
+
		}
+
	}
+
	globfree(&g);
+

+
	/* TODO: remove automatic */
+
	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", archive_error_string(ar));
+

+
	archive_entry_set_pathname(entry, "+MANIFEST");
+
	archive_entry_set_size(entry, buffer_len);
+
	archive_write_header(pkg_archive, entry);
+
	archive_write_data(pkg_archive, buffer, buffer_len);
+
	archive_entry_clear(entry);
+

+
	free(buffer);
+

+
	pkg_manifest_file_init(m);
+
	while (pkg_manifest_file_next(m) == 0) {
+
		filepath = pkg_manifest_file_path(m);
+
		snprintf(fpath, sizeof(MAXPATHLEN), "%s/%s", root, filepath);
+
		archive_entry_copy_sourcepath(entry, filepath);
+

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

+
		archive_entry_set_pathname(entry, filepath);
+
		archive_write_header(pkg_archive, entry);
+
		fd = open(filepath, O_RDONLY);
+
		if (fd != -1) {
+
			while ( (len = read(fd, buf, sizeof(buf))) > 0 )
+
				archive_write_data(pkg_archive, buf, len);
+

+
			close(fd);
+
		}
+
		archive_entry_clear(entry);
+
	}
+

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

+
	return (0);
+
}
+

+
int
+
pkg_create(char *pkgname, pkg_formats format, const char *outdir, const char *rootdir)
+
{
+
	struct pkgdb db;
+
	struct pkg pkg;
+
	const char *pkg_dbdir;
+
	char pkgpath[MAXPATHLEN];
+
	struct archive *pkg_archive;
+
	char archive_path[MAXPATHLEN];
+
	const char *ext;
+

+
	switch (format) {
+
		case TAR:
+
			ext = "tar";
+
			break;
+
		case TGZ:
+
			ext = "tgz";
+
			break;
+
		case TBZ:
+
			ext = "tbz";
+
			break;
+
		case TXZ:
+
			ext = "txz";
+
			break;
+
		default:
+
			warnx("Unsupport format");
+
			return (-1);
+
			break; /* NOT REACHED */
+
	}
+

+
	pkg_dbdir = pkgdb_get_dir();
+

+
	if (pkgdb_init(&db, pkgname, MATCH_EXACT) == -1) {
+
		pkgdb_warn(&db);
+
		return (-1);
+
	}
+

+
	if (pkgdb_query(&db, &pkg) != 0) {
+
		if (db.errnum > -1)
+
			pkgdb_warn(&db);
+
		warnx("%s: no such package", pkgname);
+
		pkgdb_free(&db);
+
		return (-1);
+
	}
+

+
	printf("Creating package %s/%s.%s\n", outdir, pkg_namever(&pkg), ext);
+
	snprintf(pkgpath, sizeof(pkgpath), "%s/%s/", pkg_dbdir, pkg_namever(&pkg));
+
	snprintf(archive_path, sizeof(archive_path), "%s/%s.%s", outdir, pkg_namever(&pkg), ext);
+

+
	pkg_archive = archive_write_new();
+

+
	switch (format) {
+
		case TAR:
+
			archive_write_set_compression_none(pkg_archive);
+
			break;
+
		case TGZ:
+
			archive_write_set_compression_gzip(pkg_archive);
+
			break;
+
		case TBZ:
+
			archive_write_set_compression_bzip2(pkg_archive);
+
			break;
+
		case TXZ:
+
			if (archive_write_set_compression_lzma(pkg_archive) != ARCHIVE_OK) {
+
				warnx("%s", archive_error_string(pkg_archive));
+
			}
+
			break;
+
	}
+

+
	archive_write_set_format_pax_restricted(pkg_archive);
+
	archive_write_open_filename(pkg_archive, archive_path);
+
	pkg_create_from_dir(pkgpath, rootdir, pkg_archive);
+
	archive_write_close(pkg_archive);
+
	archive_write_finish(pkg_archive);
+

+
	pkgdb_free(&db);
+
	return (0);
+
}
modified libpkg/pkgdb.c
@@ -20,73 +20,6 @@
#define PKGDB_LOCK "lock"
#define PKG_DBDIR "/var/db/pkg"

-
static const char *
-
pkg_getattr(struct pkg *pkg, const char **val, const char *attr)
-
{
-
	if (*val == NULL)
-
		*val = pkgdb_cache_getattr(pkg, attr);
-

-
	return (*val);
-
}
-

-
const char *
-
pkg_namever(struct pkg *pkg)
-
{
-
	return (pkg_getattr(pkg, &pkg->namever, "namever"));
-
}
-

-
const char *
-
pkg_name(struct pkg *pkg)
-
{
-
	return (pkg_getattr(pkg, &pkg->name, "name"));
-
}
-

-
const char *
-
pkg_version(struct pkg *pkg)
-
{
-
	return (pkg_getattr(pkg, &pkg->version, "version"));
-
}
-

-
const char *
-
pkg_comment(struct pkg *pkg)
-
{
-
	return (pkg_getattr(pkg, &pkg->comment, "comment"));
-
}
-

-
const char *
-
pkg_desc(struct pkg *pkg)
-
{
-
	return (pkg_getattr(pkg, &pkg->comment, "comment"));
-
}
-

-
const char *
-
pkg_origin(struct pkg *pkg)
-
{
-
	return (pkg_getattr(pkg, &pkg->origin, "origin"));
-
}
-

-
int
-
pkg_dep(struct pkg *pkg, struct pkg *dep)
-
{
-
	/* call backend dep query */
-
	return (pkgdb_cache_dep(pkg, dep));
-
}
-

-
void
-
pkg_reset(struct pkg *pkg)
-
{
-
	pkg->namever = NULL;
-
	pkg->name = NULL;
-
	pkg->version = NULL;
-
	pkg->origin = NULL;
-
	pkg->comment = NULL;
-
	pkg->desc = NULL;
-
	pkg->idx = -1;
-
	pkg->idep = 0;
-
	pkg->irdep = 0;
-
	pkg->pdb = NULL;
-
}
-

const char *
pkgdb_get_dir(void)
{
modified libpkg/pkgdb.h
@@ -3,15 +3,6 @@

#include <pkg.h>

-
/* getters */
-
const char *pkg_namever(struct pkg *);
-
const char *pkg_name(struct pkg *);
-
const char *pkg_version(struct pkg *);
-
const char *pkg_comment(struct pkg *);
-
const char *pkg_desc(struct pkg *);
-
const char *pkg_origin(struct pkg *);
-
int pkg_dep(struct pkg *, struct pkg *);
-

/* query */
int pkgdb_init(struct pkgdb *, const char *, match_t);
int pkgdb_query(struct pkgdb *, struct pkg *);
@@ -20,7 +11,6 @@ void pkgdb_free(struct pkgdb *);
/* misc */
const char *pkgdb_get_dir(void);
int pkgdb_match(struct pkgdb *, const char *);
-
void pkg_reset(struct pkg *);
int pkgdb_lock(struct pkgdb *, int);
void pkgdb_set_error(struct pkgdb *, int, const char *, ...);
void pkgdb_warn(struct pkgdb *);