From b30c7e46dfba6d14e6f45fc7d3e9f69fd7d2cb41 Mon Sep 17 00:00:00 2001 From: robin Date: Fri, 18 Sep 2026 11:38:51 +0200 Subject: [PATCH] io: close your handles --- src/data.zig | 9 ++++++++- src/gen.zig | 35 ++++++++++++++++------------------- src/handler.zig | 1 + src/helpers.zig | 4 ++-- src/lua/data.zig | 1 + src/lua/lua_require.zig | 1 + src/lua/root.zig | 2 ++ 7 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/data.zig b/src/data.zig index 9399c28..c9c9bb8 100644 --- a/src/data.zig +++ b/src/data.zig @@ -110,6 +110,7 @@ pub const Frontmatter = struct { pub fn load() !void { fern.trace().ctx("data.load").log(); const rootdir = try Io.Dir.openDirAbsolute(maivi.io, maivi.config.root, .{}); + defer rootdir.close(maivi.io); const data_dir = Io.Dir.openDir(rootdir, maivi.io, "data", .{ .iterate = true, .access_sub_paths = true }) catch |err| switch (err) { error.FileNotFound, error.NotDir => { fern.warn().ctx("data") @@ -119,6 +120,7 @@ pub fn load() !void { }, else => return err, }; + defer data_dir.close(maivi.io); try parseDir(data_dir); } @@ -152,9 +154,14 @@ fn parseDir(dir: Io.Dir) !void { return while (try it.next(maivi.io)) |x| switch (x.kind) { .file => { const file = try dir.openFile(maivi.io, x.name, .{}); + defer file.close(maivi.io); try parseFile(file); }, - .directory => try parseDir(try dir.openDir(maivi.io, x.name, .{ .iterate = true, .access_sub_paths = true })), + .directory => { + const sub_dir = try dir.openDir(maivi.io, x.name, .{ .iterate = true, .access_sub_paths = true }); + defer sub_dir.close(maivi.io); + try parseDir(sub_dir); + }, else => continue, }; } diff --git a/src/gen.zig b/src/gen.zig index 37274ff..d80173a 100644 --- a/src/gen.zig +++ b/src/gen.zig @@ -2,32 +2,28 @@ const std = @import("std"); const Io = std.Io; const Allocator = std.mem.Allocator; -const maivi = @import("root.zig"); const fern = @import("fern"); -const zlua = @import("zlua"); -const lua = maivi.lua; - -var arena: std.heap.ArenaAllocator = undefined; -var allocator: std.mem.Allocator = undefined; +const maivi = @import("root.zig"); +const Lua = maivi.lua.Lua; pub fn generate() !void { fern.info().ctx("gen.generate") .msg("generating") .str("path", maivi.config.buildroot).log(); - arena = .init(maivi.allocator); + var arena: std.heap.ArenaAllocator = .init(maivi.allocator); defer arena.deinit(); - allocator = arena.allocator(); + const allocator = arena.allocator(); - var threaded = Io.Threaded.init(maivi.allocator, .{}); - const io = threaded.io(); + const io = maivi.io; - Io.Dir.createDirAbsolute(maivi.io, maivi.config.buildroot, .default_dir) catch |err| switch (err) { + Io.Dir.createDirAbsolute(io, maivi.config.buildroot, .default_dir) catch |err| switch (err) { error.PathAlreadyExists => {}, else => return err, }; - const buildroot = try Io.Dir.openDirAbsolute(maivi.io, maivi.config.buildroot, .{ .access_sub_paths = true }); + const buildroot = try Io.Dir.openDirAbsolute(io, maivi.config.buildroot, .{ .access_sub_paths = true }); + defer buildroot.close(io); var it = maivi.router.luapaths.iterator(); while (true) { @@ -37,7 +33,7 @@ pub fn generate() !void { fern.debug().ctx("gen.generate").msg("generating path").str("path", path).log(); - genpath(io, buildroot, path, data) catch |err| { + genpath(maivi.lua.lua, io, allocator, buildroot, path, data) catch |err| { fern.err().ctx("gen.generate") .msg("could not generate path") .str("path", path) @@ -48,12 +44,12 @@ pub fn generate() !void { } } -fn genpath(io: Io, dir: Io.Dir, path: []const u8, data: maivi.Router.LuaPath) !void { +fn genpath(l: *Lua, io: Io, arena: Allocator, dir: Io.Dir, path: []const u8, data: maivi.Router.LuaPath) !void { const outpath = blk: { const outpath_base = if (path.len == 1) "." else path[1..]; var it = std.mem.splitBackwardsScalar(u8, path, '/'); break :blk if (!std.mem.containsAtLeast(u8, it.first(), 1, ".")) - try std.mem.join(allocator, "/", &.{ outpath_base, "index.html" }) + try std.mem.join(arena, "/", &.{ outpath_base, "index.html" }) else outpath_base; }; @@ -61,18 +57,19 @@ fn genpath(io: Io, dir: Io.Dir, path: []const u8, data: maivi.Router.LuaPath) !v fern.info().ctx("gen.writepath") .str("path", path) .str("outpath", outpath).log(); - if (std.mem.cutScalarLast(u8, outpath, '/')) |outpath_parts| try mkdir(dir, outpath_parts[0]); + if (std.mem.cutScalarLast(u8, outpath, '/')) |outpath_parts| try mkdir(io, dir, outpath_parts[0]); const file = try dir.createFile(io, outpath, .{ .lock = .exclusive }); + defer file.close(io); var buf: [0x1000]u8 = undefined; var writer = file.writer(io, &buf); - try maivi.Router.get(maivi.lua.lua, &writer.interface, data); + try maivi.Router.get(l, &writer.interface, data); } // helpers ==================================================================== -fn mkdir(dir: Io.Dir, path: []const u8) !void { +fn mkdir(io: Io, dir: Io.Dir, path: []const u8) !void { if (path.len == 0) return; var parts = std.mem.splitScalar(u8, path, '/'); @@ -82,7 +79,7 @@ fn mkdir(dir: Io.Dir, path: []const u8) !void { const sub_path = if (parts.index) |n| path[0 .. n - 1] else path; - Io.Dir.createDir(dir, maivi.io, sub_path, .default_dir) catch |err| switch (err) { + Io.Dir.createDir(dir, io, sub_path, .default_dir) catch |err| switch (err) { error.PathAlreadyExists => {}, else => return err, }; diff --git a/src/handler.zig b/src/handler.zig index fe5e18c..85ddc60 100644 --- a/src/handler.zig +++ b/src/handler.zig @@ -174,6 +174,7 @@ fn returnNotFound(conn: *Connection) !void { fn withNotFound(conn: *Connection, try_path: []const u8) !void { const dir = try Io.Dir.openDirAbsolute(maivi.io, maivi.config.buildroot, .{}); + defer dir.close(maivi.io); try helpers.sendfile(conn, dir, try_path); } diff --git a/src/helpers.zig b/src/helpers.zig index 9e94e35..8e148ea 100644 --- a/src/helpers.zig +++ b/src/helpers.zig @@ -57,12 +57,12 @@ pub fn sendfile(conn: *Connection, dir: std.Io.Dir, path: []const u8) !void { const sub_path = std.mem.cutPrefix(u8, path, "/") orelse path; const file = try dir.openFile(maivi.io, sub_path, .{}); + defer file.close(maivi.io); const stat = try file.stat(maivi.io); if (stat.kind == .directory) return error.IsDirectory; if (stat.kind != .file) return error.NonFile; const size = stat.size; - defer file.close(maivi.io); const content_type = content_type_for_file(path) orelse .txt; @@ -340,12 +340,12 @@ pub fn read_file(allocator: std.mem.Allocator, dir: std.Io.Dir, path: []const u8 const sub_path = std.mem.cutPrefix(u8, path, "/") orelse path; const file = try dir.openFile(maivi.io, sub_path, .{}); + defer file.close(maivi.io); const stat = try file.stat(maivi.io); if (stat.kind == .directory) return error.IsDirectory; if (stat.kind != .file) return error.NonFile; const size = stat.size; - defer file.close(maivi.io); var buf: [0x400]u8 = undefined; var file_reader = file.reader(maivi.io, &buf); diff --git a/src/lua/data.zig b/src/lua/data.zig index 04dd606..236d369 100644 --- a/src/lua/data.zig +++ b/src/lua/data.zig @@ -10,6 +10,7 @@ const helpers = maivi.helpers; /// * Pushes to Stack: `1` pub fn read(l: *Lua, path: []const u8) !void { const dir = try std.Io.Dir.openDirAbsolute(maivi.io, maivi.config.root, .{}); + defer dir.close(maivi.io); const contents = try helpers.read_file(maivi.allocator, dir, path); defer maivi.allocator.free(contents); diff --git a/src/lua/lua_require.zig b/src/lua/lua_require.zig index bc7a286..ff65a24 100644 --- a/src/lua/lua_require.zig +++ b/src/lua/lua_require.zig @@ -39,6 +39,7 @@ fn loadchunk(l: *Lua, modname: []const u8) !void { fn loadfile(modname: []const u8) !void { const dir = try std.Io.Dir.openDirAbsolute(maivi.io, maivi.config.luaroot, .{}); + defer dir.close(maivi.io); const contents = helpers.read_file(chunk_allocator, dir, modname) catch |err| switch (err) { error.IsDirectory => blk: { const new_path = try std.mem.concat(maivi.allocator, u8, &.{ modname, "/init.lua" }); diff --git a/src/lua/root.zig b/src/lua/root.zig index 71e5137..b598090 100644 --- a/src/lua/root.zig +++ b/src/lua/root.zig @@ -163,10 +163,12 @@ fn do_require(l: *Lua, modname: []const u8) !void { pub fn setup() !void { // load plugins const dir = try std.Io.Dir.openDirAbsolute(maivi.io, maivi.config.root, .{}); + defer dir.close(maivi.io); const plugindir = dir.openDir(maivi.io, "plugins", .{ .iterate = true }) catch |err| switch (err) { error.FileNotFound => return, else => return err, }; + defer plugindir.close(maivi.io); var it = plugindir.iterate(); while (try it.next(maivi.io)) |entry| { if (entry.kind != .file) continue; -- 2.51.2