diff options
author | Wes Haggard <Wes.Haggard@microsoft.com> | 2015-02-12 04:49:10 +0300 |
---|---|---|
committer | Wes Haggard <Wes.Haggard@microsoft.com> | 2015-02-12 04:49:10 +0300 |
commit | 16cd600d2ec6d3a0a323d9f3a683fc77d3adac0f (patch) | |
tree | ef52c1929af1b4c85733046982e0e1928bfb0479 /dir.targets | |
parent | 894bd6aaf5d2828f82fc01335220824231928e8d (diff) |
Harden the DownloadFile build target for race conditions
With my recent changes I see race conditions with downloading nuget.exe
in some cases so I hardened the download target a little to try
and avoid locked file race conditions. While it still isn't full
proof the window of time is much narrower now.
Diffstat (limited to 'dir.targets')
-rw-r--r-- | dir.targets | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/dir.targets b/dir.targets index 4edcfc7b03..9cfaeff3c1 100644 --- a/dir.targets +++ b/dir.targets @@ -9,14 +9,28 @@ </ParameterGroup> <Task> <Reference Include="System" /> + <Reference Include="System.IO" /> <Code Type="Fragment" Language="cs"> <![CDATA[ - var directory = System.IO.Path.GetDirectoryName(FileName); - System.IO.Directory.CreateDirectory(directory); - var client = new System.Net.WebClient(); - client.Proxy = System.Net.WebRequest.DefaultWebProxy; - client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; - client.DownloadFile(Address, FileName); + var directory = System.IO.Path.GetDirectoryName(FileName); + Directory.CreateDirectory(directory); + + var tempFile = Path.Combine(directory, Path.GetRandomFileName()); + var client = new System.Net.WebClient(); + client.Proxy = System.Net.WebRequest.DefaultWebProxy; + client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials; + client.DownloadFile(Address, tempFile); + + try + { + if (!File.Exists(FileName)) + File.Move(tempFile, FileName); + } + finally + { + if (File.Exists(tempFile)) + File.Delete(tempFile); + } ]]> </Code> </Task> |