11#import < Foundation/Foundation.h>
2+ #include < sys/stat.h>
23#include " AppList.h"
34#include " common.h"
45
@@ -135,6 +136,7 @@ int backupApp(NSString* bundlePath)
135136 return 0 ;
136137}
137138
139+ // if the app package is changed/upgraded, the directory structure may change and some paths may become invalid.
138140int restoreApp (NSString * bundlePath)
139141{
140142 SYSLOG (" restoreApp=%@" , bundlePath);
@@ -200,24 +202,39 @@ int enableForApp(NSString* bundlePath)
200202
201203 ASSERT ([fm createSymbolicLinkAtPath: [jbroot (bundlePath) stringByAppendingString: @" /.jbroot" ] withDestinationPath: jbroot (@" /" ) error: nil ]);
202204
203- ASSERT (spawnBootstrap ((char *[]){" /usr/bin/uicache" ," -p" , bundlePath.UTF8String , NULL }, nil , nil ) == 0 );
205+ NSString * log=nil ;
206+ NSString * err=nil ;
207+ if (spawnBootstrap ((char *[]){" /usr/bin/uicache" ," -p" , bundlePath.UTF8String , NULL }, &log, &err) != 0 ) {
208+ STRAPLOG (" %@\n ERR:%@" , log, err);
209+ ABORT ();
210+ }
204211 }
205212 else if ([appInfo[@" CFBundleIdentifier" ] hasPrefix: @" com.apple." ]
206213 || [NSFileManager .defaultManager fileExistsAtPath: [bundlePath stringByAppendingString: @" /../_TrollStore" ]])
207214 {
208215 ASSERT (backupApp (bundlePath) == 0 );
209216
210217 ASSERT ([fm createSymbolicLinkAtPath: [bundlePath stringByAppendingString: @" /.jbroot" ] withDestinationPath: jbroot (@" /" ) error: nil ]);
211-
212- ASSERT (spawnBootstrap ((char *[]){" /usr/bin/uicache" ," -s" ," -p" , rootfsPrefix (bundlePath).UTF8String , NULL }, nil , nil ) == 0 );
218+
219+ NSString * log=nil ;
220+ NSString * err=nil ;
221+ if (spawnBootstrap ((char *[]){" /usr/bin/uicache" ," -s" ," -p" , rootfsPrefix (bundlePath).UTF8String , NULL }, &log, &err) != 0 ) {
222+ STRAPLOG (" %@\n ERR:%@" , log, err);
223+ ABORT ();
224+ }
213225 }
214226 else
215227 {
216228 ASSERT (backupApp (bundlePath) == 0 );
217229
218230 ASSERT ([fm createSymbolicLinkAtPath: [bundlePath stringByAppendingString: @" /.jbroot" ] withDestinationPath: jbroot (@" /" ) error: nil ]);
219231
220- ASSERT (spawnBootstrap ((char *[]){" /usr/bin/uicache" ," -s" ," -p" , rootfsPrefix (bundlePath).UTF8String , NULL }, nil , nil ) == 0 );
232+ NSString * log=nil ;
233+ NSString * err=nil ;
234+ if (spawnBootstrap ((char *[]){" /usr/bin/uicache" ," -s" ," -p" , rootfsPrefix (bundlePath).UTF8String , NULL }, &log, &err) != 0 ) {
235+ STRAPLOG (" %@\n ERR:%@" , log, err);
236+ ABORT ();
237+ }
221238 }
222239
223240 return 0 ;
@@ -242,17 +259,36 @@ int disableForApp(NSString* bundlePath)
242259 else if ([appInfo[@" CFBundleIdentifier" ] hasPrefix: @" com.apple." ]
243260 || [NSFileManager .defaultManager fileExistsAtPath: [bundlePath stringByAppendingString: @" /../_TrollStore" ]])
244261 {
262+
263+ struct stat st;
264+ if (lstat ([bundlePath stringByAppendingString: @" /.jbroot" ].fileSystemRepresentation , &st)==0 )
265+ ASSERT ([fm removeItemAtPath: [bundlePath stringByAppendingString: @" /.jbroot" ] error: nil ]);
266+ if (lstat ([bundlePath stringByAppendingString: @" /.prelib" ].fileSystemRepresentation , &st)==0 )
267+ ASSERT ([fm removeItemAtPath: [bundlePath stringByAppendingString: @" /.prelib" ] error: nil ]);
268+ if (lstat ([bundlePath stringByAppendingString: @" /.preload" ].fileSystemRepresentation , &st)==0 )
269+ ASSERT ([fm removeItemAtPath: [bundlePath stringByAppendingString: @" /.preload" ] error: nil ]);
270+ if (lstat ([bundlePath stringByAppendingString: @" /.rebuild" ].fileSystemRepresentation , &st)==0 )
271+ ASSERT ([fm removeItemAtPath: [bundlePath stringByAppendingString: @" /.rebuild" ] error: nil ]);
272+
245273 ASSERT (restoreApp (bundlePath) == 0 );
246- ASSERT ([fm removeItemAtPath: [bundlePath stringByAppendingString: @" /.jbroot " ] error: nil ]);
274+
247275 ASSERT (spawnBootstrap ((char *[]){" /usr/bin/uicache" ," -s" ," -p" , rootfsPrefix (bundlePath).UTF8String , NULL }, nil , nil ) == 0 );
248276 }
249277 else
250278 {
251279 // should be an appstored app
252280
253- ASSERT (restoreApp (bundlePath) == 0 );
281+ struct stat st;
282+ if (lstat ([bundlePath stringByAppendingString: @" /.jbroot" ].fileSystemRepresentation , &st)==0 )
283+ ASSERT ([fm removeItemAtPath: [bundlePath stringByAppendingString: @" /.jbroot" ] error: nil ]);
284+ if (lstat ([bundlePath stringByAppendingString: @" /.prelib" ].fileSystemRepresentation , &st)==0 )
285+ ASSERT ([fm removeItemAtPath: [bundlePath stringByAppendingString: @" /.prelib" ] error: nil ]);
286+ if (lstat ([bundlePath stringByAppendingString: @" /.preload" ].fileSystemRepresentation , &st)==0 )
287+ ASSERT ([fm removeItemAtPath: [bundlePath stringByAppendingString: @" /.preload" ] error: nil ]);
288+ if (lstat ([bundlePath stringByAppendingString: @" /.rebuild" ].fileSystemRepresentation , &st)==0 )
289+ ASSERT ([fm removeItemAtPath: [bundlePath stringByAppendingString: @" /.rebuild" ] error: nil ]);
254290
255- ASSERT ([fm removeItemAtPath: [ bundlePath stringByAppendingString: @" /.jbroot " ] error: nil ] );
291+ ASSERT (restoreApp ( bundlePath) == 0 );
256292
257293 // unregister or respring to keep app's icon on home screen
258294 ASSERT (spawnBootstrap ((char *[]){" /usr/bin/uicache" ," -u" , rootfsPrefix (bundlePath).UTF8String , NULL }, nil , nil ) == 0 );
0 commit comments