On Windows, there is no way to replace file atomically. Instead, MoveFileExA:
1. Calls CreateFileA(access=Delete, shareMode=Delete)
2. Calls SetRenameInformationFile to rename file
3. Calls CloseFile
The problem is that if parallel process attempts to open destination file for reading between
steps 2 and 3, it will try to do that without shareMode=Delete and will hit a SHARING_VIOLATION error.
In practice, this race condition results in:
Smudge error: Error opening media file.: open .git\lfs\objects\<sha>: The process cannot access the file because it is being used by another process.
There are two solutions here:
1. Do not overwrite file if it already exists
2. Retry reading from file if got a SHARING_VIOLATION
This commit implements option 1.
Fixes#2825 (for Windows, other OSes are race-free already). Also see #3813 and #3826.
On Darwin, we're importing "C" in the Darwin-specific file, so flag it
as requiring cgo and specify the generic file for when we're not using
cgo. This ensures that cross-compilation continues to work without
errors. Since this is just a performance optimization, there's
relatively little problem with not providing the CloneFile function in a
non-cgo environment.