Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Better delete backend and frontend
Baptiste Daroussin committed 14 years ago
commit 7a058e66ad403b479b61c0919d4d13001d00fdf7
parent fedd1d7
8 files changed +89 -9
modified libpkg/pkg.h
@@ -694,6 +694,8 @@ typedef enum {
	/* informational */
	PKG_EVENT_INSTALL_BEGIN = 0,
	PKG_EVENT_INSTALL_FINISHED,
+
	PKG_EVENT_DEINSTALL_BEGIN,
+
	PKG_EVENT_DEINSTALL_FINISHED,
	PKG_EVENT_FETCHING,
	/* errors */
	PKG_EVENT_ERROR,
@@ -732,9 +734,15 @@ struct pkg_event {
		} e_install_begin;
		struct {
			struct pkg *pkg;
+
		} e_deinstall_begin;
+
		struct {
+
			struct pkg *pkg;
		} e_install_finished;
		struct {
			struct pkg *pkg;
+
		} e_install_end;
+
		struct {
+
			struct pkg *pkg;
			struct pkg_dep *dep;
		} e_missing_dep;
		struct {
modified libpkg/pkg_add.c
@@ -201,7 +201,7 @@ pkg_add(struct pkgdb *db, const char *path)
	if (extract == true && (retcode = do_extract(a, ae)) != EPKG_OK) {
		/* If the add failed, clean up */
		pkg_delete_files(pkg, 1);
-
		pkg_delete_dirs(pkg, 1);
+
		pkg_delete_dirs(db, pkg, 1);
		goto cleanup_reg;
	}

modified libpkg/pkg_delete.c
@@ -41,6 +41,7 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, int force)
	if ((ret = pkgdb_loadmtree(db, pkg)) != EPKG_OK)
		return (ret);

+
	EMIT_DEINSTALL_BEGIN(pkg);
	/* If there are dependencies */
	if (pkg_rdeps(pkg, &rdep) == EPKG_OK) {
		EMIT_REQUIRED(pkg, force);
@@ -57,9 +58,11 @@ pkg_delete(struct pkg *pkg, struct pkgdb *db, int force)
	if ((ret = pkg_script_run(pkg, PKG_SCRIPT_POST_DEINSTALL)) != EPKG_OK)
		return (ret);

-
	if ((ret = pkg_delete_dirs(pkg, force)) != EPKG_OK)
+
	if ((ret = pkg_delete_dirs(db, pkg, force)) != EPKG_OK)
		return (ret);

+
	EMIT_DEINSTALL_FINISHED(pkg);
+

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

@@ -95,14 +98,22 @@ pkg_delete_files(struct pkg *pkg, int force)
}

int
-
pkg_delete_dirs(struct pkg *pkg, int force)
+
pkg_delete_dirs(struct pkgdb *db, struct pkg *pkg, int force)
{
	struct pkg_dir *dir = NULL;
+
	int64_t nbpackage;

	while (pkg_dirs(pkg, &dir) == EPKG_OK) {
-
		if (rmdir(pkg_dir_path(dir)) == -1 && errno != ENOTEMPTY && force != 1) {
+
		nbpackage = 0;
+

+
		if (pkgdb_is_dir_used(db, pkg_dir_path(dir), &nbpackage) != EPKG_OK)
+
			return (EPKG_FATAL);
+

+
		if (nbpackage > 1)
+
			continue;
+

+
		if (rmdir(pkg_dir_path(dir)) == -1 && errno != ENOTEMPTY && force != 1)
			EMIT_ERRNO("rmdir", pkg_dir_path(dir));
-
		}
	}

	return (EPKG_OK);
modified libpkg/pkg_event.h
@@ -58,6 +58,20 @@
	_EV_EMIT; \
	_EV_END

+
#define EMIT_DEINSTALL_BEGIN(p) \
+
	_EV_START; \
+
	ev.type = PKG_EVENT_DEINSTALL_BEGIN; \
+
	ev.e_deinstall_begin.pkg = p; \
+
	_EV_EMIT; \
+
	_EV_END
+

+
#define EMIT_DEINSTALL_FINISHED(p) \
+
	_EV_START; \
+
	ev.type = PKG_EVENT_DEINSTALL_FINISHED; \
+
	ev.e_install_finished.pkg = p; \
+
	_EV_EMIT; \
+
	_EV_END
+

#define EMIT_MISSING_DEP(p, d) \
	_EV_START; \
	ev.type = PKG_EVENT_MISSING_DEP; \
modified libpkg/pkg_private.h
@@ -165,6 +165,8 @@ 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 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);

#endif
modified libpkg/pkgdb.c
@@ -659,6 +659,40 @@ pkgdb_query_which(struct pkgdb *db, const char *path)
}

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

+
	const char sql[] = ""
+
		"SELECT count(package_id) FROM pkg_directories, directories "
+
		"WHERE directory_id = directories.id AND directories.path = ?1;";
+

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

+
	sqlite3_bind_text(stmt, 1, dir, -1, SQLITE_TRANSIENT);
+

+
	ret = sqlite3_step(stmt);
+

+
	if (ret == SQLITE_ROW)
+
		*res = sqlite3_column_int64(stmt, 0);
+

+
	sqlite3_finalize(stmt);
+

+
	if (ret != SQLITE_ROW) {
+
		ERROR_SQLITE(db->sqlite);
+
		return (EPKG_FATAL);
+
	}
+
	
+
	return (EPKG_OK);
+

+

+
}
+

+
int
pkgdb_loaddeps(struct pkgdb *db, struct pkg *pkg)
{
	sqlite3_stmt *stmt;
@@ -1515,7 +1549,8 @@ pkgdb_unregister_pkg(struct pkgdb *db, const char *origin)
	return (EPKG_OK);
}

-
static int sql_exec(sqlite3 *s, const char *sql)
+
static int
+
sql_exec(sqlite3 *s, const char *sql)
{
	char *errmsg;

@@ -1528,7 +1563,9 @@ static int sql_exec(sqlite3 *s, const char *sql)
	return (EPKG_OK);
}

-
static int get_pragma(sqlite3 *s, const char *sql, int64_t *res) {
+
static int
+
get_pragma(sqlite3 *s, const char *sql, int64_t *res)
+
{
	sqlite3_stmt *stmt;
	int ret;

modified pkg/delete.c
@@ -102,7 +102,7 @@ exec_delete(int argc, char **argv)

	pkg = NULL;
	printf("The following packages will be deinstalled:\n");
-
	while (pkg_jobs(jobs, &pkg) == EPKG_OK) 
+
	while (pkg_jobs(jobs, &pkg) == EPKG_OK)
		printf("\t%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));

	if (yes == 0)
modified pkg/event.c
@@ -36,6 +36,14 @@ event_callback(void *data __unused, struct pkg_event *ev)
		if (message != NULL && message[0] != '\0')
			printf("%s\n", message);
		break;
+
	case PKG_EVENT_DEINSTALL_BEGIN:
+
		printf("Deinstalling %s-%s...",
+
			   pkg_get(ev->e_deinstall_begin.pkg, PKG_NAME),
+
			   pkg_get(ev->e_deinstall_begin.pkg, PKG_VERSION));
+
		break;
+
	case PKG_EVENT_DEINSTALL_FINISHED:
+
		printf(" done\n");
+
		break;
	case PKG_EVENT_REQUIRED:
		pkg = ev->e_required.pkg;
		fprintf(stderr, "%s-%s is required by:", pkg_get(pkg, PKG_NAME),