[dpkg] 113/200: u-a: Refactor pathname existence check into a new function
Ximin Luo
infinity0 at debian.org
Wed Apr 5 15:17:26 UTC 2017
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch master
in repository dpkg.
commit 8a3d3c40733b42aaa303fe3b0d483fa8a38325a3
Author: Guillem Jover <guillem at debian.org>
Date: Fri Jan 6 02:52:42 2017 +0100
u-a: Refactor pathname existence check into a new function
---
debian/changelog | 1 +
utils/update-alternatives.c | 57 ++++++++++++++++++---------------------------
2 files changed, 24 insertions(+), 34 deletions(-)
diff --git a/debian/changelog b/debian/changelog
index ef147d5..4d2d121 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -9,6 +9,7 @@ dpkg (1.18.19) UNRELEASED; urgency=medium
least one colon or one dash, we should expect epoch and revision numbers.
* 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.
* Portability:
- On GNU/Hurd try to use the new process executable name attribute from
libps, to properly match on start-stop-daemon --exec.
diff --git a/utils/update-alternatives.c b/utils/update-alternatives.c
index 7a41c00..65b9287 100644
--- a/utils/update-alternatives.c
+++ b/utils/update-alternatives.c
@@ -338,6 +338,21 @@ xreadlink(const char *linkname)
return buf;
}
+static bool
+pathname_is_missing(const char *pathname)
+{
+ struct stat st;
+
+ errno = 0;
+ if (stat(pathname, &st) == 0)
+ return false;
+
+ if (errno == ENOENT)
+ return true;
+
+ syserr(_("cannot stat file '%s'"), pathname);
+}
+
static void
set_action(const char *new_action)
{
@@ -566,16 +581,11 @@ fileset_has_slave(struct fileset *fs, const char *name)
static bool
fileset_can_install_slave(struct fileset *fs, const char *slave_name)
{
- struct stat st;
-
/* Decide whether the slave alternative must be setup */
if (fileset_has_slave(fs, slave_name)) {
const char *slave = fileset_get_slave(fs, slave_name);
- errno = 0;
- if (stat(slave, &st) == -1 && errno != ENOENT)
- syserr(_("cannot stat file '%s'"), slave);
- if (errno == 0)
+ if (!pathname_is_missing(slave))
return true;
}
@@ -1166,7 +1176,6 @@ alternative_parse_fileset(struct alternative *a, struct altdb_context *ctx)
{
struct fileset *fs;
struct slave_link *sl;
- struct stat st;
char *master_file;
master_file = altdb_get_line(ctx, _("master file"));
@@ -1179,12 +1188,9 @@ alternative_parse_fileset(struct alternative *a, struct altdb_context *ctx)
if (fs)
ctx->bad_format(ctx, _("duplicate path %s"), master_file);
- if (stat(master_file, &st)) {
+ if (pathname_is_missing(master_file)) {
char *junk;
- if (errno != ENOENT)
- syserr(_("cannot stat file '%s'"), master_file);
-
/* File not found - remove. */
if (ctx->flags & ALTDB_WARN_PARSER)
warning(_("alternative %s (part of link group %s) "
@@ -2121,13 +2127,7 @@ alternative_select_mode(struct alternative *a, const char *current_choice)
if (current_choice) {
/* Detect manually modified alternative, switch to manual. */
if (!alternative_has_choice(a, current_choice)) {
- struct stat st;
-
- errno = 0;
- if (stat(current_choice, &st) == -1 && errno != ENOENT)
- syserr(_("cannot stat file '%s'"), current_choice);
-
- if (errno == ENOENT) {
+ if (pathname_is_missing(current_choice)) {
warning(_("%s/%s is dangling; it will be updated "
"with best choice"), altdir, a->master_name);
alternative_set_status(a, ALT_ST_AUTO);
@@ -2151,7 +2151,6 @@ alternative_evolve_slave(struct alternative *a, const char *cur_choice,
struct slave_link *sl, struct fileset *fs)
{
struct slave_link *sl_old;
- struct stat st;
char *new_file = NULL;
const char *old, *new;
@@ -2177,13 +2176,8 @@ alternative_evolve_slave(struct alternative *a, const char *cur_choice,
alternative_path_classify(old) == ALT_PATH_SYMLINK) {
bool rename_link = false;
- if (new_file) {
- errno = 0;
- if (stat(new_file, &st) == -1 && errno != ENOENT)
- syserr(_("cannot stat file '%s'"),
- new_file);
- rename_link = (errno == 0);
- }
+ if (new_file)
+ rename_link = !pathname_is_missing(new_file);
if (rename_link) {
info(_("renaming %s slave link from %s to %s"),
@@ -2475,7 +2469,6 @@ alternative_check_install_args(struct alternative *inst_alt,
struct alternative_map *alt_map_links, *alt_map_parent;
struct alternative *found;
struct slave_link *sl;
- struct stat st;
alternative_check_name(inst_alt->master_name);
alternative_check_link(inst_alt->master_link);
@@ -2500,13 +2493,9 @@ alternative_check_install_args(struct alternative *inst_alt,
inst_alt->master_link, found->master_name);
}
- if (stat(fileset->master_file, &st) == -1) {
- if (errno == ENOENT)
- error(_("alternative path %s doesn't exist"),
- fileset->master_file);
- else
- syserr(_("cannot stat file '%s'"), fileset->master_file);
- }
+ if (pathname_is_missing(fileset->master_file))
+ error(_("alternative path %s doesn't exist"),
+ fileset->master_file);
for (sl = inst_alt->slaves; sl; sl = sl->next) {
const char *file = fileset_get_slave(fileset, sl->name);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/dpkg.git
More information about the Reproducible-commits
mailing list