╭ Attachment.zig ───────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const hash = xit.hash; 6 7 parent_id: [evt.event_id_size * 2]u8, 8 9 // what the db stores: the event's data plus the commit-derived fields. the file 10 // itself is the one entry in the event commit's tree, so its name and blob come 11 // from there rather than from the payload. 12 pub const Record = struct { 13 event: Self, 14 removed: bool = false, 15 author_email: ?[]const u8 = null, 16 created_order: u64 = 0, 17 name: []const u8, 18 blob_oid: []const u8, 19 }; 20 21 const Self = @This(); 22 23 // the moment keys `evt.merge` reads and writes for this kind 24 pub const merge_policy: evt.MergePolicy = .target_wins; 25 pub const record_map_key = "event-id->attachment"; 26 pub const id_set_key = "attachment-id-set"; 27 28 // the index a parent event's view reads 29 pub const parent_id_to_attachment_id_set_key = "parent-id->attachment-id-set"; 30 31 pub const max_size: usize = 10 * 1024 * 1024; 32 pub const name_max_len = 255; 33 34 // the name is the tree entry's name, so it must be a legal path component 35 pub fn nameValid(name: []const u8) bool { 36 if (name.len == 0 or name.len > name_max_len) return false; 37 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return false; 38 for (name) |ch| { 39 if (ch == '/' or ch == '\\' or std.ascii.isControl(ch)) return false; 40 } 41 return true; 42 } 43 44 fn parentExists( 45 comptime DB: type, 46 comptime hash_kind: hash.HashKind, 47 haxy_moment: anytype, 48 parent_id: *const [evt.event_id_size]u8, 49 ) !bool { 50 const kinds_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, evt.event_index_key)) orelse return false; 51 const kinds = try DB.HashMap(.read_only).init(kinds_cursor); 52 return try kinds.getCursor(hash.hashInt(hash_kind, parent_id)) != null; 53 } 54 55 pub fn consume( 56 comptime DB: type, 57 comptime hash_kind: hash.HashKind, 58 haxy_moment: DB.HashMap(.read_write), 59 event_id: *const [evt.event_id_size]u8, 60 record_maybe: ?Record, 61 arena: *std.heap.ArenaAllocator, 62 _: ?[]const u8, 63 ) !void { 64 const attachment_key = hash.hashInt(hash_kind, event_id); 65 66 const event_id_to_attachment_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key)); 67 const event_id_to_attachment = try DB.HashMap(.read_write).init(event_id_to_attachment_cursor); 68 69 const parent_id_to_attachment_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, parent_id_to_attachment_id_set_key)); 70 const parent_id_to_attachment_id_set = try DB.HashMap(.read_write).init(parent_id_to_attachment_id_set_cursor); 71 72 var existing_record_maybe: ?Record = null; 73 const existing_cursor_maybe = try event_id_to_attachment.getCursor(attachment_key); 74 if (existing_cursor_maybe) |existing_cursor| { 75 const existing_attachment = try DB.HashMap(.read_only).init(existing_cursor); 76 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_attachment); 77 } 78 79 var record_to_write = if (record_maybe) |record| 80 record 81 else blk: { 82 var record = existing_record_maybe orelse return error.EventNotFound; 83 record.removed = true; 84 break :blk record; 85 }; 86 87 const id_bytes = try evt.parseEventId(&record_to_write.event.parent_id); 88 if (!try parentExists(DB, hash_kind, haxy_moment, &id_bytes)) return error.ParentNotFound; 89 if (!nameValid(record_to_write.name)) return error.InvalidAttachment; 90 91 if (existing_record_maybe) |existing_record| { 92 // the file never changes, so an update preserves everything the original 93 // commit determined 94 record_to_write.created_order = existing_record.created_order; 95 record_to_write.author_email = existing_record.author_email; 96 record_to_write.name = existing_record.name; 97 record_to_write.blob_oid = existing_record.blob_oid; 98 99 // an attachment cannot move to another parent 100 if (!std.mem.eql(u8, &existing_record.event.parent_id, &record_to_write.event.parent_id)) return error.ParentChanged; 101 } 102 103 const attachment_cursor = try event_id_to_attachment.putCursor(attachment_key); 104 const attachment = try DB.HashMap(.read_write).init(attachment_cursor); 105 try evt.upsert(Record, DB, hash_kind, attachment, record_to_write); 106 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .attach, record_to_write.created_order, record_to_write.removed); 107 108 if (existing_cursor_maybe == null) { 109 const attachment_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 110 const attachment_id_set = try DB.SortedSet(.read_write).init(attachment_id_set_cursor); 111 try attachment_id_set.put(&evt.orderKeyDesc(record_to_write.created_order, event_id)); 112 } 113 114 // removed attachments stay in the parent's set; the views skip them 115 const parent_attachments_cursor = try parent_id_to_attachment_id_set.putCursor(hash.hashInt(hash_kind, &record_to_write.event.parent_id)); 116 const parent_attachments = try DB.SortedSet(.read_write).init(parent_attachments_cursor); 117 try parent_attachments.put(&evt.orderKey(record_to_write.created_order, event_id)); 118 } 119 120 // attach `blob` to a parent event and return the event id. `repo` must be 121 // writable. 122 pub fn create( 123 comptime repo_kind: rp.RepoKind, 124 comptime repo_opts: rp.RepoOpts(repo_kind), 125 io: std.Io, 126 allocator: std.mem.Allocator, 127 repo: *rp.Repo(repo_kind, repo_opts), 128 parent_id: *const [evt.event_id_size * 2]u8, 129 blob: evt.Blob, 130 author: evt.CommitAuthor, 131 ) ![evt.event_id_size * 2]u8 { 132 if (!nameValid(blob.name)) return error.InvalidFields; 133 if (blob.size == 0 or blob.size > max_size) return error.InvalidFields; 134 135 const parent_id_bytes = evt.parseEventId(parent_id) catch return error.InvalidFields; 136 137 { 138 const DB = evt.EventDB(repo_opts.hash); 139 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 140 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 141 const haxy_moment = (if (event_db_maybe) |*event_db| 142 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 143 else if (repo_kind == .git) 144 return error.ParentNotFound 145 else 146 evt.currentMoment(repo_opts, repo)) catch return error.ParentNotFound; 147 if (!try parentExists(DB, repo_opts.hash, haxy_moment, &parent_id_bytes)) return error.ParentNotFound; 148 } 149 150 var id_bytes: [evt.event_id_size]u8 = undefined; 151 io.random(&id_bytes); 152 const event_id = std.fmt.bytesToHex(id_bytes, .lower); 153 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ 154 .id = event_id, 155 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 156 .author = author, 157 .tree_entries = &.{.{ .blob = blob }}, 158 .event = .{ .attach = .{ .parent_id = parent_id.* } }, 159 }}); 160 return event_id; 161 } 162 163 // read an attachment by event id, or null if the id isn't a known attachment. 164 // field byte slices are allocated in `arena`. 165 pub fn readById( 166 comptime DB: type, 167 comptime hash_kind: hash.HashKind, 168 haxy_moment: DB.HashMap(.read_only), 169 arena: *std.heap.ArenaAllocator, 170 attachment_id: []const u8, 171 ) !?Record { 172 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 173 const records = try DB.HashMap(.read_only).init(records_cursor); 174 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, attachment_id)) orelse return null; 175 const record_map = try DB.HashMap(.read_only).init(record_cursor); 176 return try evt.read(Record, DB, hash_kind, arena, record_map); 177 } 178