diff options
Diffstat (limited to 'src/Agent.Windows/Tools/WindowsToolBackend.cs')
| -rw-r--r-- | src/Agent.Windows/Tools/WindowsToolBackend.cs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/Agent.Windows/Tools/WindowsToolBackend.cs b/src/Agent.Windows/Tools/WindowsToolBackend.cs index c0d1561..bb54c6e 100644 --- a/src/Agent.Windows/Tools/WindowsToolBackend.cs +++ b/src/Agent.Windows/Tools/WindowsToolBackend.cs @@ -78,6 +78,7 @@ public sealed class WindowsToolBackend(WindowsSandboxSettings settings, ISecurit "file_edit" => Task.FromResult(Edit(context, arguments)), "process_run" => Task.FromResult(RunProcess(taskId, arguments)), "task_update" => Task.FromResult(_taskUpdates.Execute(taskId, arguments)), + "dir" => Task.FromResult(DirectoryOp(context, arguments)), _ => throw new ToolValidationException($"Unknown internal tool '{toolName}'.") }; } @@ -172,6 +173,39 @@ public sealed class WindowsToolBackend(WindowsSandboxSettings settings, ISecurit return Success($"{operation} completed for {Path.GetFullPath(path)}", externalEffect: true); } + private ToolExecutionResult DirectoryOp(TaskContext context, JsonElement arguments) + { + string path = arguments.GetProperty("path").GetString()!; + string operation = arguments.GetProperty("operation").GetString()!; + var editor = new TransactionalFileEditor(context.Journal, settings.BackupDirectory, settings.MemoryBackupLimitBytes); + if (operation == "mkdir") + { + _paths.Demand(path, FileCapability.Write); + DirectoryMkdirTransaction transaction = editor.Mkdir(path); + transaction.Commit(); + audit.Write(new(DateTimeOffset.UtcNow, context.Journal.TaskId, "dir.mkdir", "completed", new Dictionary<string, string> { ["path"] = Path.GetFullPath(path) })); + return Success($"mkdir completed for {Path.GetFullPath(path)}", externalEffect: true); + } + if (operation == "rmdir") + { + _paths.Demand(path, FileCapability.Delete); + DirectoryRmdirTransaction transaction = editor.Rmdir(path); + transaction.Commit(); + audit.Write(new(DateTimeOffset.UtcNow, context.Journal.TaskId, "dir.rmdir", "completed", new Dictionary<string, string> { ["path"] = Path.GetFullPath(path) })); + return Success($"rmdir completed for {Path.GetFullPath(path)}", externalEffect: true); + } + if (operation == "list") + { + _paths.Demand(path, FileCapability.Read); + string canonical = CanonicalPath.Resolve(path); + var entries = Directory.EnumerateFileSystemEntries(canonical, "*", SearchOption.TopDirectoryOnly) + .Select(Path.GetFullPath) + .ToArray(); + return Success(JsonSerializer.Serialize(entries)); + } + throw new ToolValidationException($"Unsupported dir operation '{operation}'."); + } + private ToolExecutionResult RunProcess(Guid taskId, JsonElement arguments) { string executable = arguments.GetProperty("executable").GetString()!; |
