summaryrefslogtreecommitdiff
path: root/src/Agent.App/Sandbox/SandboxServices.cs
blob: 746b95bcf47edc2a661c1658a1e1e19c8934ab3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.Text.Json;

namespace Agent.App.Sandbox;

public sealed record ResolutionItem(string Kind, string LogicalPath, string ResolvedPath, string Access);

public static class ExecutableResolver
{
    public static bool IsSupportedShell(string path) =>
        Path.GetFileName(path).ToLowerInvariant() is "cmd.exe" or "powershell.exe" or "pwsh.exe";
}

internal static class AtomicJson
{
    public static void Write<T>(string path, T value, JsonSerializerOptions options)
    {
        string temporary = path + "." + Guid.NewGuid().ToString("N") + ".tmp";
        try
        {
            using (var stream = new FileStream(temporary, FileMode.CreateNew, FileAccess.Write, FileShare.None, 4096, FileOptions.WriteThrough))
            {
                JsonSerializer.Serialize(stream, value, options);
                stream.Flush(flushToDisk: true);
            }
            File.Move(temporary, path, overwrite: true);
        }
        finally
        {
            if (File.Exists(temporary)) File.Delete(temporary);
        }
    }
}