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