Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Check flock on CopyFile
Browse files Browse the repository at this point in the history
  • Loading branch information
filipnavara committed Nov 7, 2019
1 parent df40a0d commit 987d822
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions src/Native/Unix/System.Native/pal_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1223,16 +1223,35 @@ 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)
// check permissions first to ensure we don't try to unlink read-only file. Also,
// we need to check the advisory locks to align with the behavior of regular
// code path.
openFlags = O_WRONLY | O_TRUNC | O_CREAT | O_EXCL;
#if HAVE_O_CLOEXEC
openFlags |= O_CLOEXEC;
#endif
while ((outFd = open(destPath, openFlags, sourceStat.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) < 0 && errno == EINTR);
if (outFd >= 0)
{
return -1;
while ((ret = flock(outFd, LOCK_EX | LOCK_NB) < 0) && errno == EINTR);
if (ret < 0 && errno == EWOULDBLOCK)
{
close(outFd);
errno = EWOULDBLOCK;
return -1;
}

close(outFd);

while ((ret = unlink(destPath)) < 0 && errno == EINTR);
if (ret != 0)
{
return ret;
}
}

ret = unlink(destPath);
if (ret != 0)
else if (errno == EACCES || errno == EPERM)
{
return ret;
return -1;
}
#endif
}
Expand Down Expand Up @@ -1265,6 +1284,14 @@ int32_t SystemNative_CopyFile(intptr_t sourceFd, const char* srcPath, const char
fcntl(outFd, F_SETFD, FD_CLOEXEC);
#endif

while ((ret = flock(outFd, LOCK_EX | LOCK_NB) < 0) && errno == EINTR);
if (ret < 0 && errno == EWOULDBLOCK)
{
close(outFd);
errno = EWOULDBLOCK;
return -1;
}

// Get the stats on the source file.
bool copied = false;

Expand Down

0 comments on commit 987d822

Please sign in to comment.