|
| 1 | +import CLibArchive |
| 2 | +import Foundation |
| 3 | + |
| 4 | +// The code in this file consists mainly of a Swift port of the "Complete Extractor" example included in the libarchive |
| 5 | +// documentation: https://github.com/libarchive/libarchive/wiki/Examples#a-complete-extractor |
| 6 | + |
| 7 | +struct ExtractError: Error { |
| 8 | + let message: String? |
| 9 | + |
| 10 | + init(archive: OpaquePointer?) { |
| 11 | + self.message = archive_error_string(archive).map { err in |
| 12 | + String(cString: err) |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + init(message: String) { |
| 17 | + self.message = message |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/// Write the data from the given readArchive into the writeArchive. |
| 22 | +func copyData(readArchive: OpaquePointer?, writeArchive: OpaquePointer?) throws { |
| 23 | + var r = 0 |
| 24 | + var buff: UnsafeRawPointer? |
| 25 | + var size = 0 |
| 26 | + var offset: Int64 = 0 |
| 27 | + |
| 28 | + while true { |
| 29 | + r = Int(archive_read_data_block(readArchive, &buff, &size, &offset)) |
| 30 | + if r == ARCHIVE_EOF { |
| 31 | + return |
| 32 | + } |
| 33 | + guard r == ARCHIVE_OK else { |
| 34 | + throw ExtractError(archive: readArchive) |
| 35 | + } |
| 36 | + r = Int(archive_write_data_block(writeArchive, buff, size, offset)) |
| 37 | + guard r == ARCHIVE_OK else { |
| 38 | + throw ExtractError(archive: writeArchive) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// Extract the archive at the provided path. The name of each file included in the archive will be passed to |
| 44 | +/// the provided closure which will return the path the file will be written to. |
| 45 | +/// |
| 46 | +/// This uses libarchive under the hood, so a wide variety of archive formats are supported (e.g. .tar.gz). |
| 47 | +internal func extractArchive(atPath archivePath: URL, transform: (String) -> URL) throws { |
| 48 | + var flags = Int32(0) |
| 49 | + flags = ARCHIVE_EXTRACT_TIME |
| 50 | + flags |= ARCHIVE_EXTRACT_PERM |
| 51 | + flags |= ARCHIVE_EXTRACT_ACL |
| 52 | + flags |= ARCHIVE_EXTRACT_FFLAGS |
| 53 | + |
| 54 | + let a = archive_read_new() |
| 55 | + archive_read_support_format_all(a) |
| 56 | + archive_read_support_filter_all(a) |
| 57 | + |
| 58 | + let ext = archive_write_disk_new() |
| 59 | + archive_write_disk_set_options(ext, flags) |
| 60 | + archive_write_disk_set_standard_lookup(ext) |
| 61 | + |
| 62 | + defer { |
| 63 | + archive_read_close(a) |
| 64 | + archive_read_free(a) |
| 65 | + archive_write_close(ext) |
| 66 | + archive_write_free(ext) |
| 67 | + } |
| 68 | + |
| 69 | + if archive_read_open_filename(a, archivePath.path, 10240) != 0 { |
| 70 | + throw ExtractError(message: "Failed to open \"\(archivePath.path)\"") |
| 71 | + } |
| 72 | + |
| 73 | + while true { |
| 74 | + var r = Int32(0) |
| 75 | + var entry: OpaquePointer? |
| 76 | + r = archive_read_next_header(a, &entry) |
| 77 | + if r == ARCHIVE_EOF { |
| 78 | + break |
| 79 | + } |
| 80 | + guard r == ARCHIVE_OK else { |
| 81 | + throw ExtractError(archive: a) |
| 82 | + } |
| 83 | + |
| 84 | + let currentPath = String(cString: archive_entry_pathname(entry)) |
| 85 | + archive_entry_set_pathname(entry, transform(currentPath).path) |
| 86 | + r = archive_write_header(ext, entry) |
| 87 | + guard r == ARCHIVE_OK else { |
| 88 | + throw ExtractError(archive: ext) |
| 89 | + } |
| 90 | + |
| 91 | + if archive_entry_size(entry) > 0 { |
| 92 | + try copyData(readArchive: a, writeArchive: ext) |
| 93 | + } |
| 94 | + |
| 95 | + r = archive_write_finish_entry(ext) |
| 96 | + guard r == ARCHIVE_OK else { |
| 97 | + throw ExtractError(archive: ext) |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments