Skip to content

WASI: Implement fd_datasync #181

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

Merged
merged 1 commit into from
Mar 28, 2025
Merged
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
20 changes: 20 additions & 0 deletions Sources/SystemExtras/FileOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,26 @@ extension FileDescriptor {
}
#endif
}

public func datasync() throws {
return try _datasync().get()
}

internal func _datasync() -> Result<Void, Errno> {
#if os(Windows)
return self._sync()
#else
nothingOrErrno(retryOnInterrupt: false) {
#if SYSTEM_PACKAGE_DARWIN
system_fcntl(self.rawValue, F_FULLFSYNC)
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || os(Cygwin) || os(PS4)
system_fdatasync(self.rawValue)
#else
system_fsync(self.rawValue)
#endif
}
#endif
}
}

#if os(Windows)
Expand Down
7 changes: 7 additions & 0 deletions Sources/SystemExtras/Syscalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ internal func system_fsync(_ fd: Int32) -> CInt {

#endif

#if os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || os(Cygwin) || os(PS4)
// fdatasync
internal func system_fdatasync(_ fd: Int32) -> CInt {
return fdatasync(fd)
}
#endif

#if os(Linux)
// posix_fadvise
internal func system_posix_fadvise(
Expand Down
1 change: 1 addition & 0 deletions Sources/WASI/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protocol WASIFile: WASIEntry {
func setFdStatFlags(_ flags: WASIAbi.Fdflags) throws
func setFilestatSize(_ size: WASIAbi.FileSize) throws
func sync() throws
func datasync() throws

func tell() throws -> WASIAbi.FileSize
func seek(offset: WASIAbi.FileDelta, whence: WASIAbi.Whence) throws -> WASIAbi.FileSize
Expand Down
6 changes: 6 additions & 0 deletions Sources/WASI/Platform/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ extension FdWASIFile {
}
}

func datasync() throws {
try WASIAbi.Errno.translatingPlatformErrno {
try fd.datasync()
}
}

@inlinable
func write<Buffer: Sequence>(vectored buffer: Buffer) throws -> WASIAbi.Size where Buffer.Element == WASIAbi.IOVec {
guard accessMode.contains(.write) else {
Expand Down
5 changes: 4 additions & 1 deletion Sources/WASI/WASI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,10 @@ public class WASIBridgeToHost: WASI {
}

func fd_datasync(fd: WASIAbi.Fd) throws {
throw WASIAbi.Errno.ENOTSUP
guard case let .file(fileEntry) = fdTable[fd] else {
throw WASIAbi.Errno.EBADF
}
return try fileEntry.datasync()
}

func fd_fdstat_get(fileDescriptor: UInt32) throws -> WASIAbi.FdStat {
Expand Down