╭ serve_common.zig ─────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("./event.zig"); 3 4 // the outcome of resolving a requested repo path to its on-disk directory. 5 // the http and ssh paths each map the cases to their own error responses. 6 pub const RepoPath = union(enum) { 7 ok: []const u8, // resolved on-disk path; the caller owns and frees it 8 invalid, // a <owner>/<repo> path was expected but not given 9 not_found, // unknown owner, or the repo doesn't exist and we aren't creating 10 }; 11 12 // resolve a requested repo path to its on-disk directory, parsed as 13 // <owner>/<repo> through the event store, minting a repo event for a fresh push. 14 pub fn resolveRepoPath( 15 io: std.Io, 16 allocator: std.mem.Allocator, 17 repo_root_path: []const u8, 18 admin_repo_path: []const u8, 19 requested: []const u8, 20 create_if_missing: bool, 21 ) !RepoPath { 22 const owner_repo = evt.parseOwnerRepoPath(requested) orelse return .invalid; 23 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, owner_repo.owner, owner_repo.name, create_if_missing)) orelse return .not_found; 24 return .{ .ok = try std.fs.path.join(allocator, &.{ repo_root_path, &event_id_hex }) }; 25 } 26 27 // every connection task shares one error writer, so writing to it takes a 28 // lock. uncancelable, since a log line shouldn't be a cancelation point. 29 var log_mutex: std.Io.Mutex = .init; 30 31 pub fn logError(io: std.Io, err: *std.Io.Writer, comptime fmt: []const u8, args: anytype) void { 32 log_mutex.lockUncancelable(io); 33 defer log_mutex.unlock(io); 34 err.print(fmt, args) catch return; 35 err.flush() catch {}; 36 } 37 38 // accept connections forever, spawning `handleConn(context, stream)` as a task 39 // for each. the handler owns the stream (it closes it). `name` labels accept 40 // errors in the log. shared by the http, ssh, and web ui listeners. 41 pub fn runListener( 42 io: std.Io, 43 net_server: *std.Io.net.Server, 44 tasks: *std.Io.Group, 45 err: *std.Io.Writer, 46 name: []const u8, 47 context: anytype, 48 comptime handleConn: fn (@TypeOf(context), std.Io.net.Stream) void, 49 ) void { 50 const Context = @TypeOf(context); 51 52 const Conn = struct { 53 context: Context, 54 stream: std.Io.net.Stream, 55 56 fn run(c: @This()) void { 57 handleConn(c.context, c.stream); 58 } 59 }; 60 61 const Listener = struct { 62 io: std.Io, 63 net_server: *std.Io.net.Server, 64 tasks: *std.Io.Group, 65 err: *std.Io.Writer, 66 name: []const u8, 67 context: Context, 68 69 fn run(self: @This()) void { 70 while (true) { 71 const stream = self.net_server.accept(self.io) catch |accept_err| { 72 if (accept_err == error.Canceled) return; 73 logError(self.io, self.err, "{s} accept failed: {s}\n", .{ self.name, @errorName(accept_err) }); 74 continue; 75 }; 76 self.tasks.async(self.io, Conn.run, .{Conn{ .context = self.context, .stream = stream }}); 77 } 78 } 79 }; 80 81 tasks.async(io, Listener.run, .{Listener{ 82 .io = io, 83 .net_server = net_server, 84 .tasks = tasks, 85 .err = err, 86 .name = name, 87 .context = context, 88 }}); 89 } 90