Skip to content

Commit a9272da

Browse files
committed
Move syscalls into Syscalls.swift
1 parent 571db92 commit a9272da

File tree

6 files changed

+297
-129
lines changed

6 files changed

+297
-129
lines changed

Sources/System/FileStatus/FileStatus.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
11+
import Darwin
1112

1213
// FIXME: Document
1314
@frozen

Sources/System/FileStatus/FileStatusOperations.swift

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ extension FileDescriptor {
3131

3232
// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
3333
extension FilePath {
34+
/// Obtain information about the file pointed to by the FilePath.
35+
///
36+
/// - Parameters:
37+
/// - followSymlinks: Whether to follow symlinks.
38+
/// The default is `true`.
39+
/// - retryOnInterrupt: Whether to retry the read operation
40+
/// if it throws ``Errno/interrupted``.
41+
/// The default is `true`.
42+
/// Pass `false` to try only once and throw an error upon interruption.
43+
/// - Returns: A `FileStatus` for the file pointed to by FilePath.
44+
///
45+
/// Read, write or execute permission of the pointed to file is not required,
46+
/// but all intermediate directories must be searchable.
47+
///
48+
/// The corresponding C functions are `stat` and `lstat`.
3449
@_alwaysEmitIntoClient
3550
public func stat(
3651
followSymlinks: Bool = true,
@@ -48,7 +63,7 @@ extension FilePath {
4863
retryOnInterrupt: Bool
4964
) -> Result<FileStatus, Errno> {
5065
var result = CInterop.Stat()
51-
let _stat = followSymlinks ? SystemPackage._stat : _lstat
66+
let _stat = followSymlinks ? system_stat : system_lstat
5267
return withPlatformString { ptr in
5368
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
5469
_stat(ptr, &result)
@@ -67,7 +82,7 @@ extension FileDescriptor {
6782
internal func fstat(retryOnInterrupt: Bool = true) throws -> FileStatus {
6883
var result = CInterop.Stat()
6984
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
70-
_fstat(self.rawValue, &result)
85+
system_fstat(self.rawValue, &result)
7186
}.get()
7287
return FileStatus(rawValue: result)
7388
}
@@ -76,7 +91,7 @@ extension FileDescriptor {
7691
var result = CInterop.Stat()
7792
try path.withPlatformString { ptr in
7893
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
79-
_fstatat(self.rawValue, ptr, &result, fcntrl.rawValue)
94+
system_fstatat(self.rawValue, ptr, &result, fcntrl.rawValue)
8095
}.get()
8196
}
8297
return FileStatus(rawValue: result)
@@ -113,7 +128,7 @@ extension FilePath {
113128
followSymlinks: Bool,
114129
retryOnInterrupt: Bool
115130
) -> Result<Void, Errno> {
116-
let _chmod = followSymlinks ? SystemPackage._chmod : _lchmod
131+
let _chmod = followSymlinks ? system_chmod : system_lchmod
117132
return withPlatformString { ptr in
118133
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
119134
_chmod(ptr, permissions.rawValue)
@@ -126,15 +141,15 @@ extension FilePath {
126141
extension FileDescriptor {
127142
internal func fchmod(permissions: FilePermissions, retryOnInterrupt: Bool = true) throws {
128143
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
129-
_fchmod(self.rawValue, permissions.rawValue)
144+
system_fchmod(self.rawValue, permissions.rawValue)
130145
}.get()
131146
}
132147

133148
// valid flags: AT_SYMLINK_NOFOLLOW | AT_REALDEV | AT_FDONLY
134149
internal func fchmodat(path: FilePath, permissions: FilePermissions, fcntrl: FileDescriptor.ControlFlags, retryOnInterrupt: Bool = true) throws {
135150
try path.withPlatformString { ptr in
136151
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
137-
_fchmodat(self.rawValue, ptr, permissions.rawValue, fcntrl.rawValue)
152+
system_fchmodat(self.rawValue, ptr, permissions.rawValue, fcntrl.rawValue)
138153
}.get()
139154
}
140155
}
@@ -171,7 +186,7 @@ extension FilePath {
171186
followSymlinks: Bool,
172187
retryOnInterrupt: Bool
173188
) -> Result<Void, Errno> {
174-
let _chown = followSymlinks ? SystemPackage._chown : _lchown
189+
let _chown = followSymlinks ? system_chown : system_lchown
175190
return withPlatformString { ptr in
176191
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
177192
_chown(ptr, userId, groupId)
@@ -184,14 +199,14 @@ extension FilePath {
184199
extension FileDescriptor {
185200
internal func fchown(userId: CInterop.UserId, groupId: CInterop.GroupId, retryOnInterrupt: Bool = true) throws {
186201
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
187-
_fchown(self.rawValue, userId, groupId)
202+
system_fchown(self.rawValue, userId, groupId)
188203
}.get()
189204
}
190205

191206
internal func fchownat(path: FilePath, userId: CInterop.UserId, groupId: CInterop.GroupId, fcntrl: FileDescriptor.ControlFlags, retryOnInterrupt: Bool = true) throws {
192207
try path.withPlatformString { ptr in
193208
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
194-
_fchownat(self.rawValue, ptr, userId, groupId, fcntrl.rawValue)
209+
system_fchownat(self.rawValue, ptr, userId, groupId, fcntrl.rawValue)
195210
}.get()
196211
}
197212
}
@@ -225,7 +240,7 @@ extension FilePath {
225240
followSymlinks: Bool,
226241
retryOnInterrupt: Bool
227242
) -> Result<Void, Errno> {
228-
let _chflags = followSymlinks ? SystemPackage._chflags : _lchflags
243+
let _chflags = followSymlinks ? system_chflags : system_lchflags
229244
return withPlatformString { ptr in
230245
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
231246
_chflags(ptr, flags.rawValue)
@@ -238,15 +253,15 @@ extension FilePath {
238253
extension FileDescriptor {
239254
internal func fchflags(flags: FileFlags, retryOnInterrupt: Bool = true) throws {
240255
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
241-
_fchflags(self.rawValue, flags.rawValue)
256+
system_fchflags(self.rawValue, flags.rawValue)
242257
}.get()
243258
}
244259

245260
#if os(FreeBSD)
246261
internal func chflagsat(path: FilePath, flags: FileFlags, fcntrl: FileDescriptorControlFlags, retryOnInterrupt: Bool = true) throws {
247262
try path.withPlatformString { ptr in
248263
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
249-
_chflagsat(self.rawValue, ptr, flags.rawValue, fcntrl.rawValue)
264+
system_chflagsat(self.rawValue, ptr, flags.rawValue, fcntrl.rawValue)
250265
}.get()
251266
}
252267
}
@@ -255,23 +270,22 @@ extension FileDescriptor {
255270

256271
// MARK: - umask
257272

258-
// FIXME: document, shape
259-
/// The umask() routine sets the process's file mode creation mask to cmask and
260-
/// returns the previous value of the mask. The 9 low-order access permission
261-
/// bits of cmask are used by system calls, including open(2), mkdir(2),
262-
/// mkfifo(2), and mknod(2) to turn off corresponding bits requested in file
263-
/// mode. (See chmod(2)). This clearing allows each user to restrict the
264-
/// default access to his files.
273+
// FIXME: umask document, shape
274+
/// The umask() routine sets the process's file mode creation mask to newMask
275+
/// and returns the previous value of the mask. The bits of newMask are used by
276+
/// system calls, including open(2), mkdir(2), mkfifo(2), and mknod(2) to turn
277+
/// off corresponding bits requested in file mode. (See chmod(2)). This clearing
278+
/// allows each user to restrict the default access to their files.
265279
///
266280
/// The default mask value is `S_IWGRP` | `S_IWOTH` (022, write access for the
267-
/// owner only). Child processes inherit the mask of the calling process.
268-
@_alwaysEmitIntoClient
269-
public func umask(newMask: FilePermissions) -> FilePermissions {
270-
let mode = _umask(newMask.rawValue)
271-
return FileMode(rawValue: mode).permissions
272-
}
281+
/// owner only). Child processes inherit the mask of the calling process.
282+
// @_alwaysEmitIntoClient
283+
// public func umask(newMask: FilePermissions) -> FilePermissions {
284+
// let oldMode = system_umask(newMask.rawValue)
285+
// return FileMode(rawValue: oldMode).permissions
286+
// }
273287

274-
// [ ] mode_t umask(mode_t)
288+
// [x] mode_t umask(mode_t)
275289

276290
// MARK: - mkfifo, mknod, mkdir
277291

@@ -300,7 +314,7 @@ extension FilePath {
300314
) -> Result<Void, Errno> {
301315
withPlatformString { ptr in
302316
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
303-
SystemPackage._mkfifo(ptr, permissions.rawValue)
317+
system_mkfifo(ptr, permissions.rawValue)
304318
}
305319
}
306320
}
@@ -326,7 +340,7 @@ extension FilePath {
326340
) -> Result<Void, Errno> {
327341
withPlatformString { ptr in
328342
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
329-
SystemPackage._mknod(ptr, permissions.rawValue, device)
343+
system_mknod(ptr, permissions.rawValue, device)
330344
}
331345
}
332346
}
@@ -349,7 +363,7 @@ extension FilePath {
349363
) -> Result<Void, Errno> {
350364
withPlatformString { ptr in
351365
nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
352-
SystemPackage._mkdir(ptr, permissions.rawValue)
366+
system_mkdir(ptr, permissions.rawValue)
353367
}
354368
}
355369
}
@@ -359,7 +373,7 @@ extension FileDescriptor {
359373
internal func mkdirat(path: FilePath, permissions: FilePermissions, retryOnInterrupt: Bool = true) throws {
360374
try path.withPlatformString { ptr in
361375
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
362-
_mkdirat(self.rawValue, ptr, permissions.rawValue)
376+
system_mkdirat(self.rawValue, ptr, permissions.rawValue)
363377
}.get()
364378
}
365379
}
@@ -376,7 +390,7 @@ extension FileDescriptor {
376390
internal func futimens(accessTime: TimeSpecification, modificationTime: TimeSpecification, retryOnInterrupt: Bool = true) throws {
377391
let times = [accessTime.rawValue, modificationTime.rawValue]
378392
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
379-
_futimens(self.rawValue, times)
393+
system_futimens(self.rawValue, times)
380394
}.get()
381395
}
382396

@@ -385,7 +399,7 @@ extension FileDescriptor {
385399
let times = [accessTime.rawValue, modificationTime.rawValue]
386400
try path.withPlatformString { ptr in
387401
try nothingOrErrno(retryOnInterrupt: retryOnInterrupt) {
388-
_utimensat(self.rawValue, ptr, times, fcntrl.rawValue)
402+
system_utimensat(self.rawValue, ptr, times, fcntrl.rawValue)
389403
}.get()
390404
}
391405
}

Sources/System/Internals/Constants.swift

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -638,100 +638,3 @@ internal var _UTIME_NOW: Int { Int(UTIME_NOW) }
638638
@_alwaysEmitIntoClient
639639
internal var _UTIME_OMIT: Int { Int(UTIME_OMIT) }
640640
#endif
641-
642-
// MARK: - Stat.h Functions
643-
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
644-
@_alwaysEmitIntoClient
645-
internal let _stat = stat
646-
647-
@_alwaysEmitIntoClient
648-
internal let _lstat = lstat
649-
650-
@_alwaysEmitIntoClient
651-
internal let _fstat = fstat
652-
653-
@_alwaysEmitIntoClient
654-
internal let _fstatat = fstatat
655-
656-
@_alwaysEmitIntoClient
657-
internal let _statx_np = statx_np
658-
659-
@_alwaysEmitIntoClient
660-
internal let _fstatx_np = fstatx_np
661-
662-
@_alwaysEmitIntoClient
663-
internal let _lstatx_np = lstatx_np
664-
665-
@_alwaysEmitIntoClient
666-
internal let _chmod = chmod
667-
668-
@_alwaysEmitIntoClient
669-
internal let _lchmod = lchmod
670-
671-
@_alwaysEmitIntoClient
672-
internal let _fchmod = fchmod
673-
674-
@_alwaysEmitIntoClient
675-
internal let _fchmodat = fchmodat
676-
677-
@_alwaysEmitIntoClient
678-
internal let _chmodx_np = chmodx_np
679-
680-
@_alwaysEmitIntoClient
681-
internal let _fchmodx_np = fchmodx_np
682-
683-
@_alwaysEmitIntoClient
684-
internal let _chown = chown
685-
686-
@_alwaysEmitIntoClient
687-
internal let _lchown = lchown
688-
689-
@_alwaysEmitIntoClient
690-
internal let _fchown = fchown
691-
692-
@_alwaysEmitIntoClient
693-
internal let _fchownat = fchownat
694-
695-
@_alwaysEmitIntoClient
696-
internal let _chflags = chflags
697-
698-
@_alwaysEmitIntoClient
699-
internal let _lchflags = lchflags
700-
701-
@_alwaysEmitIntoClient
702-
internal let _fchflags = fchflags
703-
704-
#if os(FreeBSD)
705-
@_alwaysEmitIntoClient
706-
internal let _fchflagsat = fchflagsat
707-
#endif
708-
709-
@_alwaysEmitIntoClient
710-
internal let _umask = Darwin.umask
711-
712-
@_alwaysEmitIntoClient
713-
internal let _mkfifo = mkfifo
714-
715-
@_alwaysEmitIntoClient
716-
internal let _mkfifox_np = mkfifox_np
717-
718-
@_alwaysEmitIntoClient
719-
internal let _mknod = mknod
720-
721-
@_alwaysEmitIntoClient
722-
internal let _mkdir = mkdir
723-
724-
@_alwaysEmitIntoClient
725-
internal let _mkdirat = mkdirat
726-
727-
@_alwaysEmitIntoClient
728-
internal let _mkdirx_np = mkdirx_np
729-
730-
@available(macOS 10.13, *)
731-
@_alwaysEmitIntoClient
732-
internal let _futimens = futimens
733-
734-
@available(macOS 10.13, *)
735-
@_alwaysEmitIntoClient
736-
internal let _utimensat = utimensat
737-
#endif

Sources/System/Internals/Mocking.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ internal func _mockOffT(
192192
) -> _COffT {
193193
_COffT(mockImpl(name: name, path: path, args))
194194
}
195+
196+
internal func _mockModeT(
197+
name: String = #function, _ args: AnyHashable...
198+
) -> CInterop.Mode {
199+
CInterop.Mode(mockImpl(name: name, path: nil, args))
200+
}
201+
195202
#endif // ENABLE_MOCKING
196203

197204
// Force paths to be treated as Windows syntactically if `enabled` is

0 commit comments

Comments
 (0)