fix unix2db

This commit is contained in:
Motiejus Jakštys 2022-07-02 21:19:07 +03:00
parent e79cdaffde
commit c294d149fc
2 changed files with 13 additions and 9 deletions

View File

@ -72,7 +72,8 @@ pub fn fromReader(allocator: Allocator, reader: anytype) FromReaderError![]Group
var line = ArrayList(u8).init(allocator); var line = ArrayList(u8).init(allocator);
defer line.deinit(); defer line.deinit();
while (true) { var i: usize = 0;
while (true) : (i += 1) {
// TODO: catch and interpret different errors // TODO: catch and interpret different errors
const max = std.math.maxInt(u32); const max = std.math.maxInt(u32);
reader.readUntilDelimiterArrayList(&line, '\n', max) catch |err| switch (err) { reader.readUntilDelimiterArrayList(&line, '\n', max) catch |err| switch (err) {
@ -94,7 +95,8 @@ pub fn fromReader(allocator: Allocator, reader: anytype) FromReaderError![]Group
while (members_it.next()) |member| while (members_it.next()) |member|
try member_ptrs.append(member); try member_ptrs.append(member);
try groups.append(try init(allocator, gid, name, member_ptrs.items)); const group = try init(allocator, gid, name, member_ptrs.items);
try groups.append(group);
member_ptrs.shrinkRetainingCapacity(0); member_ptrs.shrinkRetainingCapacity(0);
} }

View File

@ -79,13 +79,17 @@ fn execute(
var users = User.fromReader(allocator, &errc, passwdFile.reader()) catch |err| var users = User.fromReader(allocator, &errc, passwdFile.reader()) catch |err|
return fail(errc.wrap("read users"), stderr, err); return fail(errc.wrap("read users"), stderr, err);
defer for (users) |*user| user.deinit(allocator); defer {
defer allocator.free(users); for (users) |*user| user.deinit(allocator);
allocator.free(users);
}
var groups = Group.fromReader(allocator, groupFile.reader()) catch |err| var groups = Group.fromReader(allocator, groupFile.reader()) catch |err|
return fail(errc.wrap("read groups"), stderr, err); return fail(errc.wrap("read groups"), stderr, err);
defer for (groups) |*group| group.deinit(allocator); defer {
defer allocator.free(groups); for (groups) |*group| group.deinit(allocator);
allocator.free(groups);
}
var corpus = Corpus.init(allocator, users, groups) catch |err| var corpus = Corpus.init(allocator, users, groups) catch |err|
return fail(errc.wrap("init corpus"), stderr, err); return fail(errc.wrap("init corpus"), stderr, err);
@ -203,7 +207,6 @@ test "smoke test" {
while (i < corpus.users.len) : (i += 1) { while (i < corpus.users.len) : (i += 1) {
const user = corpus.users.get(i); const user = corpus.users.get(i);
const line = user.toLine().constSlice(); const line = user.toLine().constSlice();
std.debug.print("({d}){s}", .{ line.len, line });
_ = try os.write(passwd_fd, line); _ = try os.write(passwd_fd, line);
} }
os.close(passwd_fd); os.close(passwd_fd);
@ -221,7 +224,6 @@ test "smoke test" {
}; };
const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args); const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args);
//if (true) return error.SkipZigTest; try testing.expectEqualStrings("total 1664 bytes. groups=5 users=4\n", stderr.items);
try testing.expectEqualStrings("", stderr.items);
try testing.expectEqual(@as(u8, 0), exit_code); try testing.expectEqual(@as(u8, 0), exit_code);
} }