sections: gc on error

This commit is contained in:
Motiejus Jakštys 2022-03-16 06:57:35 +02:00 committed by Motiejus Jakštys
parent 8a4bb62613
commit 9c2f0969dc

View File

@ -326,7 +326,7 @@ pub fn groupsSection(
const group = corpus.groups.get(i); const group = corpus.groups.get(i);
const group_offset = try math.cast(u32, blob.items.len); const group_offset = try math.cast(u32, blob.items.len);
assert(group_offset & 7 == 0); assert(group_offset & 7 == 0);
idx2offset[i] = group_offset; idx2offset[i] = @truncate(u32, group_offset >> 3);
const group_stored = groupImport.GroupStored{ const group_stored = groupImport.GroupStored{
.gid = group.gid, .gid = group.gid,
.name = group.name, .name = group.name,
@ -358,10 +358,8 @@ pub fn bdzIdx(
}; };
assert(keys.len <= math.maxInt(u32)); assert(keys.len <= math.maxInt(u32));
var result = try allocator.alloc(u32, keys.len); var result = try allocator.alloc(u32, keys.len);
for (keys) |key, i| { for (keys) |key, i|
const hash = search_fn(packed_mphf, key); result[search_fn(packed_mphf, key)] = @intCast(u32, i);
result[hash] = @intCast(u32, i);
}
return result; return result;
} }
@ -409,57 +407,49 @@ pub const AllSections = struct {
allocator: Allocator, allocator: Allocator,
corpus: *const Corpus, corpus: *const Corpus,
) error{ Overflow, OutOfMemory, InvalidRecord }!AllSections { ) error{ Overflow, OutOfMemory, InvalidRecord }!AllSections {
const bdz_gid = try cmph.packU32(allocator, corpus.groups.items(.gid)); const gids = corpus.groups.items(.gid);
const bdz_groupname = try cmph.packStr(allocator, corpus.groups.items(.name)); const gnames = corpus.groups.items(.name);
const bdz_uid = try cmph.packU32(allocator, corpus.users.items(.uid)); const uids = corpus.users.items(.uid);
const bdz_username = try cmph.packStr(allocator, corpus.users.items(.name)); const unames = corpus.users.items(.name);
const shell_sections = try shellSections(allocator, corpus);
const user_gids = try userGids(allocator, corpus);
const users = try usersSection(
allocator,
corpus,
&user_gids,
&shell_sections,
);
const group_members = try groupMembers(
allocator,
corpus,
users.idx2offset,
);
const groups = try groupsSection(
allocator,
corpus,
group_members.idx2offset,
);
const shell_reader = shellImport.ShellReader.init(
mem.sliceAsBytes(shell_sections.index.constSlice()),
mem.sliceAsBytes(shell_sections.blob.constSlice()),
);
var idx_gid2group = try bdzIdx( var bdz_gid = try cmph.packU32(allocator, gids);
u32, errdefer allocator.free(bdz_gid);
allocator,
bdz_gid, var bdz_groupname = try cmph.packStr(allocator, gnames);
corpus.groups.items(.gid), errdefer allocator.free(bdz_groupname);
);
var idx_groupname2group = try bdzIdx( var bdz_uid = try cmph.packU32(allocator, uids);
[]const u8, errdefer allocator.free(bdz_uid);
allocator,
bdz_groupname, const bdz_username = try cmph.packStr(allocator, unames);
corpus.groups.items(.name), errdefer allocator.free(bdz_username);
);
var idx_uid2user = try bdzIdx( var shell_sections = try shellSections(allocator, corpus);
u32, errdefer shell_sections.deinit();
allocator,
bdz_uid, var user_gids = try userGids(allocator, corpus);
corpus.users.items(.uid), errdefer user_gids.deinit(allocator);
);
var idx_name2user = try bdzIdx( var users = try usersSection(allocator, corpus, &user_gids, &shell_sections);
[]const u8, errdefer users.deinit(allocator);
allocator,
bdz_username, var group_members = try groupMembers(allocator, corpus, users.idx2offset);
corpus.users.items(.name), errdefer group_members.deinit(allocator);
);
var groups = try groupsSection(allocator, corpus, group_members.idx2offset);
errdefer groups.deinit(allocator);
var idx_gid2group = try bdzIdx(u32, allocator, bdz_gid, gids);
errdefer allocator.free(idx_gid2group);
var idx_groupname2group = try bdzIdx([]const u8, allocator, bdz_groupname, gnames);
errdefer allocator.free(idx_groupname2group);
var idx_uid2user = try bdzIdx(u32, allocator, bdz_uid, uids);
errdefer allocator.free(idx_uid2user);
var idx_name2user = try bdzIdx([]const u8, allocator, bdz_username, unames);
errdefer allocator.free(idx_name2user);
return AllSections{ return AllSections{
.allocator = allocator, .allocator = allocator,
@ -468,7 +458,10 @@ pub const AllSections = struct {
.bdz_uid = bdz_uid, .bdz_uid = bdz_uid,
.bdz_username = bdz_username, .bdz_username = bdz_username,
.shell_sections = shell_sections, .shell_sections = shell_sections,
.shell_reader = shell_reader, .shell_reader = shellImport.ShellReader.init(
mem.sliceAsBytes(shell_sections.index.constSlice()),
mem.sliceAsBytes(shell_sections.blob.constSlice()),
),
.user_gids = user_gids, .user_gids = user_gids,
.users = users, .users = users,
.group_members = group_members, .group_members = group_members,