From 9721e1be7fc5b89463e845be8d7d437d5434c48f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Wed, 2 Mar 2022 06:50:15 +0200 Subject: [PATCH] start with real sections --- README.md | 15 +++++++++------ src/sections.zig | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b81420f..d8652fd 100644 --- a/README.md +++ b/README.md @@ -370,12 +370,15 @@ STATUS SECTION SIZE DESCRIPTION Section creation order: -1. Groupmembers, UserGids. No dependencies. -2. ShellIndex, ShellBlob. No dependencies. -3. `bdz_*`. No depdendencies. -4. Groups. Requires Groupmembers. -5. Users. Requires Groupmembers and ShellIndex. -6. `idx_*`. Requires offsets to Groups and Users. +1. `bdz_*`. No depdendencies. +1. ShellIndex, ShellBlob. No dependencies. +1. UserGids. No dependencies. +1. Users, but without `additional_gids_offset`. No dependencies. +1. Groupmembers. Depends on Users, ex. `additional_gids_offset`. +1. Groups. Requires Groupmembers. +1. Mutate `Users.additional_gids_offset`. Requires Groupmembers and ShellIndex. +1. `idx_*`. Requires offsets to Groups and Users. +1. Header. [git-subtrac]: https://apenwarr.ca/log/20191109 [cmph]: http://cmph.sourceforge.net/ diff --git a/src/sections.zig b/src/sections.zig index fd70bf0..9219d1e 100644 --- a/src/sections.zig +++ b/src/sections.zig @@ -3,11 +3,13 @@ const unicode = std.unicode; const sort = std.sort; const Allocator = std.mem.Allocator; const ArrayListUnmanaged = std.ArrayListUnmanaged; +const ArrayList = std.ArrayList; const StringHashMap = std.StringHashMap; const AutoHashMap = std.AutoHashMap; const BufSet = std.BufSet; const pad = @import("padding.zig"); +const compress = @import("compress.zig"); const userImport = @import("user.zig"); const groupImport = @import("group.zig"); const User = userImport.User; @@ -139,6 +141,46 @@ const Corpus = struct { } }; +pub const Sections = struct { + allocator: Allocator, + corpus: *const Corpus, + + pub fn init(allocator: Allocator, corpus: *const Corpus) Sections { + return Sections{ + .allocator = allocator, + .corpus = corpus, + }; + } + + pub const GroupMembers = struct { + offsets: []const usize, + bytes: []const u8, + }; + + const groupMembersErr = error{Overflow} || Allocator.Error; + + pub fn groupMembers(self: *const Sections) groupMembersErr!GroupMembers { + var buf: [compress.maxVarintLen64]u8 = undefined; + var offsets = ArrayListUnmanaged(usize).initCapacity( + self.allocator, + self.corpus.groups.len, + ); + var bytes = ArrayList(u8).init(self.allocator); + var offset: usize = 0; + for (self.corpus.groups) |group, i| { + offsets[i] = offset; + const users = self.corpus.groupname2users.get(group.name).?; + const len = try compress.putVarint(&buf, users.len); + offset += len; + try bytes.appendSlice(buf[0..len]); + for (users) |user| { + // TODO: offset into the User's record + _ = user; + } + } + } +}; + // cmpUser compares two users for sorting. By username's utf8 codepoints, ascending. fn cmpUser(_: void, a: User, b: User) bool { var utf8_a = (unicode.Utf8View.init(a.name) catch unreachable).iterator();