Skip to content

Commit c948a86

Browse files
committed
Revert "cancellation handler (#4173)"
This reverts commit a1c8482.
1 parent a1c8482 commit c948a86

31 files changed

+117
-1012
lines changed

Sources/Basics/Archiver+Zip.swift

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,83 +12,54 @@ import TSCBasic
1212
import Dispatch
1313

1414
/// An `Archiver` that handles ZIP archives using the command-line `zip` and `unzip` tools.
15-
public struct ZipArchiver: Archiver, Cancellable {
15+
public struct ZipArchiver: Archiver {
1616
public var supportedExtensions: Set<String> { ["zip"] }
1717

1818
/// The file-system implementation used for various file-system operations and checks.
1919
private let fileSystem: FileSystem
2020

21-
/// Helper for cancelling in-fligh requests
22-
private let cancellator: Cancellator
23-
2421
/// Creates a `ZipArchiver`.
2522
///
2623
/// - Parameters:
2724
/// - fileSystem: The file-system to used by the `ZipArchiver`.
2825
public init(fileSystem: FileSystem) {
2926
self.fileSystem = fileSystem
30-
self.cancellator = Cancellator(observabilityScope: .none)
3127
}
3228

3329
public func extract(
3430
from archivePath: AbsolutePath,
3531
to destinationPath: AbsolutePath,
3632
completion: @escaping (Result<Void, Error>) -> Void
3733
) {
38-
do {
39-
guard self.fileSystem.exists(archivePath) else {
40-
throw FileSystemError(.noEntry, archivePath)
41-
}
42-
43-
guard self.fileSystem.isDirectory(destinationPath) else {
44-
throw FileSystemError(.notDirectory, destinationPath)
45-
}
34+
guard fileSystem.exists(archivePath) else {
35+
completion(.failure(FileSystemError(.noEntry, archivePath)))
36+
return
37+
}
4638

47-
let process = Process(arguments: ["unzip", archivePath.pathString, "-d", destinationPath.pathString])
48-
guard let registrationKey = self.cancellator.register(process) else {
49-
throw StringError("cancellation")
50-
}
39+
guard fileSystem.isDirectory(destinationPath) else {
40+
completion(.failure(FileSystemError(.notDirectory, destinationPath)))
41+
return
42+
}
5143

52-
DispatchQueue.sharedConcurrent.async {
53-
defer { self.cancellator.deregister(registrationKey) }
54-
completion(.init(catching: {
55-
try process.launch()
56-
let processResult = try process.waitUntilExit()
57-
guard processResult.exitStatus == .terminated(code: 0) else {
58-
throw try StringError(processResult.utf8stderrOutput())
59-
}
60-
}))
61-
}
62-
} catch {
63-
return completion(.failure(error))
44+
Process.popen(arguments: ["unzip", archivePath.pathString, "-d", destinationPath.pathString], queue: .sharedConcurrent) { result in
45+
completion(result.tryMap { processResult in
46+
guard processResult.exitStatus == .terminated(code: 0) else {
47+
throw try StringError(processResult.utf8stderrOutput())
48+
}
49+
})
6450
}
6551
}
6652

6753
public func validate(path: AbsolutePath, completion: @escaping (Result<Bool, Error>) -> Void) {
68-
do {
69-
guard self.fileSystem.exists(path) else {
70-
throw FileSystemError(.noEntry, path)
71-
}
72-
73-
let process = Process(arguments: ["unzip", "-t", path.pathString])
74-
guard let registrationKey = self.cancellator.register(process) else {
75-
throw StringError("cancellation")
76-
}
77-
78-
DispatchQueue.sharedConcurrent.async {
79-
defer { self.cancellator.deregister(registrationKey) }
80-
completion(.init(catching: {
81-
try process.launch()
82-
let processResult = try process.waitUntilExit()
83-
return processResult.exitStatus == .terminated(code: 0)
84-
}))
85-
}
86-
} catch {
87-
return completion(.failure(error))
54+
guard fileSystem.exists(path) else {
55+
completion(.failure(FileSystemError(.noEntry, path)))
56+
return
8857
}
89-
}
9058

91-
public func cancel(deadline: DispatchTime) throws {
92-
try self.cancellator.cancel(deadline: deadline)
59+
Process.popen(arguments: ["unzip", "-t", path.pathString], queue: .sharedConcurrent) { result in
60+
completion(result.tryMap { processResult in
61+
return processResult.exitStatus == .terminated(code: 0)
62+
})
63+
}
9364
}
9465
}

Sources/Basics/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ add_library(Basics
1111
Archiver+Zip.swift
1212
AuthorizationProvider.swift
1313
ByteString+Extensions.swift
14-
Cancellator.swift
1514
ConcurrencyHelpers.swift
1615
Dictionary+Extensions.swift
1716
DispatchTimeInterval+Extensions.swift

Sources/Basics/Cancellator.swift

Lines changed: 0 additions & 135 deletions
This file was deleted.

Sources/Basics/ConcurrencyHelpers.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,9 @@ public final class ThreadSafeKeyValueStore<Key, Value> where Key: Hashable {
5555
}
5656
}
5757

58-
@discardableResult
59-
public func clear() -> [Key: Value] {
58+
public func clear() {
6059
self.lock.withLock {
61-
let underlying = self.underlying
6260
self.underlying.removeAll()
63-
return underlying
6461
}
6562
}
6663

@@ -116,12 +113,9 @@ public final class ThreadSafeArrayStore<Value> {
116113
}
117114
}
118115

119-
@discardableResult
120-
public func clear() -> [Value] {
116+
public func clear() {
121117
self.lock.withLock {
122-
let underlying = self.underlying
123-
self.underlying.removeAll()
124-
return underlying
118+
self.underlying = []
125119
}
126120
}
127121

Sources/Basics/DispatchTimeInterval+Extensions.swift

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,6 @@ extension DispatchTimeInterval {
2727
}
2828
}
2929

30-
public func nanoseconds() -> Int? {
31-
switch self {
32-
case .seconds(let value):
33-
return value.multipliedReportingOverflow(by: 1_000_000_000).partialValue
34-
case .milliseconds(let value):
35-
return value.multipliedReportingOverflow(by: 1_000_000).partialValue
36-
case .microseconds(let value):
37-
return value.multipliedReportingOverflow(by: 1000).partialValue
38-
case .nanoseconds(let value):
39-
return value
40-
default:
41-
return nil
42-
}
43-
}
44-
4530
public func milliseconds() -> Int? {
4631
switch self {
4732
case .seconds(let value):
@@ -96,7 +81,7 @@ extension DispatchTimeInterval {
9681
#if os(Linux) || os(Windows) || os(Android) || os(OpenBSD)
9782
extension DispatchTime {
9883
public func distance(to: DispatchTime) -> DispatchTimeInterval {
99-
let duration = to.uptimeNanoseconds.subtractingReportingOverflow(self.uptimeNanoseconds).partialValue
84+
let duration = to.uptimeNanoseconds - self.uptimeNanoseconds
10085
return .nanoseconds(duration >= Int.max ? Int.max : Int(duration))
10186
}
10287
}

0 commit comments

Comments
 (0)