authorgravatar for johnny5jg5@gmail.comthejohnny5 <johnny5jg5@gmail.com> 2024-12-15 17:56:58-05:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-01-29 09:17:20+01:00
log78b7a446f0dd67f1d4dcb730065558bd93399957
treeb2bcbb5e81773e01fb775135977f21a4556c8a59
parent71d16106ad76bb31bc4e17dc37f8d8b5498a12dd

std: add optional times pointer for futimes, futimens, utimes, utimensat


3 files changed, 23 insertions(+), 13 deletions(-)

lib/std/c.zig+4-4
......@@ -9388,11 +9388,11 @@ pub extern "c" fn malloc(usize) ?*anyopaque;
93889388pub extern "c" fn realloc(?*anyopaque, usize) ?*anyopaque;
93899389pub extern "c" fn free(?*anyopaque) void;
93909390
9391pub extern "c" fn futimes(fd: fd_t, times: *[2]timeval) c_int;
9392pub extern "c" fn utimes(path: [*:0]const u8, times: *[2]timeval) c_int;
9391pub extern "c" fn futimes(fd: fd_t, times: ?*[2]timeval) c_int;
9392pub extern "c" fn utimes(path: [*:0]const u8, times: ?*[2]timeval) c_int;
93939393
9394pub extern "c" fn utimensat(dirfd: fd_t, pathname: [*:0]const u8, times: *[2]timespec, flags: u32) c_int;
9395pub extern "c" fn futimens(fd: fd_t, times: *const [2]timespec) c_int;
9394pub extern "c" fn utimensat(dirfd: fd_t, pathname: [*:0]const u8, times: ?*[2]timespec, flags: u32) c_int;
9395pub extern "c" fn futimens(fd: fd_t, times: ?*const [2]timespec) c_int;
93969396
93979397pub extern "c" fn pthread_create(
93989398 noalias newthread: *pthread_t,
lib/std/os/linux.zig+2-2
......@@ -608,11 +608,11 @@ pub inline fn vfork() usize {
608608 return @call(.always_inline, syscall0, .{.vfork});
609609}
610610
611pub fn futimens(fd: i32, times: *const [2]timespec) usize {
611pub fn futimens(fd: i32, times: ?*const [2]timespec) usize {
612612 return utimensat(fd, null, times, 0);
613613}
614614
615pub fn utimensat(dirfd: i32, path: ?[*:0]const u8, times: *const [2]timespec, flags: u32) usize {
615pub fn utimensat(dirfd: i32, path: ?[*:0]const u8, times: ?*const [2]timespec, flags: u32) usize {
616616 return syscall4(.utimensat, @as(usize, @bitCast(@as(isize, dirfd))), @intFromPtr(path), @intFromPtr(times), flags);
617617}
618618
lib/std/posix.zig+17-7
......@@ -5767,17 +5767,27 @@ pub const FutimensError = error{
57675767 ReadOnlyFileSystem,
57685768} || UnexpectedError;
57695769
5770pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
5770pub fn futimens(fd: fd_t, times: ?*const [2]timespec) FutimensError!void {
57715771 if (native_os == .wasi and !builtin.link_libc) {
57725772 // TODO WASI encodes `wasi.fstflags` to signify magic values
57735773 // similar to UTIME_NOW and UTIME_OMIT. Currently, we ignore
57745774 // this here, but we should really handle it somehow.
5775 const atim = times[0].toTimestamp();
5776 const mtim = times[1].toTimestamp();
5777 switch (wasi.fd_filestat_set_times(fd, atim, mtim, .{
5778 .ATIM = true,
5779 .MTIM = true,
5780 })) {
5775 const error_code = blk: {
5776 if (times) |times_arr| {
5777 const atim = times_arr[0].toTimestamp();
5778 const mtim = times_arr[1].toTimestamp();
5779 break :blk wasi.fd_filestat_set_times(fd, atim, mtim, .{
5780 .ATIM = true,
5781 .MTIM = true,
5782 });
5783 }
5784
5785 break :blk wasi.fd_filestat_set_times(fd, 0, 0, .{
5786 .ATIM_NOW = true,
5787 .MTIM_NOW = true,
5788 });
5789 };
5790 switch (error_code) {
57815791 .SUCCESS => return,
57825792 .ACCES => return error.AccessDenied,
57835793 .PERM => return error.PermissionDenied,