git: 38509270663f - main - cp: Further simplify the core logic.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Thu, 14 Dec 2023 16:07:31 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=38509270663f336103273878cc8ddc88a225b9d8

commit 38509270663f336103273878cc8ddc88a225b9d8
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2023-12-14 16:07:04 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2023-12-14 16:07:04 +0000

    cp: Further simplify the core logic.
    
    If the destination file exists but we decide unlink it, set the dne
    flag.  This means we don't need to re-check the conditions that would
    have caused us to delete the file when we later need to decide whether
    to create or replace it.
    
    MFC after:      1 week
    Sponsored by:   Klara, Inc.
    Reviewed by:    kevans
    Differential Revision:  https://reviews.freebsd.org/D43064
---
 bin/cp/utils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/bin/cp/utils.c b/bin/cp/utils.c
index 891360604631..80a0c1fce824 100644
--- a/bin/cp/utils.c
+++ b/bin/cp/utils.c
@@ -145,6 +145,7 @@ copy_file(const FTSENT *entp, int dne)
 		if (fflag) {
 			/* remove existing destination file */
 			(void)unlink(to.p_path);
+			dne = 1;
 		}
 	}
 
@@ -166,7 +167,7 @@ copy_file(const FTSENT *entp, int dne)
 		goto done;
 	}
 
-	if (!dne && !fflag) {
+	if (!dne) {
 		/* overwrite existing destination file */
 		to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
 	} else {