2022-05-25 09:42:42 +03:00
|
|
|
const std = @import("std");
|
2022-06-05 23:56:45 +03:00
|
|
|
const fs = std.fs;
|
|
|
|
const io = std.io;
|
|
|
|
const mem = std.mem;
|
|
|
|
const os = std.os;
|
|
|
|
const ArrayList = std.ArrayList;
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
const GeneralPurposeAllocator = std.heap.GeneralPurposeAllocator;
|
|
|
|
|
2022-05-25 09:42:42 +03:00
|
|
|
const flags = @import("../flags.zig");
|
2022-06-05 23:56:45 +03:00
|
|
|
const User = @import("../User.zig");
|
2022-06-07 05:32:21 +03:00
|
|
|
const Group = @import("../Group.zig");
|
2022-06-07 05:59:00 +03:00
|
|
|
const Corpus = @import("../Corpus.zig");
|
|
|
|
const DB = @import("../DB.zig");
|
2022-06-15 12:56:19 +03:00
|
|
|
const ErrCtx = @import("../ErrCtx.zig");
|
2022-06-05 23:56:45 +03:00
|
|
|
|
|
|
|
const usage =
|
|
|
|
\\usage: turbonss-unix2db [options]
|
|
|
|
\\
|
|
|
|
\\ -h Print this help message and exit
|
|
|
|
\\ --passwd Path to passwd file (default: ./passwd)
|
|
|
|
\\ --group Path to group file (default: ./group)
|
|
|
|
\\
|
|
|
|
;
|
|
|
|
|
|
|
|
pub fn main() !void {
|
|
|
|
// This line is here because of https://github.com/ziglang/zig/issues/7807
|
|
|
|
const argv: []const [*:0]const u8 = os.argv;
|
|
|
|
const gpa = GeneralPurposeAllocator(.{});
|
|
|
|
|
2022-06-22 20:59:58 +03:00
|
|
|
const stderr = try io.getStdErr();
|
|
|
|
const stdout = try io.getStdOut();
|
2022-06-05 23:56:45 +03:00
|
|
|
|
2022-06-22 20:59:58 +03:00
|
|
|
const return_code = execute(gpa, stdout, stderr, argv[1..]);
|
2022-06-05 23:56:45 +03:00
|
|
|
os.exit(return_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn execute(
|
|
|
|
allocator: Allocator,
|
2022-06-22 20:59:58 +03:00
|
|
|
stdout: anytype,
|
2022-06-05 23:56:45 +03:00
|
|
|
stderr: anytype,
|
|
|
|
argv: []const [*:0]const u8,
|
2022-06-22 16:23:02 +03:00
|
|
|
) u8 {
|
2022-06-05 23:56:45 +03:00
|
|
|
const result = flags.parse(argv, &[_]flags.Flag{
|
|
|
|
.{ .name = "-h", .kind = .boolean },
|
|
|
|
.{ .name = "--passwd", .kind = .arg },
|
|
|
|
.{ .name = "--group", .kind = .arg },
|
2022-06-07 05:59:00 +03:00
|
|
|
.{ .name = "--output", .kind = .arg },
|
2022-06-05 23:56:45 +03:00
|
|
|
}) catch {
|
2022-06-22 16:23:02 +03:00
|
|
|
stderr.writeAll(usage) catch {};
|
2022-06-05 23:56:45 +03:00
|
|
|
return 1;
|
|
|
|
};
|
2022-05-25 09:42:42 +03:00
|
|
|
|
2022-06-05 23:56:45 +03:00
|
|
|
if (result.boolFlag("-h")) {
|
2022-06-22 20:59:58 +03:00
|
|
|
stdout.writeAll(usage) catch return 1;
|
2022-06-05 23:56:45 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.args.len != 0) {
|
2022-06-22 16:23:02 +03:00
|
|
|
stderr.print("ERROR: unknown option '{s}'\n", .{result.args[0]}) catch {};
|
|
|
|
stderr.writeAll(usage) catch {};
|
2022-06-05 23:56:45 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-06-07 05:59:00 +03:00
|
|
|
const passwdFname = result.argFlag("--passwd") orelse "./passwd";
|
|
|
|
const groupFname = result.argFlag("--group") orelse "./group";
|
|
|
|
const outFile = result.argFlag("--output") orelse "./db.turbo";
|
2022-06-05 23:56:45 +03:00
|
|
|
|
2022-06-22 21:06:20 +03:00
|
|
|
// to catch an error set file.OpenError, wait for
|
2022-06-22 16:23:02 +03:00
|
|
|
// https://github.com/ziglang/zig/issues/2473
|
2022-06-22 20:59:58 +03:00
|
|
|
var errc = ErrCtx{};
|
2022-06-22 21:06:20 +03:00
|
|
|
var passwdFile = fs.cwd().openFile(passwdFname, .{ .mode = .read_only }) catch |err|
|
2022-06-28 16:44:44 +03:00
|
|
|
return fail(errc.wrapf("open '{s}'", .{passwdFname}), stderr, err);
|
2022-06-05 23:56:45 +03:00
|
|
|
defer passwdFile.close();
|
2022-06-22 21:06:20 +03:00
|
|
|
|
2022-06-22 20:59:58 +03:00
|
|
|
var groupFile = fs.cwd().openFile(groupFname, .{ .mode = .read_only }) catch |err|
|
2022-06-28 16:44:44 +03:00
|
|
|
return fail(errc.wrapf("open '{s}'", .{groupFname}), stderr, err);
|
2022-06-05 23:56:45 +03:00
|
|
|
defer groupFile.close();
|
|
|
|
|
2022-06-22 21:06:20 +03:00
|
|
|
var users = User.fromReader(allocator, &errc, passwdFile.reader()) catch |err|
|
|
|
|
return fail(errc.wrap("read users"), stderr, err);
|
2022-06-07 05:59:00 +03:00
|
|
|
defer for (users) |*user| user.deinit(allocator);
|
2022-06-12 13:38:10 +03:00
|
|
|
defer allocator.free(users);
|
2022-06-15 12:56:19 +03:00
|
|
|
|
2022-06-22 21:06:20 +03:00
|
|
|
var groups = Group.fromReader(allocator, groupFile.reader()) catch |err|
|
|
|
|
return fail(errc.wrap("read groups"), stderr, err);
|
2022-06-07 05:59:00 +03:00
|
|
|
defer for (groups) |*group| group.deinit(allocator);
|
2022-06-12 13:38:10 +03:00
|
|
|
defer allocator.free(groups);
|
2022-06-05 23:56:45 +03:00
|
|
|
|
2022-06-22 20:59:58 +03:00
|
|
|
var corpus = Corpus.init(allocator, users, groups) catch |err|
|
|
|
|
return fail(errc.wrap("init corpus"), stderr, err);
|
|
|
|
|
2022-06-07 05:59:00 +03:00
|
|
|
defer corpus.deinit();
|
2022-06-22 20:59:58 +03:00
|
|
|
var db = DB.fromCorpus(allocator, &corpus) catch |err|
|
|
|
|
return fail(errc.wrap("construct db from corpus"), stderr, err);
|
2022-06-07 05:59:00 +03:00
|
|
|
defer db.deinit(allocator);
|
|
|
|
|
2022-06-22 20:59:58 +03:00
|
|
|
const fd = os.open(outFile, os.O.WRONLY | os.O.TRUNC | os.O.CREAT, 0644) catch |err|
|
|
|
|
return fail(errc.wrapf("open for writing {s}", .{outFile}), stderr, err);
|
2022-06-07 05:59:00 +03:00
|
|
|
errdefer os.close(fd);
|
|
|
|
|
2022-06-22 20:59:58 +03:00
|
|
|
const len = os.writev(fd, db.iov().constSlice()) catch |err|
|
|
|
|
return fail(errc.wrapf("writev to {s}", .{outFile}), stderr, err);
|
|
|
|
os.fsync(fd) catch |err|
|
|
|
|
return fail(errc.wrapf("fsync {s}", .{outFile}), stderr, err);
|
2022-06-07 05:59:00 +03:00
|
|
|
os.close(fd);
|
|
|
|
|
2022-06-22 20:59:58 +03:00
|
|
|
stderr.print("total {d} bytes. groups={d} users={d}\n", .{
|
2022-06-07 05:59:00 +03:00
|
|
|
len,
|
|
|
|
users.len,
|
|
|
|
groups.len,
|
2022-06-22 20:59:58 +03:00
|
|
|
}) catch return 1;
|
|
|
|
|
2022-06-05 23:56:45 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2022-05-25 09:42:42 +03:00
|
|
|
|
2022-06-22 20:59:58 +03:00
|
|
|
fn fail(errc: *ErrCtx, stderr: anytype, err: anytype) u8 {
|
|
|
|
stderr.print("ERROR {s}", .{@errorName(err)}) catch {};
|
|
|
|
var it = errc.rev();
|
2022-06-22 21:16:08 +03:00
|
|
|
while (it.next()) |msg| stderr.print(": {s}", .{msg}) catch {};
|
2022-06-22 20:59:58 +03:00
|
|
|
stderr.print("\n", .{}) catch {};
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-05-25 09:42:42 +03:00
|
|
|
const testing = std.testing;
|
|
|
|
|
2022-06-05 23:56:45 +03:00
|
|
|
test "invalid argument" {
|
|
|
|
const allocator = testing.allocator;
|
|
|
|
const args = &[_][*:0]const u8{"--invalid-argument"};
|
|
|
|
var stderr = ArrayList(u8).init(allocator);
|
|
|
|
defer stderr.deinit();
|
2022-06-22 20:59:58 +03:00
|
|
|
var stdout = ArrayList(u8).init(allocator);
|
|
|
|
defer stdout.deinit();
|
2022-06-05 23:56:45 +03:00
|
|
|
|
2022-06-22 21:16:08 +03:00
|
|
|
const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args[0..]);
|
2022-06-05 23:56:45 +03:00
|
|
|
try testing.expectEqual(@as(u8, 1), exit_code);
|
2022-06-07 05:59:00 +03:00
|
|
|
try testing.expect(mem.startsWith(
|
|
|
|
u8,
|
|
|
|
stderr.items,
|
|
|
|
"ERROR: unknown option '--invalid-argument'",
|
|
|
|
));
|
2022-05-25 09:42:42 +03:00
|
|
|
}
|
2022-06-07 05:59:00 +03:00
|
|
|
|
2022-06-22 21:16:08 +03:00
|
|
|
test "trivial error: missing passwd file" {
|
|
|
|
const allocator = testing.allocator;
|
|
|
|
const args = &[_][*:0]const u8{};
|
|
|
|
var stderr = ArrayList(u8).init(allocator);
|
|
|
|
defer stderr.deinit();
|
|
|
|
var stdout = ArrayList(u8).init(allocator);
|
|
|
|
defer stdout.deinit();
|
|
|
|
|
|
|
|
const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args[0..]);
|
|
|
|
try testing.expectEqual(@as(u8, 1), exit_code);
|
2022-06-28 16:44:44 +03:00
|
|
|
try testing.expectEqualStrings(stderr.items, "ERROR FileNotFound: open './passwd'\n");
|
2022-06-22 21:16:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
test "fail" {
|
|
|
|
var errc = ErrCtx{};
|
2022-06-28 16:41:10 +03:00
|
|
|
var buf = ArrayList(u8).init(testing.allocator);
|
|
|
|
defer buf.deinit();
|
|
|
|
var wr = buf.writer();
|
|
|
|
const exit_code = fail(errc.wrapf("invalid user 'foo'", .{}), wr, error.NotSure);
|
2022-06-22 21:16:08 +03:00
|
|
|
try testing.expectEqual(exit_code, 1);
|
2022-06-28 16:41:10 +03:00
|
|
|
try testing.expectEqualStrings(buf.items, "ERROR NotSure: invalid user 'foo'\n");
|
2022-06-22 21:16:08 +03:00
|
|
|
}
|
|
|
|
|
2022-06-07 06:30:18 +03:00
|
|
|
test "smoke test" {
|
|
|
|
const allocator = testing.allocator;
|
|
|
|
var stderr = ArrayList(u8).init(allocator);
|
|
|
|
defer stderr.deinit();
|
2022-06-28 16:41:10 +03:00
|
|
|
var stdout = ArrayList(u8).init(allocator);
|
|
|
|
defer stdout.deinit();
|
2022-06-07 13:14:50 +03:00
|
|
|
|
|
|
|
var corpus = try Corpus.testCorpus(allocator);
|
|
|
|
defer corpus.deinit();
|
|
|
|
|
|
|
|
var tmp = testing.tmpDir(.{});
|
2022-06-12 13:27:12 +03:00
|
|
|
errdefer tmp.cleanup();
|
2022-06-07 13:14:50 +03:00
|
|
|
|
|
|
|
const tmp_path = blk: {
|
|
|
|
const relative_path = try fs.path.join(allocator, &[_][]const u8{
|
|
|
|
"zig-cache",
|
|
|
|
"tmp",
|
|
|
|
tmp.sub_path[0..],
|
|
|
|
});
|
|
|
|
const real_path = try fs.realpathAlloc(allocator, relative_path);
|
|
|
|
allocator.free(relative_path);
|
|
|
|
break :blk real_path;
|
|
|
|
};
|
|
|
|
defer allocator.free(tmp_path);
|
|
|
|
|
|
|
|
const passwdPath = try fs.path.joinZ(allocator, &[_][]const u8{ tmp_path, "passwd" });
|
|
|
|
defer allocator.free(passwdPath);
|
|
|
|
const groupPath = try fs.path.joinZ(allocator, &[_][]const u8{ tmp_path, "group" });
|
|
|
|
defer allocator.free(groupPath);
|
|
|
|
const outPath = try fs.path.joinZ(allocator, &[_][]const u8{ tmp_path, "tmp.turbo" });
|
|
|
|
defer allocator.free(outPath);
|
|
|
|
|
|
|
|
const passwd_fd = try os.open(passwdPath, os.O.CREAT | os.O.WRONLY, 0o644);
|
|
|
|
const group_fd = try os.open(groupPath, os.O.CREAT | os.O.WRONLY, 0o644);
|
|
|
|
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < corpus.users.len) : (i += 1) {
|
|
|
|
const user = corpus.users.get(i);
|
2022-07-02 16:24:47 +03:00
|
|
|
const line = user.toLine().constSlice();
|
|
|
|
std.debug.print("({d}){s}", .{ line.len, line });
|
|
|
|
_ = try os.write(passwd_fd, line);
|
2022-06-07 13:14:50 +03:00
|
|
|
}
|
|
|
|
os.close(passwd_fd);
|
|
|
|
|
|
|
|
var group_writer = (fs.File{ .handle = group_fd }).writer();
|
|
|
|
i = 0;
|
|
|
|
while (i < corpus.groups.len) : (i += 1)
|
|
|
|
try corpus.groups.get(i).writeTo(group_writer);
|
|
|
|
os.close(group_fd);
|
|
|
|
|
|
|
|
const args = &[_][*:0]const u8{
|
|
|
|
"--passwd", passwdPath,
|
|
|
|
"--group", groupPath,
|
|
|
|
"--output", outPath,
|
|
|
|
};
|
2022-06-22 16:23:02 +03:00
|
|
|
|
2022-06-28 16:41:10 +03:00
|
|
|
const exit_code = execute(allocator, stdout.writer(), stderr.writer(), args);
|
2022-07-02 16:17:10 +03:00
|
|
|
//if (true) return error.SkipZigTest;
|
2022-06-28 16:41:10 +03:00
|
|
|
try testing.expectEqualStrings("", stderr.items);
|
2022-06-07 13:14:50 +03:00
|
|
|
try testing.expectEqual(@as(u8, 0), exit_code);
|
2022-06-07 06:30:18 +03:00
|
|
|
}
|