Skip to content

Commit

Permalink
CA-365905 (XSI-1215): Create a temporary file in the target download …
Browse files Browse the repository at this point in the history
…folder instead of the user's temp...

... folder if the latter is in a different drive from the target folder.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
  • Loading branch information
kc284 committed Apr 25, 2022
1 parent 271b9c1 commit 8cd7d87
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions csharp/autogen/src/HTTP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ protected TooManyRedirectsException(SerializationInfo info, StreamingContext con
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException("info");
}
throw new ArgumentNullException(nameof(info));

info.AddValue("redirect", redirect);
info.AddValue("uri", uri, typeof(Uri));
Expand Down Expand Up @@ -773,11 +771,29 @@ public static Stream HttpGetStream(Uri uri, IWebProxy proxy, int timeoutMs)
public static void Get(DataCopiedDelegate dataCopiedDelegate, FuncBool cancellingDelegate,
Uri uri, IWebProxy proxy, string path, int timeoutMs)
{
string tmpFile = Path.GetTempFileName();
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentException(nameof(path));

var tmpFile = Path.GetTempFileName();

if (Path.GetPathRoot(path) != Path.GetPathRoot(tmpFile))
{
//CA-365905: if the target path is under a root different from
//the temp file, use instead a temp file under the target root,
//otherwise there may not be enough space for the download

var dir = Path.GetDirectoryName(path);
if (dir == null) //path is root directory
throw new ArgumentException(nameof(path));

tmpFile = Path.Combine(dir, Path.GetRandomFileName());
File.Delete(tmpFile);
}

try
{
using (Stream fileStream = new FileStream(tmpFile, FileMode.Create, FileAccess.Write, FileShare.None),
downloadStream = HttpGetStream(uri, proxy, timeoutMs))
downloadStream = HttpGetStream(uri, proxy, timeoutMs))
{
CopyStream(downloadStream, fileStream, dataCopiedDelegate, cancellingDelegate);
fileStream.Flush();
Expand Down

0 comments on commit 8cd7d87

Please sign in to comment.