From 48a5933756431a924c10917923113c0b9705febe Mon Sep 17 00:00:00 2001 From: Juan RP Date: Fri, 27 Feb 2009 17:00:41 +0100 Subject: [PATCH] xbps-bin: added -C flag for 'files' target, to check SHA256 hash in files. --HG-- extra : convert_revision : 28fffd3047de8d8a4b9401618017a880cbf03008 --- bin/xbps-bin/main.c | 41 +++++++++++++++++++++++++++++++++-------- bin/xbps-repo/util.c | 41 ++++++++++++++++++++++++++++++++++++----- bin/xbps-repo/util.h | 2 +- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/bin/xbps-bin/main.c b/bin/xbps-bin/main.c index bde83ff2d1c..296850ebc12 100644 --- a/bin/xbps-bin/main.c +++ b/bin/xbps-bin/main.c @@ -43,22 +43,24 @@ static int list_pkgs_in_dict(prop_object_t, void *, bool *); static void usage(void) { - printf("Usage: xbps-bin [options] [action] [arguments]\n\n" - " Available actions:\n" - " install, list, remove, show, files\n" - " Actions with arguments:\n" + printf("Usage: xbps-bin [options] [target] [arguments]\n\n" + " Available targets:\n" + " autoremove, install, list, remove, show, files\n" + " Targets with arguments:\n" " install\t\n" " files\t\n" " remove\t\n" " show\t\n" - " Options shared by all actions:\n" + " Options shared by all targets:\n" " -r\t\t\n" " -v\t\t\n" + " Option used by the files target:\n" + " -C\t\tTo check the SHA256 hash for any listed file.\n" "\n" " Examples:\n" " $ xbps-bin install klibc\n" " $ xbps-bin -r /path/to/root install klibc\n" - " $ xbps-bin files klibc\n" + " $ xbps-bin -C files klibc\n" " $ xbps-bin list\n" " $ xbps-bin remove klibc\n" " $ xbps-bin show klibc\n"); @@ -118,9 +120,13 @@ main(int argc, char **argv) prop_dictionary_t dict; char *plist, *root = NULL; int c, flags = 0, rv = 0; + bool chkhash = false; - while ((c = getopt(argc, argv, "r:v")) != -1) { + while ((c = getopt(argc, argv, "Cr:v")) != -1) { switch (c) { + case 'C': + chkhash = true; + break; case 'r': /* To specify the root directory */ root = optarg; @@ -212,16 +218,35 @@ main(int argc, char **argv) printf("Package %s not installed.\n", argv[1]); exit(EXIT_FAILURE); } + } else if (strcasecmp(argv[0], "files") == 0) { /* Shows files installed by a binary package. */ if (argc != 2) usage(); - rv = show_pkg_files_from_metadir(argv[1]); + rv = show_pkg_files_from_metadir(argv[1], root, chkhash); if (rv != 0) { printf("Package %s not installed.\n", argv[1]); exit(EXIT_FAILURE); } + + } else if (strcasecmp(argv[0], "autoremove") == 0) { + /* + * Removes orphaned pkgs. These packages were installed + * as dependency and any installed package does not depend + * on it. + */ + if (argc != 1) + usage(); + +#if 0 + rv = xbps_auto_remove_packages(); + if (rv != 0) { + printf("There was an error! (%s)\n", strerror(rv)); + exit(EXIT_FAILURE); + } +#endif + } else { usage(); } diff --git a/bin/xbps-repo/util.c b/bin/xbps-repo/util.c index b62d8caf3bb..8aaa37071ab 100644 --- a/bin/xbps-repo/util.c +++ b/bin/xbps-repo/util.c @@ -34,6 +34,11 @@ #include #include "util.h" +struct show_files_cb { + const char *destdir; + bool check_hash; +}; + static void show_pkg_info(prop_dictionary_t); static int show_pkg_files(prop_object_t, void *, bool *); static int show_pkg_namedesc(prop_object_t, void *, bool *); @@ -194,9 +199,10 @@ show_pkg_info_from_metadir(const char *pkgname) } int -show_pkg_files_from_metadir(const char *pkgname) +show_pkg_files_from_metadir(const char *pkgname, const char *destdir, bool hash) { prop_dictionary_t pkgd; + struct show_files_cb sf_cb; size_t len = 0; char *plist, *path; int rv = 0; @@ -223,8 +229,10 @@ show_pkg_files_from_metadir(const char *pkgname) return errno; } + sf_cb.destdir = destdir; + sf_cb.check_hash = hash; rv = xbps_callback_array_iter_in_dict(pkgd, "filelist", - show_pkg_files, NULL); + show_pkg_files, (void *)&sf_cb); prop_object_release(pkgd); free(plist); @@ -234,14 +242,37 @@ show_pkg_files_from_metadir(const char *pkgname) static int show_pkg_files(prop_object_t obj, void *arg, bool *loop_done) { - const char *file = NULL; + struct show_files_cb *sfc = arg; + const char *file = NULL, *sha256; + char *path; + int rv = 0; - (void)arg; (void)loop_done; prop_dictionary_get_cstring_nocopy(obj, "file", &file); - if (file != NULL) + if (sfc->check_hash == false && file != NULL) { printf("%s\n", file); + return 0; + } + + if (sfc->check_hash && file != NULL) { + path = xbps_append_full_path(false, sfc->destdir, file); + if (path == NULL) + return EINVAL; + + printf("%s", file); + prop_dictionary_get_cstring_nocopy(obj, "sha256", &sha256); + rv = xbps_check_file_hash(path, sha256); + if (rv != 0 && rv != ERANGE) { + free(path); + return rv; + } + if (rv == ERANGE) + printf(" WARNING! SHA256 HASH MISMATCH!"); + + printf("\n"); + free(path); + } return 0; } diff --git a/bin/xbps-repo/util.h b/bin/xbps-repo/util.h index ed381399512..f88252900d4 100644 --- a/bin/xbps-repo/util.h +++ b/bin/xbps-repo/util.h @@ -28,7 +28,7 @@ int search_string_in_pkgs(prop_object_t, void *, bool *); int show_pkg_info_from_metadir(const char *); -int show_pkg_files_from_metadir(const char *); +int show_pkg_files_from_metadir(const char *, const char *, bool); int show_pkg_info_from_repolist(prop_object_t, void *, bool *); int list_strings_in_array(prop_object_t, void *, bool *);