Skip to content

Commit 7a70a1b

Browse files
[wasm] Remove too conservative guards around file operations
Those guards were added to prevent the build from failing on WASI, but they are too conservative and the latest wasi-libc is now providing enough support for these operations.
1 parent 7d40966 commit 7a70a1b

File tree

4 files changed

+5
-35
lines changed

4 files changed

+5
-35
lines changed

Sources/Foundation/Data.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
@usableFromInline let memcpy = Musl.memcpy
3737
@usableFromInline let memcmp = Musl.memcmp
3838
#elseif canImport(WASILibc)
39+
import wasi_emulated_mman
3940
@usableFromInline let calloc = WASILibc.calloc
4041
@usableFromInline let malloc = WASILibc.malloc
4142
@usableFromInline let free = WASILibc.free
@@ -2048,7 +2049,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20482049
}
20492050
}
20502051

2051-
#if !os(WASI)
20522052
/// Initialize a `Data` with the contents of a `URL`.
20532053
///
20542054
/// - parameter url: The `URL` to read.
@@ -2061,7 +2061,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20612061
return Data(bytes: d.bytes, count: d.length)
20622062
}
20632063
}
2064-
#endif
20652064

20662065
/// Initialize a `Data` from a Base-64 encoded String using the given options.
20672066
///
@@ -2325,7 +2324,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
23252324
}
23262325
#endif
23272326

2328-
#if !os(WASI)
23292327
/// Write the contents of the `Data` to a location.
23302328
///
23312329
/// - parameter url: The location to write the data into.
@@ -2346,7 +2344,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
23462344
#endif
23472345
}
23482346
}
2349-
#endif
23502347

23512348
// MARK: -
23522349

Sources/Foundation/NSData.swift

-8
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
151151
_init(bytes: bytes, length: length, copy: false, deallocator: deallocator)
152152
}
153153

154-
#if !os(WASI)
155154
/// Initializes a data object with the contents of the file at a given path.
156155
public init(contentsOfFile path: String, options readOptionsMask: ReadingOptions = []) throws {
157156
super.init()
@@ -174,7 +173,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
174173
return nil
175174
}
176175
}
177-
#endif
178176

179177
/// Initializes a data object with the contents of another data object.
180178
public init(data: Data) {
@@ -184,7 +182,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
184182
}
185183
}
186184

187-
#if !os(WASI)
188185
/// Initializes a data object with the data from the location specified by a given URL.
189186
public init(contentsOf url: URL, options readOptionsMask: ReadingOptions = []) throws {
190187
super.init()
@@ -223,7 +220,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
223220
return try _NSNonfileURLContentLoader.current.contentsOf(url: url)
224221
}
225222
}
226-
#endif
227223

228224
/// Initializes a data object with the given Base64 encoded string.
229225
public init?(base64Encoded base64String: String, options: Base64DecodingOptions = []) {
@@ -439,7 +435,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
439435
}
440436
}
441437

442-
#if !os(WASI)
443438
internal static func readBytesFromFileWithExtendedAttributes(_ path: String, options: ReadingOptions) throws -> NSDataReadResult {
444439
guard let handle = FileHandle(path: path, flags: O_RDONLY, createMode: 0) else {
445440
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: nil)
@@ -545,7 +540,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
545540
}
546541
try write(toFile: url.path, options: writeOptionsMask)
547542
}
548-
#endif
549543

550544
// MARK: - Bytes
551545
/// Copies a number of bytes from the start of the data object into a given buffer.
@@ -1012,7 +1006,6 @@ open class NSMutableData : NSData {
10121006
super.init(data: data)
10131007
}
10141008

1015-
#if !os(WASI)
10161009
public override init?(contentsOfFile path: String) {
10171010
super.init(contentsOfFile: path)
10181011
}
@@ -1028,7 +1021,6 @@ open class NSMutableData : NSData {
10281021
public override init(contentsOf url: URL, options: NSData.ReadingOptions = []) throws {
10291022
try super.init(contentsOf: url, options: options)
10301023
}
1031-
#endif
10321024

10331025
public override init?(base64Encoded base64Data: Data, options: NSData.Base64DecodingOptions = []) {
10341026
super.init(base64Encoded: base64Data, options: options)

Sources/Foundation/NSPathUtilities.swift

+4-9
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ extension String {
195195
return temp
196196
}
197197

198-
#if !os(WASI)
199198
internal func _tryToRemovePathPrefix(_ prefix: String) -> String? {
200199
guard self != prefix else {
201200
return nil
@@ -208,7 +207,6 @@ extension String {
208207

209208
return nil
210209
}
211-
#endif
212210
}
213211

214212
extension NSString {
@@ -338,7 +336,6 @@ extension NSString {
338336
return result._stringByFixingSlashes()
339337
}
340338

341-
#if !os(WASI)
342339
public var expandingTildeInPath: String {
343340
guard hasPrefix("~") else {
344341
return _swiftObject
@@ -359,7 +356,6 @@ extension NSString {
359356

360357
return result
361358
}
362-
#endif
363359

364360
#if os(Windows)
365361
public var unixPath: String {
@@ -374,7 +370,6 @@ extension NSString {
374370
}
375371
#endif
376372

377-
#if !os(WASI)
378373
public var standardizingPath: String {
379374
#if os(Windows)
380375
let expanded = unixPath.expandingTildeInPath
@@ -422,8 +417,6 @@ extension NSString {
422417

423418
return resolvedPath
424419
}
425-
#endif
426-
427420
public func stringsByAppendingPaths(_ paths: [String]) -> [String] {
428421
if self == "" {
429422
return paths
@@ -655,7 +648,6 @@ extension NSString {
655648

656649
}
657650

658-
#if !os(WASI)
659651
extension FileManager {
660652
public enum SearchPathDirectory: UInt {
661653

@@ -731,13 +723,17 @@ public func NSHomeDirectory() -> String {
731723
}
732724

733725
public func NSHomeDirectoryForUser(_ user: String?) -> String? {
726+
#if os(WASI) // WASI does not have user concept
727+
return nil
728+
#else
734729
let userName = user?._cfObject
735730
guard let homeDir = CFCopyHomeDirectoryURLForUser(userName)?.takeRetainedValue() else {
736731
return nil
737732
}
738733

739734
let url: URL = homeDir._swiftObject
740735
return url.path
736+
#endif
741737
}
742738

743739
public func NSUserName() -> String {
@@ -831,4 +827,3 @@ internal func _NSCleanupTemporaryFile(_ auxFilePath: String, _ filePath: String)
831827
})
832828
#endif
833829
}
834-
#endif

Sources/Foundation/NSURL.swift

-14
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,6 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
556556
// TODO: should be `checkResourceIsReachableAndReturnError` with autoreleased error parameter.
557557
// Currently Autoreleased pointers is not supported on Linux.
558558
open func checkResourceIsReachable() throws -> Bool {
559-
#if os(WASI)
560-
return false
561-
#else
562559
guard isFileURL,
563560
let path = path else {
564561
throw NSError(domain: NSCocoaErrorDomain,
@@ -574,7 +571,6 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
574571
}
575572

576573
return true
577-
#endif
578574
}
579575

580576
/* Returns a file path URL that refers to the same resource as a specified URL. File path URLs use a file system style path. An error will occur if the url parameter is not a file URL. A file reference URL's resource must exist and be reachable to be converted to a file path URL. Symbol is present in iOS 4, but performs no operation.
@@ -918,12 +914,8 @@ extension NSURL {
918914
if selfPath.isAbsolutePath {
919915
absolutePath = selfPath
920916
} else {
921-
#if os(WASI)
922-
return nil
923-
#else
924917
let workingDir = FileManager.default.currentDirectoryPath
925918
absolutePath = workingDir._bridgeToObjectiveC().appendingPathComponent(selfPath)
926-
#endif
927919
}
928920

929921
#if os(Windows)
@@ -971,20 +963,16 @@ extension NSURL {
971963

972964
default:
973965
resolvedPath = resolvedPath._bridgeToObjectiveC().appendingPathComponent(component)
974-
#if !os(WASI)
975966
if let destination = FileManager.default._tryToResolveTrailingSymlinkInPath(resolvedPath) {
976967
resolvedPath = destination
977968
}
978-
#endif
979969
}
980970
}
981971

982972
// It might be a responsibility of NSURL(fileURLWithPath:). Check it.
983973
var isExistingDirectory: ObjCBool = false
984974

985-
#if !os(WASI)
986975
let _ = FileManager.default.fileExists(atPath: resolvedPath, isDirectory: &isExistingDirectory)
987-
#endif
988976

989977
if excludeSystemDirs {
990978
resolvedPath = resolvedPath._tryToRemovePathPrefix("/private") ?? resolvedPath
@@ -1063,7 +1051,6 @@ extension NSURL : _StructTypeBridgeable {
10631051

10641052
// -----
10651053

1066-
#if !os(WASI)
10671054
internal func _CFSwiftURLCopyResourcePropertyForKey(_ url: CFTypeRef, _ key: CFString, _ valuePointer: UnsafeMutablePointer<Unmanaged<CFTypeRef>?>?, _ errorPointer: UnsafeMutablePointer<Unmanaged<CFError>?>?) -> _DarwinCompatibleBoolean {
10681055
do {
10691056
let key = URLResourceKey(rawValue: key._swiftObject)
@@ -1597,7 +1584,6 @@ fileprivate extension URLResourceValuesStorage {
15971584
}
15981585
}
15991586
}
1600-
#endif
16011587

16021588
// -----
16031589

0 commit comments

Comments
 (0)