Skip to content

Commit

Permalink
Perfect backup function
Browse files Browse the repository at this point in the history
  • Loading branch information
JusterZhu committed Nov 15, 2023
1 parent 2d21a97 commit 4095c5b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using GeneralUpdate.Core.Pipelines.Context;
using GeneralUpdate.Core.Pipelines.Middleware;
using GeneralUpdate.Core.Utils;
using GeneralUpdate.Differential;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -86,11 +88,11 @@ public override bool StartApp(string appName, int appType)
{
case AppType.ClientApp:
Environment.SetEnvironmentVariable("ProcessBase64", Packet.ProcessBase64, EnvironmentVariableTarget.Machine);
WaitForProcessToStart(path, TimeSpan.FromSeconds(60));
WaitForProcessToStart(path, TimeSpan.FromSeconds(60), Restore);
break;

case AppType.UpgradeApp:
WaitForProcessToStart(path, TimeSpan.FromSeconds(60));
WaitForProcessToStart(path, TimeSpan.FromSeconds(60), Restore);
break;
}
return true;
Expand Down Expand Up @@ -137,24 +139,26 @@ private bool Clear()
/// </summary>
/// <param name="applicationPath">Process objects to monitor</param>
/// <param name="timeout">The maximum interval for waiting for the process to start (The default value is 60 seconds).</param>
/// <param name="callbackAction">If the startup fails, the result is notified to the protection service.</param>
private void WaitForProcessToStart(string applicationPath, TimeSpan timeout, Action<string,string> callbackAction = null)
/// <param name="callbackAction"></param>
private void WaitForProcessToStart(string applicationPath, TimeSpan timeout, Action<string> callbackAction = null)
{
using (Process process = Process.Start(applicationPath))
{
var startTime = DateTime.UtcNow;
while (DateTime.UtcNow - startTime < timeout)
{
Thread.Sleep(2 * 1000);
if (!process.HasExited)
{
callbackAction?.Invoke(applicationPath, Packet.TempPath);
callbackAction?.Invoke(applicationPath);
return;
}
Thread.Sleep(2 * 1000);
}
}
}

private void Restore(string targetDir) => DifferentialCore.Instance.Restore(targetDir);

#endregion Private Methods
}
}
39 changes: 37 additions & 2 deletions src/c#/GeneralUpdate.Differential/DifferentialCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,9 @@ public void Backup(string appPath, string patchPath)
_backupFiles?.Push(files);
}

public Stack<List<string>> GetBackups() => _backupFiles;

/// <summary>
/// Delete the backup file directory and recursively delete all backup content.
/// </summary>
public void DeleteRootDir()
{
if (string.IsNullOrWhiteSpace(_backupRootDir)) return;
Expand All @@ -246,6 +247,33 @@ public void DeleteRootDir()
Directory.Delete(_backupRootDir, true);
}

/// <summary>
/// Restore the backup files of each version to the original directory.
/// </summary>
/// <param name="targetDirectory"></param>
/// <returns></returns>
public bool Restore(string targetDirectory)
{
try
{
while (_backupFiles?.Count > 0)
{
List<string> currentList = _backupFiles.Pop();
foreach (var filePath in currentList)
{
string fileName = Path.GetFileName(filePath);
string destFile = Path.Combine(targetDirectory, fileName);
File.Copy(filePath, destFile, true);
}
}
return true;
}
catch (Exception)
{
return false;
}
}

#endregion Public Methods

#region Private Methods
Expand Down Expand Up @@ -302,6 +330,9 @@ private Task DirtyUnknow(string appPath, string patchPath)

private void OnCompressProgress(object sender, BaseCompressProgressEventArgs e) => _compressProgressCallback(sender, e);

/// <summary>
/// Create a root directory for backup files
/// </summary>
private void CreateRootDirInTemp()
{
if (!string.IsNullOrEmpty(_backupRootDir)) return;
Expand All @@ -311,6 +342,10 @@ private void CreateRootDirInTemp()
if (!Directory.Exists(_backupRootDir)) Directory.CreateDirectory(_backupRootDir);
}

/// <summary>
/// Create a subfolder based on the root directory of the backup file for version-by-version backup.
/// </summary>
/// <returns></returns>
private string CreateSubfolderInRootDir()
{
string subFolderName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
Expand Down
9 changes: 0 additions & 9 deletions src/c#/GeneralUpdate.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeneralUpdate.Maui.OSS", "G
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GeneralUpdate.OSSClient", "GeneralUpdate.OSSClient\GeneralUpdate.OSSClient.csproj", "{2877DCA1-7EA6-42E9-A1C2-399B51E24893}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "service", "service", "{0315E141-AF1C-426D-A032-8D3A20502E3F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneralUpdate.ProtectService", "GeneralUpdate.ProtectService\GeneralUpdate.ProtectService.csproj", "{ABEE5D49-CC5A-4366-BD50-E7625905D481}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -127,10 +123,6 @@ Global
{2877DCA1-7EA6-42E9-A1C2-399B51E24893}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2877DCA1-7EA6-42E9-A1C2-399B51E24893}.Release|Any CPU.Build.0 = Release|Any CPU
{2877DCA1-7EA6-42E9-A1C2-399B51E24893}.Release|Any CPU.Deploy.0 = Release|Any CPU
{ABEE5D49-CC5A-4366-BD50-E7625905D481}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ABEE5D49-CC5A-4366-BD50-E7625905D481}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABEE5D49-CC5A-4366-BD50-E7625905D481}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABEE5D49-CC5A-4366-BD50-E7625905D481}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -153,7 +145,6 @@ Global
{9A28F525-124A-4019-8F09-BC48030E0E70} = {50B6BB53-63A9-414B-9BB0-79A69EEF6785}
{C4BDA544-2A6E-442C-B7D0-32CD7A996933} = {91F059E6-7AD3-4FB7-9604-30A7849C6EFF}
{2877DCA1-7EA6-42E9-A1C2-399B51E24893} = {74BE0282-A10D-4A81-A0F0-FAA79A6152B7}
{ABEE5D49-CC5A-4366-BD50-E7625905D481} = {0315E141-AF1C-426D-A032-8D3A20502E3F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A7B2D0AD-E000-4749-BAC0-FF21B9872805}
Expand Down

0 comments on commit 4095c5b

Please sign in to comment.