Skip to content

Commit 76dedad

Browse files
kkebokateinoigakukun
authored andcommitted
WASI: Implement fd_sync except for Windows
1 parent fc546b2 commit 76dedad

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

Sources/SystemExtras/FileOperations.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,21 @@ extension FileDescriptor {
576576
return .success(DirectoryStream(rawValue: dirp))
577577
#endif
578578
}
579+
580+
public func sync() throws {
581+
return try _sync().get()
582+
}
583+
584+
internal func _sync() -> Result<Void, Errno> {
585+
#if os(Windows)
586+
// TODO: Implement by `FlushFileBuffers`?
587+
return .failure(Errno(rawValue: ERROR_NOT_SUPPORTED))
588+
#else
589+
nothingOrErrno(retryOnInterrupt: false) {
590+
system_fsync(self.rawValue)
591+
}
592+
#endif
593+
}
579594
}
580595

581596
#if os(Windows)

Sources/SystemExtras/Syscalls.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ internal func system_fcntl(_ fd: Int32, _ cmd: Int32) -> CInt {
4747
return fcntl(fd, cmd)
4848
}
4949

50+
// fsync
51+
internal func system_fsync(_ fd: Int32) -> CInt {
52+
return fsync(fd)
53+
}
54+
5055
#endif
5156

5257
#if os(Linux)

Sources/WASI/FileSystem.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ protocol WASIFile: WASIEntry {
2424
func fdStat() throws -> WASIAbi.FdStat
2525
func setFdStatFlags(_ flags: WASIAbi.Fdflags) throws
2626
func setFilestatSize(_ size: WASIAbi.FileSize) throws
27+
func sync() throws
2728

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

Sources/WASI/Platform/File.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ extension FdWASIFile {
2424
)
2525
}
2626

27+
func sync() throws {
28+
guard accessMode.contains(.write) else {
29+
throw WASIAbi.Errno.EBADF
30+
}
31+
try WASIAbi.Errno.translatingPlatformErrno {
32+
try fd.sync()
33+
}
34+
}
35+
2736
@inlinable
2837
func write<Buffer: Sequence>(vectored buffer: Buffer) throws -> WASIAbi.Size where Buffer.Element == WASIAbi.IOVec {
2938
guard accessMode.contains(.write) else {

Sources/WASI/WASI.swift

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

16971697
func fd_sync(fd: WASIAbi.Fd) throws {
1698-
throw WASIAbi.Errno.ENOTSUP
1698+
guard case let .file(fileEntry) = fdTable[fd] else {
1699+
throw WASIAbi.Errno.EBADF
1700+
}
1701+
return try fileEntry.sync()
16991702
}
17001703

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

0 commit comments

Comments
 (0)