writing to the db

This commit is contained in:
Motiejus Jakštys 2022-06-07 05:59:00 +03:00
parent 31b5bb2d72
commit a5c24ace80
2 changed files with 34 additions and 8 deletions

View File

@ -81,7 +81,6 @@ pub fn fromReader(allocator: Allocator, reader: anytype) FromReaderError![]Group
_ = it.next() orelse return error.InvalidRecord; // password _ = it.next() orelse return error.InvalidRecord; // password
const gids = it.next() orelse return error.InvalidRecord; const gids = it.next() orelse return error.InvalidRecord;
const members_commas = it.next() orelse return error.InvalidRecord; const members_commas = it.next() orelse return error.InvalidRecord;
// the line must be exhaustive.
if (it.next() != null) return error.InvalidRecord; if (it.next() != null) return error.InvalidRecord;
const gid = fmt.parseInt(u32, gids, 10) catch return error.InvalidRecord; const gid = fmt.parseInt(u32, gids, 10) catch return error.InvalidRecord;

View File

@ -10,6 +10,8 @@ const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator;
const flags = @import("../flags.zig"); const flags = @import("../flags.zig");
const User = @import("../User.zig"); const User = @import("../User.zig");
const Group = @import("../Group.zig"); const Group = @import("../Group.zig");
const Corpus = @import("../Corpus.zig");
const DB = @import("../DB.zig");
const usage = const usage =
\\usage: turbonss-unix2db [options] \\usage: turbonss-unix2db [options]
@ -42,6 +44,7 @@ fn execute(
.{ .name = "-h", .kind = .boolean }, .{ .name = "-h", .kind = .boolean },
.{ .name = "--passwd", .kind = .arg }, .{ .name = "--passwd", .kind = .arg },
.{ .name = "--group", .kind = .arg }, .{ .name = "--group", .kind = .arg },
.{ .name = "--output", .kind = .arg },
}) catch { }) catch {
try stderr.writeAll(usage); try stderr.writeAll(usage);
return 1; return 1;
@ -58,19 +61,37 @@ fn execute(
return 1; return 1;
} }
const passwd = result.argFlag("--passwd") orelse "./passwd"; const passwdFname = result.argFlag("--passwd") orelse "./passwd";
const group = result.argFlag("--group") orelse "./group"; const groupFname = result.argFlag("--group") orelse "./group";
const outFile = result.argFlag("--output") orelse "./db.turbo";
var passwdFile = try fs.cwd().openFile(passwd, .{ .mode = .read_only }); var passwdFile = try fs.cwd().openFile(passwdFname, .{ .mode = .read_only });
defer passwdFile.close(); defer passwdFile.close();
var groupFile = try fs.cwd().openFile(group, .{ .mode = .read_only }); var groupFile = try fs.cwd().openFile(groupFname, .{ .mode = .read_only });
defer groupFile.close(); defer groupFile.close();
var users = try User.fromReader(allocator, passwdFile.reader()); var users = try User.fromReader(allocator, passwdFile.reader());
defer for (users) |*user| user.deinit(allocator);
var groups = try Group.fromReader(allocator, groupFile.reader()); var groups = try Group.fromReader(allocator, groupFile.reader());
defer for (groups) |*group| group.deinit(allocator);
try stderr.print("read {d} users\n", .{users.len}); var corpus = try Corpus.init(allocator, users, groups);
try stderr.print("read {d} groups\n", .{groups.len}); defer corpus.deinit();
var db = try DB.fromCorpus(allocator, &corpus);
defer db.deinit(allocator);
const fd = try os.open(outFile, os.O.WRONLY | os.O.TRUNC, 0644);
errdefer os.close(fd);
const len = try os.writev(fd, db.iov().constSlice());
try os.fsync(fd);
os.close(fd);
try stderr.print("total {d} bytes. groups={d} users={d}\n", .{
len,
users.len,
groups.len,
});
return 0; return 0;
} }
@ -84,5 +105,11 @@ test "invalid argument" {
const exit_code = try execute(allocator, stderr.writer(), args[0..]); const exit_code = try execute(allocator, stderr.writer(), args[0..]);
try testing.expectEqual(@as(u8, 1), exit_code); try testing.expectEqual(@as(u8, 1), exit_code);
try testing.expect(mem.startsWith(u8, stderr.items, "ERROR: unknown ")); try testing.expect(mem.startsWith(
u8,
stderr.items,
"ERROR: unknown option '--invalid-argument'",
));
} }
test "smoke test" {}