diff --git a/src/shared/copy.c b/src/shared/copy.c index f61b16caee..21add1d5c0 100644 --- a/src/shared/copy.c +++ b/src/shared/copy.c @@ -1645,7 +1645,11 @@ int copy_file_atomic_at_full( return 0; fail: - (void) unlinkat(dir_fdt, to, 0); + /* link_tmpfile_at() succeeded, so 'to' is now published. In replacement mode, do not + * remove it again, as that may delete a pre-existing target replaced by this copy. */ + if (!FLAGS_SET(copy_flags, COPY_REPLACE)) + (void) unlinkat(dir_fdt, to, 0); + return r; }