Skip to content

Commit 6771e40

Browse files
committed
fix: failed to eject when inject fails
Signed-off-by: Lessica <82flex@gmail.com>
1 parent 876efb9 commit 6771e40

11 files changed

+73
-22
lines changed

TrollFools/Execute.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ enum Execute {
2020
let receipt = AuxiliaryExecute.spawn(
2121
command: binary,
2222
args: arguments,
23-
environment: environment,
23+
environment: environment.merging([
24+
"DISABLE_TWEAKS": "1",
25+
], uniquingKeysWith: { $1 }),
2426
personaOptions: .init(uid: 0, gid: 0),
2527
ddlog: ddlog
2628
)
@@ -42,7 +44,9 @@ enum Execute {
4244
let receipt = AuxiliaryExecute.spawn(
4345
command: binary,
4446
args: arguments,
45-
environment: environment,
47+
environment: environment.merging([
48+
"DISABLE_TWEAKS": "1",
49+
], uniquingKeysWith: { $1 }),
4650
personaOptions: .init(uid: 0, gid: 0),
4751
ddlog: ddlog
4852
)

TrollFools/InjectorV3+Bundle.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ extension InjectorV3 {
178178
try cmdChangeOwnerToInstalld(markerURL, recursively: false)
179179

180180
try filteredURLs.forEach {
181-
try cmdCopy(from: markerURL, to: $0.appendingPathComponent(Self.injectedMarkerName), overwrite: true)
181+
try cmdCopy(
182+
from: markerURL,
183+
to: $0.appendingPathComponent(Self.injectedMarkerName),
184+
clone: true,
185+
overwrite: true
186+
)
182187
}
183188
} else {
184189
try filteredURLs.forEach {
@@ -286,4 +291,9 @@ extension InjectorV3 {
286291
let pathExt = target.pathExtension.lowercased()
287292
return isPackage || (isDirectory && (pathExt == "app" || pathExt == "bundle" || pathExt == "framework"))
288293
}
294+
295+
func checkIsDirectory(_ target: URL) -> Bool {
296+
let values = try? target.resourceValues(forKeys: [.isDirectoryKey])
297+
return values?.isDirectory ?? false
298+
}
289299
}

TrollFools/InjectorV3+Command.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ extension InjectorV3 {
4444
}
4545
}()
4646

47-
func cmdCopy(from srcURL: URL, to destURL: URL, overwrite: Bool = false) throws {
47+
func cmdCopy(from srcURL: URL, to destURL: URL, clone: Bool = false, overwrite: Bool = false) throws {
4848
if overwrite {
4949
try? cmdRemove(destURL, recursively: true)
5050
}
51-
let retCode = try Execute.rootSpawn(binary: Self.cpBinaryURL.path, arguments: [
52-
"--reflink=auto", "-rfp", srcURL.path, destURL.path,
53-
], ddlog: logger)
51+
var args = [String]()
52+
if clone {
53+
args.append("--reflink=auto")
54+
}
55+
args += ["-rfp", srcURL.path, destURL.path]
56+
let retCode = try Execute.rootSpawn(binary: Self.cpBinaryURL.path, arguments: args, ddlog: logger)
5457
guard case .exit(let code) = retCode, code == EXIT_SUCCESS else {
5558
try throwCommandFailure("cp", reason: retCode)
5659
}
@@ -112,7 +115,7 @@ extension InjectorV3 {
112115
"-e", target.path,
113116
], ddlog: logger)
114117

115-
guard case .exit(let code) = receipt.terminationReason, code == 0 else {
118+
guard case .exit(let code) = receipt.terminationReason, code == EXIT_SUCCESS else {
116119
try throwCommandFailure("ldid", reason: receipt.terminationReason)
117120
}
118121

TrollFools/InjectorV3+Eject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension InjectorV3 {
5959
try targetURLs.forEach {
6060
try removeLoadCommandOfAsset(assetURL, from: $0)
6161
}
62-
try? cmdRemove(assetURL, recursively: checkIsBundle(assetURL))
62+
try? cmdRemove(assetURL, recursively: checkIsDirectory(assetURL))
6363
}
6464

6565
try targetURLs.forEach {

TrollFools/InjectorV3+Inject.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension InjectorV3 {
5050
for assetURL in assetURLs {
5151
let targetURL = bundleURL.appendingPathComponent(assetURL.lastPathComponent)
5252

53-
try cmdCopy(from: assetURL, to: targetURL, overwrite: true)
53+
try cmdCopy(from: assetURL, to: targetURL, clone: true, overwrite: true)
5454
try cmdChangeOwnerToInstalld(targetURL, recursively: true)
5555
}
5656
}
@@ -74,15 +74,17 @@ extension InjectorV3 {
7474

7575
DDLogInfo("Best matched Mach-O is \(targetMachO.path)", ddlog: logger)
7676

77+
let resourceURLs: [URL] = [substrateFwkURL] + assetURLs
7778
try makeAlternate(targetMachO)
7879
do {
79-
try copyfiles([substrateFwkURL] + assetURLs)
80+
try copyfiles(resourceURLs)
8081
for assetURL in assetURLs {
8182
try insertLoadCommandOfAsset(assetURL, to: targetMachO)
8283
}
8384
try applyCoreTrustBypass(targetMachO)
8485
} catch {
8586
try? restoreAlternate(targetMachO)
87+
try? batchRemove(resourceURLs)
8688
throw error
8789
}
8890
}
@@ -179,18 +181,21 @@ extension InjectorV3 {
179181

180182
// MARK: - Path Clone
181183

182-
@discardableResult
183-
fileprivate func copyfiles(_ assetURLs: [URL]) throws -> [URL] {
184+
fileprivate func copyfiles(_ assetURLs: [URL]) throws {
184185
let targetURLs = assetURLs.map {
185186
frameworksDirectoryURL.appendingPathComponent($0.lastPathComponent)
186187
}
187188

188189
for (assetURL, targetURL) in zip(assetURLs, targetURLs) {
189-
try cmdCopy(from: assetURL, to: targetURL, overwrite: true)
190-
try cmdChangeOwnerToInstalld(targetURL, recursively: checkIsBundle(assetURL))
190+
try cmdCopy(from: assetURL, to: targetURL, clone: true, overwrite: true)
191+
try cmdChangeOwnerToInstalld(targetURL, recursively: checkIsDirectory(assetURL))
191192
}
193+
}
192194

193-
return targetURLs
195+
fileprivate func batchRemove(_ assetURLs: [URL]) throws {
196+
try assetURLs.forEach {
197+
try cmdRemove($0, recursively: checkIsDirectory($0))
198+
}
194199
}
195200

196201
// MARK: - Path Finder

TrollFools/LogsView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct LogsView: UIViewControllerRepresentable {
2323
viewController.reversed = true
2424
viewController.allowTrash = false
2525
viewController.allowSearch = true
26+
viewController.allowShare = true
2627
viewController.allowMultiline = true
2728
viewController.pullToReload = false
2829
viewController.tapToCopy = true

TrollFools/StripedTextTableViewController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
3030
@property (nonatomic, assign) BOOL removeDuplicates;
3131
@property (nonatomic, assign) BOOL allowTrash;
3232
@property (nonatomic, assign) BOOL allowSearch;
33+
@property (nonatomic, assign) BOOL allowShare;
3334
@property (nonatomic, assign) BOOL pullToReload;
3435
@property (nonatomic, assign) BOOL tapToCopy;
3536
@property (nonatomic, assign) BOOL pressToCopy;

TrollFools/StripedTextTableViewController.m

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ @interface StripedTextTableViewController () <UISearchResultsUpdating>
1616
@property (nonatomic, strong) NSNumberFormatter *decimalNumberFormatter;
1717
@property (nonatomic, assign) NSUInteger numberOfTextRowsNotLoaded;
1818

19+
@property (nonatomic, strong) UIBarButtonItem *shareItem;
1920
@property (nonatomic, strong) UIBarButtonItem *trashItem;
2021
@property (nonatomic, strong) UISearchController *searchController;
2122

@@ -72,10 +73,18 @@ - (void)viewDidLoad {
7273
});
7374
}
7475

76+
NSMutableArray <UIBarButtonItem *> *rightBarButtonItems = [NSMutableArray arrayWithCapacity:2];
77+
78+
if (self.allowShare) {
79+
[rightBarButtonItems addObject:self.shareItem];
80+
}
81+
7582
if (self.allowTrash) {
76-
self.navigationItem.rightBarButtonItem = self.trashItem;
83+
[rightBarButtonItems addObject:self.trashItem];
7784
}
7885

86+
self.navigationItem.rightBarButtonItems = rightBarButtonItems;
87+
7988
if (self.allowSearch) {
8089
self.navigationItem.hidesSearchBarWhenScrolling = YES;
8190
self.navigationItem.searchController = self.searchController;
@@ -242,6 +251,17 @@ - (void)resetAutoReload {
242251
dispatch_resume(eventSource);
243252
}
244253

254+
- (void)shareItemTapped:(UIBarButtonItem *)sender {
255+
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[[NSURL fileURLWithPath:self.entryPath]] applicationActivities:nil];
256+
if (@available(iOS 16.0, *)) {
257+
activityViewController.popoverPresentationController.sourceItem = sender;
258+
} else {
259+
// Fallback on earlier versions
260+
activityViewController.popoverPresentationController.barButtonItem = sender;
261+
}
262+
[self presentViewController:activityViewController animated:YES completion:nil];
263+
}
264+
245265
- (void)trashItemTapped:(UIBarButtonItem *)sender {
246266
UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Confirm", @"StripedTextTableViewController") message:[NSString stringWithFormat:NSLocalizedString(@"Do you want to clear this log file “%@”?", @"StripedTextTableViewController"), [self.entryPath lastPathComponent]] preferredStyle:UIAlertControllerStyleAlert];
247267
[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"StripedTextTableViewController") style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
@@ -398,6 +418,13 @@ - (void)updateSearchResultsForSearchController:(UISearchController *)searchContr
398418

399419
#pragma mark - UIView Getters
400420

421+
- (UIBarButtonItem *)shareItem {
422+
if (!_shareItem) {
423+
_shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareItemTapped:)];
424+
}
425+
return _shareItem;
426+
}
427+
401428
- (UIBarButtonItem *)trashItem {
402429
if (!_trashItem) {
403430
_trashItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(trashItemTapped:)];

TrollFools/Version.Debug.xcconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
// Configuration settings file format documentation can be found at:
99
// https://help.apple.com/xcode/#/dev745c5c974
1010

11-
DEBUG_VERSION = 2.10
12-
DEBUG_BUILD_NUMBER = 202501142
11+
DEBUG_VERSION = 2.11
12+
DEBUG_BUILD_NUMBER = 202501171

TrollFools/Version.xcconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
// Configuration settings file format documentation can be found at:
99
// https://help.apple.com/xcode/#/dev745c5c974
1010

11-
VERSION = 2.10
12-
BUILD_NUMBER = 23
11+
VERSION = 2.11
12+
BUILD_NUMBER = 25

0 commit comments

Comments
 (0)