From dd6bc566d9139f2c1a7fd00e4d907e9f248712f0 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Tue, 9 Oct 2018 17:34:01 -0400 Subject: [PATCH 1/2] Adding a helper method to attempt both copy styles --- src/GitHub.Api/GitHub.Api.45.csproj | 1 + src/GitHub.Api/GitHub.Api.csproj | 1 + src/GitHub.Api/Installer/CopyHelper.cs | 53 ++++++++++++++++++++ src/GitHub.Api/Installer/GitInstaller.cs | 8 +-- src/GitHub.Api/Installer/OctorunInstaller.cs | 4 +- 5 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/GitHub.Api/Installer/CopyHelper.cs diff --git a/src/GitHub.Api/GitHub.Api.45.csproj b/src/GitHub.Api/GitHub.Api.45.csproj index 05a9c6b23..480e35974 100644 --- a/src/GitHub.Api/GitHub.Api.45.csproj +++ b/src/GitHub.Api/GitHub.Api.45.csproj @@ -93,6 +93,7 @@ + diff --git a/src/GitHub.Api/GitHub.Api.csproj b/src/GitHub.Api/GitHub.Api.csproj index f52c319da..bf916ed46 100644 --- a/src/GitHub.Api/GitHub.Api.csproj +++ b/src/GitHub.Api/GitHub.Api.csproj @@ -104,6 +104,7 @@ + diff --git a/src/GitHub.Api/Installer/CopyHelper.cs b/src/GitHub.Api/Installer/CopyHelper.cs new file mode 100644 index 000000000..c7f673850 --- /dev/null +++ b/src/GitHub.Api/Installer/CopyHelper.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using GitHub.Logging; + +namespace GitHub.Unity +{ + public static class CopyHelper + { + private static readonly ILogging Logger = LogHelper.GetLogger(typeof(CopyHelper)); + + public static void Copy(NPath fromPath, NPath toPath) + { + try + { + + CopyFolder(fromPath, toPath); + } + catch (Exception ex1) + { + Logger.Warning(ex1, "Error copying from " + fromPath + " to " + toPath + ". Attempting to copy contents."); + + try + { + CopyFolderContents(fromPath, toPath); + } + catch (Exception ex2) + { + Logger.Error(ex2, "Error copying from " + fromPath + " to " + toPath + "."); + throw; + } + } + } + public static void CopyFolder(NPath fromPath, NPath toPath) + { + Logger.Trace("CopyFolder fromPath: {0} toPath:{1}", fromPath.ToString(), toPath.ToString()); + + toPath.EnsureParentDirectoryExists(); + fromPath.Move(toPath); + fromPath.Delete(); + } + + public static void CopyFolderContents(NPath fromPath, NPath toPath) + { + Logger.Trace("CopyFolderContents fromPath: {0} toPath:{1}", fromPath.ToString(), toPath.ToString()); + + toPath.DeleteContents(); + fromPath.MoveFiles(toPath, true); + fromPath.Delete(); + } + } +} diff --git a/src/GitHub.Api/Installer/GitInstaller.cs b/src/GitHub.Api/Installer/GitInstaller.cs index 71dd77361..67b502a94 100644 --- a/src/GitHub.Api/Installer/GitInstaller.cs +++ b/src/GitHub.Api/Installer/GitInstaller.cs @@ -307,9 +307,7 @@ private GitInstallationState ExtractGit(GitInstallationState state) { Logger.Trace("Moving Git source:{0} target:{1}", source.ToString(), target.ToString()); - target.DeleteContents(); - source.MoveFiles(target, true); - source.Parent.Delete(); + CopyHelper.Copy(source, target); state.GitIsValid = true; @@ -335,9 +333,7 @@ private GitInstallationState ExtractGit(GitInstallationState state) { Logger.Trace("Moving GitLFS source:{0} target:{1}", source.ToString(), target.ToString()); - target.DeleteContents(); - source.MoveFiles(target, true); - source.Parent.Delete(); + CopyHelper.Copy(source, target); state.GitLfsIsValid = true; } diff --git a/src/GitHub.Api/Installer/OctorunInstaller.cs b/src/GitHub.Api/Installer/OctorunInstaller.cs index 354bf69f3..0aa655d93 100644 --- a/src/GitHub.Api/Installer/OctorunInstaller.cs +++ b/src/GitHub.Api/Installer/OctorunInstaller.cs @@ -55,9 +55,7 @@ private NPath MoveOctorun(NPath fromPath) Logger.Trace("MoveOctorun fromPath: {0} toPath:{1}", fromPath.ToString(), toPath.ToString()); - toPath.DeleteContents(); - fromPath.MoveFiles(toPath, true); - fromPath.Parent.Delete(); + CopyHelper.Copy(fromPath, toPath); return installDetails.ExecutablePath; } From 7562f7d77891d7994d006c87dc7b7d36b1168c98 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Tue, 9 Oct 2018 17:40:06 -0400 Subject: [PATCH 2/2] Fixing it up a bit --- src/GitHub.Api/Installer/CopyHelper.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/GitHub.Api/Installer/CopyHelper.cs b/src/GitHub.Api/Installer/CopyHelper.cs index c7f673850..22ec43579 100644 --- a/src/GitHub.Api/Installer/CopyHelper.cs +++ b/src/GitHub.Api/Installer/CopyHelper.cs @@ -31,6 +31,10 @@ public static void Copy(NPath fromPath, NPath toPath) throw; } } + finally + { + fromPath.DeleteIfExists(); + } } public static void CopyFolder(NPath fromPath, NPath toPath) { @@ -38,7 +42,6 @@ public static void CopyFolder(NPath fromPath, NPath toPath) toPath.EnsureParentDirectoryExists(); fromPath.Move(toPath); - fromPath.Delete(); } public static void CopyFolderContents(NPath fromPath, NPath toPath) @@ -47,7 +50,6 @@ public static void CopyFolderContents(NPath fromPath, NPath toPath) toPath.DeleteContents(); fromPath.MoveFiles(toPath, true); - fromPath.Delete(); } } }