11using System ;
22using System . IO ;
33using System . Threading . Tasks ;
4+ using GeneralUpdate . Core . Differential ;
45using GeneralUpdate . Core . FileSystem ;
56using GeneralUpdate . Core . Event ;
67using GeneralUpdate . Core . Pipeline ;
@@ -22,6 +23,9 @@ public abstract class AbstractStrategy : IStrategy
2223
2324 /// <summary>Optional reporter for update status reporting.</summary>
2425 protected IUpdateReporter ? Reporter { get ; set ; }
26+
27+ /// <summary>Optional binary differ for differential patch updates.</summary>
28+ public IBinaryDiffer ? Differ { get ; set ; }
2529
2630 public virtual void Execute ( ) => throw new NotImplementedException ( ) ;
2731
@@ -46,6 +50,7 @@ public virtual async Task ExecuteAsync()
4650 {
4751 status = ReportType . Failure ;
4852 HandleExecuteException ( e ) ;
53+ TryRollback ( ) ;
4954 }
5055 finally
5156 {
@@ -89,6 +94,8 @@ protected virtual PipelineContext CreatePipelineContext(VersionInfo version, str
8994 context . Add ( "SourcePath" , _configinfo . InstallPath ) ;
9095 context . Add ( "PatchPath" , patchPath ) ;
9196 context . Add ( "PatchEnabled" , _configinfo . PatchEnabled ) ;
97+ // Binary differ for differential patching
98+ context . Add ( "BinaryDiffer" , Differ ) ;
9299
93100 return context ;
94101 }
@@ -135,10 +142,32 @@ protected static string CheckPath(string path, string name)
135142 // The Hooks and Reporter properties are declared here so subclasses inherit them
136143 // without redeclaring.
137144
145+ /// <summary>
146+ /// Attempts to restore from backup when a pipeline execution fails.
147+ /// Only restores if a backup directory exists for the current version.
148+ /// </summary>
149+ private void TryRollback ( )
150+ {
151+ try
152+ {
153+ var backupDir = _configinfo . BackupDirectory ;
154+ if ( ! string . IsNullOrWhiteSpace ( backupDir ) && Directory . Exists ( backupDir ) )
155+ {
156+ GeneralTracer . Warn ( $ "AbstractStrategy.TryRollback: restoring from backup { backupDir } -> { _configinfo . InstallPath } ") ;
157+ StorageManager . Restore ( backupDir , _configinfo . InstallPath ) ;
158+ GeneralTracer . Info ( "AbstractStrategy.TryRollback: restore completed." ) ;
159+ }
160+ }
161+ catch ( Exception ex )
162+ {
163+ GeneralTracer . Error ( "AbstractStrategy.TryRollback: rollback failed." , ex ) ;
164+ }
165+ }
166+
138167 private static void Clear ( string path )
139168 {
140169 if ( Directory . Exists ( path ) )
141170 StorageManager . DeleteDirectory ( path ) ;
142171 }
143172 }
144- }
173+ }
0 commit comments