From 11501bfb54371d0a4a6de8ecfab1438708c2dea1 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Tue, 29 Oct 2019 08:47:44 -0400 Subject: [PATCH] Restore fchmod check in SystemNative_CopyFile and only ignore when it fails on android (#365) --- src/Native/Unix/System.Native/pal_io.c | 36 +++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Native/Unix/System.Native/pal_io.c b/src/Native/Unix/System.Native/pal_io.c index 39dfefc249c0..dc5e9e825ae0 100644 --- a/src/Native/Unix/System.Native/pal_io.c +++ b/src/Native/Unix/System.Native/pal_io.c @@ -1258,23 +1258,6 @@ int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd) struct stat_ sourceStat; bool copied = false; -#if !MONODROID - // First, stat the source file. - while ((ret = fstat_(inFd, &sourceStat)) < 0 && errno == EINTR); - if (ret != 0) - { - // If we can't stat() it, then we likely don't have permission to read it. - return -1; - } - - // Then copy permissions. - while ((ret = fchmod(outFd, sourceStat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) < 0 && errno == EINTR); - if (ret != 0) - { - return -1; - } -#endif - #if HAVE_SENDFILE_4 // If sendfile is available (Linux), try to use it, as the whole copy // can be performed in the kernel, without lots of unnecessary copying. @@ -1284,7 +1267,6 @@ int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd) return -1; } - // On 32-bit, if you use 64-bit offsets, the last argument of `sendfile' will be a // `size_t' a 32-bit integer while the `st_size' field of the stat structure will be off64_t. // So `size' will have to be `uint64_t'. In all other cases, it will be `size_t'. @@ -1352,6 +1334,24 @@ int32_t SystemNative_CopyFile(intptr_t sourceFd, intptr_t destinationFd) #endif } + if (ret != 0) + { + return -1; + } + + // Then copy permissions. + while ((ret = fchmod(outFd, sourceStat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) < 0 && errno == EINTR); + +// We are ignoring the error on Android because in the case of external storage we are not allowed +// to modify permissions, but the copy should still succeed. +// https://github.com/mono/mono/issues/17133 +#if !TARGET_ANDROID + if (ret != 0) + { + return -1; + } +#endif + return 0; #endif // HAVE_FCOPYFILE }