pkgtools

morpheus pkg tools
git clone git://git.2f30.org/pkgtools
Log | Files | Refs | README | LICENSE

removepkg.c (1399B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "pkg.h"
      3 
      4 static int pkg_remove_cb(struct db *, struct pkg *, void *);
      5 
      6 static void
      7 usage(void)
      8 {
      9 	fprintf(stderr, VERSION " (c) 2014 morpheus engineers\n");
     10 	fprintf(stderr, "usage: %s [-v] [-f] [-r path] pkg...\n", argv0);
     11 	fprintf(stderr, "  -v    Enable verbose output\n");
     12 	fprintf(stderr, "  -f    Force the removal of empty directories and symlinks\n");
     13 	fprintf(stderr, "  -r    Set alternative installation root\n");
     14 	exit(EXIT_FAILURE);
     15 }
     16 
     17 int
     18 main(int argc, char *argv[])
     19 {
     20 	struct db *db;
     21 	char *root = "/";
     22 	int i, r;
     23 
     24 	ARGBEGIN {
     25 	case 'v':
     26 		vflag = 1;
     27 		break;
     28 	case 'f':
     29 		fflag = 1;
     30 		break;
     31 	case 'r':
     32 		root = ARGF();
     33 		break;
     34 	default:
     35 		usage();
     36 	} ARGEND;
     37 
     38 	if (argc < 1)
     39 		usage();
     40 
     41 	db = db_new(root);
     42 	if (!db)
     43 		exit(EXIT_FAILURE);
     44 	r = db_load(db);
     45 	if (r < 0) {
     46 		db_free(db);
     47 		exit(EXIT_FAILURE);
     48 	}
     49 
     50 	for (i = 0; i < argc; i++) {
     51 		r = db_walk(db, pkg_remove_cb, argv[i]);
     52 		if (r < 0) {
     53 			db_free(db);
     54 			exit(EXIT_FAILURE);
     55 		} else if (r == 0) {
     56 			printf("%s is not installed\n", argv[i]);
     57 		}
     58 	}
     59 
     60 	db_free(db);
     61 
     62 	return EXIT_SUCCESS;
     63 }
     64 
     65 static int
     66 pkg_remove_cb(struct db *db, struct pkg *pkg, void *name)
     67 {
     68 	if (strcmp(pkg->name, name) == 0) {
     69 		if (pkg_remove(db, pkg) < 0)
     70 			return -1;
     71 		if (db_rm(db, pkg) < 0)
     72 			return -1;
     73 		printf("removed %s\n", pkg->name);
     74 		return 1;
     75 	}
     76 	return 0;
     77 }