Browse Source

u-a: Use areadlink() instead of lstat() + xreadlink()

We avoid an lstat() call as we catch the error from areadlink and can
react on that instead of an uncontrolled failure from xreadlink().
Guillem Jover 7 years ago
parent
commit
3d975af556
2 changed files with 3 additions and 9 deletions
  1. 1 0
      debian/changelog
  2. 2 9
      utils/update-alternatives.c

+ 1 - 0
debian/changelog

@@ -10,6 +10,7 @@ dpkg (1.18.19) UNRELEASED; urgency=medium
   * Always set SOURCE_DATE_EPOCH in dpkg-buildpackage and dpkg-source. Use
     the current date if the changelog does not have one. Closes: #849081
   * Refactor update-alternatives pathname existence check into a new function.
+  * Avoid useless repeated lstat()s in update-alternatives.
   * Portability:
     - On GNU/Hurd try to use the new process executable name attribute from
       libps, to properly match on start-stop-daemon --exec.

+ 2 - 9
utils/update-alternatives.c

@@ -1408,7 +1408,6 @@ alternative_set_current(struct alternative *a, char *new_choice)
 static const char *
 alternative_get_current(struct alternative *a)
 {
-	struct stat st;
 	char *curlink;
 	char *file;
 
@@ -1416,15 +1415,9 @@ alternative_get_current(struct alternative *a)
 		return a->current;
 
 	curlink = xasprintf("%s/%s", altdir, a->master_name);
-	if (lstat(curlink, &st)) {
-		if (errno == ENOENT) {
-			free(curlink);
-			return alternative_set_current(a, NULL);
-		}
+	file = areadlink(curlink);
+	if (file == NULL && errno != ENOENT)
 		syserr(_("cannot stat file '%s'"), curlink);
-	}
-
-	file = xreadlink(curlink);
 	free(curlink);
 
 	return alternative_set_current(a, file);