@@ -80,14 +80,11 @@ func createIpa(bundleId: String) -> Int {
8080 // Do copy
8181 try fileMgr. copyItem ( at: srcURL, to: dstURL)
8282
83- // Replace a original binary file with a dumped one
8483 let appResourceDir = ( AppUtils . sharedInstance ( ) . searchAppResourceDir ( bundleId) ! as NSString ) . lastPathComponent
8584 let fileToReplace = workPath. appendingPathComponent ( " Payload " ) . path + " / \( appResourceDir) / \( bundleExecutable) "
8685 let replacementFile = " \( documentsPath) \( bundleExecutable) .decrypted "
87- try fileMgr. removeItem ( atPath: fileToReplace)
88- try fileMgr. copyItem ( atPath: replacementFile, toPath: fileToReplace)
8986
90- // Fakesigning with ldid
87+ // Extract original entitlements from the original binary file with ldid
9188 var command = " /usr/bin/ldid "
9289 if isRootless ( ) {
9390 command = " /var/jb " + command
@@ -96,6 +93,12 @@ func createIpa(bundleId: String) -> Int {
9693 let entitlementsPath = " \( workPath) /ent.xml "
9794 let data = out. data ( using: . utf8)
9895 fileMgr. createFile ( atPath: entitlementsPath, contents: data)
96+
97+ // Replace the original binary file with a dumped one
98+ try fileMgr. removeItem ( atPath: fileToReplace)
99+ try fileMgr. copyItem ( atPath: replacementFile, toPath: fileToReplace)
100+
101+ // Fakesigning with ldid
99102 let _ = task ( launchPath: command, arguments: " -S \( entitlementsPath) " , " \( fileToReplace) " )
100103
101104 // Remove files in the Payload dir except for .app dir
@@ -191,9 +194,14 @@ func backup(arguments: [String], bundleId: String) -> Void {
191194 let decryptedFilePath = appDocumentsPath + decryptedFile
192195 let srcURL = URL ( fileURLWithPath: decryptedFilePath)
193196 let dstURL = URL ( fileURLWithPath: documentsPath + decryptedFile)
197+
194198 do {
195- try FileManager . default. copyItem ( at: srcURL, to: dstURL)
196- try FileManager . default. removeItem ( at: srcURL)
199+ let fileMgr = FileManager . default
200+ if fileMgr. fileExists ( atPath: dstURL. path) {
201+ try fileMgr. removeItem ( at: dstURL)
202+ }
203+ try fileMgr. copyItem ( at: srcURL, to: dstURL)
204+ try fileMgr. removeItem ( at: srcURL)
197205 }
198206 catch {
199207 print ( " Error: \( error. localizedDescription) " )
0 commit comments