Skip to content

Commit cc5cc99

Browse files
committed
Update Swift 6.0
1 parent ff64a86 commit cc5cc99

File tree

5 files changed

+43
-42
lines changed

5 files changed

+43
-42
lines changed

Sources/File/File.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,15 @@ public extension File {
4343
func append(_ data: Data) throws {
4444
do {
4545
let handler = try FileHandle(forWritingTo: url)
46-
_ = handler.seekToEndFactory()
47-
handler.writeFactory(data)
48-
handler.closeFileFactory()
46+
if #available(iOS 13.4, macOS 10.15.4, tvOS 13.4, watchOS 6.2, visionOS 1.0, *) {
47+
_ = handler.seekToEndFactory()
48+
handler.writeFactory(data)
49+
handler.closeFileFactory()
50+
} else {
51+
_ = handler.seekToEndOfFile()
52+
handler.write(data)
53+
handler.closeFile()
54+
}
4955
} catch {
5056
throw FileError.writeFailed(path: store.path, error: error)
5157
}

Sources/File/FileError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Logging
44
private let logger = Logger(label: "FileError")
55

66
/// Error can be thrown by PLFile
7-
public enum FileError: Error {
7+
public enum FileError: Error, Sendable {
88
/// File Create Error
99
case fileCreateError(path: Path)
1010
/// Verify that file path is empty.

Sources/File/Internal/FileHandleExt.swift

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,39 @@ extension FileHandle {
66
///
77
/// - Returns: The current offset from the beginning of the file.
88
///
9-
/// - Note: `seekToEndOfFile()` is deprecated in macOS 10.15.4, so this method provides a replacement.
9+
/// - Note: Uses the modern `seekToEnd()` method for Swift 6.0 compatibility.
10+
@available(iOS 13.4, macOS 10.15.4, tvOS 13.4, watchOS 6.2, visionOS 1.0, *)
1011
func seekToEndFactory() -> UInt64 {
11-
if #available(iOS 13.4, macOS 10.15.4, tvOS 13.4, watchOS 6.2, visionOS 1.0, *) {
12-
do {
13-
return try self.seekToEnd()
14-
} catch {
15-
return 0
16-
}
17-
} else {
18-
return self.seekToEndOfFile()
12+
do {
13+
return try self.seekToEnd()
14+
} catch {
15+
return 0
1916
}
2017
}
2118

2219
/// Writes the given data to the file handle.
2320
///
2421
/// - Parameter data: The data to write.
2522
///
26-
/// - Note: `write(_ data: Data)` is deprecated in macOS 10.15.4, so this method provides a replacement.
23+
/// - Note: Uses the modern `write(contentsOf:)` method for Swift 6.0 compatibility.
24+
@available(iOS 13.4, macOS 10.15.4, tvOS 13.4, watchOS 6.2, visionOS 1.0, *)
2725
func writeFactory(_ data: Data) {
28-
if #available(iOS 13.4, macOS 10.15.4, tvOS 13.4, watchOS 6.2, visionOS 1.0, *) {
29-
do {
30-
try self.write(contentsOf: data)
31-
} catch {
32-
return
33-
}
34-
} else {
35-
self.write(data)
26+
do {
27+
try self.write(contentsOf: data)
28+
} catch {
29+
return
3630
}
3731
}
3832

3933
/// Closes the file handle.
4034
///
41-
/// - Note: `closeFile()` is deprecated in macOS 10.15, so this method provides a replacement.
35+
/// - Note: Uses the modern `close()` method for Swift 6.0 compatibility.
36+
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, visionOS 1.0, *)
4237
func closeFileFactory() {
43-
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, visionOS 1.0, *) {
44-
do {
45-
try self.close()
46-
} catch { return }
47-
} else {
48-
self.closeFile()
38+
do {
39+
try self.close()
40+
} catch {
41+
return
4942
}
5043
}
5144
}

Sources/File/Path.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Logging
33
private let logger = Logger(label: "Path")
44

55
/// Manage the path of the PLFile.
6-
public struct Path {
6+
public struct Path: Sendable {
77
/// Root path.
88
public static let root = Path("/")
99

@@ -180,25 +180,27 @@ extension Path {
180180
/// A subscript that identifies the position of the path.
181181
public subscript(_ position: Int) -> Path {
182182
let component = pathComponent
183-
if position >= component.count || position < 0 {
183+
guard position >= 0 && position < component.count else {
184184
fatalError("Path index out of range")
185-
} else {
186-
var result = component.first!
187-
for index in 1..<position + 1 {
188-
result = Path(result.rawValue + component[index].rawValue)
189-
}
190-
return result
191185
}
186+
guard let first = component.first else {
187+
fatalError("Path component is empty")
188+
}
189+
var result = first
190+
for index in 1...position {
191+
result = Path(result.rawValue + component[index].rawValue)
192+
}
193+
return result
192194
}
193195
/// A subscript that identifies the bound out of the path.
194196
public subscript(_ bounds: Range<Int>) -> Path {
195197
let component = self.pathComponent
196-
if bounds.upperBound >= component.count || bounds.lowerBound < 0 {
198+
guard bounds.lowerBound >= 0 && bounds.upperBound <= component.count && bounds.lowerBound < bounds.upperBound else {
197199
fatalError("Path bounds out of range")
198200
}
199201
var result = component[bounds.lowerBound]
200-
for index in (bounds.lowerBound + 1)..<(bounds.upperBound) {
201-
result = Path(result.rawValue + component[index].rawValue)
202+
for index in (bounds.lowerBound + 1)..<bounds.upperBound {
203+
result = Path(result.rawValue + component[index].rawValue)
202204
}
203205
return result
204206
}

Sources/File/Store.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Dispatch
33
import Logging
44

55
/// A common part of the File and Folder functionality that allows you to set up the required paths and FileManager that the file system should use.
6-
public final class Store<fileSystem: FileSystem> {
6+
public final class Store<fileSystem: FileSystem>: @unchecked Sendable {
77
public var path: Path
88
private let fileManager: FileManager
99

@@ -233,7 +233,7 @@ public extension Store {
233233
/// - eventHandler: Called when a change is detected.
234234
/// - Returns: A DispatchSourceFileSystemObject? (macOS, iOS), or nil if not supported. Uses swift-log for logging.
235235
@discardableResult
236-
func watch(eventHandler: @escaping () -> Void) -> Any? {
236+
func watch(eventHandler: @escaping @Sendable () -> Void) -> Any? {
237237
#if os(macOS) || os(iOS)
238238
let fd = open(path.rawValue, O_EVTONLY)
239239
guard fd != -1 else { return nil }

0 commit comments

Comments
 (0)