mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
Add the sync functions
This commit is contained in:
parent
12ce6eb8f6
commit
25f6663304
@ -5328,3 +5328,71 @@ pub fn signalfd(fd: fd_t, mask: *const sigset_t, flags: u32) !fd_t {
|
||||
else => |err| return std.os.unexpectedErrno(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub const SyncError = error{
|
||||
InputOutput,
|
||||
NoSpaceLeft,
|
||||
DiskQuota,
|
||||
AccessDenied,
|
||||
} || UnexpectedError;
|
||||
|
||||
/// Write all pending file contents and metadata modifications to all filesystems.
|
||||
pub fn sync() void {
|
||||
system.sync();
|
||||
}
|
||||
|
||||
/// Write all pending file contents and metadata modifications to the filesystem which contains the specified file.
|
||||
pub fn syncfs(fd: fd_t) SyncError!void {
|
||||
const rc = system.syncfs(fd);
|
||||
switch (errno(rc)) {
|
||||
0 => return,
|
||||
EBADF, EINVAL, EROFS => unreachable,
|
||||
EIO => return error.InputOutput,
|
||||
ENOSPC => return error.NoSpaceLeft,
|
||||
EDQUOT => return error.DiskQuota,
|
||||
else => |err| return std.os.unexpectedErrno(err),
|
||||
}
|
||||
}
|
||||
|
||||
/// Write all pending file contents and metadata modifications for the specified file descriptor to the underlying filesystem.
|
||||
pub fn fsync(fd: fd_t) SyncError!void {
|
||||
if (std.Target.current.os.tag == .windows) {
|
||||
if (windows.kernel32.FlushFileBuffers(fd) != 0)
|
||||
return;
|
||||
switch (windows.kernel32.GetLastError()) {
|
||||
.SUCCESS => return,
|
||||
.INVALID_HANDLE => unreachable,
|
||||
.ACCESS_DENIED => return error.AccessDenied, // a sync was performed but the system couldn't update the access time
|
||||
.UNEXP_NET_ERR => return error.InputOutput,
|
||||
else => return error.InputOutput,
|
||||
}
|
||||
}
|
||||
const rc = system.fsync(fd);
|
||||
switch (errno(rc)) {
|
||||
0 => return,
|
||||
EBADF, EINVAL, EROFS => unreachable,
|
||||
EIO => return error.InputOutput,
|
||||
ENOSPC => return error.NoSpaceLeft,
|
||||
EDQUOT => return error.DiskQuota,
|
||||
else => |err| return std.os.unexpectedErrno(err),
|
||||
}
|
||||
}
|
||||
|
||||
/// Write all pending file contents for the specified file descriptor to the underlying filesystem, but not necessarily the metadata.
|
||||
pub fn fdatasync(fd: fd_t) SyncError!void {
|
||||
if (std.Target.current.os.tag == .windows) {
|
||||
return fsync(fd) catch |err| switch (err) {
|
||||
SyncError.AccessDenied => return, // fdatasync doesn't promise that the access time was synced
|
||||
else => return err,
|
||||
};
|
||||
}
|
||||
const rc = system.fdatasync(fd);
|
||||
switch (errno(rc)) {
|
||||
0 => return,
|
||||
EBADF, EINVAL, EROFS => unreachable,
|
||||
EIO => return error.InputOutput,
|
||||
ENOSPC => return error.NoSpaceLeft,
|
||||
EDQUOT => return error.DiskQuota,
|
||||
else => |err| return std.os.unexpectedErrno(err),
|
||||
}
|
||||
}
|
||||
|
||||
@ -1226,6 +1226,22 @@ pub fn bpf(cmd: BPF.Cmd, attr: *BPF.Attr, size: u32) usize {
|
||||
return syscall3(.bpf, @enumToInt(cmd), @ptrToInt(attr), size);
|
||||
}
|
||||
|
||||
pub fn sync() void {
|
||||
_ = syscall0(.sync);
|
||||
}
|
||||
|
||||
pub fn syncfs(fd: fd_t) usize {
|
||||
return syscall1(.syncfs, @bitCast(usize, @as(isize, fd)));
|
||||
}
|
||||
|
||||
pub fn fsync(fd: fd_t) usize {
|
||||
return syscall1(.fsync, @bitCast(usize, @as(isize, fd)));
|
||||
}
|
||||
|
||||
pub fn fdatasync(fd: fd_t) usize {
|
||||
return syscall1(.fdatasync, @bitCast(usize, @as(isize, fd)));
|
||||
}
|
||||
|
||||
test "" {
|
||||
if (builtin.os.tag == .linux) {
|
||||
_ = @import("linux/test.zig");
|
||||
|
||||
@ -287,3 +287,5 @@ pub extern "kernel32" fn K32GetWsChangesEx(hProcess: HANDLE, lpWatchInfoEx: PPSA
|
||||
pub extern "kernel32" fn K32InitializeProcessForWsWatch(hProcess: HANDLE) callconv(.Stdcall) BOOL;
|
||||
pub extern "kernel32" fn K32QueryWorkingSet(hProcess: HANDLE, pv: PVOID, cb: DWORD) callconv(.Stdcall) BOOL;
|
||||
pub extern "kernel32" fn K32QueryWorkingSetEx(hProcess: HANDLE, pv: PVOID, cb: DWORD) callconv(.Stdcall) BOOL;
|
||||
|
||||
pub extern "kernel32" fn FlushFileBuffers(hFile: HANDLE) callconv(.Stdcall) BOOL;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user