File: File.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (114 lines) | stat: -rw-r--r-- 3,414 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import SystemPackage

protocol FdWASIEntry: WASIEntry {
    var fd: FileDescriptor { get }
}

protocol FdWASIFile: WASIFile, FdWASIEntry {
    var accessMode: FileAccessMode { get }
}

extension FdWASIFile {
    func fdStat() throws -> WASIAbi.FdStat {
        var fsRightsBase: WASIAbi.Rights = []
        if accessMode.contains(.read) {
            fsRightsBase.insert(.FD_READ)
        }
        if accessMode.contains(.write) {
            fsRightsBase.insert(.FD_WRITE)
        }
        return try WASIAbi.FdStat(
            fsFileType: self.fileType(),
            fsFlags: self.status(),
            fsRightsBase: fsRightsBase, fsRightsInheriting: []
        )
    }

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

    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 {
            throw WASIAbi.Errno.EBADF
        }
        // TODO: Use `writev`
        var bytesWritten: UInt32 = 0
        for iovec in buffer {
            bytesWritten += try iovec.withHostBufferPointer {
                UInt32(try fd.write(UnsafeRawBufferPointer($0)))
            }
        }
        return bytesWritten
    }

    @inlinable
    func pwrite<Buffer: Sequence>(vectored buffer: Buffer, offset: WASIAbi.FileSize) throws -> WASIAbi.Size where Buffer.Element == WASIAbi.IOVec {
        // TODO: Use `pwritev`
        var currentOffset: Int64 = Int64(offset)
        for iovec in buffer {
            currentOffset += try iovec.withHostBufferPointer {
                Int64(try fd.writeAll(toAbsoluteOffset: currentOffset, $0))
            }
        }
        let nwritten = WASIAbi.FileSize(currentOffset) - offset
        return WASIAbi.Size(nwritten)
    }

    @inlinable
    func read<Buffer: Sequence>(into buffer: Buffer) throws -> WASIAbi.Size where Buffer.Element == WASIAbi.IOVec {
        var nread: UInt32 = 0
        for iovec in buffer {
            nread += try iovec.withHostBufferPointer {
                try UInt32(fd.read(into: $0))
            }
        }
        return nread
    }

    @inlinable
    func pread<Buffer: Sequence>(into buffer: Buffer, offset: WASIAbi.FileSize) throws -> WASIAbi.Size where Buffer.Element == WASIAbi.IOVec {
        // TODO: Use `preadv`
        var nread: UInt32 = 0
        for iovec in buffer {
            nread += try iovec.withHostBufferPointer {
                try UInt32(fd.read(fromAbsoluteOffset: Int64(offset + UInt64(nread)), into: $0))
            }
        }
        return nread
    }
}

struct RegularFileEntry: FdWASIFile {
    let fd: FileDescriptor
    let accessMode: FileAccessMode
}

extension FdWASIFile {
    func setFdStatFlags(_ flags: WASIAbi.Fdflags) throws {
        try WASIAbi.Errno.translatingPlatformErrno {
            try fd.setStatus(flags.platformOpenOptions)
        }
    }
}

struct StdioFileEntry: FdWASIFile {
    let fd: FileDescriptor
    let accessMode: FileAccessMode

    func attributes() throws -> WASIAbi.Filestat {
        return WASIAbi.Filestat(
            dev: 0, ino: 0, filetype: .CHARACTER_DEVICE,
            nlink: 0, size: 0, atim: 0, mtim: 0, ctim: 0
        )
    }
}