Skip to content

Commit b5ba56b

Browse files
committed
FoundationEssentials: perform explicit conversion of mode_t
Windows uses `int` instead of a `mode_t` type. Create a shim alias in `_CShims` and use explicit type conversions to allow sharing of code paths across platforms. This helps reduce some of the build errors enabling further work to re-enable building FoundationEssentials on Windows.
1 parent ce0326c commit b5ba56b

File tree

8 files changed

+36
-26
lines changed

8 files changed

+36
-26
lines changed

Sources/FoundationEssentials/Data/Data+Reading.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,17 @@ internal func readBytesFromFile(path inPath: PathOrURL, reportProgress: Bool, ma
209209
}
210210

211211
let fileSize = min(Int(clamping: filestat.st_size), maxLength ?? Int.max)
212-
let fileType = filestat.st_mode & S_IFMT
212+
let fileType = mode_t(filestat.st_mode) & mode_t(S_IFMT)
213213
let preferredChunkSize = filestat.st_blksize
214214
#if !NO_FILESYSTEM
215215
let shouldMap = shouldMapFileDescriptor(fd, path: inPath, options: options)
216216
#else
217217
let shouldMap = false
218218
#endif
219219

220-
if fileType != S_IFREG {
220+
if fileType != mode_t(S_IFREG) {
221221
// EACCES is still an odd choice, but at least we have a better error for directories.
222-
let code = (fileType == S_IFDIR) ? EISDIR : EACCES
222+
let code = (fileType == mode_t(S_IFDIR)) ? EISDIR : EACCES
223223
throw CocoaError.errorWithFilePath(inPath, errno: code, reading: true)
224224
}
225225

Sources/FoundationEssentials/FileManager/FileManager+Basics.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import Darwin
1515
#elseif canImport(Glibc)
1616
import Glibc
1717
#elseif os(Windows)
18-
import CRT
18+
import ucrt
19+
internal import _CShims
1920
#endif
2021

2122
internal struct _FileManagerImpl {
@@ -64,7 +65,7 @@ internal struct _FileManagerImpl {
6465
var statBuf = stat()
6566
let fd = open(path, 0, 0)
6667
guard fd >= 0 else { return nil }
67-
if fstat(fd, &statBuf) < 0 || statBuf.st_mode & S_IFMT == S_IFDIR {
68+
if fstat(fd, &statBuf) < 0 || mode_t(statBuf.st_mode) & mode_t(S_IFMT) == mode_t(S_IFDIR) {
6869
close(fd)
6970
return nil
7071
}
@@ -83,7 +84,7 @@ internal struct _FileManagerImpl {
8384
}
8485

8586
/* check for being same type */
86-
if myInfo.st_mode & S_IFMT != otherInfo.st_mode & S_IFMT {
87+
if myInfo.st_mode & mode_t(S_IFMT) != otherInfo.st_mode & mode_t(S_IFMT) {
8788
return false
8889
}
8990

Sources/FoundationEssentials/FileManager/FileManager+Files.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Glibc
2222
internal import _CShims
2323
#elseif os(Windows)
2424
import CRT
25+
internal import _CShims
2526
#endif
2627

2728
extension Date {
@@ -46,13 +47,13 @@ private func _nameFor(gid: gid_t) -> String? {
4647

4748
extension mode_t {
4849
private var _fileType: FileAttributeType {
49-
switch self & S_IFMT {
50-
case S_IFCHR: .typeCharacterSpecial
51-
case S_IFDIR: .typeDirectory
52-
case S_IFBLK: .typeBlockSpecial
53-
case S_IFREG: .typeRegular
54-
case S_IFLNK: .typeSymbolicLink
55-
case S_IFSOCK: .typeSocket
50+
switch self & mode_t(S_IFMT) {
51+
case mode_t(S_IFCHR): .typeCharacterSpecial
52+
case mode_t(S_IFDIR): .typeDirectory
53+
case mode_t(S_IFBLK): .typeBlockSpecial
54+
case mode_t(S_IFREG): .typeRegular
55+
case mode_t(S_IFLNK): .typeSymbolicLink
56+
case mode_t(S_IFSOCK): .typeSocket
5657
default: .typeUnknown
5758
}
5859
}
@@ -344,7 +345,7 @@ extension _FileManagerImpl {
344345
guard stat(rep, &fileInfo) == 0 else {
345346
return (false, false)
346347
}
347-
let isDir = (fileInfo.st_mode & S_IFMT) == S_IFDIR
348+
let isDir = (mode_t(fileInfo.st_mode) & mode_t(S_IFMT)) == mode_t(S_IFDIR)
348349
return (true, isDir)
349350
}
350351
}
@@ -644,7 +645,7 @@ extension _FileManagerImpl {
644645
}
645646
}
646647

647-
if mode_t(mode) & S_IWUSR != 0 {
648+
if mode_t(mode) & mode_t(S_IWUSR) != 0 {
648649
try setMode?()
649650
setMode = nil
650651
}

Sources/FoundationEssentials/FileManager/FileManager+Utilities.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,28 @@ import Darwin
2727
import Glibc
2828
internal import _CShims
2929
#elseif os(Windows)
30-
import CRT
30+
import ucrt
31+
internal import _CShims
3132
#endif
3233

3334
extension stat {
3435
var isDirectory: Bool {
35-
(self.st_mode & S_IFMT) == S_IFDIR
36+
(mode_t(self.st_mode) & mode_t(S_IFMT)) == mode_t(S_IFDIR)
3637
}
3738

3839
var isRegular: Bool {
39-
(self.st_mode & S_IFMT) == S_IFREG
40+
(mode_t(self.st_mode) & mode_t(S_IFMT)) == mode_t(S_IFREG)
4041
}
41-
42+
43+
#if !os(Windows)
4244
var isSymbolicLink: Bool {
43-
(self.st_mode & S_IFMT) == S_IFLNK
45+
(mode_t(self.st_mode) & mode_t(S_IFMT)) == mode_t(S_IFLNK)
4446
}
45-
47+
#endif
48+
4649
var isSpecial: Bool {
47-
let type = self.st_mode & S_IFMT
48-
return type == S_IFBLK || type == S_IFCHR
50+
let type = mode_t(self.st_mode) & mode_t(S_IFMT)
51+
return type == mode_t(S_IFBLK) || type == mode_t(S_IFCHR)
4952
}
5053
}
5154

Sources/FoundationEssentials/FileManager/FileOperations+Enumeration.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Glibc
1717
internal import _CShims
1818
#elseif os(Windows)
1919
import CRT
20+
internal import _CShims
2021
#endif
2122

2223
// MARK: Directory Iteration
@@ -266,7 +267,7 @@ struct _POSIXDirectoryContentsSequence: Sequence {
266267
let statDir = directoryPath + "/" + fileName
267268
if stat(statDir, &statBuf) == 0 {
268269
// #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
269-
if (statBuf.st_mode & S_IFMT) == S_IFDIR {
270+
if (mode_t(statBuf.st_mode) & mode_t(S_IFMT)) == mode_t(S_IFDIR) {
270271
isDirectory = true
271272
}
272273
}

Sources/FoundationEssentials/FileManager/FileOperations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ enum _FileOperations {
556556
defer { close(dstfd) }
557557

558558
// Set the file permissions using fchmod() instead of when open()ing to avoid umask() issues
559-
let permissions = fileInfo.st_mode & ~S_IFMT
559+
let permissions = mode_t(fileInfo.st_mode) & ~mode_t(S_IFMT)
560560
guard fchmod(dstfd, permissions) == 0 else {
561561
try delegate.throwIfNecessary(errno, srcPtr, dstPtr)
562562
return

Sources/FoundationEssentials/String/String+Path.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ extension String {
406406
if lstat(buffer.baseAddress!, &statBuf) < 0 {
407407
return nil
408408
}
409-
if statBuf.st_mode & S_IFMT == S_IFLNK {
409+
if st_mode(statBuf.st_mode) & st_mode(S_IFMT) == st_mode(S_IFLNK) {
410410
/* Examples:
411411
* fspath == /foo/bar0baz/quux/froboz
412412
* linkx == /tic/tac/toe

Sources/_CShims/include/platform_shims.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#include <libkern/OSThermalNotification.h>
2525
#endif
2626

27+
#if defined(_WIN32)
28+
typedef int mode_t;
29+
#endif
30+
2731
INTERNAL char * _Nullable * _Nullable _platform_shims_get_environ();
2832

2933
INTERNAL void _platform_shims_lock_environ();

0 commit comments

Comments
 (0)