From 4487d75818d36085e3540934f609a997d256f20c Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Fri, 3 Oct 2025 19:01:17 +0200 Subject: use thread-safe localtime_r MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Robert Günzler --- src/fs.zig | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/fs.zig') diff --git a/src/fs.zig b/src/fs.zig index 49041e2..7a8717a 100644 --- a/src/fs.zig +++ b/src/fs.zig @@ -117,13 +117,17 @@ pub fn get_mtime(alloc: std.mem.Allocator, path: []const u8) !std.os.linux.times // use libc strftime to format time string pub fn format_mtime(alloc: std.mem.Allocator, mtime: std.os.linux.timespec, fmt: [*c]const u8) ![]const u8 { const mtime_str = try alloc.alloc(u8, std.os.linux.PATH_MAX); - const tm = c.localtime(&mtime.sec); - const n = c.strftime( + var tm: c.struct_tm = undefined; + _ = c.localtime_r(&mtime.sec, &tm); + const rc = c.strftime( mtime_str.ptr, mtime_str.len, fmt, - tm, + &tm, ); - if (n < 0) return error.UnknownStrftimeError; - return try alloc.realloc(mtime_str, n); + switch (std.posix.errno(rc)) { + .SUCCESS => {}, + else => |err| return std.posix.unexpectedErrno(err), + } + return try alloc.realloc(mtime_str, rc); } -- cgit 1.4.1