Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/std/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9387,11 +9387,11 @@ pub extern "c" fn malloc(usize) ?*anyopaque;
pub extern "c" fn realloc(?*anyopaque, usize) ?*anyopaque;
pub extern "c" fn free(?*anyopaque) void;

pub extern "c" fn futimes(fd: fd_t, times: *[2]timeval) c_int;
pub extern "c" fn utimes(path: [*:0]const u8, times: *[2]timeval) c_int;
pub extern "c" fn futimes(fd: fd_t, times: ?*[2]timeval) c_int;
pub extern "c" fn utimes(path: [*:0]const u8, times: ?*[2]timeval) c_int;

pub extern "c" fn utimensat(dirfd: fd_t, pathname: [*:0]const u8, times: *[2]timespec, flags: u32) c_int;
pub extern "c" fn futimens(fd: fd_t, times: *const [2]timespec) c_int;
pub extern "c" fn utimensat(dirfd: fd_t, pathname: [*:0]const u8, times: ?*[2]timespec, flags: u32) c_int;
pub extern "c" fn futimens(fd: fd_t, times: ?*const [2]timespec) c_int;

pub extern "c" fn pthread_create(
noalias newthread: *pthread_t,
Expand Down
4 changes: 2 additions & 2 deletions lib/std/os/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,11 @@ pub inline fn vfork() usize {
return @call(.always_inline, syscall0, .{.vfork});
}

pub fn futimens(fd: i32, times: *const [2]timespec) usize {
pub fn futimens(fd: i32, times: ?*const [2]timespec) usize {
return utimensat(fd, null, times, 0);
}

pub fn utimensat(dirfd: i32, path: ?[*:0]const u8, times: *const [2]timespec, flags: u32) usize {
pub fn utimensat(dirfd: i32, path: ?[*:0]const u8, times: ?*const [2]timespec, flags: u32) usize {
return syscall4(.utimensat, @as(usize, @bitCast(@as(isize, dirfd))), @intFromPtr(path), @intFromPtr(times), flags);
}

Expand Down
24 changes: 17 additions & 7 deletions lib/std/posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5767,17 +5767,27 @@ pub const FutimensError = error{
ReadOnlyFileSystem,
} || UnexpectedError;

pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
pub fn futimens(fd: fd_t, times: ?*const [2]timespec) FutimensError!void {
if (native_os == .wasi and !builtin.link_libc) {
// TODO WASI encodes `wasi.fstflags` to signify magic values
// similar to UTIME_NOW and UTIME_OMIT. Currently, we ignore
// this here, but we should really handle it somehow.
const atim = times[0].toTimestamp();
const mtim = times[1].toTimestamp();
switch (wasi.fd_filestat_set_times(fd, atim, mtim, .{
.ATIM = true,
.MTIM = true,
})) {
const error_code = blk: {
if (times) |times_arr| {
const atim = times_arr[0].toTimestamp();
const mtim = times_arr[1].toTimestamp();
break :blk wasi.fd_filestat_set_times(fd, atim, mtim, .{
.ATIM = true,
.MTIM = true,
});
}

break :blk wasi.fd_filestat_set_times(fd, 0, 0, .{
.ATIM_NOW = true,
.MTIM_NOW = true,
});
};
switch (error_code) {
.SUCCESS => return,
.ACCES => return error.AccessDenied,
.PERM => return error.PermissionDenied,
Expand Down
Loading