Add etc hosts replication

This commit is contained in:
Muaz Ahmad 2024-05-12 03:30:36 +05:00
parent 004aad9b3b
commit 6d8710545e
2 changed files with 37 additions and 4 deletions

View file

@ -1,6 +1,39 @@
const std = @import("std"); const std = @import("std");
const util = @import("util.zig"); const util = @import("util.zig");
pub fn update_hosts(ips: util.IP) !void { pub fn update_hosts(ip: util.IP) !void {
std.debug.print("{}\n", .{ips}); try create_tmp_hosts(ip);
try move_tmp_hosts();
} }
fn create_tmp_hosts(ip: util.IP) !void {
_ = ip;
var old_hosts = try std.fs.openFileAbsolute("/etc/hosts", .{ .mode = .read_only });
defer old_hosts.close();
var tmp_hosts = try std.fs.createFileAbsolute("/tmp/hosts", .{ .truncate = true });
defer tmp_hosts.close();
var buff = [_]u8{0x00} ** 50;
var buff_stream = std.io.fixedBufferStream(&buff);
const buff_reader = buff_stream.reader();
const buff_writer = buff_stream.writer();
var old_reader = old_hosts.reader();
const tmp_writer = tmp_hosts.writer();
while (old_reader.streamUntilDelimiter(buff_writer, '\n', 50 - 1)) |_| {
var n = buff_stream.pos;
// do things to line
buff[n] = '\n';
n += 1;
buff_stream.pos = 0;
try buff_reader.streamUntilDelimiter(tmp_writer, '\n', 50);
try tmp_writer.writeByte('\n');
buff_stream.pos = 0;
} else |_| {
// EOF
}
}
fn move_tmp_hosts() !void {}

View file

@ -6,6 +6,6 @@ const hosts = @import("hosts.zig");
pub fn main() !void { pub fn main() !void {
try util.check_perms(); try util.check_perms();
const domain, const ip_ver = try util.get_input(); const domain, const ip_ver = try util.get_input();
const ips = try mdns.get_mdns(domain, ip_ver); const ip = try mdns.get_mdns(domain, ip_ver);
try hosts.update_hosts(ips); try hosts.update_hosts(ip);
} }