|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014-2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import struct TSCBasic.AbsolutePath |
| 14 | +import protocol TSCBasic.FileSystem |
| 15 | + |
| 16 | +/// An `Archiver` that handles multiple formats by delegating to other existing archivers each dedicated to its own |
| 17 | +/// format. |
| 18 | +public struct UniversalArchiver: Archiver { |
| 19 | + public var supportedExtensions: Set<String> |
| 20 | + |
| 21 | + /// Errors specific to the implementation of ``UniversalArchiver``. |
| 22 | + enum Error: Swift.Error { |
| 23 | + case unknownFormat(String, AbsolutePath) |
| 24 | + case noFileNameExtension(AbsolutePath) |
| 25 | + |
| 26 | + var description: String { |
| 27 | + switch self { |
| 28 | + case .unknownFormat(let ext, let path): |
| 29 | + return "unknown format with extension \(ext) at path `\(path)`" |
| 30 | + case .noFileNameExtension(let path): |
| 31 | + return "file at path `\(path)` has no extension to detect archival format from" |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// A dictionary that maps file extension strings to archiver instances that supports these extensions. |
| 37 | + private let formatMapping: [String: any Archiver] |
| 38 | + |
| 39 | + public init(_ fileSystem: any FileSystem, _ cancellator: Cancellator? = nil) { |
| 40 | + var formatMapping = [String: any Archiver]() |
| 41 | + var supportedExtensions = Set<String>() |
| 42 | + |
| 43 | + for archiver in [ |
| 44 | + ZipArchiver(fileSystem: fileSystem, cancellator: cancellator), |
| 45 | + TarArchiver(fileSystem: fileSystem, cancellator: cancellator), |
| 46 | + ] as [any Archiver] { |
| 47 | + supportedExtensions.formUnion(archiver.supportedExtensions) |
| 48 | + for ext in archiver.supportedExtensions { |
| 49 | + formatMapping[ext] = archiver |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + self.formatMapping = formatMapping |
| 54 | + self.supportedExtensions = supportedExtensions |
| 55 | + } |
| 56 | + |
| 57 | + private func archiver(for archivePath: AbsolutePath) throws -> any Archiver { |
| 58 | + guard let extensions = archivePath.allExtensions else { |
| 59 | + throw Error.noFileNameExtension(archivePath) |
| 60 | + } |
| 61 | + |
| 62 | + guard let archiver = self.formatMapping[extensions] else { |
| 63 | + throw Error.unknownFormat(extensions, archivePath) |
| 64 | + } |
| 65 | + |
| 66 | + return archiver |
| 67 | + } |
| 68 | + |
| 69 | + public func extract( |
| 70 | + from archivePath: AbsolutePath, |
| 71 | + to destinationPath: AbsolutePath, |
| 72 | + completion: @escaping (Result<Void, Swift.Error>) -> Void |
| 73 | + ) { |
| 74 | + do { |
| 75 | + let archiver = try archiver(for: archivePath) |
| 76 | + archiver.extract(from: archivePath, to: destinationPath, completion: completion) |
| 77 | + } catch { |
| 78 | + completion(.failure(error)) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public func compress( |
| 83 | + directory: AbsolutePath, |
| 84 | + to destinationPath: AbsolutePath, |
| 85 | + completion: @escaping (Result<Void, Swift.Error>) -> Void |
| 86 | + ) { |
| 87 | + do { |
| 88 | + let archiver = try archiver(for: destinationPath) |
| 89 | + archiver.compress(directory: directory, to: destinationPath, completion: completion) |
| 90 | + } catch { |
| 91 | + completion(.failure(error)) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public func validate( |
| 96 | + path: AbsolutePath, |
| 97 | + completion: @escaping (Result<Bool, Swift.Error>) -> Void |
| 98 | + ) { |
| 99 | + do { |
| 100 | + let archiver = try archiver(for: path) |
| 101 | + archiver.validate(path: path, completion: completion) |
| 102 | + } catch { |
| 103 | + completion(.failure(error)) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments