From ddd3d95ba65af4bb9801df1469b8f3af8257bdb7 Mon Sep 17 00:00:00 2001 From: claudio Date: Wed, 7 Apr 2021 14:19:31 +0000 Subject: [PATCH] When merging a repo even files to delete can be part of the temporary work dir. So unlink can return an ENOENT error for the main repo. In which case the temp dir should be tried. Refactor this code a bit since there is no way rrdp_filename() should fail in this part of the code. OK tb@ --- usr.sbin/rpki-client/repo.c | 39 +++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/usr.sbin/rpki-client/repo.c b/usr.sbin/rpki-client/repo.c index c146111d396..a5720ed44f5 100644 --- a/usr.sbin/rpki-client/repo.c +++ b/usr.sbin/rpki-client/repo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: repo.c,v 1.3 2021/04/02 05:16:29 tb Exp $ */ +/* $OpenBSD: repo.c,v 1.4 2021/04/07 14:19:31 claudio Exp $ */ /* * Copyright (c) 2021 Claudio Jeker * Copyright (c) 2019 Kristaps Dzonsons @@ -838,22 +838,37 @@ rrdp_merge_repo(struct rrdprepo *rr) /* XXX should delay deletes */ RB_FOREACH_SAFE(fp, filepath_tree, &rr->deleted, nfp) { - if ((fn = rrdp_filename(rr, fp->file, 0)) != NULL) { - if (unlink(fn) == -1) - warn("%s: unlink", fn); - free(fn); + fn = rrdp_filename(rr, fp->file, 1); + rfn = rrdp_filename(rr, fp->file, 0); + + if (fn == NULL || rfn == NULL) + errx(1, "bad filepath"); /* should not happen */ + + if (unlink(rfn) == -1) { + if (errno == ENOENT) { + if (unlink(fn) == -1) + warn("%s: unlink", fn); + } else + warn("%s: unlink", rfn); } + + free(rfn); + free(fn); filepath_put(&rr->deleted, fp); } RB_FOREACH_SAFE(fp, filepath_tree, &rr->added, nfp) { - if ((fn = rrdp_filename(rr, fp->file, 1)) != NULL && - (rfn = rrdp_filename(rr, fp->file, 0)) != NULL) { - repo_mkpath(rfn); - if (rename(fn, rfn) == -1) - warn("%s: link", rfn); - free(rfn); - } + fn = rrdp_filename(rr, fp->file, 1); + rfn = rrdp_filename(rr, fp->file, 0); + + if (fn == NULL || rfn == NULL) + errx(1, "bad filepath"); /* should not happen */ + + repo_mkpath(rfn); + if (rename(fn, rfn) == -1) + warn("%s: rename", rfn); + + free(rfn); free(fn); filepath_put(&rr->added, fp); } -- 2.20.1