From: Timo Sirainen Date: Tue, 14 Oct 2025 10:00:30 +0000 (+0300) Subject: fs-posix: Fix fs_copy() potentially doing unnecessary unlink() X-Git-Tag: 2.4.2~82 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7e92385d8f72f76524643ab64c0c606af10322e8;p=thirdparty%2Fdovecot%2Fcore.git fs-posix: Fix fs_copy() potentially doing unnecessary unlink() We need to check if link() failed before checking errno. --- diff --git a/src/lib-fs/fs-posix.c b/src/lib-fs/fs-posix.c index 4097151bb5..7b2ba2f476 100644 --- a/src/lib-fs/fs-posix.c +++ b/src/lib-fs/fs-posix.c @@ -836,7 +836,8 @@ static int fs_posix_copy(struct fs_file *_src, struct fs_file *_dest) fs_posix_write_rename_if_needed(dest); ret = link(src->full_path, dest->full_path); - if (errno == EEXIST && dest->open_mode == FS_OPEN_MODE_REPLACE) { + if (ret < 0 && errno == EEXIST && + dest->open_mode == FS_OPEN_MODE_REPLACE) { /* destination file already exists - replace it */ i_unlink_if_exists(dest->full_path); ret = link(src->full_path, dest->full_path);