Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Make 'pkg deinstall' able work on multiple packages
Marin Atanasov Nikolov committed 14 years ago
commit 8de0eb7e52a8ae134c4802780977449cb6dd1d55
parent 3ef1e57
5 files changed +66 -40
modified pkg/autoremove.c
@@ -12,27 +12,9 @@

#include <pkg.h>

+
#include "utils.h"
#include "autoremove.h"

-
static int
-
query_yesno(const char *msg)
-
{
-
	int c, r = 0;
-

-
	printf(msg);
-

-
	c = getchar();
-
	if (c == 'y' || c == 'Y')
-
		r = 1;
-
	else if (c == '\n' || c == EOF)
-
		return 0;
-

-
	while((c = getchar()) != '\n' && c != EOF)
-
		continue;
-

-
	return r;
-
}
-

void
usage_autoremove(void)
{
modified pkg/delete.c
@@ -7,12 +7,13 @@

#include <pkg.h>

+
#include "utils.h"
#include "delete.h"

void
usage_delete(void)
{
-
	fprintf(stderr, "usage: pkg delete [-f] <pkg-name>\n");
+
	fprintf(stderr, "usage: pkg delete [-f] <pkg-name> <...>\n");
	fprintf(stderr, "       pkg delete -a\n\n");
	fprintf(stderr, "For more information see 'pkg help delete'.\n");
}
@@ -25,8 +26,7 @@ exec_delete(int argc, char **argv)
	struct pkgdb *db = NULL;
	struct pkgdb_it *it = NULL;
	match_t match = MATCH_EXACT;
-
	char *origin = NULL;
-
	int ch;
+
	int i, ch;
	int flags = PKG_LOAD_BASIC;
	int force = 0;
	int retcode = EPKG_OK;
@@ -47,7 +47,7 @@ exec_delete(int argc, char **argv)
	argc -= optind;
	argv += optind;

-
	if (argc != 1 && match == MATCH_EXACT) {
+
	if (argc < 1) {
		usage_delete();
		return (EX_USAGE);
	}
@@ -58,33 +58,55 @@ exec_delete(int argc, char **argv)
	}
	
	if ((retcode = pkgdb_open(&db, PKGDB_DEFAULT)) != EPKG_OK) {
-
		goto cleanup;
+
		return (EPKG_FATAL);
	}

-
	if (argc == 1)
-
		origin = argv[0];
-

	if ((retcode = pkg_jobs_new(&jobs, PKG_JOBS_DEINSTALL, db)) != EPKG_OK) {
-
		goto cleanup;
+
		pkgdb_close(db);
+
		return (EPKG_FATAL);
	}

-
	if ((it = pkgdb_query(db, origin, match)) == NULL) {
-
		retcode = EPKG_FATAL;
-
		goto cleanup;
-
	}
+
	if (match == MATCH_ALL) {
+
		if ((it = pkgdb_query(db, NULL, match)) == NULL) {
+
			retcode = EPKG_FATAL;
+
			goto cleanup;
+
		}

-
	while ((retcode = pkgdb_it_next(it, &pkg, flags)) == EPKG_OK) {
-
		pkg_jobs_add(jobs, pkg);
-
		pkg = NULL;
+
		while ((retcode = pkgdb_it_next(it, &pkg, flags)) == EPKG_OK) {
+
			pkg_jobs_add(jobs, pkg);
+
			pkg = NULL;
+
		}
+
	} else {
+
		for (i = 0; i < argc; i++) {
+
			if ((it = pkgdb_query(db, argv[i], match)) == NULL) {
+
				retcode = EPKG_FATAL;
+
				goto cleanup;
+
			}
+

+
			while ((retcode = pkgdb_it_next(it, &pkg, flags)) == EPKG_OK) {
+
				pkg_jobs_add(jobs, pkg);
+
				pkg = NULL;
+
			}
+
		}
	}

-
	if (retcode != EPKG_END) {
+
	/* check if we have something to deinstall */
+
	pkg = NULL;
+
	if ((retcode != EPKG_END) || (pkg_jobs(jobs, &pkg) != EPKG_OK)) {
		goto cleanup;
	}

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

+
	if (query_yesno("\nProceed with deinstalling packages [y/N]: ")) {
+
		pkg = NULL;
+
		if ((retcode = pkg_jobs_apply(jobs, force)) != EPKG_OK) 
+
			goto cleanup;
+
	} else
		goto cleanup;
-
	}

	retcode = pkgdb_compact(db);

modified pkg/install.c
@@ -10,6 +10,7 @@

#include <pkg.h>

+
#include "utils.h"
#include "install.h"

void
@@ -60,10 +61,11 @@ exec_install(int argc, char **argv)
	pkg = NULL;
	printf("The following packages will be installed:\n");
	while (pkg_jobs(jobs, &pkg) == EPKG_OK) {
-
		printf("%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
+
		printf("\t%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
	}

-
	retcode = pkg_jobs_apply(jobs, 0);
+
	if (query_yesno("\nProceed with installing packages [y/N]: "))
+
		retcode = pkg_jobs_apply(jobs, 0);

	cleanup:
	
modified pkg/utils.c
@@ -9,6 +9,25 @@
#include "utils.h"

int
+
query_yesno(const char *msg)
+
{
+
        int c, r = 0;
+

+
        printf(msg);
+

+
        c = getchar();
+
        if (c == 'y' || c == 'Y')
+
                r = 1;
+
        else if (c == '\n' || c == EOF)
+
                return 0;
+

+
        while((c = getchar()) != '\n' && c != EOF)
+
                continue;
+

+
        return r;
+
}
+

+
int
mkdirs(const char *_path)
{
	char path[MAXPATHLEN];
modified pkg/utils.h
@@ -1,6 +1,7 @@
#ifndef _UTILS_H
#define _UTILS_H

+
int query_yesno(const char *msg);
int mkdirs(const char *path);

#endif