mirror of
https://github.com/ziglang/zig.git
synced 2026-02-20 16:24:51 +00:00
implement std.fs.File.updateTimes for windows
This commit is contained in:
parent
95fdff3feb
commit
aa170a7eff
@ -257,11 +257,16 @@ pub const File = struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub const UpdateTimesError = os.FutimensError;
|
||||
pub const UpdateTimesError = os.FutimensError || windows.SetFileTimeError;
|
||||
|
||||
/// `atime`: access timestamp in nanoseconds
|
||||
/// `mtime`: last modification timestamp in nanoseconds
|
||||
pub fn updateTimes(self: File, atime: u64, mtime: u64) UpdateTimesError!void {
|
||||
if (windows.is_the_target) {
|
||||
const atime_ft = windows.nanoSecondsToFileTime(atime);
|
||||
const mtime_ft = windows.nanoSecondsToFileTime(mtime);
|
||||
return windows.SetFileTime(self.handle, null, &atime_ft, &mtime_ft);
|
||||
}
|
||||
const times = [2]os.timespec{
|
||||
os.timespec{
|
||||
.tv_sec = @bitCast(isize, atime / std.time.ns_per_s),
|
||||
|
||||
@ -767,11 +767,35 @@ pub fn GetFileInformationByHandle(
|
||||
return info;
|
||||
}
|
||||
|
||||
pub const SetFileTimeError = error{Unexpected};
|
||||
|
||||
pub fn SetFileTime(
|
||||
hFile: HANDLE,
|
||||
lpCreationTime: ?*const FILETIME,
|
||||
lpLastAccessTime: ?*const FILETIME,
|
||||
lpLastWriteTime: ?*const FILETIME,
|
||||
) SetFileTimeError!void {
|
||||
const rc = kernel32.SetFileTime(hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
|
||||
if (rc == 0) {
|
||||
switch (kernel32.GetLastError()) {
|
||||
else => |err| return unexpectedError(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fileTimeToNanoSeconds(ft: FILETIME) u64 {
|
||||
const sec = (u64(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
|
||||
return sec * std.time.ns_per_s;
|
||||
}
|
||||
|
||||
pub fn nanoSecondsToFileTime(ns: u64) FILETIME {
|
||||
const sec = ns / std.time.ns_per_s;
|
||||
return FILETIME{
|
||||
.dwHighDateTime = @truncate(u32, sec >> 32),
|
||||
.dwLowDateTime = @truncate(u32, sec),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn cStrToPrefixedFileW(s: [*]const u8) ![PATH_MAX_WIDE + 1]u16 {
|
||||
return sliceToPrefixedFileW(mem.toSliceConst(u8, s));
|
||||
}
|
||||
|
||||
@ -170,6 +170,13 @@ pub extern "kernel32" stdcallcc fn SetFilePointerEx(
|
||||
in_dwMoveMethod: DWORD,
|
||||
) BOOL;
|
||||
|
||||
pub extern "kernel32" stdcallcc fn SetFileTime(
|
||||
hFile: HANDLE,
|
||||
lpCreationTime: ?*const FILETIME,
|
||||
lpLastAccessTime: ?*const FILETIME,
|
||||
lpLastWriteTime: ?*const FILETIME,
|
||||
) BOOL;
|
||||
|
||||
pub extern "kernel32" stdcallcc fn SetHandleInformation(hObject: HANDLE, dwMask: DWORD, dwFlags: DWORD) BOOL;
|
||||
|
||||
pub extern "kernel32" stdcallcc fn Sleep(dwMilliseconds: DWORD) void;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user