[TAR] Remove compatibility with pre-Swift 5.2 compilers

This commit is contained in:
Timofey Solomko
2022-09-24 13:25:46 +03:00
parent b59d1eedd7
commit 52886a0bbf
3 changed files with 24 additions and 45 deletions
+15 -27
View File
@@ -146,45 +146,33 @@ public struct TarReader {
}
private func getOffset() throws -> UInt64 {
#if compiler(<5.2)
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
return try handle.offset()
} else {
return handle.offsetInFile
#else
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
return try handle.offset()
} else {
return handle.offsetInFile
}
#endif
}
}
private func set(offset: UInt64) throws {
#if compiler(<5.2)
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
try handle.seek(toOffset: offset)
} else {
handle.seek(toFileOffset: offset)
#else
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
try handle.seek(toOffset: offset)
} else {
handle.seek(toFileOffset: offset)
}
#endif
}
}
private func getData(size: Int) throws -> Data {
assert(size >= 0, "TarReader.getData(size:): negative size.")
guard size > 0
else { return Data() }
#if compiler(<5.2)
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
guard let chunkData = try handle.read(upToCount: size)
else { throw DataError.truncated }
return chunkData
} else {
// Technically, this can throw NSException, but since it is ObjC exception we cannot handle it in Swift.
return handle.readData(ofLength: size)
#else
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
guard let chunkData = try handle.read(upToCount: size)
else { throw DataError.truncated }
return chunkData
} else {
// Technically, this can throw NSException, but since it is ObjC exception we cannot handle it in Swift.
return handle.readData(ofLength: size)
}
#endif
}
}
}
+5 -10
View File
@@ -133,18 +133,13 @@ public struct TarWriter {
}
private func write(_ data: Data) throws {
#if compiler(<5.2)
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
try handle.write(contentsOf: data)
try handle.synchronize()
} else {
handle.write(data)
handle.synchronizeFile()
#else
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
try handle.write(contentsOf: data)
try handle.synchronize()
} else {
handle.write(data)
handle.synchronizeFile()
}
#endif
}
}
}
+4 -8
View File
@@ -8,15 +8,11 @@ import Foundation
extension FileHandle {
func closeCompat() throws {
#if compiler(<5.2)
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
try self.close()
} else {
self.closeFile()
#else
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
try self.close()
} else {
self.closeFile()
}
#endif
}
}
}