summaryrefslogtreecommitdiff
path: root/src/Agent.Windows/Recovery
diff options
context:
space:
mode:
Diffstat (limited to 'src/Agent.Windows/Recovery')
-rw-r--r--src/Agent.Windows/Recovery/RecoveryManager.cs16
-rw-r--r--src/Agent.Windows/Recovery/TransactionalFileEditor.cs51
2 files changed, 67 insertions, 0 deletions
diff --git a/src/Agent.Windows/Recovery/RecoveryManager.cs b/src/Agent.Windows/Recovery/RecoveryManager.cs
index df69a5e..8fd7368 100644
--- a/src/Agent.Windows/Recovery/RecoveryManager.cs
+++ b/src/Agent.Windows/Recovery/RecoveryManager.cs
@@ -49,6 +49,8 @@ public sealed class RecoveryManager(string journalDirectory, ISecurityAuditSink
{
case "file.replace": RecoverFile(entry); break;
case "file.delete": RecoverDeletedFile(entry); break;
+ case "dir.mkdir": RecoverMkdir(entry); break;
+ case "dir.rmdir": RecoverRmdir(entry); break;
case "acl": RecoverAcl(entry); break;
case "wfp.dynamic": break; // BFE removes every object when the originating dynamic session ends.
case "network.loopback_exemption": RecoverLoopback(entry); break;
@@ -94,6 +96,20 @@ public sealed class RecoveryManager(string journalDirectory, ISecurityAuditSink
File.WriteAllBytes(entry.Target, original);
}
+ private static void RecoverMkdir(JournalEntry entry)
+ {
+ if (!Directory.Exists(entry.Target)) return;
+ if (Directory.EnumerateFileSystemEntries(entry.Target, "*", SearchOption.TopDirectoryOnly).Any())
+ throw new InvalidOperationException($"Directory '{entry.Target}' created by mkdir is not empty; automatic rollback refused.");
+ Directory.Delete(entry.Target, recursive: false);
+ }
+
+ private static void RecoverRmdir(JournalEntry entry)
+ {
+ if (Directory.Exists(entry.Target)) throw new InvalidOperationException($"Directory '{entry.Target}' was recreated after crash; automatic recovery refused.");
+ Directory.CreateDirectory(entry.Target);
+ }
+
private static void CleanupPrepared(JournalEntry entry)
{
if (entry.Kind == "network.loopback_exemption" && !bool.Parse(entry.Data["originally_present"]))
diff --git a/src/Agent.Windows/Recovery/TransactionalFileEditor.cs b/src/Agent.Windows/Recovery/TransactionalFileEditor.cs
index 52963a0..805a069 100644
--- a/src/Agent.Windows/Recovery/TransactionalFileEditor.cs
+++ b/src/Agent.Windows/Recovery/TransactionalFileEditor.cs
@@ -1,5 +1,6 @@
using System.Security.Cryptography;
using System.Text;
+using Agent.Core.Tools;
using Agent.Windows.Security;
namespace Agent.Windows.Recovery;
@@ -68,6 +69,30 @@ public sealed class TransactionalFileEditor(TaskResourceJournal journal, string
return new(journal, entryId, canonical, originalHash, backupPath, inline);
}
+ public DirectoryMkdirTransaction Mkdir(string path)
+ {
+ string canonical = CanonicalPath.Resolve(path, requireExisting: false);
+ bool existed = Directory.Exists(canonical);
+ if (existed) throw new InvalidOperationException($"Directory already exists: '{canonical}'.");
+ if (File.Exists(canonical)) throw new InvalidOperationException($"A file exists at the target path: '{canonical}'.");
+ Guid entryId = journal.Prepare("dir.mkdir", canonical, new Dictionary<string, string> { ["existed"] = existed.ToString() });
+ Directory.CreateDirectory(canonical);
+ journal.MarkApplied(entryId);
+ return new(journal, entryId, canonical);
+ }
+
+ public DirectoryRmdirTransaction Rmdir(string path)
+ {
+ string canonical = CanonicalPath.Resolve(path);
+ if (!Directory.Exists(canonical)) throw new DirectoryNotFoundException($"Directory does not exist: '{canonical}'.");
+ if (Directory.EnumerateFileSystemEntries(canonical, "*", SearchOption.TopDirectoryOnly).Any())
+ throw new ToolValidationException("Directory is not empty. Remove contents first before removing the directory.");
+ Guid entryId = journal.Prepare("dir.rmdir", canonical, new Dictionary<string, string>());
+ Directory.Delete(canonical, recursive: false);
+ journal.MarkApplied(entryId);
+ return new(journal, entryId, canonical);
+ }
+
private static string NormalizeHash(string hash) => hash.Replace("-", string.Empty, StringComparison.Ordinal).Trim().ToUpperInvariant();
}
@@ -93,6 +118,32 @@ public sealed class FileDeleteTransaction(TaskResourceJournal journal, Guid entr
}
}
+public sealed class DirectoryMkdirTransaction(TaskResourceJournal journal, Guid entryId, string path)
+{
+ private bool _finished;
+ public void Commit() { if (_finished) return; journal.MarkCleaned(entryId); _finished = true; }
+ public void Rollback()
+ {
+ if (_finished) return;
+ if (Directory.Exists(path)) Directory.Delete(path, recursive: false);
+ journal.MarkCleaned(entryId);
+ _finished = true;
+ }
+}
+
+public sealed class DirectoryRmdirTransaction(TaskResourceJournal journal, Guid entryId, string path)
+{
+ private bool _finished;
+ public void Commit() { if (_finished) return; journal.MarkCleaned(entryId); _finished = true; }
+ public void Rollback()
+ {
+ if (_finished) return;
+ if (!Directory.Exists(path)) Directory.CreateDirectory(path);
+ journal.MarkCleaned(entryId);
+ _finished = true;
+ }
+}
+
public sealed class FileEditTransaction(
TaskResourceJournal journal, Guid entryId, string path, string originalHash, string replacementHash,
string backupPath, string originalBase64, bool originallyExisted)