finish getpwnam
This commit is contained in:
parent
4469d174ea
commit
7d4623411f
33
lib/DB.zig
33
lib/DB.zig
@ -76,6 +76,12 @@ pub fn fromCorpus(
|
|||||||
var shell = try shellSections(allocator, corpus);
|
var shell = try shellSections(allocator, corpus);
|
||||||
defer shell.deinit();
|
defer shell.deinit();
|
||||||
|
|
||||||
|
const shell_index = try allocator.dupe(u16, shell.index.constSlice());
|
||||||
|
errdefer allocator.free(shell_index);
|
||||||
|
|
||||||
|
const shell_blob = try allocator.dupe(u8, shell.blob.constSlice());
|
||||||
|
errdefer allocator.free(shell_blob);
|
||||||
|
|
||||||
const additional_gids = try additionalGids(allocator, corpus);
|
const additional_gids = try additionalGids(allocator, corpus);
|
||||||
errdefer allocator.free(additional_gids.blob);
|
errdefer allocator.free(additional_gids.blob);
|
||||||
defer allocator.free(additional_gids.idx2offset);
|
defer allocator.free(additional_gids.idx2offset);
|
||||||
@ -124,8 +130,6 @@ pub fn fromCorpus(
|
|||||||
.getpw_bufsize = corpus.getpw_bufsize,
|
.getpw_bufsize = corpus.getpw_bufsize,
|
||||||
};
|
};
|
||||||
|
|
||||||
std.debug.print("fromCorpus shell.index: {any}\n", .{shell.index.constSlice()});
|
|
||||||
|
|
||||||
return DB{
|
return DB{
|
||||||
.header = header,
|
.header = header,
|
||||||
.bdz_gid = bdz_gid,
|
.bdz_gid = bdz_gid,
|
||||||
@ -136,8 +140,8 @@ pub fn fromCorpus(
|
|||||||
.idx_groupname2group = idx_groupname2group,
|
.idx_groupname2group = idx_groupname2group,
|
||||||
.idx_uid2user = idx_uid2user,
|
.idx_uid2user = idx_uid2user,
|
||||||
.idx_name2user = idx_name2user,
|
.idx_name2user = idx_name2user,
|
||||||
.shell_index = shell.index.constSlice(),
|
.shell_index = shell_index,
|
||||||
.shell_blob = shell.blob.constSlice(),
|
.shell_blob = shell_blob,
|
||||||
.groups = groups.blob,
|
.groups = groups.blob,
|
||||||
.users = users.blob,
|
.users = users.blob,
|
||||||
.groupmembers = groupmembers.blob,
|
.groupmembers = groupmembers.blob,
|
||||||
@ -155,6 +159,8 @@ pub fn deinit(self: *DB, allocator: Allocator) void {
|
|||||||
allocator.free(self.idx_groupname2group);
|
allocator.free(self.idx_groupname2group);
|
||||||
allocator.free(self.idx_uid2user);
|
allocator.free(self.idx_uid2user);
|
||||||
allocator.free(self.idx_name2user);
|
allocator.free(self.idx_name2user);
|
||||||
|
allocator.free(self.shell_index);
|
||||||
|
allocator.free(self.shell_blob);
|
||||||
allocator.free(self.groups);
|
allocator.free(self.groups);
|
||||||
allocator.free(self.users);
|
allocator.free(self.users);
|
||||||
allocator.free(self.groupmembers);
|
allocator.free(self.groupmembers);
|
||||||
@ -172,8 +178,6 @@ pub fn iov(self: *const DB) BoundedArray(os.iovec_const, DB_fields.len * 2) {
|
|||||||
*const Header => mem.asBytes(value),
|
*const Header => mem.asBytes(value),
|
||||||
else => mem.sliceAsBytes(value),
|
else => mem.sliceAsBytes(value),
|
||||||
};
|
};
|
||||||
if (mem.eql(u8, field.name, "shell_index"))
|
|
||||||
std.debug.print("shell_index original: {any}\n", .{value});
|
|
||||||
result.appendAssumeCapacity(os.iovec_const{
|
result.appendAssumeCapacity(os.iovec_const{
|
||||||
.iov_base = bytes.ptr,
|
.iov_base = bytes.ptr,
|
||||||
.iov_len = bytes.len,
|
.iov_len = bytes.len,
|
||||||
@ -224,8 +228,6 @@ pub fn fromBytes(buf: []align(8) const u8) InvalidHeader!DB {
|
|||||||
offset += lengths[i];
|
offset += lengths[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.print("GOT shell_index: {any}\n", .{result.shell_index});
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -652,34 +654,25 @@ test "getgrnam/getgrgid" {
|
|||||||
try testing.expectEqual(all.gid, 9999);
|
try testing.expectEqual(all.gid, 9999);
|
||||||
try testing.expectEqualStrings(all.name[0..3], "all");
|
try testing.expectEqualStrings(all.name[0..3], "all");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
test "getgr_bufsize" {
|
|
||||||
var arena = ArenaAllocator.init(testing.allocator);
|
|
||||||
defer arena.deinit();
|
|
||||||
var corpus = try Corpus.testCorpus(arena.allocator());
|
|
||||||
var db = try DB.fromCorpus(arena.allocator(), &corpus);
|
|
||||||
var buf = try arena.allocator().alloc(u8, db.header.getgr_bufsize);
|
|
||||||
|
|
||||||
_ = try db.getgrnam("all", &buf);
|
_ = try db.getgrnam("all", &buf);
|
||||||
buf.len -= 1;
|
buf.len -= 1;
|
||||||
try testing.expectError(error.OutOfMemory, db.getgrnam("all", &buf));
|
try testing.expectError(error.OutOfMemory, db.getgrnam("all", &buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
test "getpwnam" {
|
test "getpwnam/getpwuid" {
|
||||||
var arena = ArenaAllocator.init(testing.allocator);
|
var arena = ArenaAllocator.init(testing.allocator);
|
||||||
defer arena.deinit();
|
defer arena.deinit();
|
||||||
var corpus = try Corpus.testCorpus(arena.allocator());
|
var corpus = try Corpus.testCorpus(arena.allocator());
|
||||||
var db = try DB.fromCorpus(arena.allocator(), &corpus);
|
var db = try DB.fromCorpus(arena.allocator(), &corpus);
|
||||||
std.debug.print("db.shell.index: {any}\n", .{db.shell_index});
|
|
||||||
var buf = try arena.allocator().alloc(u8, db.header.getpw_bufsize);
|
var buf = try arena.allocator().alloc(u8, db.header.getpw_bufsize);
|
||||||
|
|
||||||
const vidmantas = (try db.getpwnam("vidmantas", &buf)).?;
|
const vidmantas = (try db.getpwnam("vidmantas", &buf)).?;
|
||||||
try testing.expectEqual(vidmantas.pw_uid, 128);
|
try testing.expectEqual(vidmantas.pw_uid, 128);
|
||||||
try testing.expectEqual(vidmantas.pw_gid, 128);
|
try testing.expectEqual(vidmantas.pw_gid, 128);
|
||||||
try testing.expectEqualStrings(vidmantas.pw_name[0..9], "vidmantas");
|
try testing.expectEqualStrings(vidmantas.pw_name[0..9], "vidmantas");
|
||||||
try testing.expectEqualStrings(vidmantas.pw_gecos[0..10], "Vidmantas Kaminskas");
|
try testing.expectEqualStrings(vidmantas.pw_gecos[0..19], "Vidmantas Kaminskas");
|
||||||
try testing.expectEqualStrings(vidmantas.pw_dir[0..10], "/home/vidmantas");
|
try testing.expectEqualStrings(vidmantas.pw_dir[0..15], "/home/vidmantas");
|
||||||
}
|
}
|
||||||
|
|
||||||
test "additionalGids" {
|
test "additionalGids" {
|
||||||
|
@ -22,8 +22,6 @@ pub const ShellReader = struct {
|
|||||||
|
|
||||||
// get returns a shell at the given index.
|
// get returns a shell at the given index.
|
||||||
pub fn get(self: *const ShellReader, idx: u8) []const u8 {
|
pub fn get(self: *const ShellReader, idx: u8) []const u8 {
|
||||||
std.debug.print("\nrequested shell at index {d}; ", .{idx});
|
|
||||||
std.debug.print("start:{d} end:{d}\n", .{ self.index[idx], self.index[idx + 1] });
|
|
||||||
return self.blob[self.index[idx]..self.index[idx + 1]];
|
return self.blob[self.index[idx]..self.index[idx + 1]];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -75,18 +73,9 @@ pub const ShellWriter = struct {
|
|||||||
self.index.set(idx8, offset);
|
self.index.set(idx8, offset);
|
||||||
}
|
}
|
||||||
self.index.appendAssumeCapacity(@intCast(u8, self.blob.len));
|
self.index.appendAssumeCapacity(@intCast(u8, self.blob.len));
|
||||||
std.debug.print("\nGEN shell_index: {any}\n", .{self.index.constSlice()});
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn section_index(self: *const ShellSections) []align(2) const u8 {
|
|
||||||
return std.mem.sliceAsBytes(self.index.constSlice());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn section_blob(self: *const ShellSections) []const u8 {
|
|
||||||
return self.blob.constSlice();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *ShellSections) void {
|
pub fn deinit(self: *ShellSections) void {
|
||||||
self.shell2idx.deinit();
|
self.shell2idx.deinit();
|
||||||
self.* = undefined;
|
self.* = undefined;
|
||||||
@ -184,11 +173,11 @@ test "basic shellpopcon" {
|
|||||||
try testing.expectEqual(sections.getIndex(zsh).?, 1);
|
try testing.expectEqual(sections.getIndex(zsh).?, 1);
|
||||||
try testing.expectEqual(sections.getIndex(bash).?, 2);
|
try testing.expectEqual(sections.getIndex(bash).?, 2);
|
||||||
try testing.expectEqual(sections.getIndex(nobody), null);
|
try testing.expectEqual(sections.getIndex(nobody), null);
|
||||||
try testing.expectEqual(sections.section_blob().len, bash.len + zsh.len + long.len);
|
try testing.expectEqual(sections.blob.constSlice().len, bash.len + zsh.len + long.len);
|
||||||
|
|
||||||
const shellReader = ShellReader.init(
|
const shellReader = ShellReader.init(
|
||||||
sections.section_index(),
|
std.mem.sliceAsBytes(sections.index.constSlice()),
|
||||||
sections.section_blob(),
|
sections.blob.constSlice(),
|
||||||
);
|
);
|
||||||
try testing.expectEqualStrings(shellReader.get(0), long);
|
try testing.expectEqualStrings(shellReader.get(0), long);
|
||||||
try testing.expectEqualStrings(shellReader.get(1), zsh);
|
try testing.expectEqualStrings(shellReader.get(1), zsh);
|
||||||
|
Loading…
Reference in New Issue
Block a user