]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
fs-posix: Fix fs_copy() potentially doing unnecessary unlink()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 14 Oct 2025 10:00:30 +0000 (13:00 +0300)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Tue, 14 Oct 2025 15:27:29 +0000 (15:27 +0000)
We need to check if link() failed before checking errno.

src/lib-fs/fs-posix.c

index 4097151bb5cfd9bd7ee9a13abc4112424d02ac79..7b2ba2f47682d981272d7c41a9b06e1b423e232c 100644 (file)
@@ -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);