buh / ZipPinch

Remote access to files inside a ZIP file for macOS, iOS and watchOS.
MIT License
10 stars 4 forks source link

Cocoa error 5377 when trying to download Entry #3

Open ninxsoft opened 4 months ago

ninxsoft commented 4 months ago

I seem to be hitting a The operation couldn’t be completed. (Cocoa error 5377.) error when attempting to download a ~13MB entry from within a remote ZIP archive approximately ~12-13GB in size.

Here is the stripped down version of the code I am running:

//  test.swift

import Foundation
import ZipPinch

enum TestError: Error, CustomStringConvertible {
    case generalError(String)

    var description: String {
        switch self {
        case let .generalError(string):
            return string
        }
    }
}

func fetchFile(_ filePath: String, from url: String) async throws -> Data {
    guard let url: URL = .init(string: url) else {
        throw TestError.generalError("Invalid URL '\(url)'")
    }

    let request: URLRequest = .init(url: url)
    let entries: [ZIPEntry] = try await URLSession.shared.zipEntries(for: request)

    guard let entry: ZIPEntry = entries.first(where: { $0.filePath == filePath }) else {
        throw TestError.generalError("'\(filePath)' not found in '\(url)'")
    }

    return try await URLSession.shared.zipEntryData(entry, for: request)
}

let filePath: String = "BuildManifest.plist"
let url: String = "https://updates.cdn-apple.com/2024SpringFCS/fullrestores/062-01897/C874907B-9F82-4109-87EB-6B3C9BF1507D/UniversalMac_14.5_23F79_Restore.ipsw"

do {
    let buildManifest: Data = try await fetchFile(filePath, from: url)
    // do things with buildManifest...
} catch {
    if let error: TestError = error as? TestError {
        print(error)
    } else {
        print(error.localizedDescription)
    }
}

When I try a different file, eg. let filePath: String = "Restore.plist", the app crashes with the following stacktrace:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** NSAllocateMemoryPages(18446744073709488500) failed'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000019f6baccc __exceptionPreprocess + 176
    1   libobjc.A.dylib                     0x000000019f1a2788 objc_exception_throw + 60
    2   Foundation                          0x00000001a07b7200 NSAllocateMemoryPages + 184
    3   Foundation                          0x00000001a0724b64 -[_NSPlaceholderData initWithBytes:length:copy:deallocator:] + 132
    4   Test                                0x000000010000f634 $sSo6NSDataC5bytes6lengthABSVSg_SitcfcTO + 32
    5   Test                                0x000000010000f604 $sSo6NSDataC5bytes6lengthABSVSg_SitcfC + 52
    6   Test                                0x000000010000cd38 $sSo12NSURLSessionC8ZipPinchE12zipEntryData_3for8delegate8progress10Foundation0F0VAC8ZIPEntryV_AH10URLRequestVSo0A12TaskDelegate_pSgAC11ZIPProgressVSgtYaKFTY4_ + 2092
    7   Test                                0x0000000100004e61 $s4Test9fetchFile_4from10Foundation4DataVSS_SStYaKFTQ3_ + 1
    8   Test                                0x0000000100003c99 async_MainTQ0_ + 1
    9   Test                                0x0000000100004179 $sIetH_yts5Error_pIegHrzo_TRTQ0_ + 1
    10  Test                                0x00000001000042a1 $sIetH_yts5Error_pIegHrzo_TRTATQ0_ + 1
    11  libswift_Concurrency.dylib          0x0000000265c5b0f9 _ZL23completeTaskWithClosurePN5swift12AsyncContextEPNS_10SwiftErrorE + 1
)
libc++abi: terminating due to uncaught exception of type NSException

I am running Swift 5.10:

swift --version              
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Please let me know if I'm holding it wrong, and thank you for your work on this project 🙌

buh commented 4 months ago

Thanks for reporting @ninxsoft! I'll take a look and let you know.

buh commented 4 months ago

If you put the URL in the demo app, you can find that at least it could read the content of the file:

and the decoding failed for some files: image

Most likely due to a miscalculation of the data location. I'll investigate more.

buh commented 4 months ago

Ok, something wrong in the meta data of the file. Sometimes it's calculating the wrong Data length, e.g. -64007.

buh commented 4 months ago

I found the problem. I'll make the patch soon.