Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Manage dirs separately from files (pkg info -l wont list dirs).
jlaffaye committed 14 years ago
commit d6bbca74e737ce80e3d517917cbfaea97ffb6896
parent d5b58ad
9 files changed +175 -76
modified libpkg/pkg.c
@@ -216,6 +216,18 @@ pkg_files(struct pkg *pkg)
	return ((struct pkg_file **)pkg->files.data);
}

+
const char **
+
pkg_dirs(struct pkg *pkg)
+
{
+
	if (pkg == NULL) {
+
		ERROR_BAD_ARG("pkg");
+
		return (NULL);
+
	}
+

+
	array_init(&pkg->dirs, 1);
+
	return ((const char **)pkg->dirs.data);
+
}
+

struct pkg_conflict **
pkg_conflicts(struct pkg *pkg)
{
@@ -427,6 +439,7 @@ pkg_reset(struct pkg *pkg, pkg_t type)
	array_reset(&pkg->rdeps, &pkg_free_void);
	array_reset(&pkg->conflicts, &pkg_conflict_free_void);
	array_reset(&pkg->files, &free);
+
	array_reset(&pkg->dirs, &free);
	array_reset(&pkg->scripts, &pkg_script_free_void);
	array_reset(&pkg->options, &pkg_option_free_void);

@@ -446,6 +459,7 @@ pkg_free(struct pkg *pkg)
	array_free(&pkg->rdeps, &pkg_free_void);
	array_free(&pkg->conflicts, &pkg_conflict_free_void);
	array_free(&pkg->files, &free);
+
	array_free(&pkg->dirs, &free);
	array_free(&pkg->scripts, &pkg_script_free_void);
	array_free(&pkg->options, &pkg_option_free_void);

@@ -681,6 +695,21 @@ pkg_addfile(struct pkg *pkg, const char *path, const char *sha256)
}

int
+
pkg_adddir(struct pkg *pkg, const char *path)
+
{
+
	if (pkg == NULL)
+
		return (ERROR_BAD_ARG("pkg"));
+

+
	if (path == NULL || path[0] == '\0')
+
		return (ERROR_BAD_ARG("path"));
+

+
	array_init(&pkg->dirs, 10);
+
	array_append(&pkg->dirs, strdup(path));
+

+
	return (EPKG_OK);
+
}
+

+
int
pkg_addconflict(struct pkg *pkg, const char *glob)
{
	struct pkg_conflict *conflict;
modified libpkg/pkg.h
@@ -222,6 +222,11 @@ struct pkg ** pkg_rdeps(struct pkg *);
struct pkg_file ** pkg_files(struct pkg *);

/**
+
 * @return NULL-terminated array of C strings.
+
 */
+
const char ** pkg_dirs(struct pkg *pkg);
+

+
/**
 * @return NULL-terminated array of pkg_conflict.
 */
struct pkg_conflict ** pkg_conflicts(struct pkg *);
@@ -293,6 +298,12 @@ int pkg_adddep(struct pkg *pkg, const char *name, const char *origin, const
int pkg_addfile(struct pkg *pkg, const char *path, const char *sha256);

/**
+
 * Add a path
+
 * @return An error code.
+
 */
+
int pkg_adddir(struct pkg *pkg, const char *path);
+

+
/**
 * Allocate a new struct pkg_conflict and add it to the conflicts of pkg.
 * @return An error code.
 */
@@ -432,6 +443,7 @@ struct pkgdb_it * pkgdb_query_which(struct pkgdb *db, const char *path);
#define PKG_LOAD_SCRIPTS (1<<5)
#define PKG_LOAD_OPTIONS (1<<6)
#define PKG_LOAD_MTREE (1<<7)
+
#define PKG_LOAD_DIRS (1<<8)

/**
 * Get the next pkg.
@@ -451,6 +463,7 @@ int pkgdb_loaddeps(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadrdeps(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadconflicts(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadfiles(struct pkgdb *db, struct pkg *pkg);
+
int pkgdb_loaddirs(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadscripts(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadoptions(struct pkgdb *db, struct pkg *pkg);
int pkgdb_loadmtree(struct pkgdb *db, struct pkg *pkg);
modified libpkg/pkg_create.c
@@ -163,7 +163,7 @@ pkg_create_installed(const char *outdir, pkg_formats format, const char *rootdir
{
	struct packing *pkg_archive;
	int required_flags = PKG_LOAD_DEPS | PKG_LOAD_CONFLICTS | PKG_LOAD_FILES |
-
						 PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
+
						 PKG_LOAD_DIRS | PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
						 PKG_LOAD_MTREE;

	if (pkg->type != PKG_INSTALLED)
modified libpkg/pkg_delete.c
@@ -10,6 +10,8 @@
#include "pkg_error.h"
#include "pkg_util.h"

+
static int pkg_delete_dirs(struct pkg *pkg);
+

int
pkg_delete(struct pkg *pkg, struct pkgdb *db, int force)
{
@@ -30,6 +32,8 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, int force)
		return (ret);
	if ((ret = pkgdb_loadfiles(db, pkg)) != EPKG_OK)
		return (ret);
+
	if ((ret = pkgdb_loaddirs(db, pkg)) != EPKG_OK)
+
		return (ret);
	if ((ret = pkgdb_loadscripts(db, pkg)) != EPKG_OK)
		return (ret);
	if ((ret = pkgdb_loadmtree(db, pkg)) != EPKG_OK)
@@ -65,6 +69,9 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, int force)
	if ((ret = pkg_script_post_deinstall(pkg)) != EPKG_OK)
		return (ret);

+
	if ((ret = pkg_delete_dirs(pkg)) != EPKG_OK)
+
		return (ret);
+

	return (pkgdb_unregister_pkg(db, pkg_get(pkg, PKG_ORIGIN)));
}

@@ -72,7 +79,6 @@ int
pkg_delete_files(struct pkg *pkg, int force)
{
	int i;
-
	int ret = EPKG_OK;
	struct pkg_file **files;
	char sha256[65];
	const char *path;
@@ -81,16 +87,6 @@ pkg_delete_files(struct pkg *pkg, int force)
	for (i = 0; files[i] != NULL; i++) {
		path = pkg_file_path(files[i]);

-
		/* Directories */
-
		if (path[strlen(path) - 1] == '/') {
-
			/*
-
			 * currently do not warn on this because multiple
-
			 * packages can own the same directory
-
			 */
-
			rmdir(path);
-
			continue;
-
		}
-

		/* Regular files and links */
		/* check sha256 */
		if (!force && pkg_file_sha256(files[i])[0] != '\0') {
@@ -109,5 +105,21 @@ pkg_delete_files(struct pkg *pkg, int force)
		}
	}

-
	return (ret);
+
	return (EPKG_OK);
+
}
+

+
static int
+
pkg_delete_dirs(struct pkg *pkg)
+
{
+
	int i;
+
	const char **dirs;
+

+
	dirs = pkg_dirs(pkg);
+
	for (i = 0; dirs[i] != NULL; i++) {
+
		if (rmdir(dirs[i]) == -1 && errno != ENOTEMPTY) {
+
			warn("rmdir(%s)", dirs[i]);
+
		}
+
	}
+

+
	return (EPKG_OK);
}
modified libpkg/pkg_manifest.c
@@ -29,6 +29,7 @@ static int m_parse_conflict(struct pkg *pkg, char *buf);
static int m_parse_maintainer(struct pkg *pkg, char *buf);
static int m_parse_prefix(struct pkg *pkg, char *buf);
static int m_parse_file(struct pkg *pkg, char *buf);
+
static int m_parse_dir(struct pkg *pkg, char *buf);
static int m_parse_set_string(struct pkg *pkg, char *buf, pkg_attr attr);

#define MANIFEST_FORMAT_KEY "@pkg_format_version"
@@ -51,6 +52,7 @@ static struct manifest_key {
	{ "@maintainer", m_parse_maintainer},
	{ "@prefix", m_parse_prefix},
	{ "@file", m_parse_file},
+
	{ "@dir", m_parse_dir},
};

#define manifest_key_len (int)(sizeof(manifest_key)/sizeof(manifest_key[0]))
@@ -230,6 +232,19 @@ m_parse_file(struct pkg *pkg, char *buf)
	return (EPKG_OK);
}

+
static int
+
m_parse_dir(struct pkg *pkg, char *buf)
+
{
+
	while (isspace(*buf))
+
		buf++;
+

+
	if (*buf == '\0')
+
		return (EPKG_FATAL);
+

+
	pkg_adddir(pkg, buf);
+
	return (EPKG_OK);
+
}
+

int
pkg_load_manifest_file(struct pkg *pkg, const char *fpath)
{
@@ -303,6 +318,7 @@ pkg_emit_manifest(struct pkg *pkg, char **dest)
	struct pkg_conflict **conflicts;
	struct pkg_option **options;
	struct pkg_file **files;
+
	const char **dirs;
	int i;
	int len = 0;

@@ -331,35 +347,34 @@ pkg_emit_manifest(struct pkg *pkg, char **dest)
			pkg_flatsize(pkg)
			);

-
	if ((deps = pkg_deps(pkg)) != NULL) {
-
		for (i = 0; deps[i] != NULL; i++) {
-
			sbuf_printf(manifest, "@dep %s %s %s\n",
+
	deps = pkg_deps(pkg);
+
	for (i = 0; deps[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@dep %s %s %s\n",
					pkg_get(deps[i], PKG_NAME),
					pkg_get(deps[i], PKG_ORIGIN),
					pkg_get(deps[i], PKG_VERSION));
-
		}
	}

-
	if ((conflicts = pkg_conflicts(pkg)) != NULL) {
-
		for (i = 0; conflicts[i] != NULL; i++) {
-
			sbuf_printf(manifest, "@conflict %s\n", pkg_conflict_glob(conflicts[i]));
-
		}
+
	conflicts = pkg_conflicts(pkg);
+
	for (i = 0; conflicts[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@conflict %s\n", pkg_conflict_glob(conflicts[i]));
	}

-
	if ((options = pkg_options(pkg)) != NULL)  {
-
		for (i = 0; options[i] != NULL; i++) {
-
			sbuf_printf(manifest, "@option %s %s\n",
-
					pkg_option_opt(options[i]),
+
	options = pkg_options(pkg);
+
	for (i = 0; options[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@option %s %s\n", pkg_option_opt(options[i]),
					pkg_option_value(options[i]));
-
					
-
		}
	}

-
	if ((files = pkg_files(pkg)) != NULL) {
-
		for (i = 0; files[i] != NULL; i++) {
-
			sbuf_printf(manifest, "@file %s %s\n", pkg_file_path(files[i]),
-
						pkg_file_sha256(files[i]));
-
		}
+
	files = pkg_files(pkg);
+
	for (i = 0; files[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@file %s %s\n", pkg_file_path(files[i]),
+
					pkg_file_sha256(files[i]));
+
	}
+

+
	dirs = pkg_dirs(pkg);
+
	for (i = 0; dirs[i] != NULL; i++) {
+
		sbuf_printf(manifest, "@dir %s\n", dirs[i]);
	}

	sbuf_finish(manifest);
modified libpkg/pkg_ports.c
@@ -109,7 +109,7 @@ ports_parse_plist(struct pkg *pkg, char *plist)

				snprintf(path, MAXPATHLEN, "%s%s%s/", prefix, slash, buf);

-
				ret += pkg_addfile(pkg, path, NULL);
+
				ret += pkg_adddir(pkg, path);

			} else {
				warnx("%s is deprecated, ignoring", plist_p);
modified libpkg/pkg_private.h
@@ -28,6 +28,7 @@ struct pkg {
	struct array rdeps;
	struct array conflicts;
	struct array files;
+
	struct array dirs;
	struct array scripts;
	struct array options;
	int flags;
modified libpkg/pkgdb.c
@@ -403,6 +403,10 @@ pkgdb_it_next(struct pkgdb_it *it, struct pkg **pkg_p, int flags)
			if ((ret = pkgdb_loadfiles(it->db, pkg)) != EPKG_OK)
				return (ret);

+
		if (flags & PKG_LOAD_DIRS)
+
			if ((ret = pkgdb_loaddirs(it->db, pkg)) != EPKG_OK)
+
				return (ret);
+

		if (flags & PKG_LOAD_SCRIPTS)
			if ((ret = pkgdb_loadscripts(it->db, pkg)) != EPKG_OK)
				return (ret);
@@ -666,11 +670,6 @@ pkgdb_loadfiles(struct pkgdb *db, struct pkg *pkg)
		"FROM files "
		"WHERE package_id = ?1 "
		"ORDER BY PATH ASC";
-
	const char sqldir[] = ""
-
		"SELECT path "
-
		"FROM pkg_dirs "
-
		"WHERE origin = ?1 "
-
		"ORDER by path DESC";

	if (pkg->type != PKG_INSTALLED)
		return (ERROR_BAD_ARG("pkg"));
@@ -698,19 +697,45 @@ pkgdb_loadfiles(struct pkgdb *db, struct pkg *pkg)
		return (ERROR_SQLITE(db->sqlite));
	}

-
	if (sqlite3_prepare_v2(db->sqlite, sqldir, -1, &stmt, NULL) != SQLITE_OK)
+
	pkg->flags |= PKG_LOAD_FILES;
+
	return (EPKG_OK);
+
}
+

+
int
+
pkgdb_loaddirs(struct pkgdb *db, struct pkg *pkg)
+
{
+
	sqlite3_stmt *stmt;
+
	char *path;
+
	int ret;
+

+
	const char sql[] = ""
+
		"SELECT path "
+
		"FROM pkg_dirs "
+
		"WHERE origin = ?1 "
+
		"ORDER by path DESC";
+

+
	if (pkg->flags & PKG_LOAD_DIRS)
+
		return (EPKG_OK);
+

+
	if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK)
		return (ERROR_SQLITE(db->sqlite));

+
	array_init(&pkg->dirs, 5);
+

	sqlite3_bind_text(stmt, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);

	while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) {
-
		pkg_file_new(&f);
-
		strlcpy(f->path, sqlite3_column_text(stmt, 0), sizeof(f->path));
-
		array_append(&pkg->files, f);
+
		path = strdup(sqlite3_column_text(stmt, 0));
+
		array_append(&pkg->dirs, path);
	}
	sqlite3_finalize(stmt);

-
	pkg->flags |= PKG_LOAD_FILES;
+
	if (ret != SQLITE_DONE) {
+
		array_reset(&pkg->dirs, &free);
+
		return (ERROR_SQLITE(db->sqlite));
+
	}
+

+
	pkg->flags |= PKG_LOAD_DIRS;
	return (EPKG_OK);
}

@@ -850,6 +875,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
{
	struct pkg **deps;
	struct pkg_file **files;
+
	const char **dirs;
	struct pkg_conflict **conflicts;
	struct pkg_script **scripts;
	struct pkg_option **options;
@@ -1017,8 +1043,7 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
	}

	/*
-
	 * Insert file
-
	 * and dirs
+
	 * Insert files.
	 */

	if (sqlite3_prepare_v2(s, sql_file, -1, &stmt_file, NULL) != SQLITE_OK) {
@@ -1026,43 +1051,47 @@ pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg)
		goto cleanup;
	}

+
	files = pkg_files(pkg);
+
	for (i = 0; files[i] != NULL; i++) {
+
		path = pkg_file_path(files[i]);
+
		sqlite3_bind_text(stmt_file, 1, pkg_file_path(files[i]), -1, SQLITE_STATIC);
+
		sqlite3_bind_text(stmt_file, 2, pkg_file_sha256(files[i]), -1, SQLITE_STATIC);
+
		sqlite3_bind_int64(stmt_file, 3, package_id);
+

+
		if ((ret = sqlite3_step(stmt_file)) != SQLITE_DONE) {
+
			if (ret == SQLITE_CONSTRAINT)
+
				retcode = pkg_error_set(EPKG_FATAL, "constraint violation on "
+
						"path with %s", pkg_file_path(files[i]));
+
			else
+
				retcode = ERROR_SQLITE(s);
+
			goto cleanup;
+
		}
+
		sqlite3_reset(stmt_file);
+
	}
+

+
	/*
+
	 * Insert dirs.
+
	 */
+

	if (sqlite3_prepare_v2(s, sql_dir, -1, &stmt_dirs, NULL) != SQLITE_OK) {
		retcode = ERROR_SQLITE(s);
		goto cleanup;
	}

-
	files = pkg_files(pkg);
-
	for (i = 0; files[i] != NULL; i++) {
-
		path = pkg_file_path(files[i]);
-
		if (path[strlen(path) - 1 ] != '/') {
-
			sqlite3_bind_text(stmt_file, 1, pkg_file_path(files[i]), -1, SQLITE_STATIC);
-
			sqlite3_bind_text(stmt_file, 2, pkg_file_sha256(files[i]), -1, SQLITE_STATIC);
-
			sqlite3_bind_int64(stmt_file, 3, package_id);
-

-
			if ((ret = sqlite3_step(stmt_file)) != SQLITE_DONE) {
-
				if ( ret == SQLITE_CONSTRAINT)
-
					retcode = pkg_error_set(EPKG_FATAL, "constraint violation on "
-
							"path with %s", pkg_file_path(files[i]));
-
				else
-
					retcode = ERROR_SQLITE(s);
-
				goto cleanup;
-
			}
-
			sqlite3_reset(stmt_file);
-
		} else {
-
			sqlite3_bind_text(stmt_dirs, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);
-
			sqlite3_bind_text(stmt_dirs, 2, pkg_file_path(files[i]), -1, SQLITE_STATIC);
+
	dirs = pkg_dirs(pkg);
+
	for (i = 0; dirs[i] != NULL; i++) {
+
		sqlite3_bind_text(stmt_dirs, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC);
+
		sqlite3_bind_text(stmt_dirs, 2, dirs[i], -1, SQLITE_STATIC);
			
-
			if ((ret = sqlite3_step(stmt_dirs)) != SQLITE_DONE) {
-
				if ( ret == SQLITE_CONSTRAINT)
-
					retcode = pkg_error_set(EPKG_FATAL, "constraint violation on "
-
							"dirs with %s", pkg_file_path(files[i]));
-
				else
-
					retcode = ERROR_SQLITE(s);
-
				goto cleanup;
-
			}
-
			sqlite3_reset(stmt_dirs);
+
		if ((ret = sqlite3_step(stmt_dirs)) != SQLITE_DONE) {
+
			if ( ret == SQLITE_CONSTRAINT)
+
				retcode = pkg_error_set(EPKG_FATAL, "constraint violation on "
+
						"dirs with %s", dirs[i]);
+
			else
+
				retcode = ERROR_SQLITE(s);
+
			goto cleanup;
		}
-

+
		sqlite3_reset(stmt_dirs);
	}

	/*
modified pkg/create.c
@@ -26,7 +26,7 @@ pkg_create_matches(int argc, char **argv, match_t match, pkg_formats fmt, const
	struct pkgdb_it *it = NULL;
	struct pkg *pkg = NULL;
	int query_flags = PKG_LOAD_DEPS | PKG_LOAD_CONFLICTS | PKG_LOAD_FILES |
-
					  PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
+
					  PKG_LOAD_DIRS | PKG_LOAD_SCRIPTS | PKG_LOAD_OPTIONS |
					  PKG_LOAD_MTREE;

	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {