getpwent/getgrent: advance iterator only on success
This commit is contained in:
parent
2ceb3d5c29
commit
4bd44d10b7
@ -1,15 +1,16 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
|
const mem = std.mem;
|
||||||
|
const assert = std.debug.assert;
|
||||||
|
const Allocator = mem.Allocator;
|
||||||
|
const ArrayList = std.ArrayList;
|
||||||
|
const BufSet = std.BufSet;
|
||||||
|
|
||||||
const pad = @import("padding.zig");
|
const pad = @import("padding.zig");
|
||||||
const validate = @import("validate.zig");
|
const validate = @import("validate.zig");
|
||||||
const compress = @import("compress.zig");
|
const compress = @import("compress.zig");
|
||||||
const InvalidRecord = validate.InvalidRecord;
|
const InvalidRecord = validate.InvalidRecord;
|
||||||
|
|
||||||
const mem = std.mem;
|
|
||||||
const Allocator = mem.Allocator;
|
|
||||||
const ArrayList = std.ArrayList;
|
|
||||||
const BufSet = std.BufSet;
|
|
||||||
|
|
||||||
const PackedGroup = @This();
|
const PackedGroup = @This();
|
||||||
|
|
||||||
pub const GroupStored = struct {
|
pub const GroupStored = struct {
|
||||||
@ -65,18 +66,30 @@ fn validateUtf8(s: []const u8) InvalidRecord!void {
|
|||||||
|
|
||||||
pub const Iterator = struct {
|
pub const Iterator = struct {
|
||||||
section: []const u8,
|
section: []const u8,
|
||||||
nextStart: usize = 0,
|
next_start: usize = 0,
|
||||||
idx: u32 = 0,
|
idx: u32 = 0,
|
||||||
total: u32,
|
total: u32,
|
||||||
|
advance_by: usize = 0,
|
||||||
|
|
||||||
pub fn next(it: *Iterator) ?PackedGroup {
|
pub fn next(it: *Iterator) ?PackedGroup {
|
||||||
if (it.idx == it.total) return null;
|
const gr = it.peek() orelse return null;
|
||||||
|
it.advance();
|
||||||
|
return gr;
|
||||||
|
}
|
||||||
|
|
||||||
const entry = fromBytes(it.section[it.nextStart..]);
|
pub fn peek(it: *Iterator) ?PackedGroup {
|
||||||
it.nextStart += entry.end;
|
if (it.idx == it.total) return null;
|
||||||
it.idx += 1;
|
const entry = fromBytes(it.section[it.next_start..]);
|
||||||
|
it.advance_by = entry.end;
|
||||||
return entry.group;
|
return entry.group;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn advance(it: *Iterator) void {
|
||||||
|
assert(it.advance_by > 0);
|
||||||
|
it.idx += 1;
|
||||||
|
it.next_start += it.advance_by;
|
||||||
|
it.advance_by = 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn iterator(section: []const u8, total: u32) Iterator {
|
pub fn iterator(section: []const u8, total: u32) Iterator {
|
||||||
|
@ -105,19 +105,31 @@ pub fn fromBytes(bytes: []const u8) Entry {
|
|||||||
|
|
||||||
pub const Iterator = struct {
|
pub const Iterator = struct {
|
||||||
section: []const u8,
|
section: []const u8,
|
||||||
nextStart: usize = 0,
|
next_start: usize = 0,
|
||||||
shell_reader: ShellReader,
|
shell_reader: ShellReader,
|
||||||
idx: u32 = 0,
|
idx: u32 = 0,
|
||||||
total: u32,
|
total: u32,
|
||||||
|
advance_by: usize = 0,
|
||||||
|
|
||||||
pub fn next(it: *Iterator) ?PackedUser {
|
pub fn next(it: *Iterator) ?PackedUser {
|
||||||
if (it.idx == it.total) return null;
|
const u = it.peek() orelse return null;
|
||||||
|
it.advance();
|
||||||
|
return u;
|
||||||
|
}
|
||||||
|
|
||||||
const entry = PackedUser.fromBytes(it.section[it.nextStart..]);
|
pub fn peek(it: *Iterator) ?PackedUser {
|
||||||
it.nextStart += entry.end;
|
if (it.idx == it.total) return null;
|
||||||
it.idx += 1;
|
const entry = fromBytes(it.section[it.next_start..]);
|
||||||
|
it.advance_by = entry.end;
|
||||||
return entry.user;
|
return entry.user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn advance(it: *Iterator) void {
|
||||||
|
assert(it.advance_by > 0);
|
||||||
|
it.idx += 1;
|
||||||
|
it.next_start += it.advance_by;
|
||||||
|
it.advance_by = 0;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn iterator(section: []const u8, total: u32, shell_reader: ShellReader) Iterator {
|
pub fn iterator(section: []const u8, total: u32, shell_reader: ShellReader) Iterator {
|
||||||
|
@ -370,7 +370,7 @@ fn getgrent_r(
|
|||||||
defer state.getgrent_iterator_mu.unlock();
|
defer state.getgrent_iterator_mu.unlock();
|
||||||
|
|
||||||
if (state.getgrent_iterator) |*it| {
|
if (state.getgrent_iterator) |*it| {
|
||||||
const group = it.next() orelse {
|
const group = it.peek() orelse {
|
||||||
errnop.* = 0;
|
errnop.* = 0;
|
||||||
return c.NSS_STATUS_NOTFOUND;
|
return c.NSS_STATUS_NOTFOUND;
|
||||||
};
|
};
|
||||||
@ -381,6 +381,7 @@ fn getgrent_r(
|
|||||||
state.file.db.packCGroup(&group, buffer[0..buflen]);
|
state.file.db.packCGroup(&group, buffer[0..buflen]);
|
||||||
|
|
||||||
if (cgroup1) |cgroup| {
|
if (cgroup1) |cgroup| {
|
||||||
|
it.advance();
|
||||||
result.* = cgroup;
|
result.* = cgroup;
|
||||||
return c.NSS_STATUS_SUCCESS;
|
return c.NSS_STATUS_SUCCESS;
|
||||||
} else |err| switch (err) {
|
} else |err| switch (err) {
|
||||||
@ -422,7 +423,7 @@ fn getpwent_r(
|
|||||||
defer state.getpwent_iterator_mu.unlock();
|
defer state.getpwent_iterator_mu.unlock();
|
||||||
|
|
||||||
if (state.getpwent_iterator) |*it| {
|
if (state.getpwent_iterator) |*it| {
|
||||||
const user = it.next() orelse {
|
const user = it.peek() orelse {
|
||||||
errnop.* = 0;
|
errnop.* = 0;
|
||||||
return c.NSS_STATUS_NOTFOUND;
|
return c.NSS_STATUS_NOTFOUND;
|
||||||
};
|
};
|
||||||
@ -435,6 +436,7 @@ fn getpwent_r(
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
it.advance();
|
||||||
result.* = cuser;
|
result.* = cuser;
|
||||||
return c.NSS_STATUS_SUCCESS;
|
return c.NSS_STATUS_SUCCESS;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user