Skip to content

Commit 3451ceb

Browse files
Merge pull request swiftlang#4893 from kateinoigakukun/pr-60e5fee97c1f1f8f2dedb269343dbb7d1c1cf9b8
[wasm] Remove too conservative guards around file operations
2 parents 778dde9 + 92f7dd1 commit 3451ceb

File tree

4 files changed

+9
-35
lines changed

4 files changed

+9
-35
lines changed

Sources/Foundation/Data.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
@usableFromInline let memcpy = Musl.memcpy
3737
@usableFromInline let memcmp = Musl.memcmp
3838
#elseif canImport(WASILibc)
39+
#if swift(>=6.0)
40+
private import wasi_emulated_mman
41+
#else
42+
import wasi_emulated_mman
43+
#endif
3944
@usableFromInline let calloc = WASILibc.calloc
4045
@usableFromInline let malloc = WASILibc.malloc
4146
@usableFromInline let free = WASILibc.free
@@ -2048,7 +2053,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20482053
}
20492054
}
20502055

2051-
#if !os(WASI)
20522056
/// Initialize a `Data` with the contents of a `URL`.
20532057
///
20542058
/// - parameter url: The `URL` to read.
@@ -2061,7 +2065,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
20612065
return Data(bytes: d.bytes, count: d.length)
20622066
}
20632067
}
2064-
#endif
20652068

20662069
/// Initialize a `Data` from a Base-64 encoded String using the given options.
20672070
///
@@ -2325,7 +2328,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
23252328
}
23262329
#endif
23272330

2328-
#if !os(WASI)
23292331
/// Write the contents of the `Data` to a location.
23302332
///
23312333
/// - parameter url: The location to write the data into.
@@ -2346,7 +2348,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
23462348
#endif
23472349
}
23482350
}
2349-
#endif
23502351

23512352
// MARK: -
23522353

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)
@@ -547,7 +542,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
547542
}
548543
try write(toFile: url.path, options: writeOptionsMask)
549544
}
550-
#endif
551545

552546
// MARK: - Bytes
553547
/// Copies a number of bytes from the start of the data object into a given buffer.
@@ -1014,7 +1008,6 @@ open class NSMutableData : NSData {
10141008
super.init(data: data)
10151009
}
10161010

1017-
#if !os(WASI)
10181011
public override init?(contentsOfFile path: String) {
10191012
super.init(contentsOfFile: path)
10201013
}
@@ -1030,7 +1023,6 @@ open class NSMutableData : NSData {
10301023
public override init(contentsOf url: URL, options: NSData.ReadingOptions = []) throws {
10311024
try super.init(contentsOf: url, options: options)
10321025
}
1033-
#endif
10341026

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

Sources/Foundation/NSPathUtilities.swift

+4-9
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ extension String {
204204
return temp
205205
}
206206

207-
#if !os(WASI)
208207
internal func _tryToRemovePathPrefix(_ prefix: String) -> String? {
209208
guard self != prefix else {
210209
return nil
@@ -217,7 +216,6 @@ extension String {
217216

218217
return nil
219218
}
220-
#endif
221219
}
222220

223221
extension NSString {
@@ -347,7 +345,6 @@ extension NSString {
347345
return result._stringByFixingSlashes()
348346
}
349347

350-
#if !os(WASI)
351348
public var expandingTildeInPath: String {
352349
guard hasPrefix("~") else {
353350
return _swiftObject
@@ -368,7 +365,6 @@ extension NSString {
368365

369366
return result
370367
}
371-
#endif
372368

373369
#if os(Windows)
374370
public var unixPath: String {
@@ -383,7 +379,6 @@ extension NSString {
383379
}
384380
#endif
385381

386-
#if !os(WASI)
387382
public var standardizingPath: String {
388383
#if os(Windows)
389384
let expanded = unixPath.expandingTildeInPath
@@ -431,8 +426,6 @@ extension NSString {
431426

432427
return resolvedPath
433428
}
434-
#endif
435-
436429
public func stringsByAppendingPaths(_ paths: [String]) -> [String] {
437430
if self == "" {
438431
return paths
@@ -664,7 +657,6 @@ extension NSString {
664657

665658
}
666659

667-
#if !os(WASI)
668660
extension FileManager {
669661
public enum SearchPathDirectory: UInt {
670662

@@ -740,13 +732,17 @@ public func NSHomeDirectory() -> String {
740732
}
741733

742734
public func NSHomeDirectoryForUser(_ user: String?) -> String? {
735+
#if os(WASI) // WASI does not have user concept
736+
return nil
737+
#else
743738
let userName = user?._cfObject
744739
guard let homeDir = CFCopyHomeDirectoryURLForUser(userName)?.takeRetainedValue() else {
745740
return nil
746741
}
747742

748743
let url: URL = homeDir._swiftObject
749744
return url.path
745+
#endif
750746
}
751747

752748
public func NSUserName() -> String {
@@ -844,4 +840,3 @@ internal func _NSCleanupTemporaryFile(_ auxFilePath: String, _ filePath: String)
844840
})
845841
#endif
846842
}
847-
#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)