lib/sandbox/src/result.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 const change = @import("change.zig");
 3 const command = @import("command.zig");
 4 const layer_data = @import("layer.zig");
 5 const audit_data = @import("audit.zig");
 6 
 7 const Allocator = std.mem.Allocator;
 8 
 9 /// Holds the resources and captured outputs of an execution, owning the
10 /// standard output and standard error buffers, the recorded change set, the
11 /// configuration `Audit`, and the layer directory.
12 ///
13 /// Calling `deinit` frees the stream buffers and change set memory, then
14 /// deletes the temporary layer directory. Callers that want to keep files
15 /// generated in the layer must read or copy them out before calling `deinit`.
16 pub const Result = struct {
17     allocator: Allocator,
18     layer: layer_data.Layer,
19     status: command.Status,
20     stdout: []u8,
21     stderr: []u8,
22     changes: change.Set,
23     audit: audit_data.Audit,
24 
25     pub fn deinit(self: *Result) void {
26         self.changes.deinit();
27         self.allocator.free(self.stdout);
28         self.allocator.free(self.stderr);
29         self.layer.deinit();
30         self.* = undefined;
31     }
32 
33     /// Reads an output file through the layer directory handle into a buffer
34     /// allocated with `allocator`, transferring ownership of the slice to the
35     /// caller, who must free it using the supplied allocator. Relative path
36     /// resolution starts at the root of the layer directory but performs no
37     /// path-containment checks, allowing lookups that navigate outside the
38     /// layer. The `limit` argument sets the maximum number of bytes to read.
39     pub fn readFileAlloc(self: *const Result, allocator: Allocator, path: []const u8, limit: usize) ![]u8 {
40         return try self.layer.readFileAlloc(allocator, path, limit);
41     }
42 };