The following commit has been merged in the master branch: commit c7c7522dd3e29af582eddda7b10b521337a7b21c Author: Guillem Jover <guil...@debian.org> Date: Tue Sep 6 04:19:03 2011 +0200
u-a: Split xreadlink() allocation code into areadlink() Using a bool as an argument for a function already taking several arguments is almost never a good interface, it's not clear from the call sites what it refers to and as such prone to confusion. Split the main xreadlink() code into a function that allocates but can return NULL, and make xreadlink() use the common semantics of never failing. diff --git a/utils/update-alternatives.c b/utils/update-alternatives.c index 8e82bb6..44a3114 100644 --- a/utils/update-alternatives.c +++ b/utils/update-alternatives.c @@ -280,34 +280,44 @@ xstrdup(const char *str) } static char * -xreadlink(const char *linkname, bool error_out) +areadlink(const char *linkname) { struct stat st; char *buf; ssize_t size; /* Allocate required memory to store the value of the symlink */ - if (lstat(linkname, &st)) { - if (!error_out) - return NULL; - syserr(_("cannot stat file '%s'"), linkname); - } + if (lstat(linkname, &st)) + return NULL; buf = xmalloc(st.st_size + 1); /* Read it and terminate the string properly */ size = readlink(linkname, buf, st.st_size); if (size == -1) { - if (!error_out) { - free(buf); - return NULL; - } - syserr(_("unable to read link `%.255s'"), linkname); + int saved_errno = errno; + + free(buf); + errno = saved_errno; + + return NULL; } buf[size] = '\0'; return buf; } +static char * +xreadlink(const char *linkname) +{ + char *buf; + + buf = areadlink(linkname); + if (buf == NULL) + syserr(_("unable to read link `%.255s'"), linkname); + + return buf; +} + static int DPKG_ATTR_VPRINTF(2) xvasprintf(char **strp, const char *fmt, va_list args) { @@ -1421,7 +1431,7 @@ alternative_get_current(struct alternative *a) return NULL; xasprintf(&curlink, "%s/%s", altdir, a->master_name); - file = xreadlink(curlink, true); + file = xreadlink(curlink); free(curlink); return file; @@ -1757,7 +1767,7 @@ alternative_is_broken(struct alternative *a) return true; /* Check master link */ - altlnk = xreadlink(a->master_link, false); + altlnk = areadlink(a->master_link); if (!altlnk) return true; xasprintf(&wanted, "%s/%s", altdir, a->master_name); @@ -1784,7 +1794,7 @@ alternative_is_broken(struct alternative *a) char *sl_altlnk, *sl_current; /* Verify link -> /etc/alternatives/foo */ - sl_altlnk = xreadlink(sl->link, false); + sl_altlnk = areadlink(sl->link); if (!sl_altlnk) return true; xasprintf(&wanted, "%s/%s", altdir, sl->name); @@ -1795,7 +1805,7 @@ alternative_is_broken(struct alternative *a) } free(sl_altlnk); /* Verify /etc/alternatives/foo -> file */ - sl_current = xreadlink(wanted, false); + sl_current = areadlink(wanted); free(wanted); if (!sl_current) return true; @@ -2065,7 +2075,7 @@ alternative_evolve(struct alternative *a, struct alternative *b, char *lnk; xasprintf(&lnk, "%s/%s", altdir, sl->name); - new_file = xreadlink(lnk, false); + new_file = areadlink(lnk); free(lnk); } if (strcmp(old, new) != 0 && lstat(old, &st) == 0 && -- dpkg's main repository -- To UNSUBSCRIBE, email to debian-dpkg-cvs-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org