Simplify xbps_end() and don't continue if xbps_init() did not succeed.

This commit is contained in:
Juan RP 2012-01-17 16:56:17 +01:00
parent f2b05d6438
commit d0e1477eeb
6 changed files with 75 additions and 86 deletions

View File

@ -39,11 +39,9 @@
#include "../xbps-repo/defs.h"
static void __attribute__((noreturn))
usage(struct xbps_handle *xhp)
usage(void)
{
if (xhp != NULL)
xbps_end(xhp);
xbps_end();
fprintf(stderr,
"Usage: xbps-bin [options] [target] [arguments]\n"
"See xbps-bin(8) for more information.\n");
@ -53,9 +51,7 @@ usage(struct xbps_handle *xhp)
static void __attribute__((noreturn))
cleanup(int signum)
{
struct xbps_handle *xhp = xbps_handle_get();
xbps_end(xhp);
xbps_end();
exit(signum);
}
@ -127,7 +123,7 @@ main(int argc, char **argv)
break;
case '?':
default:
usage(NULL);
usage();
}
}
@ -135,7 +131,7 @@ main(int argc, char **argv)
argv += optind;
if (argc < 1)
usage(NULL);
usage();
/* Specifying -A and -M is illegal */
if (install_manual && install_auto) {
@ -180,7 +176,7 @@ main(int argc, char **argv)
if (strcasecmp(argv[0], "list") == 0) {
/* Lists packages currently registered in database. */
if (argc < 1 || argc > 2)
usage(&xh);
usage();
lpc.check_state = true;
lpc.state = 0;
@ -216,7 +212,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "install") == 0) {
/* Installs a binary package and required deps. */
if (argc < 2)
usage(&xh);
usage();
for (i = 1; i < argc; i++)
if ((rv = install_new_pkg(argv[i], reinstall)) != 0)
@ -227,7 +223,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "update") == 0) {
/* Update an installed package. */
if (argc < 2)
usage(&xh);
usage();
for (i = 1; i < argc; i++)
if ((rv = update_pkg(argv[i])) != 0)
@ -238,7 +234,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "remove") == 0) {
/* Removes a package. */
if (argc < 2)
usage(&xh);
usage();
for (i = 1; i < argc; i++) {
rv = remove_pkg(argv[i], recursive_rm);
@ -258,7 +254,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "show") == 0) {
/* Shows info about an installed binary package. */
if (argc != 2)
usage(&xh);
usage();
rv = show_pkg_info_from_metadir(argv[1], option);
if (rv != 0) {
@ -269,7 +265,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "show-files") == 0) {
/* Shows files installed by a binary package. */
if (argc != 2)
usage(&xh);
usage();
rv = show_pkg_files_from_metadir(argv[1]);
if (rv != 0) {
@ -280,7 +276,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "check") == 0) {
/* Checks the integrity of an installed package. */
if (argc != 2)
usage(&xh);
usage();
if (strcasecmp(argv[1], "all") == 0)
rv = check_pkg_integrity_all();
@ -292,7 +288,7 @@ main(int argc, char **argv)
* To update all packages currently installed.
*/
if (argc != 1)
usage(&xh);
usage();
rv = autoupdate_pkgs(yes, show_download_pkglist_url);
@ -302,7 +298,7 @@ main(int argc, char **argv)
* orphans.
*/
if (argc != 1)
usage(&xh);
usage();
rv = show_orphans();
@ -313,7 +309,7 @@ main(int argc, char **argv)
* on it currently.
*/
if (argc != 1)
usage(&xh);
usage();
rv = autoremove_pkgs(yes);
@ -322,7 +318,7 @@ main(int argc, char **argv)
* Reconfigure a package.
*/
if (argc != 2)
usage(&xh);
usage();
if (strcasecmp(argv[1], "all") == 0)
rv = xbps_configure_packages(true);
@ -334,7 +330,7 @@ main(int argc, char **argv)
* Show dependencies for a package.
*/
if (argc != 2)
usage(&xh);
usage();
rv = show_pkg_deps(argv[1]);
@ -344,7 +340,7 @@ main(int argc, char **argv)
* dependencies.
*/
if (argc != 1)
usage(&xh);
usage();
rv = xbps_regpkgdb_foreach_pkg_cb(list_manual_pkgs, NULL);
@ -353,7 +349,7 @@ main(int argc, char **argv)
* Show reverse dependencies for a package.
*/
if (argc != 2)
usage(&xh);
usage();
rv = show_pkg_reverse_deps(argv[1]);
@ -363,15 +359,15 @@ main(int argc, char **argv)
* packages.
*/
if (argc < 2)
usage(&xh);
usage();
rv = find_files_in_packages(argc, argv);
} else {
usage(&xh);
usage();
}
out:
xbps_end(&xh);
xbps_end();
exit(rv);
}

View File

@ -1,5 +1,5 @@
/*-
* Copyright (c) 2010-2011 Juan Romero Pardines.
* Copyright (c) 2010-2012 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -94,7 +94,7 @@ die(const char *fmt, ...)
vfprintf(stderr, fmt, ap);
fprintf(stderr, " (%s)\n", strerror(save_errno));
va_end(ap);
xbps_end(xbps_handle_get());
xbps_end();
exit(EXIT_FAILURE);
}

View File

@ -39,11 +39,9 @@
#include "../xbps-bin/defs.h"
static void __attribute__((noreturn))
usage(struct xbps_handle *xhp)
usage(void)
{
if (xhp != NULL)
xbps_end(xhp);
xbps_end();
fprintf(stderr,
"Usage: xbps-repo [options] [action] [arguments]\n"
"See xbps-repo(8) for more information.\n");
@ -86,7 +84,7 @@ main(int argc, char **argv)
exit(EXIT_SUCCESS);
case '?':
default:
usage(NULL);
usage();
}
}
@ -94,7 +92,7 @@ main(int argc, char **argv)
argv += optind;
if (argc < 1)
usage(NULL);
usage();
/*
* Initialize XBPS subsystems.
@ -117,7 +115,7 @@ main(int argc, char **argv)
if (strcasecmp(argv[0], "list") == 0) {
/* Lists all repositories registered in pool. */
if (argc != 1)
usage(&xh);
usage();
rv = xbps_repository_pool_foreach(repo_list_uri_cb, NULL);
if (rv == ENOTSUP)
@ -131,7 +129,7 @@ main(int argc, char **argv)
* Only list packages for the target repository.
*/
if (argc < 1 || argc > 2)
usage(&xh);
usage();
rv = xbps_repository_pool_foreach(repo_pkg_list_cb, argv[1]);
if (rv == ENOTSUP)
@ -146,7 +144,7 @@ main(int argc, char **argv)
* by using shell style match patterns (fnmatch(3)).
*/
if (argc < 2)
usage(&xh);
usage();
rsd = malloc(sizeof(*rsd));
if (rsd == NULL) {
@ -166,7 +164,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "show") == 0) {
/* Shows info about a binary package. */
if (argc != 2)
usage(&xh);
usage();
rv = show_pkg_info_from_repolist(argv[1], option);
if (rv == ENOENT) {
@ -182,7 +180,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "show-deps") == 0) {
/* Shows the required run dependencies for a package. */
if (argc != 2)
usage(&xh);
usage();
rv = show_pkg_deps_from_repolist(argv[1]);
if (rv == ENOENT) {
@ -198,7 +196,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "show-files") == 0) {
/* Shows the package files in a binary package */
if (argc != 2)
usage(&xh);
usage();
pkgd = xbps_repository_pool_dictionary_metadata_plist(argv[1],
XBPS_PKGFILES);
@ -222,7 +220,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "find-files") == 0) {
/* Finds files by patterns, exact matches and components. */
if (argc < 2)
usage(&xh);
usage();
rv = repo_find_files_in_packages(argc, argv);
if (rv == ENOTSUP) {
@ -232,7 +230,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "genindex") == 0) {
/* Generates a package repository index plist file. */
if (argc != 2)
usage(&xh);
usage();
rv = repo_genindex(argv[1]);
if (rv == 0)
@ -241,7 +239,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "sync") == 0) {
/* Syncs the pkg index for all registered remote repos */
if (argc != 1)
usage(&xh);
usage();
rv = xbps_repository_pool_sync(&xh);
if (rv == ENOTSUP) {
@ -249,10 +247,10 @@ main(int argc, char **argv)
"currently registered!\n");
}
} else {
usage(&xh);
usage();
}
out:
xbps_end(&xh);
xbps_end();
exit(rv ? EXIT_FAILURE : EXIT_SUCCESS);
}

View File

@ -58,11 +58,9 @@ write_plist_file(prop_dictionary_t dict, const char *file)
}
static void __attribute__((noreturn))
usage(struct xbps_handle *xhp)
usage(void)
{
if (xhp != NULL)
xbps_end(xhp);
xbps_end();
fprintf(stderr,
"usage: xbps-uhelper [options] [action] [args]\n"
"\n"
@ -135,7 +133,7 @@ main(int argc, char **argv)
exit(EXIT_SUCCESS);
case '?':
default:
usage(NULL);
usage();
}
}
@ -143,7 +141,7 @@ main(int argc, char **argv)
argv += optind;
if (argc < 1)
usage(NULL);
usage();
if ((strcasecmp(argv[0], "register") == 0) ||
(strcasecmp(argv[0], "unregister") == 0) ||
@ -172,7 +170,7 @@ main(int argc, char **argv)
if (strcasecmp(argv[0], "register") == 0) {
/* Registers a package into the database */
if (argc != 4)
usage(&xh);
usage();
dict = prop_dictionary_create();
if (dict == NULL) {
@ -225,7 +223,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "unregister") == 0) {
/* Unregisters a package from the database */
if (argc != 3)
usage(&xh);
usage();
rv = xbps_unregister_pkg(argv[1], argv[2], true);
if (rv == ENOENT) {
@ -244,7 +242,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "version") == 0) {
/* Prints version of an installed package */
if (argc != 2)
usage(&xh);
usage();
dict = xbps_regpkgdb_get_pkgd(argv[1], false);
if (dict == NULL) {
@ -258,7 +256,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "sanitize-plist") == 0) {
/* Sanitize a plist file (properly indent the file) */
if (argc != 2)
usage(NULL);
usage();
dict = prop_dictionary_internalize_from_zfile(argv[1]);
if (dict == NULL) {
@ -272,7 +270,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "getpkgversion") == 0) {
/* Returns the version of a pkg string */
if (argc != 2)
usage(NULL);
usage();
version = xbps_pkg_version(argv[1]);
if (version == NULL) {
@ -285,7 +283,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "getpkgname") == 0) {
/* Returns the name of a pkg string */
if (argc != 2)
usage(NULL);
usage();
pkgname = xbps_pkg_name(argv[1]);
if (pkgname == NULL) {
@ -299,7 +297,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "getpkgrevision") == 0) {
/* Returns the revision of a pkg string */
if (argc != 2)
usage(NULL);
usage();
version = xbps_pkg_revision(argv[1]);
if (version == NULL)
@ -310,7 +308,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "getpkgdepname") == 0) {
/* Returns the pkgname of a dependency */
if (argc != 2)
usage(NULL);
usage();
pkgname = xbps_pkgpattern_name(argv[1]);
if (pkgname == NULL)
@ -321,7 +319,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "getpkgdepversion") == 0) {
/* returns the version of a package pattern dependency */
if (argc != 2)
usage(NULL);
usage();
version = xbps_pkgpattern_version(argv[1]);
if (version == NULL)
@ -332,21 +330,21 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "pkgmatch") == 0) {
/* Matches a pkg with a pattern */
if (argc != 3)
usage(NULL);
usage();
exit(xbps_pkgpattern_match(argv[1], argv[2]));
} else if (strcasecmp(argv[0], "cmpver") == 0) {
/* Compare two version strings, installed vs required */
if (argc != 3)
usage(NULL);
usage();
exit(xbps_cmpver(argv[1], argv[2]));
} else if (strcasecmp(argv[0], "digest") == 0) {
/* Prints SHA256 hashes for specified files */
if (argc < 2)
usage(NULL);
usage();
for (i = 1; i < argc; i++) {
hash = xbps_file_hash(argv[i]);
@ -363,7 +361,7 @@ main(int argc, char **argv)
} else if (strcasecmp(argv[0], "fetch") == 0) {
/* Fetch a file from specified URL */
if (argc != 2)
usage(&xh);
usage();
for (i = 1; i < argc; i++) {
rv = xbps_fetch_file(argv[i], ".", false, "v");
@ -378,10 +376,10 @@ main(int argc, char **argv)
}
} else {
usage(NULL);
usage();
}
out:
xbps_end(&xh);
xbps_end();
exit(rv ? EXIT_FAILURE : EXIT_SUCCESS);
}

View File

@ -56,7 +56,7 @@
*/
#define XBPS_PKGINDEX_VERSION "1.3"
#define XBPS_API_VERSION "20120117-1"
#define XBPS_API_VERSION "20120117-2"
#define XBPS_VERSION "0.12"
/**
@ -580,14 +580,11 @@ int xbps_init(struct xbps_handle *xhp);
/**
* Releases all resources used by libxbps.
*
* @param[in] xhp Pointer to an xbps_handle structure.
* @note It's assumed that \a xhp is a valid pointer.
*/
void xbps_end(struct xbps_handle *xhp);
void xbps_end(void);
/**
* Returns a pointer to the xbps_handle structure set by xbps_init().
* Returns a pointer to the xbps_handle structure passed to xbps_init().
*/
struct xbps_handle *xbps_handle_get(void);

View File

@ -40,6 +40,7 @@
* using libxbps and finalize usage to release resources at the end.
*/
static bool debug;
static bool xbps_initialized;
static struct xbps_handle *xhp;
static void
@ -125,7 +126,6 @@ xbps_init(struct xbps_handle *xh)
* We'll use defaults without any repo or
* virtual packages.
*/
xbps_end(xh);
return rv;
}
errno = 0;
@ -133,7 +133,6 @@ xbps_init(struct xbps_handle *xh)
/*
* Parser error from configuration file.
*/
xbps_end(xh);
return ENOTSUP;
}
}
@ -156,10 +155,9 @@ xbps_init(struct xbps_handle *xh)
xhp->cachedir = cfg_getstr(xhp->cfg, "cachedir");
}
get_cachedir(xhp);
if (xhp->cachedir_priv == NULL) {
xbps_end(xh);
if (xhp->cachedir_priv == NULL)
return ENOMEM;
}
xhp->cachedir = xhp->cachedir_priv;
if (xhp->cfg == NULL) {
@ -187,31 +185,33 @@ xbps_init(struct xbps_handle *xh)
xbps_dbg_printf("TransactionFrequencyFlush=%u\n",
xhp->transaction_frequency_flush);
xbps_initialized = true;
return 0;
}
void
xbps_end(struct xbps_handle *xh)
xbps_end(void)
{
if (xh == NULL)
if (!xbps_initialized)
return;
xbps_regpkgdb_dictionary_release(xh);
xbps_repository_pool_release(xh);
xbps_regpkgdb_dictionary_release(xhp);
xbps_repository_pool_release(xhp);
xbps_fetch_unset_cache_connection();
if (xh->cfg != NULL)
cfg_free(xh->cfg);
if (xh->cachedir_priv != NULL)
free(xh->cachedir_priv);
if (xhp->cfg != NULL)
cfg_free(xhp->cfg);
if (xhp->cachedir_priv != NULL)
free(xhp->cachedir_priv);
xhp = NULL;
xbps_initialized = false;
}
struct xbps_handle *
xbps_handle_get(void)
{
assert(xhp != NULL);
return xhp;
}