diff --git a/src/Native/Unix/System.Native/pal_io.c b/src/Native/Unix/System.Native/pal_io.c index 1b2d3423fe59..120a6b09eba5 100644 --- a/src/Native/Unix/System.Native/pal_io.c +++ b/src/Native/Unix/System.Native/pal_io.c @@ -1219,13 +1219,16 @@ int32_t SystemNative_CopyFile(intptr_t sourceFd, const char* srcPath, const char #if HAVE_CLONEFILE // For clonefile we need to unlink the destination file first but we need to // check permission first to ensure we don't try to unlink read-only file - if (access(destPath, W_OK) == 0) + if (access(destPath, W_OK) != 0) { - ret = unlink(destPath); - if (ret != 0) - { - return ret; - } + errno = EACCES; + return -1; + } + + ret = unlink(destPath); + if (ret != 0) + { + return ret; } #endif }