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
This commit is contained in:
Dag-Erling Smørgrav 2023-12-14 17:07:04 +01:00
parent 0f4467ce44
commit 3850927066

View file

@ -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 {