diff --git a/Sources/ZIPFoundation/Archive+BackingConfiguration.swift b/Sources/ZIPFoundation/Archive+BackingConfiguration.swift index dcd1a0e0..92d0922d 100644 --- a/Sources/ZIPFoundation/Archive+BackingConfiguration.swift +++ b/Sources/ZIPFoundation/Archive+BackingConfiguration.swift @@ -13,13 +13,13 @@ import Foundation extension Archive { struct BackingConfiguration { - let file: UnsafeMutablePointer + let file: FILEPointer let endOfCentralDirectoryRecord: EndOfCentralDirectoryRecord let zip64EndOfCentralDirectory: ZIP64EndOfCentralDirectory? #if swift(>=5.0) let memoryFile: MemoryFile? - init(file: UnsafeMutablePointer, + init(file: FILEPointer, endOfCentralDirectoryRecord: EndOfCentralDirectoryRecord, zip64EndOfCentralDirectory: ZIP64EndOfCentralDirectory? = nil, memoryFile: MemoryFile? = nil) { @@ -30,7 +30,7 @@ extension Archive { } #else - init(file: UnsafeMutablePointer, + init(file: FILEPointer, endOfCentralDirectoryRecord: EndOfCentralDirectoryRecord, zip64EndOfCentralDirectory: ZIP64EndOfCentralDirectory?) { self.file = file diff --git a/Sources/ZIPFoundation/Archive+MemoryFile.swift b/Sources/ZIPFoundation/Archive+MemoryFile.swift index 916eff63..69e77008 100644 --- a/Sources/ZIPFoundation/Archive+MemoryFile.swift +++ b/Sources/ZIPFoundation/Archive+MemoryFile.swift @@ -29,11 +29,11 @@ class MemoryFile { self.data = data } - func open(mode: String) -> UnsafeMutablePointer? { + func open(mode: String) -> FILEPointer? { let cookie = Unmanaged.passRetained(self) let writable = mode.count > 0 && (mode.first! != "r" || mode.last! == "+") let append = mode.count > 0 && mode.first! == "a" - #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) + #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(Android) let result = writable ? funopen(cookie.toOpaque(), readStub, writeStub, seekStub, closeStub) : funopen(cookie.toOpaque(), readStub, nil, seekStub, closeStub) @@ -99,7 +99,7 @@ private func closeStub(_ cookie: UnsafeMutableRawPointer?) -> Int32 { return 0 } -#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) +#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(Android) private func readStub(_ cookie: UnsafeMutableRawPointer?, _ bytePtr: UnsafeMutablePointer?, _ count: Int32) -> Int32 { diff --git a/Sources/ZIPFoundation/Archive+Reading.swift b/Sources/ZIPFoundation/Archive+Reading.swift index cfdf3f5e..96bb3273 100644 --- a/Sources/ZIPFoundation/Archive+Reading.swift +++ b/Sources/ZIPFoundation/Archive+Reading.swift @@ -35,7 +35,7 @@ extension Archive { } try fileManager.createParentDirectoryStructure(for: url) let destinationRepresentation = fileManager.fileSystemRepresentation(withPath: url.path) - guard let destinationFile: UnsafeMutablePointer = fopen(destinationRepresentation, "wb+") else { + guard let destinationFile: FILEPointer = fopen(destinationRepresentation, "wb+") else { throw CocoaError(.fileNoSuchFile) } defer { fclose(destinationFile) } diff --git a/Sources/ZIPFoundation/Archive+Writing.swift b/Sources/ZIPFoundation/Archive+Writing.swift index 4c9224e0..3dcb1ef5 100644 --- a/Sources/ZIPFoundation/Archive+Writing.swift +++ b/Sources/ZIPFoundation/Archive+Writing.swift @@ -66,7 +66,7 @@ extension Archive { switch type { case .file: let entryFileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path) - guard let entryFile: UnsafeMutablePointer = fopen(entryFileSystemRepresentation, "rb") else { + guard let entryFile: FILEPointer = fopen(entryFileSystemRepresentation, "rb") else { throw CocoaError(.fileNoSuchFile) } defer { fclose(entryFile) } diff --git a/Sources/ZIPFoundation/Archive.swift b/Sources/ZIPFoundation/Archive.swift index f82af1fc..279f4693 100644 --- a/Sources/ZIPFoundation/Archive.swift +++ b/Sources/ZIPFoundation/Archive.swift @@ -126,7 +126,7 @@ public final class Archive: Sequence { public let url: URL /// Access mode for an archive file. public let accessMode: AccessMode - var archiveFile: UnsafeMutablePointer + var archiveFile: FILEPointer var endOfCentralDirectoryRecord: EndOfCentralDirectoryRecord var zip64EndOfCentralDirectory: ZIP64EndOfCentralDirectory? var preferredEncoding: String.Encoding? @@ -267,7 +267,7 @@ public final class Archive: Sequence { // MARK: - Helpers - static func scanForEndOfCentralDirectoryRecord(in file: UnsafeMutablePointer) + static func scanForEndOfCentralDirectoryRecord(in file: FILEPointer) -> EndOfCentralDirectoryStructure? { var eocdOffset: UInt64 = 0 var index = minEndOfCentralDirectoryOffset @@ -290,7 +290,7 @@ public final class Archive: Sequence { return nil } - private static func scanForZIP64EndOfCentralDirectory(in file: UnsafeMutablePointer, eocdOffset: UInt64) + private static func scanForZIP64EndOfCentralDirectory(in file: FILEPointer, eocdOffset: UInt64) -> ZIP64EndOfCentralDirectory? { guard UInt64(ZIP64EndOfCentralDirectoryLocator.size) < eocdOffset else { return nil diff --git a/Sources/ZIPFoundation/Data+Serialization.swift b/Sources/ZIPFoundation/Data+Serialization.swift index 663f3f9b..07915bcd 100644 --- a/Sources/ZIPFoundation/Data+Serialization.swift +++ b/Sources/ZIPFoundation/Data+Serialization.swift @@ -10,6 +10,12 @@ import Foundation +#if os(Android) +public typealias FILEPointer = OpaquePointer +#else +public typealias FILEPointer = UnsafeMutablePointer +#endif + protocol DataSerializable { static var size: Int { get } init?(data: Data, additionalDataProvider: (Int) throws -> Data) @@ -31,7 +37,7 @@ extension Data { #endif } - static func readStruct(from file: UnsafeMutablePointer, at offset: UInt64) + static func readStruct(from file: FILEPointer, at offset: UInt64) -> T? where T: DataSerializable { guard offset <= .max else { return nil } fseeko(file, off_t(offset), SEEK_SET) @@ -68,7 +74,7 @@ extension Data { return checksum } - static func readChunk(of size: Int, from file: UnsafeMutablePointer) throws -> Data { + static func readChunk(of size: Int, from file: FILEPointer) throws -> Data { let alignment = MemoryLayout.alignment #if swift(>=4.1) let bytes = UnsafeMutableRawPointer.allocate(byteCount: size, alignment: alignment) @@ -88,7 +94,7 @@ extension Data { #endif } - static func write(chunk: Data, to file: UnsafeMutablePointer) throws -> Int { + static func write(chunk: Data, to file: FILEPointer) throws -> Int { var sizeWritten: Int = 0 chunk.withUnsafeBytes { (rawBufferPointer) in if let baseAddress = rawBufferPointer.baseAddress, rawBufferPointer.count > 0 { @@ -104,7 +110,7 @@ extension Data { } static func writeLargeChunk(_ chunk: Data, size: UInt64, bufferSize: Int, - to file: UnsafeMutablePointer) throws -> UInt64 { + to file: FILEPointer) throws -> UInt64 { var sizeWritten: UInt64 = 0 chunk.withUnsafeBytes { (rawBufferPointer) in if let baseAddress = rawBufferPointer.baseAddress, rawBufferPointer.count > 0 { diff --git a/Sources/ZIPFoundation/FileManager+ZIP.swift b/Sources/ZIPFoundation/FileManager+ZIP.swift index 8259c5e8..b14d22d3 100644 --- a/Sources/ZIPFoundation/FileManager+ZIP.swift +++ b/Sources/ZIPFoundation/FileManager+ZIP.swift @@ -255,7 +255,7 @@ extension FileManager { let entryFileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: url.path) var fileStat = stat() lstat(entryFileSystemRepresentation, &fileStat) - return Entry.EntryType(mode: fileStat.st_mode) + return Entry.EntryType(mode: mode_t(fileStat.st_mode)) } } diff --git a/Tests/ZIPFoundationTests/ZIPFoundationDataSerializationTests.swift b/Tests/ZIPFoundationTests/ZIPFoundationDataSerializationTests.swift index aaaba34e..ff9ec4af 100644 --- a/Tests/ZIPFoundationTests/ZIPFoundationDataSerializationTests.swift +++ b/Tests/ZIPFoundationTests/ZIPFoundationDataSerializationTests.swift @@ -21,7 +21,7 @@ extension ZIPFoundationTests { attributes: nil) XCTAssert(result == true) let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path) - let file: UnsafeMutablePointer = fopen(fileSystemRepresentation, "rb") + let file: FILEPointer = fopen(fileSystemRepresentation, "rb") // Close the file to exercise the error path during readStructure that deals with // unreadable file data. fclose(file) @@ -38,7 +38,7 @@ extension ZIPFoundationTests { attributes: nil) XCTAssert(result == true) let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path) - let file: UnsafeMutablePointer = fopen(fileSystemRepresentation, "rb") + let file: FILEPointer = fopen(fileSystemRepresentation, "rb") // Close the file to exercise the error path during readChunk that deals with // unreadable file data. fclose(file) @@ -60,7 +60,7 @@ extension ZIPFoundationTests { attributes: nil) XCTAssert(result == true) let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path) - let file: UnsafeMutablePointer = fopen(fileSystemRepresentation, "rb") + let file: FILEPointer = fopen(fileSystemRepresentation, "rb") // Close the file to exercise the error path during writeChunk that deals with // unwritable files. fclose(file) @@ -92,7 +92,7 @@ extension ZIPFoundationTests { attributes: nil) XCTAssert(result == true) let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path) - let file: UnsafeMutablePointer = fopen(fileSystemRepresentation, "rb+") + let file: FILEPointer = fopen(fileSystemRepresentation, "rb+") let data = Data.makeRandomData(size: 1024) do { let writtenSize = try Data.writeLargeChunk(data, size: 1024, bufferSize: 256, to: file) @@ -114,7 +114,7 @@ extension ZIPFoundationTests { attributes: nil) XCTAssert(result == true) let fileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: fileURL.path) - let file: UnsafeMutablePointer = fopen(fileSystemRepresentation, "rb") + let file: FILEPointer = fopen(fileSystemRepresentation, "rb") let data = Data.makeRandomData(size: 1024) // Close the file to exercise the error path during writeChunk that deals with // unwritable files. diff --git a/Tests/ZIPFoundationTests/ZIPFoundationReadingTests.swift b/Tests/ZIPFoundationTests/ZIPFoundationReadingTests.swift index f720219d..256b8ed8 100644 --- a/Tests/ZIPFoundationTests/ZIPFoundationReadingTests.swift +++ b/Tests/ZIPFoundationTests/ZIPFoundationReadingTests.swift @@ -154,7 +154,7 @@ extension ZIPFoundationTests { let archiveURL = self.resourceURL(for: #function, pathExtension: "zip") let fileManager = FileManager() let destinationFileSystemRepresentation = fileManager.fileSystemRepresentation(withPath: archiveURL.path) - let destinationFile: UnsafeMutablePointer = fopen(destinationFileSystemRepresentation, "r+b") + let destinationFile: FILEPointer = fopen(destinationFileSystemRepresentation, "r+b") do { fseek(destinationFile, 64, SEEK_SET) diff --git a/Tests/ZIPFoundationTests/ZIPFoundationTests.swift b/Tests/ZIPFoundationTests/ZIPFoundationTests.swift index 2bf0684a..9c6c9975 100644 --- a/Tests/ZIPFoundationTests/ZIPFoundationTests.swift +++ b/Tests/ZIPFoundationTests/ZIPFoundationTests.swift @@ -137,7 +137,7 @@ class ZIPFoundationTests: XCTestCase { } func runWithFileDescriptorLimit(_ limit: UInt64, handler: () -> Void) { - #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) + #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(Android) let fileNoFlag = RLIMIT_NOFILE #else let fileNoFlag = Int32(RLIMIT_NOFILE.rawValue)