Browse files

Add error functions and wrapper functions

Previously, we used to return errors by:
ASSERT(some_condition, RET_ERR(SOME_ERR, return_val));

Which looks pretty ugly. Now, there is an "error" function which serves the
purpose of setting pwerrno, outputting the error message to stdout and has a
return value of -1. This suffices to replace almost all instances of RET_ERR
with:

return error(some_error);

if the function returns -1 upon failure.

In addition, wrapper.c and wrapper.h are new files which provide very simple
wrapper for the following functions:

calloc
malloc
strdup

These wrapper functions will cause the program to exit immediately upon memory
allocation error.

Signed-off-by: Pang Yan Han <pangyanhan@gmail.com>
  • Loading branch information...
1 parent c6c015a commit b848e42b88a1cb9eb0c45acbfd1fc237ee1d0294 @yanhan committed May 25, 2011
Showing with 235 additions and 114 deletions.
  1. +4 −4 Makefile.in
  2. +5 −2 conf.c
  3. +2 −5 curl.c
  4. +19 −13 download.c
  5. +10 −7 environment.c
  6. +63 −9 error.c
  7. +4 −0 error.h
  8. +5 −10 handle.c
  9. +4 −5 json.c
  10. +15 −12 package.c
  11. +10 −10 powaur.c
  12. +8 −3 powaur.h
  13. +3 −1 query.c
  14. +13 −13 sync.c
  15. +19 −16 util.c
  16. +5 −4 util.h
  17. +38 −0 wrapper.c
  18. +8 −0 wrapper.h
View
8 Makefile.in
@@ -31,10 +31,10 @@ POWAUR-VERSION-FILE: FORCE
-include POWAUR-VERSION-FILE
SRC=conf.c curl.c download.c environment.c error.c handle.c json.c package.c \
- powaur.c query.c sync.c util.c
+ powaur.c query.c sync.c wrapper.c util.c
HDRS=conf.h curl.h download.h environment.h error.h handle.h json.h package.h \
- powaur.h query.h sync.h util.h
+ powaur.h query.h sync.h wrapper.h util.h
OBJS=$(SRC:.c=.o)
@@ -51,16 +51,16 @@ powaur.o: powaur.c POWAUR-VERSION-GEN
powaur.o: EXTRA_CPPFLAGS = -DPOWAUR_VERSION='"$(POWAUR_VERSION)"'
-$(OBJS): environment.h powaur.h util.h
+$(OBJS): error.h environment.h powaur.h util.h
conf.o environment.o query.o: conf.h
download.o json.o sync.o: curl.h
download.o powaur.o sync.o: download.h
-error.o util.o: error.h
handle.o json.o powaur.o: handle.h
handle.o powaur.o sync.o: json.h
query.o powaur.o sync.o: package.h
json.o query.o: query.h
powaur.o sync.o: sync.h
+conf.o environment.o handle.o package.o util.o wrapper.o: wrapper.h
install: all
$(INSTALL) -d -m 755 $(DESTDIR)$(bindir)
View
7 conf.c
@@ -10,11 +10,12 @@
#include "environment.h"
#include "powaur.h"
#include "util.h"
+#include "wrapper.h"
struct config_t *config_init(void)
{
struct config_t *conf;
- CALLOC(conf, 1, sizeof(struct config_t), RET_ERR(PW_ERR_MEMORY, NULL));
+ conf = xcalloc(1, sizeof(struct config_t));
conf->op = PW_OP_MAIN;
conf->loglvl = PW_LOG_NORM | PW_LOG_INFO | PW_LOG_WARNING | PW_LOG_ERROR;
@@ -306,7 +307,9 @@ int parse_pmconfig(void)
int parsed_options = 0;
fp = fopen(comstrs.pmconf, "r");
- ASSERT(fp != NULL, RET_ERR(PW_ERR_PM_CONF_OPEN, -1));
+ if (!fp) {
+ return error(PW_ERR_PM_CONF_OPEN);
+ }
pw_printf(PW_LOG_DEBUG, "%s : Parsing %s\n", __func__, comstrs.pmconf);
View
7 curl.c
@@ -11,16 +11,13 @@ void curl_init(void)
curl = curl_easy_init();
if (!curl) {
- pw_fprintf(PW_LOG_ERROR, stderr, "Curl initialization failed.\n");
- return;
+ die_errno(PW_ERR_CURL_INIT);
}
initialized = 1;
- }
- if (curl) {
+ } else {
curl_easy_reset(curl);
-
/* Set timeout */
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
View
32 download.c
@@ -17,7 +17,9 @@ int download_single_file(const char *url, FILE *fp)
/* Must do this here since this function is shared */
curl_init();
- ASSERT(curl != NULL, RET_ERR(PW_ERR_CURL_INIT, -1));
+ if (!curl) {
+ return error(PW_ERR_CURL_INIT);
+ }
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
@@ -68,13 +70,11 @@ int download_single_package(const char *pkgname, alpm_list_t **failed_packages)
switch (errno) {
case EACCES:
- PW_SETERRNO(PW_ERR_ACCESS);
- break;
+ return error(PW_ERR_ACCESS, getcwd(filename, PATH_MAX));
case EISDIR:
- PW_SETERRNO(PW_ERR_ISDIR);
- break;
+ return error(PW_ERR_ISDIR, getcwd(filename, PATH_MAX));
default:
- PW_SETERRNO(PW_ERR_FOPEN);
+ return error(PW_ERR_ACCESS, filename);
}
ret = -1;
@@ -86,7 +86,7 @@ int download_single_package(const char *pkgname, alpm_list_t **failed_packages)
ret = download_single_file(url, fp);
cleanup:
- FCLOSE(fp);
+ fclose(fp);
if (!ret) {
pw_printf(PW_LOG_INFO, "Downloaded %s\n", filename);
@@ -150,24 +150,30 @@ int powaur_get(alpm_list_t *targets)
failed_packages = resolve = new_resolve = NULL;
- ASSERT(targets != NULL, RET_ERR(PW_ERR_TARGETS_NULL, -1));
+ if (!targets) {
+ return error(PW_ERR_TARGETS_NULL, "-G");
+ }
curl_init();
- ASSERT(curl != NULL, RET_ERR(PW_ERR_CURL_INIT, -1));
+ if (!curl) {
+ return error(PW_ERR_CURL_INIT);
+ }
/* Check for --target */
if (config->target_dir) {
if (!realpath(config->target_dir, dirpath)) {
- RET_ERR(PW_ERR_PATH_RESOLVE, -1);
+ return error(PW_ERR_PATH_RESOLVE, config->target_dir);
}
if (chdir(dirpath)) {
- RET_ERR(PW_ERR_CHDIR, -1);
+ return error(PW_ERR_CHDIR, dirpath);
}
-
- pw_printf(PW_LOG_INFO, "Downloading files to %s\n", dirpath);
+ } else {
+ getcwd(dirpath, PATH_MAX);
}
+ pw_printf(PW_LOG_INFO, "Downloading files to %s\n", dirpath);
+
for (i = targets; i; i = i->next) {
pwerrno = 0;
errors += dl_extract_single_package(i->data, &failed_packages);
View
17 environment.c
@@ -8,6 +8,7 @@
#include "conf.h"
#include "environment.h"
#include "util.h"
+#include "wrapper.h"
struct config_t *config;
@@ -43,7 +44,9 @@ struct commonstrings comstrs = {
int setup_config(void)
{
config = config_init();
- ASSERT(config != NULL, RET_ERR(PW_ERR_INIT_CONFIG, -1));
+ if (!config) {
+ return error(PW_ERR_INIT_CONFIG);
+ }
return 0;
}
@@ -61,20 +64,20 @@ static int setup_pacman_environment(int reload)
if (parse_pmconfig()) {
/* Free cachedirs */
FREELIST(pacman_cachedirs);
- RET_ERR(PW_ERR_PM_CONF_PARSE, -1);
+ return error(PW_ERR_PM_CONF_PARSE);
}
if (!pacman_rootdir) {
- STRDUP(pacman_rootdir, PACMAN_DEF_ROOTDIR, RET_ERR(PW_ERR_MEMORY, -1));
+ pacman_rootdir = xstrdup(PACMAN_DEF_ROOTDIR);
}
if (!pacman_dbpath) {
- STRDUP(pacman_dbpath, PACMAN_DEF_DBPATH, RET_ERR(PW_ERR_MEMORY, -1));
+ pacman_dbpath = xstrdup(PACMAN_DEF_DBPATH);
}
if (!pacman_cachedirs) {
pacman_cachedirs = alpm_list_add(pacman_cachedirs,
- strdup(PACMAN_DEF_CACHEDIR));
+ xstrdup(PACMAN_DEF_CACHEDIR));
}
alpm_option_set_root(pacman_rootdir);
@@ -146,13 +149,13 @@ static int setup_powaur_config(void)
/* Use default settings for unspecified options */
if (!powaur_dir) {
- STRDUP(powaur_dir, PW_DEF_DIR, RET_ERR(PW_ERR_MEMORY, -1));
+ powaur_dir = xstrdup(PW_DEF_DIR);
pw_printf(PW_LOG_DEBUG, "%sFalling back to default directory %s\n",
comstrs.tab, powaur_dir);
}
if (!powaur_editor) {
- STRDUP(powaur_editor, PW_DEF_EDITOR, RET_ERR(PW_ERR_MEMORY, -1));
+ powaur_editor = xstrdup(PW_DEF_EDITOR);
pw_printf(PW_LOG_DEBUG, "%sFalling back to default editor %s\n",
comstrs.tab, powaur_editor);
}
View
72 error.c
@@ -1,6 +1,52 @@
+#include <limits.h>
+#include <stdarg.h>
+
#include "environment.h"
#include "error.h"
#include "powaur.h"
+#include "util.h"
+
+int error(enum _pw_errno_t err, ...)
+{
+ char buf[PATH_MAX];
+ va_list args;
+
+ pwerrno = err;
+ va_start(args, err);
+ vsnprintf(buf, PATH_MAX, pw_strerror(err), args);
+ fprintf(stderr, "error: %s\n", buf);
+ va_end(args);
+
+ return -1;
+}
+
+void die(const char *msg, ...)
+{
+ char buf[PATH_MAX];
+ va_list ap;
+ va_start(ap, msg);
+
+ vsnprintf(buf, PATH_MAX, msg, ap);
+ fprintf(stderr, "fatal: %s\n", buf);
+
+ va_end(ap);
+ exit(1);
+}
+
+void die_errno(enum _pw_errno_t err, ...)
+{
+ pwerrno = err;
+
+ char buf[PATH_MAX];
+ va_list ap;
+ va_start(ap, err);
+
+ vsnprintf(buf, PATH_MAX, pw_strerror(err), ap);
+ fprintf(stderr, "fatal: %s\n", buf);
+
+ va_end(ap);
+ exit(1);
+}
const char *pw_strerrorlast(void)
{
@@ -18,14 +64,16 @@ const char *pw_strerror(enum _pw_errno_t err)
return "Handle initialization failed";
case PW_ERR_INIT_DIR:
return "Failed to setup powaur_dir";
- case PW_ERR_INIT_LOCAL_DB:
+ case PW_ERR_INIT_LOCALDB:
return "Failed to initialize local db";
/* Command parsing errors */
case PW_ERR_OP_UNKNOWN:
return "Unknown option";
case PW_ERR_OP_MULTI:
return "Multiple operations not allowed";
+ case PW_ERR_OP_NULL:
+ return "no operation specified (use -h for help)";
case PW_ERR_PM_CONF_OPEN:
return "Error opening /etc/pacman.conf";
@@ -34,7 +82,7 @@ const char *pw_strerror(enum _pw_errno_t err)
/* Fatal errors */
case PW_ERR_ACCESS:
- return "Insufficient permissions to write to directory";
+ return "Insufficient permissions to write to directory %s";
/* libalpm errors */
case PW_ERR_ALPM_RELOAD:
@@ -47,24 +95,30 @@ const char *pw_strerror(enum _pw_errno_t err)
/* General errors */
case PW_ERR_MEMORY:
return "Memory allocation error";
+
+ /* Path related errors */
case PW_ERR_GETCWD:
return "Error getting CWD";
case PW_ERR_RESTORECWD:
return "Error restoring CWD";
+ case PW_ERR_CHDIR:
+ return "Error changing dir to %s";
+ case PW_ERR_PATH_RESOLVE:
+ return "Failed to resolve path: %s";
+
+ /* File related errors */
case PW_ERR_ISDIR:
- return "Writing to an existing directory";
+ return "Name clash with existing directory \"%s\"";
case PW_ERR_FOPEN:
- return "Error opening file";
+ return "Error opening file %s";
case PW_ERR_FILE_EXTRACT:
return "File extraction failed";
case PW_ERR_OPENDIR:
return "Failed to open directory";
case PW_ERR_STAT:
- return "Failed to stat file/directory";
- case PW_ERR_PATH_RESOLVE:
- return "Failed to resolve path";
+ return "Failed to stat %s";
- /* fork errors */
+ /* Fork errors */
case PW_ERR_FORK_FAILED:
return "Forking of process failed";
case PW_ERR_WAITPID_FAILED:
@@ -94,7 +148,7 @@ const char *pw_strerror(enum _pw_errno_t err)
/* Search errors */
case PW_ERR_TARGETS_NULL:
- return "No target packages";
+ return "No package specified for %s";
default:
return "Unknown error";
View
4 error.h
@@ -3,6 +3,10 @@
#include "powaur.h"
+int error(enum _pw_errno_t err, ...);
+void die(const char *msg, ...);
+void die_errno(enum _pw_errno_t err, ...);
+
const char *pw_strerror(enum _pw_errno_t err);
const char *pw_strerrorlast(void);
View
15 handle.c
@@ -3,27 +3,22 @@
#include "handle.h"
#include "json.h"
-#include "util.h"
+#include "wrapper.h"
struct pwhandle_t *pwhandle = NULL;
struct pwhandle_t *_pwhandle_init(void)
{
struct pwhandle_t *hand;
- hand = calloc(1, sizeof(struct pwhandle_t));
- ASSERT(hand != NULL, RET_ERR(PW_ERR_MEMORY, NULL));
-
- hand->json_ctx = calloc(1, sizeof(struct json_ctx_t));
- ASSERT(hand->json_ctx != NULL, free(hand);
- RET_ERR(PW_ERR_MEMORY, NULL));
-
+ hand = xcalloc(1, sizeof(struct pwhandle_t));
+ hand->json_ctx = xcalloc(1, sizeof(struct json_ctx_t));
return hand;
}
void _pwhandle_free(struct pwhandle_t *hand)
{
if (hand) {
- FREE(hand->json_ctx);
- FREE(hand);
+ free(hand->json_ctx);
+ free(hand);
}
}
View
9 json.c
@@ -5,6 +5,7 @@
#include "curl.h"
#include "environment.h"
+#include "error.h"
#include "handle.h"
#include "json.h"
#include "powaur.h"
@@ -19,7 +20,9 @@ yajl_handle yajl_init(void)
pwhandle->json_ctx->jsondepth = 0;
yajl_handle yajl_hand = yajl_alloc(yajl_cbs, NULL, pwhandle->json_ctx);
- ASSERT(yajl_hand != NULL, RET_ERR(PW_ERR_MEMORY, NULL));
+ if (!yajl_hand) {
+ die_errno(PW_ERR_MEMORY);
+ }
return yajl_hand;
}
@@ -38,13 +41,9 @@ alpm_list_t *query_aur(const char *searchstr, enum aurquery_t query_type)
long httpresp;
curl_init();
- ASSERT(curl != NULL, RET_ERR(PW_ERR_CURL_INIT, NULL));
-
hand = yajl_init();
- ASSERT(hand != NULL, RET_ERR(PW_ERR_MEMORY, NULL));
/* Query AUR */
- curl_easy_reset(curl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, parse_json);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, hand);
View
27 package.c
@@ -11,11 +11,12 @@
#include "package.h"
#include "powaur.h"
#include "util.h"
+#include "wrapper.h"
struct aurpkg_t *aurpkg_new(void)
{
struct aurpkg_t *pkg;
- CALLOC(pkg, 1, sizeof(struct aurpkg_t), RET_ERR(PW_ERR_MEMORY, NULL));
+ pkg = xcalloc(1, sizeof(struct aurpkg_t));
return pkg;
}
@@ -25,14 +26,14 @@ void aurpkg_free(struct aurpkg_t *pkg)
return;
}
- FREE(pkg->id);
- FREE(pkg->name);
- FREE(pkg->version);
- FREE(pkg->category);
- FREE(pkg->desc);
- FREE(pkg->url);
- FREE(pkg->urlpath);
- FREE(pkg->license);
+ free(pkg->id);
+ free(pkg->name);
+ free(pkg->version);
+ free(pkg->category);
+ free(pkg->desc);
+ free(pkg->url);
+ free(pkg->urlpath);
+ free(pkg->license);
FREELIST(pkg->arch);
FREELIST(pkg->conflicts);
@@ -59,8 +60,7 @@ int aurpkg_vote_cmp(const void *a, const void *b)
struct pkginfo_t *pkginfo_new(const char *name, const char *ver, time_t d)
{
struct pkginfo_t *info;
- CALLOC(info, 1, sizeof(struct pkginfo_t), RET_ERR(PW_ERR_MEMORY,NULL));
-
+ info = xcalloc(1, sizeof(struct pkginfo_t));
info->name = strdup(name);
info->version = strdup(ver);
info->install_date = d;
@@ -354,7 +354,10 @@ alpm_list_t *resolve_dependencies(alpm_list_t *packages)
struct stat st;
localdb = alpm_option_get_localdb();
- ASSERT(localdb != NULL, RET_ERR(PW_ERR_LOCALDB_NULL, NULL));
+ if (!localdb) {
+ error(PW_ERR_LOCALDB_NULL);
+ return NULL;
+ }
newdeps = NULL;
dbcache = alpm_db_get_pkgcache(localdb);
View
20 powaur.c
@@ -43,15 +43,17 @@ static int powaur_init(void)
}
if (setup_environment()) {
- RET_ERR(PW_ERR_INIT_ENV, -1);
+ return error(PW_ERR_INIT_ENV);
}
pwhandle = _pwhandle_init();
- ASSERT(pwhandle != NULL, RET_ERR(PW_ERR_INIT_HANDLE, -1));
+ if (!pwhandle) {
+ return error(PW_ERR_INIT_HANDLE);
+ }
if (stat(powaur_dir, &st) != 0) {
if (mkdir(powaur_dir, 0755)) {
- RET_ERR(PW_ERR_INIT_DIR, -1);
+ return error(PW_ERR_INIT_DIR);
}
}
@@ -260,22 +262,20 @@ static int parseargs(int argc, char *argv[])
} else if (opt == 0) {
continue;
} else if (opt == '?') {
- RET_ERR(PW_ERR_OP_UNKNOWN, -1);
+ return error(PW_ERR_OP_UNKNOWN);
}
parsearg_op(opt, 0);
}
if (config->op == PW_OP_INVAL) {
- RET_ERR(PW_ERR_OP_MULTI, -1);
+ return error(PW_ERR_OP_MULTI);
} else if (config->version) {
version();
} else if (config->help) {
usage(config->op);
} else if (config->op == PW_OP_MAIN) {
- pw_fprintf(PW_LOG_ERROR, stderr,
- "no operation specified (use -h for help)\n");
- return -1;
+ return error(PW_ERR_OP_NULL);
}
/* Parse remaining arguments */
@@ -287,7 +287,7 @@ static int parseargs(int argc, char *argv[])
break;
continue;
} else if (opt == '?') {
- RET_ERR(PW_ERR_OP_UNKNOWN, -1);
+ return error(PW_ERR_OP_UNKNOWN);
} else if (parsearg_op(opt, 1) == 0) {
/* Operation */
continue;
@@ -315,7 +315,7 @@ static int parseargs(int argc, char *argv[])
/* Parse global options */
res = parsearg_global(opt);
if (res != 0) {
- RET_ERR(PW_ERR_OP_UNKNOWN, -1);
+ return error(PW_ERR_OP_UNKNOWN);
}
}
View
11 powaur.h
@@ -41,11 +41,12 @@ enum _pw_errno_t {
PW_ERR_INIT_ENV,
PW_ERR_INIT_HANDLE,
PW_ERR_INIT_DIR,
- PW_ERR_INIT_LOCAL_DB,
+ PW_ERR_INIT_LOCALDB,
/* Command parsing errors */
PW_ERR_OP_UNKNOWN,
PW_ERR_OP_MULTI,
+ PW_ERR_OP_NULL,
/* /etc/pacman.conf parsing errors */
PW_ERR_PM_CONF_OPEN,
@@ -61,15 +62,19 @@ enum _pw_errno_t {
/* General errors */
PW_ERR_MEMORY,
+
+ /* Path related errors */
PW_ERR_GETCWD,
- PW_ERR_CHDIR,
PW_ERR_RESTORECWD,
+ PW_ERR_CHDIR,
+ PW_ERR_PATH_RESOLVE,
+
+ /* File related errors */
PW_ERR_ISDIR,
PW_ERR_FOPEN,
PW_ERR_FILE_EXTRACT,
PW_ERR_OPENDIR,
PW_ERR_STAT,
- PW_ERR_PATH_RESOLVE,
/* Fork errors */
PW_ERR_FORK_FAILED,
View
4 query.c
@@ -77,7 +77,9 @@ static int query_search(pmdb_t *localdb, const char *pkgname)
int powaur_query(alpm_list_t *targets)
{
pmdb_t *localdb = alpm_option_get_localdb();
- ASSERT(localdb != NULL, RET_ERR(PW_ERR_INIT_LOCAL_DB, -1));
+ if (!localdb) {
+ return error(PW_ERR_INIT_LOCALDB);
+ }
alpm_list_t *dblist = NULL;
alpm_list_t *i, *j, *dbcache;
View
26 sync.c
@@ -193,15 +193,15 @@ static int install_single_package(char *pkgname)
ret = extract_file(buf);
if (ret) {
- RET_ERR(PW_ERR_FILE_EXTRACT, -1);
+ return error(PW_ERR_FILE_EXTRACT);
}
if (!getcwd(cwd, PATH_MAX)) {
- RET_ERR(PW_ERR_GETCWD, -1);
+ return error(PW_ERR_GETCWD);
}
if (chdir(pkgname)) {
- RET_ERR(PW_ERR_CHDIR, -1);
+ return error(PW_ERR_CHDIR, pkgname);
}
/* Ask user to edit PKGBUILD */
@@ -219,7 +219,7 @@ static int install_single_package(char *pkgname)
/* Fork editor */
pid = fork();
if (pid == (pid_t) -1) {
- RET_ERR(PW_ERR_FORK_FAILED, -1);
+ return error(PW_ERR_FORK_FAILED);
} else if (pid == 0) {
/* Open editor */
execlp(powaur_editor, powaur_editor, "PKGBUILD", NULL);
@@ -247,7 +247,7 @@ static int install_single_package(char *pkgname)
pid = fork();
if (pid == (pid_t) - 1) {
- RET_ERR(PW_ERR_FORK_FAILED, -1);
+ return error(PW_ERR_FORK_FAILED);
} else if (pid == 0) {
execlp(powaur_editor, powaur_editor, dotinstall, NULL);
} else {
@@ -268,7 +268,7 @@ static int install_single_package(char *pkgname)
pid = fork();
if (pid == (pid_t) -1) {
- RET_ERR(PW_ERR_FORK_FAILED, -1);
+ return error(PW_ERR_FORK_FAILED);
} else if (pid == 0) {
/* Install using pacman */
execlp("makepkg", "makepkg", "-si", NULL);
@@ -394,11 +394,11 @@ static int sync_info(alpm_list_t *targets)
struct aurpkg_t *pkg;
if (!getcwd(cwd, PATH_MAX)) {
- RET_ERR(PW_ERR_GETCWD, -1);
+ return error(PW_ERR_GETCWD);
}
if (chdir(powaur_dir)) {
- RET_ERR(PW_ERR_CHDIR, -1);
+ return error(PW_ERR_CHDIR, powaur_dir);
}
found = ret = pkgcount = 0;
@@ -429,13 +429,13 @@ static int sync_info(alpm_list_t *targets)
fd = mkstemp(filename);
if (fd < 0) {
- PW_SETERRNO(PW_ERR_FOPEN);
+ error(PW_ERR_FOPEN, filename);
goto garbage_collect;
}
fp = fdopen(fd, "w+");
if (!fp) {
- PW_SETERRNO(PW_ERR_FOPEN);
+ error(PW_ERR_FOPEN, filename);
goto garbage_collect;
}
@@ -497,7 +497,7 @@ static int sync_info(alpm_list_t *targets)
alpm_list_free(free_list);
if (chdir(cwd)) {
- RET_ERR(PW_ERR_RESTORECWD, -1);
+ return error(PW_ERR_RESTORECWD);
}
return found ? 0 : -1;
@@ -553,11 +553,11 @@ int powaur_sync(alpm_list_t *targets)
/* Save our current directory */
if (!getcwd(orgdir, PATH_MAX)) {
- RET_ERR(PW_ERR_GETCWD, -1);
+ return error(PW_ERR_GETCWD);
}
if (ret = chdir(powaur_dir)) {
- PW_SETERRNO(PW_ERR_CHDIR);
+ error(PW_ERR_CHDIR, powaur_dir);
goto cleanup;
}
View
35 util.c
@@ -19,7 +19,7 @@
#include "powaur.h"
#include "util.h"
-int pw_printf(enum pwloglevel_t lvl, char *fmt, ...)
+int pw_printf(enum pwloglevel_t lvl, const char *fmt, ...)
{
int ret;
va_list ap;
@@ -31,7 +31,7 @@ int pw_printf(enum pwloglevel_t lvl, char *fmt, ...)
return ret;
}
-int pw_fprintf(enum pwloglevel_t lvl, FILE *stream, char *fmt, ...)
+int pw_fprintf(enum pwloglevel_t lvl, FILE *stream, const char *fmt, ...)
{
int ret;
va_list ap;
@@ -43,7 +43,7 @@ int pw_fprintf(enum pwloglevel_t lvl, FILE *stream, char *fmt, ...)
return ret;
}
-int pw_vfprintf(enum pwloglevel_t lvl, FILE *stream, char *fmt, va_list ap)
+int pw_vfprintf(enum pwloglevel_t lvl, FILE *stream, const char *fmt, va_list ap)
{
int ret = 0;
va_list copy_list;
@@ -87,15 +87,17 @@ int extract_file(const char *filename)
int extract_flags = ARCHIVE_EXTRACT_PERM | ARCHIVE_EXTRACT_TIME;
archive = archive_read_new();
- ASSERT(archive != NULL, RET_ERR(PW_ERR_ARCHIVE_CREATE, -1));
+ if (!archive) {
+ return error(PW_ERR_ARCHIVE_CREATE);
+ }
archive_read_support_compression_all(archive);
archive_read_support_format_all(archive);
ret = archive_read_open_filename(archive, filename,
ARCHIVE_DEFAULT_BYTES_PER_BLOCK);
if (ret != ARCHIVE_OK) {
- RET_ERR(PW_ERR_ARCHIVE_OPEN, -1);
+ return error(PW_ERR_ARCHIVE_OPEN);
}
while (archive_read_next_header(archive, &entry) == ARCHIVE_OK) {
@@ -174,13 +176,13 @@ int wait_or_whine(pid_t pid, char *argv0)
;
if (waiting < 0) {
- RET_ERR(PW_ERR_WAITPID_FAILED, -1);
+ return error(PW_ERR_WAITPID_FAILED);
} else if (waiting != pid) {
- RET_ERR(PW_ERR_WAITPID_CONFUSED, -1);
+ return error(PW_ERR_WAITPID_CONFUSED);
} else if (WIFSIGNALED(status)) {
ret = WTERMSIG(status);
- RET_ERR(PW_ERR_WAITPID_SIGNAL, -1);
ret -= 127;
+ RET_ERR(PW_ERR_WAITPID_SIGNAL, ret);
} else if (WIFEXITED(status)) {
ret = WEXITSTATUS(status);
@@ -191,7 +193,7 @@ int wait_or_whine(pid_t pid, char *argv0)
}
} else {
- RET_ERR(PW_ERR_WAITPID_CONFUSED, -1);
+ return error(PW_ERR_WAITPID_CONFUSED);
}
return ret;
@@ -468,7 +470,8 @@ char *have_dotinstall(void)
fp = fopen("PKGBUILD", "r");
if (!fp) {
- RET_ERR(PW_ERR_FOPEN, NULL);
+ error(PW_ERR_FOPEN, "PKGBUILD");
+ return NULL;
}
while (str = fgets(buf, PATH_MAX, fp)) {
@@ -569,7 +572,7 @@ static int write_dir_archive(char *dirname, struct archive *a)
dirp = opendir(dirname);
if (!dirp) {
- RET_ERR_VOID(PW_ERR_OPENDIR);
+ return error(PW_ERR_OPENDIR);
}
while (dir_entry = readdir(dirp)) {
@@ -652,7 +655,7 @@ int powaur_backup(alpm_list_t *targets)
a = archive_write_new();
if (!a) {
- RET_ERR(PW_ERR_ARCHIVE_CREATE, -1);
+ return error(PW_ERR_ARCHIVE_CREATE);
}
archive_write_set_compression_bzip2(a);
@@ -677,26 +680,26 @@ int powaur_backup(alpm_list_t *targets)
}
if (!getcwd(cwd, PATH_MAX)) {
- PW_SETERRNO(PW_ERR_GETCWD);
+ error(PW_ERR_GETCWD);
ret = -1;
goto cleanup;
}
if (ret = chdir(pacman_dbpath)) {
- PW_SETERRNO(PW_ERR_CHDIR);
+ error(PW_ERR_CHDIR, pacman_dbpath);
goto restore_cwd;
}
/* Create entry for the current directory. */
entry = archive_entry_new();
if (!entry) {
- PW_SETERRNO(PW_ERR_ARCHIVE_ENTRY);
+ error(PW_ERR_ARCHIVE_ENTRY);
goto restore_cwd;
}
snprintf(localdb, PATH_MAX, "%s", "local");
if (ret = stat(localdb, &st)) {
- PW_SETERRNO(PW_ERR_STAT);
+ error(PW_ERR_STAT, localdb);
goto free_entry;
}
View
9 util.h
@@ -11,16 +11,17 @@
#include "error.h"
#include "powaur.h"
#include "environment.h"
+#include "wrapper.h"
int powaur_backup(alpm_list_t *targets);
-int pw_printf(enum pwloglevel_t lvl, char *fmt, ...)
+int pw_printf(enum pwloglevel_t lvl, const char *fmt, ...)
__attribute__((format (printf, 2, 3)));
-int pw_fprintf(enum pwloglevel_t lvl, FILE *stream, char *fmt, ...)
+int pw_fprintf(enum pwloglevel_t lvl, FILE *stream, const char *fmt, ...)
__attribute__((format (printf, 3, 4)));
-int pw_vfprintf(enum pwloglevel_t lvl, FILE *stream, char *fmt, va_list ap)
+int pw_vfprintf(enum pwloglevel_t lvl, FILE *stream, const char *fmt, va_list ap)
__attribute__((format (printf, 3, 0)));
int extract_file(const char *filename);
@@ -73,7 +74,7 @@ alpm_list_t *list_intersect(alpm_list_t *left, alpm_list_t *right,
} while (0)
#define CALLOC(myptr, nmemb, mysz, act) do {\
- myptr = calloc(nmemb, mysz);\
+ myptr = xcalloc(nmemb, mysz);\
if (!myptr) {\
act;\
}\
View
38 wrapper.c
@@ -0,0 +1,38 @@
+#include <stdlib.h>
+#include <string.h>
+
+#include "error.h"
+#include "powaur.h"
+#include "wrapper.h"
+
+/* Simple wrappers around some functions */
+
+void *xcalloc(size_t nmemb, size_t sz)
+{
+ void *ret = calloc(nmemb, sz);
+ if (!ret) {
+ die_errno(PW_ERR_MEMORY);
+ }
+
+ return ret;
+}
+
+void *xmalloc(size_t sz)
+{
+ void *ret = malloc(sz);
+ if (!ret) {
+ die_errno(PW_ERR_MEMORY);
+ }
+
+ return ret;
+}
+
+char *xstrdup(const char *str)
+{
+ char *ret = strdup(str);
+ if (!ret) {
+ die_errno(PW_ERR_MEMORY);
+ }
+
+ return ret;
+}
View
8 wrapper.h
@@ -0,0 +1,8 @@
+#ifndef POWAUR_WRAPPER_H
+#define POWAUR_WRAPPER_H
+
+void *xcalloc(size_t nmemb, size_t sz);
+void *xmalloc(size_t sz);
+char *xstrdup(const char *str);
+
+#endif

0 comments on commit b848e42

Please sign in to comment.