Introduce xbps_xasprintf() and use it where required.

--HG--
extra : convert_revision : 324a92a4824476059e0993d00c2b925c732a79f8
This commit is contained in:
Juan RP 2009-04-04 19:32:39 +02:00
parent f5dd58df5d
commit 4bf5c5f750
14 changed files with 108 additions and 175 deletions

View file

@ -128,7 +128,7 @@ main(int argc, char **argv)
prop_array_t reqby, orphans;
prop_object_t obj;
prop_object_iterator_t iter;
const char *pkgname, *version, *sep;
const char *pkgname, *version, *sep, *rootdir;
char *plist;
int c, flags = 0, rv = 0;
bool chkhash = false, forcerm = false, verbose = false;
@ -169,7 +169,9 @@ main(int argc, char **argv)
if (argc != 1)
usage();
plist = xbps_append_full_path(true, NULL, XBPS_REGPKGDB);
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REGPKGDB);
if (plist == NULL)
exit(EXIT_FAILURE);

View file

@ -114,7 +114,7 @@ main(int argc, char **argv)
if (argc < 1)
usage();
plist = xbps_append_full_path(true, NULL, XBPS_REGPKGDB);
plist = xbps_xasprintf("%s/%s/%s", root, XBPS_META_PATH, XBPS_REGPKGDB);
if (plist == NULL) {
printf("=> ERROR: couldn't find regpkdb file (%s)\n",
strerror(errno));

View file

@ -262,7 +262,7 @@ xbps_repo_genindex(const char *pkgdir)
(strcmp(archdirs[i], "noarch")))
continue;
path = xbps_append_full_path(false, pkgdir, archdirs[i]);
path = xbps_xasprintf("%s/%s", pkgdir, archdirs[i]);
if (path == NULL)
return errno;
@ -282,8 +282,7 @@ xbps_repo_genindex(const char *pkgdir)
continue;
foundpkg = true;
binfile = xbps_append_full_path(false, path,
dp->d_name);
binfile = xbps_xasprintf("%s/%s", path, dp->d_name);
if (binfile == NULL) {
(void)closedir(dirp);
free(path);

View file

@ -181,24 +181,15 @@ int
show_pkg_info_from_metadir(const char *pkgname)
{
prop_dictionary_t pkgd;
size_t len = 0;
char *plist, *path;
const char *rootdir;
char *plist;
/* XBPS_META_PATH/metadata/<pkgname> + NULL */
len = strlen(XBPS_META_PATH) + strlen(pkgname) + 12;
path = malloc(len);
if (path == NULL)
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/metadata/%s/%s", rootdir,
XBPS_META_PATH, pkgname, XBPS_PKGPROPS);
if (plist == NULL)
return EINVAL;
(void)snprintf(path, len, "%s/metadata/%s", XBPS_META_PATH, pkgname);
plist = xbps_append_full_path(true, path, XBPS_PKGPROPS);
if (plist == NULL) {
free(path);
return EINVAL;
}
free(path);
pkgd = prop_dictionary_internalize_from_file(plist);
if (pkgd == NULL) {
free(plist);
@ -217,27 +208,15 @@ show_pkg_files_from_metadir(const char *pkgname, bool hash)
{
prop_dictionary_t pkgd;
struct show_files_cb sfc;
size_t len = 0;
const char *destdir = xbps_get_rootdir();
char *plist, *path;
char *plist;
int rv = 0;
/* XBPS_META_PATH/metadata/<pkgname>/XBPS_PKGFILES + NULL */
len = strlen(XBPS_META_PATH) + strlen(pkgname) +
strlen(XBPS_PKGFILES) + 12;
path = malloc(len);
if (path == NULL)
plist = xbps_xasprintf("%s/%s/metadata/%s/%s", destdir,
XBPS_META_PATH, pkgname, XBPS_PKGFILES);
if (plist == NULL)
return EINVAL;
(void)snprintf(path, len, "%s/metadata/%s", XBPS_META_PATH, pkgname);
plist = xbps_append_full_path(true, path, XBPS_PKGFILES);
if (plist == NULL) {
free(path);
return EINVAL;
}
free(path);
pkgd = prop_dictionary_internalize_from_file(plist);
if (pkgd == NULL) {
free(plist);
@ -277,7 +256,7 @@ show_pkg_files(prop_object_t obj, void *arg, bool *loop_done)
if (sfc->check_hash && file != NULL) {
printf("%s", file);
if (sfc->destdir) {
path = xbps_append_full_path(false, sfc->destdir, file);
path = xbps_xasprintf("%s/%s", sfc->destdir, file);
if (path == NULL)
return EINVAL;
}

View file

@ -27,7 +27,7 @@
#define _XBPS_UTIL_H_
/* From lib/util.c */
char * xbps_append_full_path(bool, const char *, const char *);
char * xbps_xasprintf(const char *, ...);
char * xbps_get_file_hash(const char *);
int xbps_check_file_hash(const char *, const char *);
int xbps_check_pkg_file_hash(prop_dictionary_t, const char *);

View file

@ -93,14 +93,18 @@ xbps_install_binary_pkg(const char *pkgname, bool update)
prop_array_t array;
prop_object_t obj;
prop_object_iterator_t repolist_iter;
size_t len = 0;
const char *repoloc, *instver;
const char *repoloc, *instver, *rootdir;
char *plist, *pkg;
int rv = 0;
assert(pkgname != NULL);
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
rootdir = xbps_get_rootdir();
if (rootdir == NULL)
rootdir = "";
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REPOLIST);
if (plist == NULL)
return EINVAL;
@ -164,13 +168,11 @@ xbps_install_binary_pkg(const char *pkgname, bool update)
* installed, and return immediately in that case.
*/
prop_dictionary_get_cstring_nocopy(pkgrd, "version", &instver);
len = strlen(pkgname) + strlen(instver) + 2;
pkg = malloc(len);
pkg = xbps_xasprintf("%s-%s", pkgname, instver);
if (pkg == NULL) {
rv = errno;
goto out;
}
(void)snprintf(pkg, len, "%s-%s", pkgname, instver);
if (xbps_check_is_installed_pkg(pkg) == 0) {
free(pkg);
rv = EEXIST;
@ -243,11 +245,13 @@ xbps_register_pkg(prop_dictionary_t pkgrd, bool update, bool automatic)
{
prop_dictionary_t dict, pkgd, newpkgd;
prop_array_t array;
const char *pkgname, *version, *desc;
const char *pkgname, *version, *desc, *rootdir;
char *plist;
int rv = 0;
plist = xbps_append_full_path(true, NULL, XBPS_REGPKGDB);
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REGPKGDB);
if (plist == NULL)
return EINVAL;

View file

@ -66,7 +66,6 @@ find_indirect_orphan_pkg(prop_object_t obj, void *arg, bool *loop_done)
{
struct orphan *orphan = arg;
prop_array_t reqby;
size_t len = 0;
char *pkg;
bool automatic = false;
@ -82,13 +81,10 @@ find_indirect_orphan_pkg(prop_object_t obj, void *arg, bool *loop_done)
if (reqby == NULL || prop_array_count(reqby) != 1)
return 0;
len = strlen(orphan->pkgname) + strlen(orphan->version) + 2;
pkg = malloc(len);
pkg = xbps_xasprintf("%s-%s", orphan->pkgname, orphan->version);
if (pkg == NULL)
return ENOMEM;
(void)snprintf(pkg, len, "%s-%s", orphan->pkgname, orphan->version);
if (xbps_find_string_in_array(reqby, pkg)) {
if (!prop_array_add(orphan->array, obj)) {
free(pkg);
@ -107,10 +103,13 @@ xbps_find_orphan_packages(void)
prop_object_t obj;
prop_object_iterator_t iter;
struct orphan orphan;
const char *rootdir;
char *plist;
int rv = 0;
plist = xbps_append_full_path(true, NULL, XBPS_REGPKGDB);
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REGPKGDB);
if (plist == NULL)
return NULL;

View file

@ -70,12 +70,15 @@ xbps_callback_array_iter_in_repolist(int (*fn)(prop_object_t, void *, bool *),
void *arg)
{
prop_dictionary_t repolistd;
const char *rootdir;
char *plist;
int rv = 0;
assert(fn != NULL);
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REPOLIST);
if (plist == NULL)
return EINVAL;
@ -158,9 +161,12 @@ prop_dictionary_t
xbps_find_pkg_installed_from_plist(const char *pkgname)
{
prop_dictionary_t pkgd;
const char *rootdir;
char *plist;
plist = xbps_append_full_path(true, NULL, XBPS_REGPKGDB);
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REGPKGDB);
if (plist == NULL)
return NULL;

View file

@ -41,12 +41,15 @@ static int remove_pkg_files(prop_object_t, void *, bool *);
int
xbps_unregister_pkg(const char *pkgname)
{
const char *rootdir;
char *plist;
int rv = 0;
assert(pkgname != NULL);
plist = xbps_append_full_path(true, NULL, XBPS_REGPKGDB);
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REGPKGDB);
if (plist == NULL)
return EINVAL;
@ -63,18 +66,19 @@ xbps_remove_binary_pkg_meta(const char *pkgname)
{
struct dirent *dp;
DIR *dirp;
const char *rootdir = xbps_get_rootdir();
char metadir[PATH_MAX - 1], path[PATH_MAX - 1];
const char *rootdir;
char *metadir, *path;
int flags = 0, rv = 0;
assert(pkgname != NULL);
rootdir = xbps_get_rootdir();
if (rootdir == NULL)
rootdir = "";
flags = xbps_get_flags();
(void)snprintf(metadir, sizeof(metadir), "%s%s/metadata/%s",
rootdir, XBPS_META_PATH, pkgname);
metadir = xbps_xasprintf("%s/%s/metadata/%s", rootdir,
XBPS_META_PATH, pkgname);
if (metadir == NULL)
return errno;
dirp = opendir(metadir);
if (dirp == NULL)
@ -85,9 +89,10 @@ xbps_remove_binary_pkg_meta(const char *pkgname)
(strcmp(dp->d_name, "..") == 0))
continue;
if (snprintf(path, sizeof(path), "%s%s/metadata/%s/%s",
rootdir, XBPS_META_PATH, pkgname, dp->d_name) < 0) {
path = xbps_xasprintf("%s/%s", metadir, dp->d_name);
if (path == NULL) {
(void)closedir(dirp);
free(metadir);
return -1;
}
@ -96,10 +101,11 @@ xbps_remove_binary_pkg_meta(const char *pkgname)
printf("WARNING: can't remove %s (%s)\n",
pkgname, strerror(errno));
}
(void)memset(&path, 0, sizeof(path));
free(path);
}
(void)closedir(dirp);
rv = rmdir(metadir);
free(metadir);
return rv;
}
@ -108,7 +114,7 @@ static int
remove_pkg_files(prop_object_t obj, void *arg, bool *loop_done)
{
prop_bool_t bobj;
const char *file = NULL, *rootdir, *sha256, *type;
const char *file, *rootdir, *sha256, *type;
char *path = NULL;
int flags = 0, rv = 0;
@ -116,14 +122,12 @@ remove_pkg_files(prop_object_t obj, void *arg, bool *loop_done)
(void)loop_done;
rootdir = xbps_get_rootdir();
if (rootdir == NULL)
rootdir = "";
flags = xbps_get_flags();
if (!prop_dictionary_get_cstring_nocopy(obj, "file", &file))
return EINVAL;
path = xbps_append_full_path(false, rootdir, file);
path = xbps_xasprintf("%s/%s", rootdir, file);
if (path == NULL)
return EINVAL;
@ -209,9 +213,8 @@ xbps_remove_binary_pkg(const char *pkgname, bool update)
{
prop_dictionary_t dict;
const char *rootdir = xbps_get_rootdir();
char path[PATH_MAX - 1], *buf;
char *path, *buf;
int fd, rv = 0;
size_t len = 0;
bool prepostf = false;
assert(pkgname != NULL);
@ -229,20 +232,11 @@ xbps_remove_binary_pkg(const char *pkgname, bool update)
if (xbps_check_is_installed_pkgname(pkgname) == false)
return ENOENT;
/*
* This length is '%s%s/metadata/%s/REMOVE' + NULL.
*/
len = strlen(XBPS_META_PATH) + strlen(rootdir) + strlen(pkgname) + 19;
buf = malloc(len);
buf = xbps_xasprintf("%s/%s/metadata/%s/REMOVE", rootdir,
XBPS_META_PATH, pkgname);
if (buf == NULL)
return errno;
if (snprintf(buf, len, "%s%s/metadata/%s/REMOVE",
rootdir, XBPS_META_PATH, pkgname) < 0) {
free(buf);
return -1;
}
/*
* Find out if the REMOVE file exists.
*/
@ -272,14 +266,20 @@ xbps_remove_binary_pkg(const char *pkgname, bool update)
* Iterate over the pkg file list dictionary and remove all
* files/dirs associated.
*/
(void)snprintf(path, sizeof(path), "%s%s/metadata/%s/files.plist",
path = xbps_xasprintf("%s/%s/metadata/%s/files.plist",
rootdir, XBPS_META_PATH, pkgname);
if (path == NULL) {
free(buf);
return errno;
}
dict = prop_dictionary_internalize_from_file(path);
if (dict == NULL) {
free(buf);
free(path);
return errno;
}
free(path);
rv = xbps_callback_array_iter_in_dict(dict, "filelist",
remove_pkg_files, NULL);

View file

@ -39,12 +39,15 @@ xbps_register_repository(const char *uri)
prop_dictionary_t dict;
prop_array_t array;
prop_object_t obj = NULL;
const char *rootdir;
char *plist;
int rv = 0;
assert(uri != NULL);
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REPOLIST);
if (plist == NULL)
return errno;
@ -112,12 +115,15 @@ xbps_unregister_repository(const char *uri)
{
prop_dictionary_t dict;
prop_array_t array;
const char *rootdir;
char *plist;
int rv = 0;
assert(uri != NULL);
plist = xbps_append_full_path(true, NULL, XBPS_REPOLIST);
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REPOLIST);
if (plist == NULL)
return errno;

View file

@ -161,10 +161,13 @@ int
xbps_requiredby_pkg_remove(const char *pkgname)
{
prop_dictionary_t dict;
const char *rootdir;
char *plist;
int rv = 0;
plist = xbps_append_full_path(true, NULL, XBPS_REGPKGDB);
rootdir = xbps_get_rootdir();
plist = xbps_xasprintf("%s/%s/%s", rootdir,
XBPS_META_PATH, XBPS_REGPKGDB);
if (plist == NULL)
return EINVAL;
@ -193,20 +196,16 @@ xbps_requiredby_pkg_add(prop_array_t regar, prop_dictionary_t pkg, bool update)
prop_array_t rdeps;
prop_object_t obj, obj2;
prop_object_iterator_t iter, iter2;
size_t len = 0;
const char *reqname, *pkgname, *version;
char *rdepname, *fpkgn;
int rv = 0;
prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(pkg, "version", &version);
len = strlen(pkgname) + strlen(version) + 2;
fpkgn = malloc(len);
fpkgn = xbps_xasprintf("%s-%s", pkgname, version);
if (fpkgn == NULL)
return ENOMEM;
(void)snprintf(fpkgn, len, "%s-%s", pkgname, version);
rdeps = prop_dictionary_get(pkg, "run_depends");
if (rdeps == NULL || prop_array_count(rdeps) == 0) {
free(fpkgn);

View file

@ -42,7 +42,7 @@ int
xbps_unpack_binary_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
{
prop_string_t filename, repoloc, arch;
char *binfile, *path;
char *binfile;
int rv = 0;
assert(pkg != NULL);
@ -55,19 +55,12 @@ xbps_unpack_binary_pkg(prop_dictionary_t repo, prop_dictionary_t pkg)
else
repoloc = prop_dictionary_get(pkg, "repository");
path = xbps_append_full_path(false,
binfile = xbps_xasprintf("%s/%s/%s",
prop_string_cstring_nocopy(repoloc),
prop_string_cstring_nocopy(arch));
if (path == NULL)
return EINVAL;
binfile = xbps_append_full_path(false, path,
prop_string_cstring_nocopy(arch),
prop_string_cstring_nocopy(filename));
if (binfile == NULL) {
free(path);
if (binfile == NULL)
return EINVAL;
}
free(path);
rv = unpack_archive_init(pkg, binfile);
free(binfile);
@ -136,7 +129,6 @@ static int
unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg)
{
struct archive_entry *entry;
size_t len;
const char *prepost = "./INSTALL";
const char *pkgname, *version, *rootdir;
char *buf;
@ -154,7 +146,6 @@ unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg)
} else {
if (chdir("/") == -1)
return errno;
rootdir = "";
}
prop_dictionary_get_cstring_nocopy(pkg, "pkgname", &pkgname);
@ -165,19 +156,11 @@ unpack_archive_fini(struct archive *ar, prop_dictionary_t pkg)
else
lflags = EXTRACT_FLAGS;
/*
* This length is '.%s/metadata/%s/INSTALL' + NULL.
*/
len = strlen(XBPS_META_PATH) + strlen(pkgname) + 20;
buf = malloc(len);
buf = xbps_xasprintf(".%s/metadata/%s/INSTALL",
XBPS_META_PATH, pkgname);
if (buf == NULL)
return ENOMEM;
return errno;
if (snprintf(buf, len, ".%s/metadata/%s/INSTALL",
XBPS_META_PATH, pkgname) < 0) {
free(buf);
return -1;
}
while (archive_read_next_header(ar, &entry) == ARCHIVE_OK) {
/*
* Run the pre installation action target if there's a script

View file

@ -26,6 +26,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
@ -80,7 +81,7 @@ int
xbps_check_pkg_file_hash(prop_dictionary_t pkgd, const char *repoloc)
{
const char *sha256, *arch, *filename;
char *binfile, *path;
char *binfile;
int rv = 0;
assert(repoloc != NULL);
@ -95,17 +96,10 @@ xbps_check_pkg_file_hash(prop_dictionary_t pkgd, const char *repoloc)
if (!prop_dictionary_get_cstring_nocopy(pkgd, "architecture", &arch))
return EINVAL;
path = xbps_append_full_path(false, repoloc, arch);
if (path == NULL)
binfile = xbps_xasprintf("%s/%s/%s", repoloc, arch, filename);
if (binfile == NULL)
return EINVAL;
binfile = xbps_append_full_path(false, path, filename);
if (binfile == NULL) {
free(path);
return EINVAL;
}
free(path);
printf("Checking SHA256 for %s ... ", filename);
(void)fflush(stdout);
@ -212,25 +206,13 @@ char *
xbps_get_pkg_index_plist(const char *path)
{
struct utsname un;
char *plist, *p;
assert(path != NULL);
if (uname(&un) == -1)
return NULL;
p = xbps_append_full_path(false, path, un.machine);
if (p == NULL)
return NULL;
plist = xbps_append_full_path(false, p, XBPS_PKGINDEX);
if (plist == NULL) {
free(p);
return NULL;
}
free(p);
return plist;
return xbps_xasprintf("%s/%s/%s", path, un.machine, XBPS_PKGINDEX);
}
bool
@ -256,6 +238,9 @@ xbps_set_rootdir(const char *dir)
const char *
xbps_get_rootdir(void)
{
if (rootdir == NULL)
rootdir = "";
return rootdir;
}
@ -272,45 +257,15 @@ xbps_get_flags(void)
}
char *
xbps_append_full_path(bool use_rootdir, const char *basedir, const char *plist)
xbps_xasprintf(const char *fmt, ...)
{
const char *env;
char *buf = NULL;
size_t len = 0;
va_list ap;
char *buf;
assert(plist != NULL);
if (basedir)
env = basedir;
else
env = XBPS_META_PATH;
if (rootdir && use_rootdir) {
len = strlen(rootdir) + strlen(env) + strlen(plist) + 2;
buf = malloc(len + 1);
if (buf == NULL) {
errno = ENOMEM;
return NULL;
}
if (snprintf(buf, len + 1, "%s/%s/%s",
rootdir, env, plist) < 0) {
errno = ENOSPC;
return NULL;
}
} else {
len = strlen(env) + strlen(plist) + 1;
buf = malloc(len + 1);
if (buf == NULL) {
errno = ENOMEM;
return NULL;
}
if (snprintf(buf, len + 1, "%s/%s", env, plist) < 0) {
errno = ENOSPC;
return NULL;
}
}
va_start(ap, fmt);
if (vasprintf(&buf, fmt, ap) == -1)
return NULL;
va_end(ap);
return buf;
}

View file

@ -9,6 +9,7 @@ INSTALL_STRIPPED ?= -s
LDFLAGS += -L$(TOPDIR)/lib -L$(PREFIX)/lib -lxbps
CPPFLAGS += -I$(TOPDIR)/include -D_BSD_SOURCE -D_XOPEN_SOURCE=600
CPPFLAGS += -D_GNU_SOURCE
WARNFLAGS ?= -pedantic -std=c99 -Wall -Wextra -Werror -Wshadow -Wformat=2
WARNFLAGS += -Wmissing-declarations -Wcomment -Wunused-macros -Wendif-labels
CFLAGS += $(WARNFLAGS) -O2 -fPIC -DPIC