tiny.smg.storage.files
Defined in storage.
API (3)
Actions
Public operations.
Source
Source: tools/smg/src/storage/files.zig
zig
const pretty_json = @import("pretty").json;const std = @import("std");const sys = @import("sys");const smg = @import("../root.zig");const paths = smg.storage.paths;const text = smg.text;pub fn rulesPath(allocator: std.mem.Allocator, root: []const u8) ![]const u8 { return std.fs.path.join(allocator, &.{ root, paths.rules_relative_path });}pub fn loadRules( allocator: std.mem.Allocator, root: []const u8, limits: smg.StorageLimits,) ![]const std.json.Value { const path = try rulesPath(allocator, root); const raw = text.readFile(allocator, path, limits.max_rules_bytes) catch |err| switch (err) { error.FileNotFound => return &.{}, else => return err, }; return std.json.parseFromSliceLeaky([]std.json.Value, allocator, raw, .{}) catch |err| switch (err) { error.OutOfMemory => error.OutOfMemory, else => error.InvalidRules, };}pub fn saveRules(allocator: std.mem.Allocator, root: []const u8, documents: []const []const u8) !void { const path = try rulesPath(allocator, root); var out: std.Io.Writer.Allocating = .init(allocator); errdefer out.deinit(); var writer = pretty_json.Writer.init(&out.writer, .indent_2); const rules = try writer.array(); for (documents) |document| try rules.raw(document); try rules.endLine(); const tmp_path = try std.fmt.allocPrint(allocator, "{s}.tmp", .{path}); try text.writeFile(tmp_path, out.written()); try sys.fs.rename(tmp_path, path);}test "rules are one owned JSON ledger rooted in the project" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); const root = try testRoot(allocator); defer sys.fs.deleteTree(root) catch {}; try sys.fs.createDirPath(root); const missing = try loadRules(allocator, root, smg.default_limits.storage); try std.testing.expectEqual(@as(usize, 0), missing.len); const path = try rulesPath(allocator, root); try std.testing.expect(std.mem.endsWith(u8, path, paths.rules_relative_path)); try saveRules(allocator, root, &.{}); try std.testing.expectEqualStrings( "[]\n", try text.readFile(allocator, path, 4096), ); try saveRules(allocator, root, &.{ "{\"name\":\"deny\"}", "{\"name\":\"allow\"}" }); const raw = try text.readFile(allocator, path, 4096); try std.testing.expectEqualStrings( "[\n {\"name\":\"deny\"},\n {\"name\":\"allow\"}\n]\n", raw, ); const values = try loadRules(allocator, root, smg.default_limits.storage); try std.testing.expectEqual(@as(usize, 2), values.len); try std.testing.expectEqualStrings("deny", values[0].object.get("name").?.string); try std.testing.expectEqualStrings("allow", values[1].object.get("name").?.string);}fn testRoot(allocator: std.mem.Allocator) ![]const u8 { return try std.fmt.allocPrint(allocator, "/tmp/smg-storage-files-test-{x}", .{@as(u64, @intCast(@max(0, sys.time.realMilliTimestamp())))});}Source: tools/smg/src/storage/root.zig:3
zig
pub const files = @import("files.zig");Audit
| Definitions | 4 |
|---|---|
| Public names | 4 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |