pkgtools

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

infopkg.c (1628B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include "pkg.h"
      3 
      4 static int own_pkg_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 [-r path] [-o filename...]\n", argv0);
     11 	fprintf(stderr, "  -r	 Set alternative installation root\n");
     12 	fprintf(stderr, "  -o	 Look for the packages that own the given filename(s)\n");
     13 	exit(EXIT_FAILURE);
     14 }
     15 
     16 int
     17 main(int argc, char *argv[])
     18 {
     19 	struct db *db;
     20 	char path[PATH_MAX];
     21 	char *root = "/";
     22 	int oflag = 0;
     23 	int i, r;
     24 
     25 	ARGBEGIN {
     26 	case 'o':
     27 		oflag = 1;
     28 		break;
     29 	case 'r':
     30 		root = ARGF();
     31 		break;
     32 	default:
     33 		usage();
     34 	} ARGEND;
     35 
     36 	if (oflag == 0 || argc < 1)
     37 		usage();
     38 
     39 	db = db_new(root);
     40 	if (!db)
     41 		exit(EXIT_FAILURE);
     42 	r = db_load(db);
     43 	if (r < 0) {
     44 		db_free(db);
     45 		exit(EXIT_FAILURE);
     46 	}
     47 
     48 	for (i = 0; i < argc; i++) {
     49 		if (!realpath(argv[i], path)) {
     50 			weprintf("realpath %s:", argv[i]);
     51 			db_free(db);
     52 			exit(EXIT_FAILURE);
     53 		}
     54 		r = db_walk(db, own_pkg_cb, path);
     55 		if (r < 0) {
     56 			db_free(db);
     57 			exit(EXIT_FAILURE);
     58 		}
     59 	}
     60 
     61 	db_free(db);
     62 
     63 	return EXIT_SUCCESS;
     64 }
     65 
     66 static int
     67 own_pkg_cb(struct db *db, struct pkg *pkg, void *file)
     68 {
     69 	char *path = file;
     70 	struct pkgentry *pe;
     71 	struct stat sb1, sb2;
     72 
     73 	(void) db;
     74 
     75 	if (lstat(path, &sb1) < 0)
     76 		eprintf("lstat %s:", path);
     77 
     78 	TAILQ_FOREACH(pe, &pkg->pe_head, entry) {
     79 		if (lstat(pe->path, &sb2) < 0) {
     80 			weprintf("lstat %s:", pe->path);
     81 			continue;
     82 		}
     83 		if (sb1.st_dev == sb2.st_dev &&
     84 		    sb1.st_ino == sb2.st_ino) {
     85 			printf("%s is owned by %s\n", path, pkg->name);
     86 			break;
     87 		}
     88 	}
     89 	return 0;
     90 }