Skip to content

WASI: Implement fd_sync #180

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 4 commits into from
Mar 27, 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
22 changes: 22 additions & 0 deletions Sources/SystemExtras/FileOperations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,28 @@ extension FileDescriptor {
return .success(DirectoryStream(rawValue: dirp))
#endif
}

public func sync() throws {
return try _sync().get()
}

internal func _sync() -> Result<Void, Errno> {
#if os(Windows)
let handle = HANDLE(bitPattern: _get_osfhandle(self.rawValue))
return nothingOrErrno(retryOnInterrupt: false) {
let ok = FlushFileBuffers(handle)
return ok ? 0 : -1
}
#else
nothingOrErrno(retryOnInterrupt: false) {
#if SYSTEM_PACKAGE_DARWIN
system_fcntl(self.rawValue, F_FULLFSYNC)
#else
system_fsync(self.rawValue)
#endif
}
#endif
}
}

#if os(Windows)
Expand Down
5 changes: 5 additions & 0 deletions Sources/SystemExtras/Syscalls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ internal func system_fcntl(_ fd: Int32, _ cmd: Int32) -> CInt {
return fcntl(fd, cmd)
}

// fsync
internal func system_fsync(_ fd: Int32) -> CInt {
return fsync(fd)
}

#endif

#if os(Linux)
Expand Down
1 change: 1 addition & 0 deletions Sources/WASI/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protocol WASIFile: WASIEntry {
func fdStat() throws -> WASIAbi.FdStat
func setFdStatFlags(_ flags: WASIAbi.Fdflags) throws
func setFilestatSize(_ size: WASIAbi.FileSize) throws
func sync() 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 @@ -24,6 +24,12 @@ extension FdWASIFile {
)
}

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

@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 @@ -1695,7 +1695,10 @@ public class WASIBridgeToHost: WASI {
}

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

func fd_tell(fd: WASIAbi.Fd) throws -> WASIAbi.FileSize {
Expand Down