From 527049ed3d48e6135b931c08c4dca31315269035 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Wed, 3 May 2023 03:21:30 +0200 Subject: [PATCH] Following the new Zig std. --- build.zig | 4 ++-- makefile | 3 ++- src/context.zig | 18 +++++++++--------- src/switch.zig | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/build.zig b/build.zig index ae859ba..2c40477 100644 --- a/build.zig +++ b/build.zig @@ -33,7 +33,7 @@ pub fn build(b: *std.Build) void { // This declares intent for the library to be installed into the standard // location when the user invokes the "install" step (the default step when // running `zig build`). - static_lib.install(); + b.installArtifact(static_lib); const shared_lib = b.addSharedLibrary(.{ .name = "ipc", @@ -43,7 +43,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); shared_lib.linkLibC(); - shared_lib.install(); + b.installArtifact(shared_lib); // Creates a step for unit testing. const main_tests = b.addTest(.{ diff --git a/makefile b/makefile index f416acd..904070a 100644 --- a/makefile +++ b/makefile @@ -3,9 +3,10 @@ all: build help: @echo "usage: make [build|install|doc|serve-doc]" +ZIGOPTS ?= ZIGOPTIM ?= ReleaseSafe build: - zig build -Doptimize=$(ZIGOPTIM) + zig build -Doptimize=$(ZIGOPTIM) $(ZIGOPTS) PREFIX ?= /usr/local LIBDIR ?= $(PREFIX)/lib diff --git a/src/context.zig b/src/context.zig index 20b4e1d..2b10393 100644 --- a/src/context.zig +++ b/src/context.zig @@ -66,12 +66,12 @@ pub const Context = struct { defer _ = umask(previous_mask); // Create the run directory, where all UNIX sockets will be. - std.os.mkdir(rundir, 0o0770) catch |err| switch(err) { + std.os.mkdir(rundir, 0o0770) catch |err| switch (err) { error.PathAlreadyExists => { log.warn("runtime directory ({s}) already exists, (everything is fine, ignoring)", .{rundir}); }, else => { - log.warn("runtime directory ({s}): {any}", .{rundir, err}); + log.warn("runtime directory ({s}): {any}", .{ rundir, err }); return err; }, }; @@ -218,7 +218,7 @@ pub const Context = struct { // net.StreamServer var serverfd = self.pollfd.items[server_index].fd; var path = self.connections.items[server_index].path orelse return error.ServerWithNoPath; - var server = net.StreamServer{ .sockfd = serverfd, .kernel_backlog = 100, .reuse_address = false, .listen_address = try net.Address.initUnix(path) }; + var server = net.StreamServer{ .sockfd = serverfd, .kernel_backlog = 100, .reuse_address = false, .reuse_port = false, .listen_address = try net.Address.initUnix(path) }; var client = try server.accept(); // net.StreamServer.Connection const newfd = client.stream.handle; @@ -236,15 +236,15 @@ pub const Context = struct { var buffer: [1000]u8 = undefined; var buffer_lock: [1000]u8 = undefined; var path = try std.fmt.bufPrint(&buffer, "{s}/{s}", .{ self.rundir, service_name }); - var lock = try std.fmt.bufPrint(&buffer_lock, "{s}.lock", .{ path }); + var lock = try std.fmt.bufPrint(&buffer_lock, "{s}.lock", .{path}); // Create a lock file (and lock it) in order to prevent a race condition. // While the program is running, the lock is enabled. // Once the program stops (even if it crashes), the lock is then disabled. // Quit if the lock is still active. - const lock_opts = .{.lock = .Exclusive, .lock_nonblocking = true}; + const lock_opts = .{ .lock = .Exclusive, .lock_nonblocking = true }; _ = std.fs.createFileAbsolute(lock, lock_opts) catch |err| { - log.err("cannot init server at {s}, lock {s} is causing a problem: {any}", .{path, lock, err}); + log.err("cannot init server at {s}, lock {s} is causing a problem: {any}", .{ path, lock, err }); log.err("you may have lauched the service twice.", .{}); return err; }; @@ -255,7 +255,7 @@ pub const Context = struct { defer _ = umask(previous_mask); // Remove the old UNIX socket. - std.os.unlink(path) catch |err| switch(err) { + std.os.unlink(path) catch |err| switch (err) { error.FileNotFound => log.debug("no unlink necessary for {s}", .{path}), else => return err, }; @@ -403,7 +403,7 @@ pub const Context = struct { // handle messages // => loop over self.pollfd.items - for (self.pollfd.items) |*fd, i| { + for (self.pollfd.items, 0..) |*fd, i| { // .revents is POLLIN if (fd.revents & std.os.linux.POLL.IN > 0) { // SERVER = new connection @@ -470,7 +470,7 @@ pub const Context = struct { fd.events &= ~@as(i16, std.os.linux.POLL.OUT); var index: usize = undefined; - for (self.tx.items) |m, index_| { + for (self.tx.items, 0..) |m, index_| { if (m.fd == self.pollfd.items[i].fd) { index = index_; break; diff --git a/src/switch.zig b/src/switch.zig index e7e2466..7ceb01b 100644 --- a/src/switch.zig +++ b/src/switch.zig @@ -52,7 +52,7 @@ pub const SwitchDB = struct { } pub fn format(self: Self, comptime _: []const u8, _: fmt.FormatOptions, out_stream: anytype) !void { - for (self.db.keys()) |k, i| { + for (self.db.keys(), 0..) |k, i| { try fmt.format(out_stream, "({},{})", .{ k, self.db.values()[i].dest }); } }