Skip to content

Commit 6a8ad59

Browse files
committed
WASI: Implement fd_datasync
1 parent 34149ae commit 6a8ad59

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

Sources/SystemExtras/FileOperations.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,26 @@ extension FileDescriptor {
598598
}
599599
#endif
600600
}
601+
602+
public func datasync() throws {
603+
return try _datasync().get()
604+
}
605+
606+
internal func _datasync() -> Result<Void, Errno> {
607+
#if os(Windows)
608+
return self._sync()
609+
#else
610+
nothingOrErrno(retryOnInterrupt: false) {
611+
#if SYSTEM_PACKAGE_DARWIN
612+
system_fcntl(self.rawValue, F_FULLFSYNC)
613+
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || os(Cygwin) || os(PS4)
614+
system_fdatasync(self.rawValue)
615+
#else
616+
system_fsync(self.rawValue)
617+
#endif
618+
}
619+
#endif
620+
}
601621
}
602622

603623
#if os(Windows)

Sources/SystemExtras/Syscalls.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ internal func system_fsync(_ fd: Int32) -> CInt {
5454

5555
#endif
5656

57+
#if os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Android) || os(Cygwin) || os(PS4)
58+
// fdatasync
59+
internal func system_fdatasync(_ fd: Int32) -> CInt {
60+
return fdatasync(fd)
61+
}
62+
#endif
63+
5764
#if os(Linux)
5865
// posix_fadvise
5966
internal func system_posix_fadvise(

Sources/WASI/FileSystem.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ protocol WASIFile: WASIEntry {
2525
func setFdStatFlags(_ flags: WASIAbi.Fdflags) throws
2626
func setFilestatSize(_ size: WASIAbi.FileSize) throws
2727
func sync() throws
28+
func datasync() throws
2829

2930
func tell() throws -> WASIAbi.FileSize
3031
func seek(offset: WASIAbi.FileDelta, whence: WASIAbi.Whence) throws -> WASIAbi.FileSize

Sources/WASI/Platform/File.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ extension FdWASIFile {
3030
}
3131
}
3232

33+
func datasync() throws {
34+
try WASIAbi.Errno.translatingPlatformErrno {
35+
try fd.datasync()
36+
}
37+
}
38+
3339
@inlinable
3440
func write<Buffer: Sequence>(vectored buffer: Buffer) throws -> WASIAbi.Size where Buffer.Element == WASIAbi.IOVec {
3541
guard accessMode.contains(.write) else {

Sources/WASI/WASI.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,10 @@ public class WASIBridgeToHost: WASI {
15161516
}
15171517

15181518
func fd_datasync(fd: WASIAbi.Fd) throws {
1519-
throw WASIAbi.Errno.ENOTSUP
1519+
guard case let .file(fileEntry) = fdTable[fd] else {
1520+
throw WASIAbi.Errno.EBADF
1521+
}
1522+
return try fileEntry.datasync()
15201523
}
15211524

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

0 commit comments

Comments
 (0)