Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Move private headers to private/ subfolder. Dont use private headers in the frontend.
Julien Laffaye committed 14 years ago
commit b6d30db4465f0465dad7e215b9f663f6550e78be
parent 49e3442
38 files changed +817 -819
modified libpkg/Makefile
@@ -9,7 +9,10 @@ INCLUDEDIR= ${PREFIX}/include
SHLIB_MAJOR=	0

#gr_utils.c has to be deleted as soon as it goes in base
-
SRCS=		pkg.c \
+
SRCS=		backup.c \
+
		fetch.c \
+
		packing.c \
+
		pkg.c \
		pkg_add.c \
		pkg_attributes.c \
		pkg_config.c \
@@ -21,15 +24,12 @@ SRCS= pkg.c \
		pkg_manifest.c \
		pkg_ports.c \
		pkg_repo.c \
-
		pkg_util.c \
		pkg_version.c \
		pkgdb.c \
-
		backup.c \
-
		fetch.c \
-
		packing.c \
-
		scripts.c \
		rcscripts.c \
-
		usergroup.c
+
		scripts.c \
+
		usergroup.c \
+
		utils.c

OSVERSION!=	awk '/^\#define[[:blank:]]__FreeBSD_version/ {print $$3}' /usr/include/sys/param.h

modified libpkg/backup.c
@@ -2,8 +2,8 @@
#include <string.h>

#include "pkg.h"
-
#include "pkg_private.h"
-
#include "pkg_event.h"
+
#include "private/event.h"
+
#include "private/pkg.h"

int
pkgdb_dump(struct pkgdb *db, char *dest)
deleted libpkg/db_upgrades.h
@@ -1,128 +0,0 @@
-
#ifndef _DB_UPGRADES
-
#define _DB_UPGRADES
-

-
static struct db_upgrades {
-
	int version;
-
	const char *sql;
-
} db_upgrades[] = {
-
	{1,
-
	"CREATE TABLE licenses ("
-
		"id INTEGER PRIMARY KEY, "
-
		"license TEXT NOT NULL UNIQUE "
-
	");"
-
	"CREATE TABLE pkg_licenses_assoc ("
-
		"package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
-
			" ON UPDATE CASCADE, "
-
		"license_id INTEGER REFERENCES licenses(id) ON DELETE RESTRICT"
-
			" ON UPDATE RESTRICT, "
-
		"PRIMARY KEY (package_id, license_id)"
-
	");"
-
	"CREATE VIEW pkg_licenses AS SELECT origin, license FROM packages "
-
	"INNER JOIN pkg_licenses_assoc ON packages.id = pkg_licenses_assoc.package_id "
-
	"INNER JOIN licenses ON pkg_licenses_assoc.license_id = licenses.id;"
-
	"CREATE TRIGGER license_insert INSTEAD OF INSERT ON pkg_licenses "
-
		"FOR EACH ROW BEGIN "
-
			"INSERT OR IGNORE INTO licenses(license) values (NEW.license);"
-
			"INSERT INTO pkg_licenses_assoc(package_id, license_id) VALUES "
-
				"((SELECT id FROM packages where origin = NEW.origin), "
-
				"(SELECT id FROM categories WHERE name = NEW.name));"
-
		"END;"
-
	},
-

-
	{2,
-
	"ALTER TABLE packages ADD licenselogic INTEGER NOT NULL DEFAULT(1);"
-
	},
-

-
	{3,
-
	"DROP VIEW pkg_licenses;"
-
	"DROP TRIGGER license_insert;"
-
	"ALTER TABLE licenses RENAME TO todelete;"
-
	"CREATE TABLE licenses (id INTERGER PRIMARY KEY, name TEXT NOT NULL UNIQUE);"
-
	"INSERT INTO licenses(id, name) SELECT id, license FROM todelete;"
-
	"CREATE VIEW pkg_licenses AS SELECT origin, licenses.name FROM packages "
-
	"INNER JOIN pkg_licenses_assoc ON packages.id = pkg_licenses_assoc.package_id "
-
	"INNER JOIN licenses ON pkg_licenses_assoc.license_id = licenses.id;"
-
	"CREATE TRIGGER license_insert INSTEAD OF INSERT ON pkg_licenses "
-
		"FOR EACH ROW BEGIN "
-
			"INSERT OR IGNORE INTO licenses(name) values (NEW.name);"
-
			"INSERT INTO pkg_licenses_assoc(package_id, license_id) VALUES "
-
				"((SELECT id FROM packages where origin = NEW.origin), "
-
				"(SELECT id FROM licenses WHERE name = NEW.name));"
-
		"END;"
-
	"DROP VIEW pkg_mtree;"
-
	"CREATE VIEW pkg_mtree AS "
-
		"SELECT origin, name, version, comment, desc, mtree.content AS "
-
			"mtree, message, arch, osversion, maintainer, www, prefix, "
-
			"flatsize, automatic, licenselogic, pkg_format_version "
-
			"FROM packages "
-
	"INNER JOIN mtree ON packages.mtree_id = mtree.id;"
-
	"DROP TRIGGER pkg_insert;"
-
	"CREATE TRIGGER pkg_insert INSTEAD OF INSERT ON pkg_mtree "
-
		"FOR EACH ROW BEGIN "
-
			"INSERT OR IGNORE INTO mtree (content) VALUES (NEW.mtree);"
-
			"INSERT OR REPLACE INTO packages(origin, name, version, comment, desc, mtree_id, "
-
				"message, arch, osversion, maintainer, www, prefix, flatsize, automatic, licenselogic) "
-
				"VALUES (NEW.origin, NEW.name, NEW.version, NEW.comment, NEW.desc, "
-
				"(SELECT id FROM mtree WHERE content = NEW.mtree), "
-
				"NEW.message, NEW.arch, NEW.osversion, NEW.maintainer, NEW.www, NEW.prefix, "
-
				"NEW.flatsize, NEW.automatic, NEW.licenselogic);"
-
		"END;"
-
	"DROP TABLE todelete;"
-
	},
-

-
	{4,
-
	"DROP VIEW pkg_mtree;"
-
	"DROP TRIGGER CLEAN_MTREE;"
-
	"DROP TRIGGER pkg_insert;"
-
	"DROP VIEW pkg_dirs;"
-
	"DROP TRIGGER dir_insert;"
-
	"ALTER TABLE pkg_dirs_assoc RENAME TO pkg_directories;"
-
	"DROP VIEW pkg_categories;"
-
	"DROP TRIGGER category_insert;"
-
	"ALTER TABLE pkg_categories_assoc RENAME TO pkg_categories;"
-
	"DROP VIEW pkg_licenses;"
-
	"DROP TRIGGER licenses_insert;"
-
	"ALTER TABLE pkg_licenses_assoc RENAME TO pkg_licenses;"
-
	},
-

-
	{5,
-
	"CREATE TABLE users ("
-
		"id INTEGER PRIMARY KEY, "
-
		"name TEXT NOT NULL UNIQUE "
-
	");"
-
	"CREATE TABLE pkg_users ("
-
		"package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
-
			" ON UPDATE CASCADE, "
-
		"user_id INTEGER REFERENCES users(id) ON DELETE RESTRICT"
-
			" ON UPDATE RESTRICT, "
-
		"UNIQUE(package_id, user_id)"
-
	");"
-
	"CREATE TABLE groups ("
-
		"id INTEGER PRIMARY KEY, "
-
		"name TEXT NOT NULL UNIQUE "
-
	");"
-
	"CREATE TABLE pkg_groups ("
-
		"package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
-
			" ON UPDATE CASCADE, "
-
		"group_id INTEGER REFERENCES groups(id) ON DELETE RESTRICT"
-
			" ON UPDATE RESTRICT, "
-
		"UNIQUE(package_id, group_id)"
-
	");"
-
	},
-
	{6,
-
	"ALTER TABLE pkg_directories ADD try INTEGER;"
-
	"UPDATE pkg_directories SET try = 1;"
-
	},
-
	{7,
-
	"CREATE INDEX deporigini on deps(origin);"
-
	},
-
	{8,
-
	"DROP TABLE conflicts;"
-
	},
-

-
	/* Mark the end of the array */
-
	{ -1, NULL },
-

-
};
-

-
#endif
modified libpkg/fetch.c
@@ -11,7 +11,7 @@
#include <fetch.h>

#include "pkg.h"
-
#include "pkg_event.h"
+
#include "private/event.h"

int
pkg_fetch_file(const char *url, const char *dest)
modified libpkg/gr_util.c
@@ -16,7 +16,7 @@
#include <string.h>
#include <unistd.h>

-
#include "gr_util.h"
+
#include "private/gr_util.h"

static int lockfd = -1;
static char group_dir[PATH_MAX];
deleted libpkg/gr_util.h
@@ -1,6 +0,0 @@
-
int gr_copy(int __ffd, int _tfd, const struct group *_gr, struct group *_old_gr);
-
void gr_fini(void);
-
int gr_init(const char *_dir, const char *_master);
-
int gr_lock(void);
-
int gr_mkdb(void);
-
int gr_tmp(int _mdf);
modified libpkg/packing.c
@@ -10,8 +10,8 @@
#include <string.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"

static const char *packing_set_format(struct archive *a, pkg_formats format);

modified libpkg/pkg.c
@@ -11,9 +11,9 @@
#include <sysexits.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
-
#include "pkg_util.h"
+
#include "private/event.h"
+
#include "private/pkg.h"
+
#include "private/utils.h"

static struct _fields {
	int type;
modified libpkg/pkg_add.c
@@ -10,8 +10,8 @@
#include <errno.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"

static int
dep_installed(struct pkg_dep *dep, struct pkgdb *db) {
modified libpkg/pkg_attributes.c
@@ -2,8 +2,8 @@
#include <stdlib.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"

/*
 * Dep
modified libpkg/pkg_config.c
@@ -10,7 +10,7 @@
#include <yaml.h>

#include "pkg.h"
-
#include "pkg_event.h"
+
#include "private/event.h"

#define STRING 0
#define BOOL 1
modified libpkg/pkg_create.c
@@ -12,8 +12,8 @@
#include <fcntl.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"

static int pkg_create_from_dir(struct pkg *, const char *, struct packing *);

modified libpkg/pkg_delete.c
@@ -8,9 +8,9 @@
#include <stdlib.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
-
#include "pkg_util.h"
+
#include "private/event.h"
+
#include "private/pkg.h"
+
#include "private/utils.h"

int
pkg_delete(struct pkg *pkg, struct pkgdb *db, int flags)
modified libpkg/pkg_elf.c
@@ -7,8 +7,8 @@
#include <string.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"

static int
analyse_elf(struct pkgdb *db, struct pkg *pkg, const char *fpath)
modified libpkg/pkg_event.c
@@ -1,7 +1,7 @@
#include <syslog.h>

#include "pkg.h"
-
#include "pkg_event.h"
+
#include "private/event.h"

static pkg_event_cb _cb = NULL;
static void *_data = NULL;
deleted libpkg/pkg_event.h
@@ -1,19 +0,0 @@
-
#ifndef _PKG_EVENT
-
#define _PKG_EVENT
-

-
void pkg_emit_error(const char *fmt, ...);
-
void pkg_emit_errno(const char *func, const char *arg);
-
void pkg_emit_already_installed(struct pkg *p);
-
void pkg_emit_fetching(const char *url, off_t total, off_t done, time_t elapsed);
-
void pkg_emit_install_begin(struct pkg *p);
-
void pkg_emit_install_finished(struct pkg *p);
-
void pkg_emit_deinstall_begin(struct pkg *p);
-
void pkg_emit_deinstall_finished(struct pkg *p);
-
void pkg_emit_upgrade_begin(struct pkg *p);
-
void pkg_emit_upgrade_finished(struct pkg *p);
-
void pkg_emit_missing_dep(struct pkg *p, struct pkg_dep *d);
-
void pkg_emit_required(struct pkg *p, int force);
-
void pkg_emit_integritycheck_begin(void);
-
void pkg_emit_integritycheck_finished(void);
-

-
#endif
modified libpkg/pkg_jobs.c
@@ -9,9 +9,9 @@
#include <string.h>

#include "pkg.h"
-
#include "pkgdb.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"
+
#include "private/pkgdb.h"

int
pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
modified libpkg/pkg_manifest.c
@@ -13,9 +13,9 @@
#include <wchar.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_util.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"
+
#include "private/utils.h"

#define PKG_UNKNOWN -1
#define PKG_DEPS -2
modified libpkg/pkg_ports.c
@@ -13,8 +13,8 @@
#include <yaml.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"


struct hardlinks {
deleted libpkg/pkg_private.h
@@ -1,221 +0,0 @@
-
#ifndef _PKG_PRIVATE_H
-
#define _PKG_PRIVATE_H
-

-
#include <sys/param.h>
-
#include <sys/queue.h>
-
#include <sys/sbuf.h>
-
#include <sys/types.h>
-

-
#include <archive.h>
-
#include <sqlite3.h>
-
#include <openssl/sha.h>
-
#include <stdbool.h>
-

-
#include "pkg_util.h"
-

-
#define PKG_NUM_FIELDS 18
-

-
#define EXTRACT_ARCHIVE_FLAGS  (ARCHIVE_EXTRACT_OWNER |ARCHIVE_EXTRACT_PERM| \
-
		ARCHIVE_EXTRACT_TIME  |ARCHIVE_EXTRACT_ACL | \
-
		ARCHIVE_EXTRACT_FFLAGS|ARCHIVE_EXTRACT_XATTR)
-

-
#define LIST_FREE(head, data, free_func) do { \
-
	while (!STAILQ_EMPTY(head)) { \
-
		data = STAILQ_FIRST(head); \
-
		STAILQ_REMOVE_HEAD(head, next); \
-
		free_func(data); \
-
	}  \
-
	} while (0)
-

-
struct pkg {
-
	struct sbuf * fields[PKG_NUM_FIELDS];
-
	bool automatic;
-
	int64_t flatsize;
-
	int64_t new_flatsize;
-
	int64_t new_pkgsize;
-
	STAILQ_HEAD(categories, pkg_category) categories;
-
	STAILQ_HEAD(licenses, pkg_license) licenses;
-
	STAILQ_HEAD(deps, pkg_dep) deps;
-
	STAILQ_HEAD(rdeps, pkg_dep) rdeps;
-
	STAILQ_HEAD(files, pkg_file) files;
-
	STAILQ_HEAD(dirs, pkg_dir) dirs;
-
	STAILQ_HEAD(scripts, pkg_script) scripts;
-
	STAILQ_HEAD(options, pkg_option) options;
-
	STAILQ_HEAD(users, pkg_user) users;
-
	STAILQ_HEAD(groups, pkg_group) groups;
-
	int flags;
-
	int64_t rowid;
-
	lic_t licenselogic;
-
	pkg_t type;
-
	STAILQ_ENTRY(pkg) next;
-
};
-

-
struct pkg_dep {
-
	struct sbuf *origin;
-
	struct sbuf *name;
-
	struct sbuf *version;
-
	STAILQ_ENTRY(pkg_dep) next;
-
};
-

-
struct pkg_license {
-
	struct sbuf *name;
-
	STAILQ_ENTRY(pkg_license) next;
-
};
-

-
struct pkg_category {
-
	struct sbuf *name;
-
	STAILQ_ENTRY(pkg_category) next;
-
};
-

-
struct pkg_file {
-
	char path[MAXPATHLEN +1];
-
	char sum[SHA256_DIGEST_LENGTH * 2 +1];
-
	char uname[MAXLOGNAME +1];
-
	char gname[MAXLOGNAME +1];
-
	int keep;
-
	mode_t perm;
-
	STAILQ_ENTRY(pkg_file) next;
-
};
-

-
struct pkg_dir {
-
	char path[MAXPATHLEN +1];
-
	char uname[MAXLOGNAME +1];
-
	char gname[MAXLOGNAME +1];
-
	mode_t perm;
-
	int keep;
-
	int try;
-
	STAILQ_ENTRY(pkg_dir) next;
-
};
-

-
struct pkg_script {
-
	struct sbuf *data;
-
	pkg_script_t type;
-
	STAILQ_ENTRY(pkg_script) next;
-
};
-

-
struct pkg_option {
-
	struct sbuf *key;
-
	struct sbuf *value;
-
	STAILQ_ENTRY(pkg_option) next;
-
};
-

-
struct pkg_jobs {
-
	STAILQ_HEAD(jobs, pkg) jobs;
-
	struct pkgdb *db;
-
	pkg_jobs_t type;
-
};
-

-
struct pkg_jobs_node {
-
	struct pkg *pkg;
-
	size_t nrefs;
-
	struct pkg_jobs_node **parents; /* rdeps */
-
	size_t parents_len;
-
	size_t parents_cap;
-
	LIST_ENTRY(pkg_jobs_node) entries;
-
};
-

-
struct pkg_user {
-
	char name[MAXLOGNAME+1];
-
	char uidstr[8192]; /* taken from pw_util.c */
-
	STAILQ_ENTRY(pkg_user) next;
-
};
-

-
struct pkg_group {
-
	char name[MAXLOGNAME+1];
-
	char gidstr[8192]; /* taken from gw_util.c */
-
	STAILQ_ENTRY(pkg_group) next;
-
};
-

-
/**
-
 * Remove and unregister the package.
-
 * @param pkg An installed package to delete
-
 * @param db An opened pkgdb
-
 * @param force If set to one, the function will not fail if the package is
-
 * required by other packages.
-
 * @return An error code.
-
 */
-
int pkg_delete(struct pkg *pkg, struct pkgdb *db, int flags);
-
#define PKG_DELETE_FORCE (1<<0)
-
#define PKG_DELETE_UPGRADE (1<<1)
-

-
int pkg_repo_fetch(struct pkg *pkg);
-

-
int pkg_stop_rc_scripts(struct pkg *);
-
int pkg_start_rc_scripts(struct pkg *);
-

-
int pkg_script_run(struct pkg *, pkg_script_t type);
-

-
int pkg_add_user_group(struct pkg *pkg);
-
int pkg_delete_user_group(struct pkgdb *db, struct pkg *pkg);
-

-
int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae, const char *path, struct sbuf *mbuf);
-

-
void pkg_list_free(struct pkg *, pkg_list);
-

-
int pkg_dep_new(struct pkg_dep **);
-
void pkg_dep_free(struct pkg_dep *);
-

-
int pkg_file_new(struct pkg_file **);
-
void pkg_file_free(struct pkg_file *);
-

-
int pkg_dir_new(struct pkg_dir **);
-
void pkg_dir_free(struct pkg_dir *);
-

-
int pkg_category_new(struct pkg_category **);
-
void pkg_category_free(struct pkg_category *);
-

-
int pkg_license_new(struct pkg_license **);
-
void pkg_license_free(struct pkg_license *);
-

-
int pkg_script_new(struct pkg_script **);
-
void pkg_script_free(struct pkg_script *);
-

-
int pkg_option_new(struct pkg_option **);
-
void pkg_option_free(struct pkg_option *);
-

-
int pkg_user_new(struct pkg_user **);
-
void pkg_user_free(struct pkg_user *);
-

-
int pkg_group_new(struct pkg_group **);
-
void pkg_group_free(struct pkg_group *);
-

-
int pkg_jobs_resolv(struct pkg_jobs *jobs);
-

-
struct packing;
-

-
int packing_init(struct packing **pack, const char *path, pkg_formats format);
-
int packing_append_file(struct packing *pack, const char *filepath, const char *newpath);
-
int packing_append_file_attr(struct packing *pack, const char *filepath, const char *newpath, const char *uname, const char *gname, mode_t perm);
-
int packing_append_buffer(struct packing *pack, const char *buffer, const char *path, int size);
-
int packing_append_tree(struct packing *pack, const char *treepath, const char *newroot);
-
int packing_finish(struct packing *pack);
-
pkg_formats packing_format_from_string(const char *str);
-

-
int pkg_delete_files(struct pkg *pkg, int force);
-
int pkg_delete_dirs(struct pkgdb *db, struct pkg *pkg, int force);
-

-
int pkgdb_is_dir_used(struct pkgdb *db, const char *dir, int64_t *res);
-

-
int pkgdb_integrity_append(struct pkgdb *db, struct pkg *p);
-
int pkgdb_integrity_check(struct pkgdb *db);
-
struct pkgdb_it *pkgdb_integrity_conflict_local(struct pkgdb *db, const char *origin);
-

-
int pkg_set_mtree(struct pkg *, const char *mtree);
-

-
/* pkgdb commands */
-
int sql_exec(sqlite3 *, const char *, ...);
-

-
int pkgdb_load_deps(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_rdeps(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_files(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_dirs(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_scripts(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_options(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_mtree(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_category(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_license(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_user(struct pkgdb *db, struct pkg *pkg);
-
int pkgdb_load_group(struct pkgdb *db, struct pkg *pkg);
-

-

-
#endif
modified libpkg/pkg_repo.c
@@ -18,8 +18,8 @@
#include <openssl/ssl.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"

int
pkg_repo_fetch(struct pkg *pkg)
deleted libpkg/pkg_util.c
@@ -1,328 +0,0 @@
-
#include <sys/stat.h>
-
#include <sys/param.h>
-
#include <stdio.h>
-

-
#include <assert.h>
-
#include <dirent.h>
-
#include <errno.h>
-
#include <fcntl.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <string.h>
-

-
#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_util.h"
-

-
int
-
sbuf_set(struct sbuf **buf, const char *str)
-
{
-
	if (*buf == NULL)
-
		*buf = sbuf_new_auto();
-

-
	if (str == NULL)
-
		return (-1);
-

-
	sbuf_cpy(*buf, str);
-
	sbuf_finish(*buf);
-
	return (0);
-
}
-

-
char *
-
sbuf_get(struct sbuf *buf)
-
{
-

-
	assert(buf != NULL);
-
	if (sbuf_done(buf) == 0)
-
		sbuf_finish(buf);
-

-
	return (sbuf_data(buf));
-
}
-

-
void
-
sbuf_reset(struct sbuf *buf)
-
{
-
	if (buf != NULL) {
-
		sbuf_clear(buf);
-
		sbuf_finish(buf);
-
	}
-
}
-

-
void
-
sbuf_free(struct sbuf *buf)
-
{
-
	if (buf != NULL)
-
		sbuf_delete(buf);
-
}
-

-
int
-
mkdirs(const char *_path)
-
{
-
	char path[MAXPATHLEN + 1];
-
	char *p;
-

-
	strlcpy(path, _path, sizeof(path));
-
	p = path;
-
	if (*p == '/')
-
		p++;
-

-
	for (;;) {
-
		if ((p = strchr(p, '/')) != NULL)
-
			*p = '\0';
-

-
		if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
-
			if (errno != EEXIST && errno != EISDIR) {
-
				pkg_emit_errno("mkdir", path);
-
				return (EPKG_FATAL);
-
			}
-

-
		/* that was the last element of the path */
-
		if (p == NULL)
-
			break;
-

-
		*p = '/';
-
		p++;
-
	}
-

-
	return (EPKG_OK);
-
}
-

-
int
-
file_to_buffer(const char *path, char **buffer, off_t *sz)
-
{
-
	int fd = -1;
-
	struct stat st;
-
	int retcode = EPKG_OK;
-

-
	assert(path != NULL && path[0] != '\0');
-
	assert(buffer != NULL);
-
	assert(sz != NULL);
-

-
	if ((fd = open(path, O_RDONLY)) == -1) {
-
		pkg_emit_errno("open", path);
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	if (fstat(fd, &st) == -1) {
-
		close(fd);
-
		pkg_emit_errno("fstat", path);
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	if ((*buffer = malloc(st.st_size + 1)) == NULL) {
-
		close(fd);
-
		pkg_emit_errno("malloc", "");
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	if (read(fd, *buffer, st.st_size) == -1) {
-
		close(fd);
-
		pkg_emit_errno("read", path);
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
-

-
	cleanup:
-
	if (fd > 0)
-
		close(fd);
-

-
	if (retcode == EPKG_OK) {
-
		(*buffer)[st.st_size] = '\0';
-
		*sz = st.st_size;
-
	} else {
-
		*buffer = NULL;
-
		*sz = -1;
-
	}
-
	return (retcode);
-
}
-

-
int
-
format_exec_cmd(char **dest, const char *in, const char *prefix, const char *plist_file, char *line)
-
{
-
	struct sbuf *buf = sbuf_new_auto();
-
	char path[MAXPATHLEN + 1];
-
	char *cp;
-

-
	while (in[0] != '\0') {
-
		if (in[0] == '%') {
-
			in++;
-
			switch(in[0]) {
-
				case 'D':
-
					sbuf_cat(buf, prefix);
-
					break;
-
				case 'F':
-
					if (plist_file == NULL) {
-
						pkg_emit_error("No files defined %%F couldn't be expanded, ignoring %s", in);
-
						sbuf_finish(buf);
-
						sbuf_free(buf);
-
						return (EPKG_FATAL);
-
					}
-
					sbuf_cat(buf, plist_file);
-
					break;
-
				case 'f':
-
					if (plist_file == NULL) {
-
						pkg_emit_error("No files defined %%f couldn't be expanded, ignoring %s", in);
-
						sbuf_finish(buf);
-
						sbuf_free(buf);
-
						return (EPKG_FATAL);
-
					}
-
					if (prefix[strlen(prefix) - 1] == '/')
-
						snprintf(path, sizeof(path), "%s%s", prefix, plist_file);
-
					else
-
						snprintf(path, sizeof(path), "%s/%s", prefix, plist_file);
-
					cp = strrchr(path, '/');
-
					cp ++;
-
					sbuf_cat(buf, cp);
-
					break;
-
				case 'B':
-
					if (plist_file == NULL) {
-
						pkg_emit_error("No files defined %%B couldn't be expanded, ignoring %s", in);
-
						sbuf_finish(buf);
-
						sbuf_free(buf);
-
						return (EPKG_FATAL);
-
					}
-
					if (prefix[strlen(prefix) - 1] == '/')
-
						snprintf(path, sizeof(path), "%s%s", prefix, plist_file);
-
					else
-
						snprintf(path, sizeof(path), "%s/%s", prefix, plist_file);
-
					cp = strrchr(path, '/');
-
					cp[0] = '\0';
-
					sbuf_cat(buf, path);
-
					break;
-
				case '@':
-
					if (line != NULL) {
-
						sbuf_cat(buf, line);
-
						break;
-
					}
-

-
					/*
-
					 * no break here because if line is not
-
					 * given (default exec) %@ does not
-
					 * exists
-
					 */
-
				default:
-
					sbuf_putc(buf, '%');
-
					sbuf_putc(buf, in[0]);
-
					break;
-
			}
-

-
		} else {
-
			sbuf_putc(buf, in[0]);
-
		}
-

-
		in++;
-
	}
-

-
	sbuf_finish(buf);
-
	*dest = strdup(sbuf_data(buf));
-
	sbuf_free(buf);
-
	
-
	return (EPKG_OK);
-
}
-

-
int
-
split_chr(char *str, char sep)
-
{
-
	char *next;
-
	char *buf = str;
-
	int nbel = 0;
-

-
	while ((next = strchr(buf, sep)) != NULL) {
-
		nbel++;
-
		buf = next;
-
		buf[0] = '\0';
-
		buf++;
-
	}
-

-
	return nbel;
-
}
-

-
int
-
is_dir(const char *path)
-
{
-
	struct stat st;
-

-
	return (stat(path, &st) == 0 && S_ISDIR(st.st_mode));
-
}
-

-
static void
-
sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH], char out[SHA256_DIGEST_LENGTH * 2 + 1])
-
{
-
	int i;
-
	for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
-
		sprintf(out + (i * 2), "%02x", hash[i]);
-

-
	out[SHA256_DIGEST_LENGTH * 2] = '\0';
-
}
-

-
void
-
sha256_str(const char *string, char out[SHA256_DIGEST_LENGTH * 2 + 1])
-
{
-
	unsigned char hash[SHA256_DIGEST_LENGTH];
-
	SHA256_CTX sha256;
-

-
	SHA256_Init(&sha256);
-
	SHA256_Update(&sha256, string, strlen(string));
-
	SHA256_Final(hash, &sha256);
-

-
	sha256_hash(hash, out);
-
}
-

-
int
-
sha256_file(const char *path, char out[SHA256_DIGEST_LENGTH * 2 + 1])
-
{
-
	FILE *fp;
-
	char buffer[BUFSIZ];
-
	unsigned char hash[SHA256_DIGEST_LENGTH];
-
	size_t r = 0;
-
	SHA256_CTX sha256;
-

-
	if ((fp = fopen(path, "rb")) == NULL) {
-
		pkg_emit_errno("fopen", path);
-
		return EPKG_FATAL;
-
	}
-

-
	SHA256_Init(&sha256);
-

-
	while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
-
		SHA256_Update(&sha256, buffer, r);
-

-
	if (ferror(fp) != 0) {
-
		fclose(fp);
-
		out[0] = '\0';
-
		pkg_emit_errno("fread", path);
-
		return EPKG_FATAL;
-
	}
-

-
	fclose(fp);
-

-
	SHA256_Final(hash, &sha256);
-
	sha256_hash(hash, out);
-

-
	return (EPKG_OK);
-
}
-

-
int
-
is_conf_file(const char *path, char *newpath, size_t len)
-
{
-
	size_t n;
-
	char *p = NULL;
-

-
	n = strlen(path);
-

-
	if (n < 8)
-
		return (0);
-

-
	p = strrchr(path, '.');
-

-
	if (p != NULL && !strcmp(p, ".pkgconf")) {
-
		strlcpy(newpath, path, len);
-
		newpath[n - 8] = '\0';
-
		return (1);
-
	}
-

-
	return (0);
-
}
deleted libpkg/pkg_util.h
@@ -1,30 +0,0 @@
-
#ifndef _PKG_UTIL_H
-
#define _PKG_UTIL_H
-

-
#include <sys/types.h>
-
#include <sys/sbuf.h>
-
#include <sys/param.h>
-

-
#include <openssl/sha.h>
-

-
#define STARTS_WITH(string, needle) (strncasecmp(string, needle, strlen(needle)) == 0)
-

-
#define ERROR_SQLITE(db) \
-
	pkg_emit_error("sqlite: %s", sqlite3_errmsg(db))
-

-
int sbuf_set(struct sbuf **, const char *);
-
char * sbuf_get(struct sbuf *);
-
void sbuf_reset(struct sbuf *);
-
void sbuf_free(struct sbuf *);
-

-
int mkdirs(const char *path);
-
int file_to_buffer(const char *, char **, off_t *);
-
int format_exec_cmd(char **, const char *, const char *, const char *, char *);
-
int split_chr(char *, char);
-
int file_fetch(const char *, const char *);
-
int is_dir(const char *);
-
int is_conf_file(const char *path, char *newpath, size_t len);
-

-
int sha256_file(const char *, char[SHA256_DIGEST_LENGTH * 2 +1]);
-
void sha256_str(const char *, char[SHA256_DIGEST_LENGTH * 2 +1]);
-
#endif
modified libpkg/pkg_version.c
@@ -4,7 +4,7 @@
#include <ctype.h>

#include "pkg.h"
-
#include "pkg_event.h"
+
#include "private/event.h"

/*
 * split_version(pkgname, endname, epoch, revision) returns a pointer to
modified libpkg/pkgdb.c
@@ -17,12 +17,12 @@
#include <sqlite3.h>

#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
-
#include "pkgdb.h"
-
#include "pkg_util.h"
+
#include "private/event.h"
+
#include "private/pkg.h"
+
#include "private/pkgdb.h"
+
#include "private/utils.h"

-
#include "db_upgrades.h"
+
#include "private/db_upgrades.h"
#define DBVERSION 8

static struct pkgdb_it * pkgdb_it_new(struct pkgdb *, sqlite3_stmt *, int);
deleted libpkg/pkgdb.h
@@ -1,20 +0,0 @@
-
#ifndef _PKGDB_H
-
#define _PKGDB_H
-

-
#include "pkg.h"
-

-
#include "sqlite3.h"
-

-
struct pkgdb {
-
	sqlite3 *sqlite;
-
	pkgdb_t type;
-
	unsigned int writable :1;
-
};
-

-
struct pkgdb_it {
-
	struct pkgdb *db;
-
	sqlite3_stmt *stmt;
-
	int type;
-
};
-

-
#endif
added libpkg/private/db_upgrades.h
@@ -0,0 +1,128 @@
+
#ifndef _DB_UPGRADES
+
#define _DB_UPGRADES
+

+
static struct db_upgrades {
+
	int version;
+
	const char *sql;
+
} db_upgrades[] = {
+
	{1,
+
	"CREATE TABLE licenses ("
+
		"id INTEGER PRIMARY KEY, "
+
		"license TEXT NOT NULL UNIQUE "
+
	");"
+
	"CREATE TABLE pkg_licenses_assoc ("
+
		"package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
+
			" ON UPDATE CASCADE, "
+
		"license_id INTEGER REFERENCES licenses(id) ON DELETE RESTRICT"
+
			" ON UPDATE RESTRICT, "
+
		"PRIMARY KEY (package_id, license_id)"
+
	");"
+
	"CREATE VIEW pkg_licenses AS SELECT origin, license FROM packages "
+
	"INNER JOIN pkg_licenses_assoc ON packages.id = pkg_licenses_assoc.package_id "
+
	"INNER JOIN licenses ON pkg_licenses_assoc.license_id = licenses.id;"
+
	"CREATE TRIGGER license_insert INSTEAD OF INSERT ON pkg_licenses "
+
		"FOR EACH ROW BEGIN "
+
			"INSERT OR IGNORE INTO licenses(license) values (NEW.license);"
+
			"INSERT INTO pkg_licenses_assoc(package_id, license_id) VALUES "
+
				"((SELECT id FROM packages where origin = NEW.origin), "
+
				"(SELECT id FROM categories WHERE name = NEW.name));"
+
		"END;"
+
	},
+

+
	{2,
+
	"ALTER TABLE packages ADD licenselogic INTEGER NOT NULL DEFAULT(1);"
+
	},
+

+
	{3,
+
	"DROP VIEW pkg_licenses;"
+
	"DROP TRIGGER license_insert;"
+
	"ALTER TABLE licenses RENAME TO todelete;"
+
	"CREATE TABLE licenses (id INTERGER PRIMARY KEY, name TEXT NOT NULL UNIQUE);"
+
	"INSERT INTO licenses(id, name) SELECT id, license FROM todelete;"
+
	"CREATE VIEW pkg_licenses AS SELECT origin, licenses.name FROM packages "
+
	"INNER JOIN pkg_licenses_assoc ON packages.id = pkg_licenses_assoc.package_id "
+
	"INNER JOIN licenses ON pkg_licenses_assoc.license_id = licenses.id;"
+
	"CREATE TRIGGER license_insert INSTEAD OF INSERT ON pkg_licenses "
+
		"FOR EACH ROW BEGIN "
+
			"INSERT OR IGNORE INTO licenses(name) values (NEW.name);"
+
			"INSERT INTO pkg_licenses_assoc(package_id, license_id) VALUES "
+
				"((SELECT id FROM packages where origin = NEW.origin), "
+
				"(SELECT id FROM licenses WHERE name = NEW.name));"
+
		"END;"
+
	"DROP VIEW pkg_mtree;"
+
	"CREATE VIEW pkg_mtree AS "
+
		"SELECT origin, name, version, comment, desc, mtree.content AS "
+
			"mtree, message, arch, osversion, maintainer, www, prefix, "
+
			"flatsize, automatic, licenselogic, pkg_format_version "
+
			"FROM packages "
+
	"INNER JOIN mtree ON packages.mtree_id = mtree.id;"
+
	"DROP TRIGGER pkg_insert;"
+
	"CREATE TRIGGER pkg_insert INSTEAD OF INSERT ON pkg_mtree "
+
		"FOR EACH ROW BEGIN "
+
			"INSERT OR IGNORE INTO mtree (content) VALUES (NEW.mtree);"
+
			"INSERT OR REPLACE INTO packages(origin, name, version, comment, desc, mtree_id, "
+
				"message, arch, osversion, maintainer, www, prefix, flatsize, automatic, licenselogic) "
+
				"VALUES (NEW.origin, NEW.name, NEW.version, NEW.comment, NEW.desc, "
+
				"(SELECT id FROM mtree WHERE content = NEW.mtree), "
+
				"NEW.message, NEW.arch, NEW.osversion, NEW.maintainer, NEW.www, NEW.prefix, "
+
				"NEW.flatsize, NEW.automatic, NEW.licenselogic);"
+
		"END;"
+
	"DROP TABLE todelete;"
+
	},
+

+
	{4,
+
	"DROP VIEW pkg_mtree;"
+
	"DROP TRIGGER CLEAN_MTREE;"
+
	"DROP TRIGGER pkg_insert;"
+
	"DROP VIEW pkg_dirs;"
+
	"DROP TRIGGER dir_insert;"
+
	"ALTER TABLE pkg_dirs_assoc RENAME TO pkg_directories;"
+
	"DROP VIEW pkg_categories;"
+
	"DROP TRIGGER category_insert;"
+
	"ALTER TABLE pkg_categories_assoc RENAME TO pkg_categories;"
+
	"DROP VIEW pkg_licenses;"
+
	"DROP TRIGGER licenses_insert;"
+
	"ALTER TABLE pkg_licenses_assoc RENAME TO pkg_licenses;"
+
	},
+

+
	{5,
+
	"CREATE TABLE users ("
+
		"id INTEGER PRIMARY KEY, "
+
		"name TEXT NOT NULL UNIQUE "
+
	");"
+
	"CREATE TABLE pkg_users ("
+
		"package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
+
			" ON UPDATE CASCADE, "
+
		"user_id INTEGER REFERENCES users(id) ON DELETE RESTRICT"
+
			" ON UPDATE RESTRICT, "
+
		"UNIQUE(package_id, user_id)"
+
	");"
+
	"CREATE TABLE groups ("
+
		"id INTEGER PRIMARY KEY, "
+
		"name TEXT NOT NULL UNIQUE "
+
	");"
+
	"CREATE TABLE pkg_groups ("
+
		"package_id INTEGER REFERENCES packages(id) ON DELETE CASCADE"
+
			" ON UPDATE CASCADE, "
+
		"group_id INTEGER REFERENCES groups(id) ON DELETE RESTRICT"
+
			" ON UPDATE RESTRICT, "
+
		"UNIQUE(package_id, group_id)"
+
	");"
+
	},
+
	{6,
+
	"ALTER TABLE pkg_directories ADD try INTEGER;"
+
	"UPDATE pkg_directories SET try = 1;"
+
	},
+
	{7,
+
	"CREATE INDEX deporigini on deps(origin);"
+
	},
+
	{8,
+
	"DROP TABLE conflicts;"
+
	},
+

+
	/* Mark the end of the array */
+
	{ -1, NULL },
+

+
};
+

+
#endif
added libpkg/private/event.h
@@ -0,0 +1,19 @@
+
#ifndef _PKG_EVENT
+
#define _PKG_EVENT
+

+
void pkg_emit_error(const char *fmt, ...);
+
void pkg_emit_errno(const char *func, const char *arg);
+
void pkg_emit_already_installed(struct pkg *p);
+
void pkg_emit_fetching(const char *url, off_t total, off_t done, time_t elapsed);
+
void pkg_emit_install_begin(struct pkg *p);
+
void pkg_emit_install_finished(struct pkg *p);
+
void pkg_emit_deinstall_begin(struct pkg *p);
+
void pkg_emit_deinstall_finished(struct pkg *p);
+
void pkg_emit_upgrade_begin(struct pkg *p);
+
void pkg_emit_upgrade_finished(struct pkg *p);
+
void pkg_emit_missing_dep(struct pkg *p, struct pkg_dep *d);
+
void pkg_emit_required(struct pkg *p, int force);
+
void pkg_emit_integritycheck_begin(void);
+
void pkg_emit_integritycheck_finished(void);
+

+
#endif
added libpkg/private/gr_util.h
@@ -0,0 +1,6 @@
+
int gr_copy(int __ffd, int _tfd, const struct group *_gr, struct group *_old_gr);
+
void gr_fini(void);
+
int gr_init(const char *_dir, const char *_master);
+
int gr_lock(void);
+
int gr_mkdb(void);
+
int gr_tmp(int _mdf);
added libpkg/private/pkg.h
@@ -0,0 +1,221 @@
+
#ifndef _PKG_PRIVATE_H
+
#define _PKG_PRIVATE_H
+

+
#include <sys/param.h>
+
#include <sys/queue.h>
+
#include <sys/sbuf.h>
+
#include <sys/types.h>
+

+
#include <archive.h>
+
#include <sqlite3.h>
+
#include <openssl/sha.h>
+
#include <stdbool.h>
+

+
#include "private/utils.h"
+

+
#define PKG_NUM_FIELDS 18
+

+
#define EXTRACT_ARCHIVE_FLAGS  (ARCHIVE_EXTRACT_OWNER |ARCHIVE_EXTRACT_PERM| \
+
		ARCHIVE_EXTRACT_TIME  |ARCHIVE_EXTRACT_ACL | \
+
		ARCHIVE_EXTRACT_FFLAGS|ARCHIVE_EXTRACT_XATTR)
+

+
#define LIST_FREE(head, data, free_func) do { \
+
	while (!STAILQ_EMPTY(head)) { \
+
		data = STAILQ_FIRST(head); \
+
		STAILQ_REMOVE_HEAD(head, next); \
+
		free_func(data); \
+
	}  \
+
	} while (0)
+

+
struct pkg {
+
	struct sbuf * fields[PKG_NUM_FIELDS];
+
	bool automatic;
+
	int64_t flatsize;
+
	int64_t new_flatsize;
+
	int64_t new_pkgsize;
+
	STAILQ_HEAD(categories, pkg_category) categories;
+
	STAILQ_HEAD(licenses, pkg_license) licenses;
+
	STAILQ_HEAD(deps, pkg_dep) deps;
+
	STAILQ_HEAD(rdeps, pkg_dep) rdeps;
+
	STAILQ_HEAD(files, pkg_file) files;
+
	STAILQ_HEAD(dirs, pkg_dir) dirs;
+
	STAILQ_HEAD(scripts, pkg_script) scripts;
+
	STAILQ_HEAD(options, pkg_option) options;
+
	STAILQ_HEAD(users, pkg_user) users;
+
	STAILQ_HEAD(groups, pkg_group) groups;
+
	int flags;
+
	int64_t rowid;
+
	lic_t licenselogic;
+
	pkg_t type;
+
	STAILQ_ENTRY(pkg) next;
+
};
+

+
struct pkg_dep {
+
	struct sbuf *origin;
+
	struct sbuf *name;
+
	struct sbuf *version;
+
	STAILQ_ENTRY(pkg_dep) next;
+
};
+

+
struct pkg_license {
+
	struct sbuf *name;
+
	STAILQ_ENTRY(pkg_license) next;
+
};
+

+
struct pkg_category {
+
	struct sbuf *name;
+
	STAILQ_ENTRY(pkg_category) next;
+
};
+

+
struct pkg_file {
+
	char path[MAXPATHLEN +1];
+
	char sum[SHA256_DIGEST_LENGTH * 2 +1];
+
	char uname[MAXLOGNAME +1];
+
	char gname[MAXLOGNAME +1];
+
	int keep;
+
	mode_t perm;
+
	STAILQ_ENTRY(pkg_file) next;
+
};
+

+
struct pkg_dir {
+
	char path[MAXPATHLEN +1];
+
	char uname[MAXLOGNAME +1];
+
	char gname[MAXLOGNAME +1];
+
	mode_t perm;
+
	int keep;
+
	int try;
+
	STAILQ_ENTRY(pkg_dir) next;
+
};
+

+
struct pkg_script {
+
	struct sbuf *data;
+
	pkg_script_t type;
+
	STAILQ_ENTRY(pkg_script) next;
+
};
+

+
struct pkg_option {
+
	struct sbuf *key;
+
	struct sbuf *value;
+
	STAILQ_ENTRY(pkg_option) next;
+
};
+

+
struct pkg_jobs {
+
	STAILQ_HEAD(jobs, pkg) jobs;
+
	struct pkgdb *db;
+
	pkg_jobs_t type;
+
};
+

+
struct pkg_jobs_node {
+
	struct pkg *pkg;
+
	size_t nrefs;
+
	struct pkg_jobs_node **parents; /* rdeps */
+
	size_t parents_len;
+
	size_t parents_cap;
+
	LIST_ENTRY(pkg_jobs_node) entries;
+
};
+

+
struct pkg_user {
+
	char name[MAXLOGNAME+1];
+
	char uidstr[8192]; /* taken from pw_util.c */
+
	STAILQ_ENTRY(pkg_user) next;
+
};
+

+
struct pkg_group {
+
	char name[MAXLOGNAME+1];
+
	char gidstr[8192]; /* taken from gw_util.c */
+
	STAILQ_ENTRY(pkg_group) next;
+
};
+

+
/**
+
 * Remove and unregister the package.
+
 * @param pkg An installed package to delete
+
 * @param db An opened pkgdb
+
 * @param force If set to one, the function will not fail if the package is
+
 * required by other packages.
+
 * @return An error code.
+
 */
+
int pkg_delete(struct pkg *pkg, struct pkgdb *db, int flags);
+
#define PKG_DELETE_FORCE (1<<0)
+
#define PKG_DELETE_UPGRADE (1<<1)
+

+
int pkg_repo_fetch(struct pkg *pkg);
+

+
int pkg_stop_rc_scripts(struct pkg *);
+
int pkg_start_rc_scripts(struct pkg *);
+

+
int pkg_script_run(struct pkg *, pkg_script_t type);
+

+
int pkg_add_user_group(struct pkg *pkg);
+
int pkg_delete_user_group(struct pkgdb *db, struct pkg *pkg);
+

+
int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae, const char *path, struct sbuf *mbuf);
+

+
void pkg_list_free(struct pkg *, pkg_list);
+

+
int pkg_dep_new(struct pkg_dep **);
+
void pkg_dep_free(struct pkg_dep *);
+

+
int pkg_file_new(struct pkg_file **);
+
void pkg_file_free(struct pkg_file *);
+

+
int pkg_dir_new(struct pkg_dir **);
+
void pkg_dir_free(struct pkg_dir *);
+

+
int pkg_category_new(struct pkg_category **);
+
void pkg_category_free(struct pkg_category *);
+

+
int pkg_license_new(struct pkg_license **);
+
void pkg_license_free(struct pkg_license *);
+

+
int pkg_script_new(struct pkg_script **);
+
void pkg_script_free(struct pkg_script *);
+

+
int pkg_option_new(struct pkg_option **);
+
void pkg_option_free(struct pkg_option *);
+

+
int pkg_user_new(struct pkg_user **);
+
void pkg_user_free(struct pkg_user *);
+

+
int pkg_group_new(struct pkg_group **);
+
void pkg_group_free(struct pkg_group *);
+

+
int pkg_jobs_resolv(struct pkg_jobs *jobs);
+

+
struct packing;
+

+
int packing_init(struct packing **pack, const char *path, pkg_formats format);
+
int packing_append_file(struct packing *pack, const char *filepath, const char *newpath);
+
int packing_append_file_attr(struct packing *pack, const char *filepath, const char *newpath, const char *uname, const char *gname, mode_t perm);
+
int packing_append_buffer(struct packing *pack, const char *buffer, const char *path, int size);
+
int packing_append_tree(struct packing *pack, const char *treepath, const char *newroot);
+
int packing_finish(struct packing *pack);
+
pkg_formats packing_format_from_string(const char *str);
+

+
int pkg_delete_files(struct pkg *pkg, int force);
+
int pkg_delete_dirs(struct pkgdb *db, struct pkg *pkg, int force);
+

+
int pkgdb_is_dir_used(struct pkgdb *db, const char *dir, int64_t *res);
+

+
int pkgdb_integrity_append(struct pkgdb *db, struct pkg *p);
+
int pkgdb_integrity_check(struct pkgdb *db);
+
struct pkgdb_it *pkgdb_integrity_conflict_local(struct pkgdb *db, const char *origin);
+

+
int pkg_set_mtree(struct pkg *, const char *mtree);
+

+
/* pkgdb commands */
+
int sql_exec(sqlite3 *, const char *, ...);
+

+
int pkgdb_load_deps(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_rdeps(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_files(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_dirs(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_scripts(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_options(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_mtree(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_category(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_license(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_user(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_load_group(struct pkgdb *db, struct pkg *pkg);
+

+

+
#endif
added libpkg/private/pkgdb.h
@@ -0,0 +1,20 @@
+
#ifndef _PKGDB_H
+
#define _PKGDB_H
+

+
#include "pkg.h"
+

+
#include "sqlite3.h"
+

+
struct pkgdb {
+
	sqlite3 *sqlite;
+
	pkgdb_t type;
+
	unsigned int writable :1;
+
};
+

+
struct pkgdb_it {
+
	struct pkgdb *db;
+
	sqlite3_stmt *stmt;
+
	int type;
+
};
+

+
#endif
added libpkg/private/utils.h
@@ -0,0 +1,30 @@
+
#ifndef _PKG_UTIL_H
+
#define _PKG_UTIL_H
+

+
#include <sys/types.h>
+
#include <sys/sbuf.h>
+
#include <sys/param.h>
+

+
#include <openssl/sha.h>
+

+
#define STARTS_WITH(string, needle) (strncasecmp(string, needle, strlen(needle)) == 0)
+

+
#define ERROR_SQLITE(db) \
+
	pkg_emit_error("sqlite: %s", sqlite3_errmsg(db))
+

+
int sbuf_set(struct sbuf **, const char *);
+
char * sbuf_get(struct sbuf *);
+
void sbuf_reset(struct sbuf *);
+
void sbuf_free(struct sbuf *);
+

+
int mkdirs(const char *path);
+
int file_to_buffer(const char *, char **, off_t *);
+
int format_exec_cmd(char **, const char *, const char *, const char *, char *);
+
int split_chr(char *, char);
+
int file_fetch(const char *, const char *);
+
int is_dir(const char *);
+
int is_conf_file(const char *path, char *newpath, size_t len);
+

+
int sha256_file(const char *, char[SHA256_DIGEST_LENGTH * 2 +1]);
+
void sha256_str(const char *, char[SHA256_DIGEST_LENGTH * 2 +1]);
+
#endif
modified libpkg/rcscripts.c
@@ -8,8 +8,8 @@
#include <unistd.h>

#include "pkg.h"
-
#include "pkg_private.h"
-
#include "pkg_event.h"
+
#include "private/event.h"
+
#include "private/pkg.h"

static int rc_stop(const char *);
static int rc_start(const char *);
modified libpkg/scripts.c
@@ -1,7 +1,7 @@
#include <assert.h>

#include "pkg.h"
-
#include "pkg_private.h"
+
#include "private/pkg.h"

int
pkg_script_run(struct pkg * const pkg, pkg_script_t type)
modified libpkg/usergroup.c
@@ -7,11 +7,11 @@
#include <string.h>

#if __FreeBSD_version < 1000000
-
#include "gr_util.h"
+
#include "private/gr_util.h"
#endif
#include "pkg.h"
-
#include "pkg_event.h"
-
#include "pkg_private.h"
+
#include "private/event.h"
+
#include "private/pkg.h"

int
pkg_add_user_group(struct pkg *pkg)
added libpkg/utils.c
@@ -0,0 +1,328 @@
+
#include <sys/stat.h>
+
#include <sys/param.h>
+
#include <stdio.h>
+

+
#include <assert.h>
+
#include <dirent.h>
+
#include <errno.h>
+
#include <fcntl.h>
+
#include <stdlib.h>
+
#include <unistd.h>
+
#include <string.h>
+

+
#include "pkg.h"
+
#include "private/event.h"
+
#include "private/utils.h"
+

+
int
+
sbuf_set(struct sbuf **buf, const char *str)
+
{
+
	if (*buf == NULL)
+
		*buf = sbuf_new_auto();
+

+
	if (str == NULL)
+
		return (-1);
+

+
	sbuf_cpy(*buf, str);
+
	sbuf_finish(*buf);
+
	return (0);
+
}
+

+
char *
+
sbuf_get(struct sbuf *buf)
+
{
+

+
	assert(buf != NULL);
+
	if (sbuf_done(buf) == 0)
+
		sbuf_finish(buf);
+

+
	return (sbuf_data(buf));
+
}
+

+
void
+
sbuf_reset(struct sbuf *buf)
+
{
+
	if (buf != NULL) {
+
		sbuf_clear(buf);
+
		sbuf_finish(buf);
+
	}
+
}
+

+
void
+
sbuf_free(struct sbuf *buf)
+
{
+
	if (buf != NULL)
+
		sbuf_delete(buf);
+
}
+

+
int
+
mkdirs(const char *_path)
+
{
+
	char path[MAXPATHLEN + 1];
+
	char *p;
+

+
	strlcpy(path, _path, sizeof(path));
+
	p = path;
+
	if (*p == '/')
+
		p++;
+

+
	for (;;) {
+
		if ((p = strchr(p, '/')) != NULL)
+
			*p = '\0';
+

+
		if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO) < 0)
+
			if (errno != EEXIST && errno != EISDIR) {
+
				pkg_emit_errno("mkdir", path);
+
				return (EPKG_FATAL);
+
			}
+

+
		/* that was the last element of the path */
+
		if (p == NULL)
+
			break;
+

+
		*p = '/';
+
		p++;
+
	}
+

+
	return (EPKG_OK);
+
}
+

+
int
+
file_to_buffer(const char *path, char **buffer, off_t *sz)
+
{
+
	int fd = -1;
+
	struct stat st;
+
	int retcode = EPKG_OK;
+

+
	assert(path != NULL && path[0] != '\0');
+
	assert(buffer != NULL);
+
	assert(sz != NULL);
+

+
	if ((fd = open(path, O_RDONLY)) == -1) {
+
		pkg_emit_errno("open", path);
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	if (fstat(fd, &st) == -1) {
+
		close(fd);
+
		pkg_emit_errno("fstat", path);
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	if ((*buffer = malloc(st.st_size + 1)) == NULL) {
+
		close(fd);
+
		pkg_emit_errno("malloc", "");
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	if (read(fd, *buffer, st.st_size) == -1) {
+
		close(fd);
+
		pkg_emit_errno("read", path);
+
		retcode = EPKG_FATAL;
+
		goto cleanup;
+
	}
+

+
	cleanup:
+
	if (fd > 0)
+
		close(fd);
+

+
	if (retcode == EPKG_OK) {
+
		(*buffer)[st.st_size] = '\0';
+
		*sz = st.st_size;
+
	} else {
+
		*buffer = NULL;
+
		*sz = -1;
+
	}
+
	return (retcode);
+
}
+

+
int
+
format_exec_cmd(char **dest, const char *in, const char *prefix, const char *plist_file, char *line)
+
{
+
	struct sbuf *buf = sbuf_new_auto();
+
	char path[MAXPATHLEN + 1];
+
	char *cp;
+

+
	while (in[0] != '\0') {
+
		if (in[0] == '%') {
+
			in++;
+
			switch(in[0]) {
+
				case 'D':
+
					sbuf_cat(buf, prefix);
+
					break;
+
				case 'F':
+
					if (plist_file == NULL) {
+
						pkg_emit_error("No files defined %%F couldn't be expanded, ignoring %s", in);
+
						sbuf_finish(buf);
+
						sbuf_free(buf);
+
						return (EPKG_FATAL);
+
					}
+
					sbuf_cat(buf, plist_file);
+
					break;
+
				case 'f':
+
					if (plist_file == NULL) {
+
						pkg_emit_error("No files defined %%f couldn't be expanded, ignoring %s", in);
+
						sbuf_finish(buf);
+
						sbuf_free(buf);
+
						return (EPKG_FATAL);
+
					}
+
					if (prefix[strlen(prefix) - 1] == '/')
+
						snprintf(path, sizeof(path), "%s%s", prefix, plist_file);
+
					else
+
						snprintf(path, sizeof(path), "%s/%s", prefix, plist_file);
+
					cp = strrchr(path, '/');
+
					cp ++;
+
					sbuf_cat(buf, cp);
+
					break;
+
				case 'B':
+
					if (plist_file == NULL) {
+
						pkg_emit_error("No files defined %%B couldn't be expanded, ignoring %s", in);
+
						sbuf_finish(buf);
+
						sbuf_free(buf);
+
						return (EPKG_FATAL);
+
					}
+
					if (prefix[strlen(prefix) - 1] == '/')
+
						snprintf(path, sizeof(path), "%s%s", prefix, plist_file);
+
					else
+
						snprintf(path, sizeof(path), "%s/%s", prefix, plist_file);
+
					cp = strrchr(path, '/');
+
					cp[0] = '\0';
+
					sbuf_cat(buf, path);
+
					break;
+
				case '@':
+
					if (line != NULL) {
+
						sbuf_cat(buf, line);
+
						break;
+
					}
+

+
					/*
+
					 * no break here because if line is not
+
					 * given (default exec) %@ does not
+
					 * exists
+
					 */
+
				default:
+
					sbuf_putc(buf, '%');
+
					sbuf_putc(buf, in[0]);
+
					break;
+
			}
+

+
		} else {
+
			sbuf_putc(buf, in[0]);
+
		}
+

+
		in++;
+
	}
+

+
	sbuf_finish(buf);
+
	*dest = strdup(sbuf_data(buf));
+
	sbuf_free(buf);
+
	
+
	return (EPKG_OK);
+
}
+

+
int
+
split_chr(char *str, char sep)
+
{
+
	char *next;
+
	char *buf = str;
+
	int nbel = 0;
+

+
	while ((next = strchr(buf, sep)) != NULL) {
+
		nbel++;
+
		buf = next;
+
		buf[0] = '\0';
+
		buf++;
+
	}
+

+
	return nbel;
+
}
+

+
int
+
is_dir(const char *path)
+
{
+
	struct stat st;
+

+
	return (stat(path, &st) == 0 && S_ISDIR(st.st_mode));
+
}
+

+
static void
+
sha256_hash(unsigned char hash[SHA256_DIGEST_LENGTH], char out[SHA256_DIGEST_LENGTH * 2 + 1])
+
{
+
	int i;
+
	for (i = 0; i < SHA256_DIGEST_LENGTH; i++)
+
		sprintf(out + (i * 2), "%02x", hash[i]);
+

+
	out[SHA256_DIGEST_LENGTH * 2] = '\0';
+
}
+

+
void
+
sha256_str(const char *string, char out[SHA256_DIGEST_LENGTH * 2 + 1])
+
{
+
	unsigned char hash[SHA256_DIGEST_LENGTH];
+
	SHA256_CTX sha256;
+

+
	SHA256_Init(&sha256);
+
	SHA256_Update(&sha256, string, strlen(string));
+
	SHA256_Final(hash, &sha256);
+

+
	sha256_hash(hash, out);
+
}
+

+
int
+
sha256_file(const char *path, char out[SHA256_DIGEST_LENGTH * 2 + 1])
+
{
+
	FILE *fp;
+
	char buffer[BUFSIZ];
+
	unsigned char hash[SHA256_DIGEST_LENGTH];
+
	size_t r = 0;
+
	SHA256_CTX sha256;
+

+
	if ((fp = fopen(path, "rb")) == NULL) {
+
		pkg_emit_errno("fopen", path);
+
		return EPKG_FATAL;
+
	}
+

+
	SHA256_Init(&sha256);
+

+
	while ((r = fread(buffer, 1, BUFSIZ, fp)) > 0)
+
		SHA256_Update(&sha256, buffer, r);
+

+
	if (ferror(fp) != 0) {
+
		fclose(fp);
+
		out[0] = '\0';
+
		pkg_emit_errno("fread", path);
+
		return EPKG_FATAL;
+
	}
+

+
	fclose(fp);
+

+
	SHA256_Final(hash, &sha256);
+
	sha256_hash(hash, out);
+

+
	return (EPKG_OK);
+
}
+

+
int
+
is_conf_file(const char *path, char *newpath, size_t len)
+
{
+
	size_t n;
+
	char *p = NULL;
+

+
	n = strlen(path);
+

+
	if (n < 8)
+
		return (0);
+

+
	p = strrchr(path, '.');
+

+
	if (p != NULL && !strcmp(p, ".pkgconf")) {
+
		strlcpy(newpath, path, len);
+
		newpath[n - 8] = '\0';
+
		return (1);
+
	}
+

+
	return (0);
+
}
modified pkg/query.c
@@ -5,7 +5,6 @@
#include <inttypes.h>
#include <libutil.h>
#include <pkg.h>
-
#include <pkg_util.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -258,66 +257,66 @@ print_query(struct pkg *pkg, char *qstr, char multiline)
		case 'd':
			while (pkg_deps(pkg, &dep) == EPKG_OK) {
				format_str(pkg, output, qstr, dep);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
				break;
		}
		case 'r':
			while (pkg_rdeps(pkg, &dep) == EPKG_OK) {
				format_str(pkg, output, qstr, dep);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
			}
			break;
		case 'C':
			while (pkg_categories(pkg, &cat) == EPKG_OK) {
				format_str(pkg, output, qstr, cat);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
			}
			break;
		case 'O':
			while (pkg_options(pkg, &option) == EPKG_OK) {
				format_str(pkg, output, qstr, option);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
			}
			break;
		case 'F':
			while (pkg_files(pkg, &file) == EPKG_OK) {
				format_str(pkg, output, qstr, file);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
			}
			break;
		case 'D':
			while (pkg_dirs(pkg, &dir) == EPKG_OK) {
				format_str(pkg, output, qstr, dir);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
			}
			break;
		case 'L':
			while (pkg_licenses(pkg, &lic) == EPKG_OK) {
				format_str(pkg, output, qstr, lic);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
			}
			break;
		case 'U':
			while (pkg_users(pkg, &user) == EPKG_OK) {
				format_str(pkg, output, qstr, user);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
			}
			break;
		case 'G':
			while (pkg_users(pkg, &user) == EPKG_OK) {
				format_str(pkg, output, qstr, group);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
			}
			break;
		case 'S':
			while (pkg_scripts(pkg, &scripts) == EPKG_OK) {
				format_str(pkg, output, qstr, scripts);
-
				printf("%s\n", sbuf_get(output));
+
				printf("%s\n", sbuf_data(output));
			}
			break;
		default:
			format_str(pkg, output, qstr, dep);
-
			printf("%s\n", sbuf_get(output));
+
			printf("%s\n", sbuf_data(output));
			break;
	}
	sbuf_delete(output);
modified pkg/version.c
@@ -6,7 +6,6 @@
#define _WITH_GETLINE
#include <err.h>
#include <pkg.h>
-
#include <pkg_util.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -315,8 +314,8 @@ exec_version(int argc, char **argv)
			pkg_get(pkg, PKG_ORIGIN, &origin);
			sbuf_printf(cmd, "make -C %s/%s -VPKGVERSION", portsdir, origin);
			sbuf_finish(cmd);
-
			if ((res = exec_buf(sbuf_get(cmd))) != NULL) {
-
				buf = sbuf_get(res);
+
			if ((res = exec_buf(sbuf_data(cmd))) != NULL) {
+
				buf = sbuf_data(res);
				while (*buf != '\0') {
					if (*buf == '\n') {
						*buf = '\0';
@@ -324,7 +323,7 @@ exec_version(int argc, char **argv)
					}
					buf++;
				}
-
				print_version(pkg, "port", sbuf_get(res), limchar, opt);
+
				print_version(pkg, "port", sbuf_data(res), limchar, opt);
				sbuf_delete(res);
			} else {
				print_version(pkg, NULL, NULL, limchar, opt);