@@ -12,83 +12,54 @@ import TSCBasic
12
12
import Dispatch
13
13
14
14
/// 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 {
16
16
public var supportedExtensions : Set < String > { [ " zip " ] }
17
17
18
18
/// The file-system implementation used for various file-system operations and checks.
19
19
private let fileSystem : FileSystem
20
20
21
- /// Helper for cancelling in-fligh requests
22
- private let cancellator : Cancellator
23
-
24
21
/// Creates a `ZipArchiver`.
25
22
///
26
23
/// - Parameters:
27
24
/// - fileSystem: The file-system to used by the `ZipArchiver`.
28
25
public init ( fileSystem: FileSystem ) {
29
26
self . fileSystem = fileSystem
30
- self . cancellator = Cancellator ( observabilityScope: . none)
31
27
}
32
28
33
29
public func extract(
34
30
from archivePath: AbsolutePath ,
35
31
to destinationPath: AbsolutePath ,
36
32
completion: @escaping ( Result < Void , Error > ) -> Void
37
33
) {
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
+ }
46
38
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
+ }
51
43
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
+ } )
64
50
}
65
51
}
66
52
67
53
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
88
57
}
89
- }
90
58
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
+ }
93
64
}
94
65
}
0 commit comments