From 4496d4571d69006163ef4828a5bce6fbe2bff098 Mon Sep 17 00:00:00 2001 From: Konstantina Chremmou Date: Fri, 22 Apr 2022 14:07:20 +0100 Subject: [PATCH] CA-365905 (XSI-1215): Create a temporary file in the target download folder if the latter is in a different drive from the default temporary folder. Signed-off-by: Konstantina Chremmou --- XenModel/XenAPI/HTTP.cs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/XenModel/XenAPI/HTTP.cs b/XenModel/XenAPI/HTTP.cs index 57ae14b4d..9b990f0a2 100644 --- a/XenModel/XenAPI/HTTP.cs +++ b/XenModel/XenAPI/HTTP.cs @@ -75,9 +75,7 @@ namespace XenAPI 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)); @@ -767,11 +765,29 @@ namespace XenAPI 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();