Browse Source

trying to make smarter with control file auditing and editing

Kevin Bradley 6 months ago
parent
commit
03537c4f00

+ 8 - 1
bootstrapTool/Classes/ErrorReturn.h

@@ -1,8 +1,15 @@
 
+typedef NS_ENUM(NSInteger, ErrorReturnType) {
+    ErrorReturnTypeNone,
+    ErrorReturnTypeOverwriteWarnings,
+    ErrorReturnTypeForbiddenFailure,
+};
+
+
 @interface ErrorReturn: NSObject
 
 @property (nonatomic, strong) NSArray *overwriteFiles;
-@property (readwrite, assign) NSInteger returnStatus;
+@property (readwrite, assign) ErrorReturnType returnStatus;
 
 
 @end

+ 1 - 0
bootstrapTool/Classes/HelperClass.m

@@ -251,6 +251,7 @@
         c=getchar();
     }
     buf[i] = '\0';
+    if (i == 0) return -1;
     return atoi(buf);
 }
 

+ 34 - 43
bootstrapTool/Classes/InputPackage.m

@@ -97,9 +97,8 @@
     NSString *packageVersion = [HelperClass singleLineReturnForProcess:@"%@ -f %@ Version", dpkg, debFile];
     NSString *architecture = [HelperClass singleLineReturnForProcess:@"%@ -f %@ Architecture", dpkg, debFile];
     NSArray <InputPackageFile *> *fileList = [HelperClass returnForProcess:[NSString stringWithFormat:@"%@ -c %@", dpkg, debFile]];
-    
     __block NSMutableArray *finalArray = [NSMutableArray new];
-    
+    NSArray *excludeArray = [excludesFile excludeArrayFromFile];
     [fileList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         InputPackageFile *file = [InputPackage packageFileFromLine:obj];
         if (file) {
@@ -107,7 +106,6 @@
             [finalArray addObject:file];
         }
     }];
-    
     InputPackage *pkg = [InputPackage new];
     pkg.excludeFile = excludesFile;
     pkg.files = finalArray;
@@ -116,6 +114,20 @@
     pkg.version = packageVersion;
     pkg.architecture = architecture;
     pkg.validLDIDPath = [HelperClass validLDIDPath];
+    if (excludeArray) {
+        __block NSMutableArray *filesToRemove = [NSMutableArray new];
+        [excludeArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+            if ([obj length] > 0){
+                NSArray *items = [fileList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF LIKE[c] %@", obj]];
+                if (items.count > 0) {
+                    //DLog(@"found items to remove: %@", items);
+                    [filesToRemove addObjectsFromArray:items];
+                }
+            }
+        }];
+        //DLog(@"\nShould remove the following: %@", filesToRemove);
+        pkg.excludedFiles = filesToRemove;
+    }
     return pkg;
     
 }
@@ -134,7 +146,6 @@
         
     }];
     return [NSString stringWithFormat:@"%@ = %@", orig, details];
-    
 }
 
 - (NSArray *)rootPaths {
@@ -155,9 +166,11 @@
     [self.files enumerateObjectsUsingBlock:^(InputPackageFile * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         
         if ([obj.fileType isEqualToString:@"link"]){ //does this need to handle things differently?
-            [outFiles addObject:obj.linkDestination];
+            [outFiles addObject:[obj.linkDestination substringFromIndex:1]];
         } else {
-            [outFiles addObject:obj.path];
+            if (obj.path.length > 1){
+                [outFiles addObject:obj.path];
+            }
         }
     }];
     return [outFiles componentsJoinedByString:@"\n"];
@@ -253,33 +266,23 @@
             default:
                 break;
         }
-        
-        
-        
     }
     
     ErrorReturn *safePackage = [self errorReturnForBootstrap:bootstrapPath];
-    
-    if (safePackage.returnStatus != 0) { //zero is success
-        
-        if (safePackage.returnStatus == 1) //ovewrwrites, just warnings!
-        {
-            
+    if (safePackage.returnStatus != ErrorReturnTypeNone) {
+        if (safePackage.returnStatus == ErrorReturnTypeOverwriteWarnings) {
             NSString *error = [NSString stringWithFormat:@" [WARNING] %@ will overwrite the following files: \n\n\t%@\n\n", self.path.lastPathComponent, [safePackage.overwriteFiles componentsJoinedByString:@"\n\t"]];
             if (!self.forceOverwrite) {
                 if(![HelperClass shouldContinueWithPrompt:error]){
-                    
                     return -1;
                 }
             }
-        } else if (safePackage.returnStatus == 2) //bail!!"
-        {
+        } else if (safePackage.returnStatus == ErrorReturnTypeForbiddenFailure) {
             if (![self forceInstall]) {
-                return 2;
+                return ErrorReturnTypeForbiddenFailure;
             }
         }
     }
-    
     NSString *pwd = [[HelperClass returnForProcess:@"/bin/pwd"] componentsJoinedByString:@"\n"];
     
     NSString *tmpPath = [pwd stringByAppendingPathComponent:self.packageName];
@@ -288,14 +291,13 @@
     DLog(@"\nExtracting deb for processing...\n");
     [HelperClass returnForProcess:[NSString stringWithFormat:@"%@ -x %@ %@", dpkg, self.path, tmpPath]];
     
-    NSArray *fileList = [self getFileListing];
-    
-    if (fileList.count > 0) {
+    NSString *listOutput = [self listfile];
+    if (listOutput.length > 10) {
         
         NSString *baseFile = [NSString stringWithFormat:@"%@.list", self.packageName];
         
         NSString *listFile = [bootstrapPath.relativeInfoFolderPath stringByAppendingPathComponent:baseFile];
-        NSString *listOutput = [fileList componentsJoinedByString:@"\n"];
+        //NSString *listOutput = [fileList componentsJoinedByString:@"\n"];
         DLog(@"\nCreating list file '%@'...\n", listFile);
         //DLog(@"list: %@", listOutput);
         [[listOutput stringByAppendingString:@"\n"] writeToFile:listFile atomically:TRUE encoding:NSUTF8StringEncoding error:nil];
@@ -310,17 +312,6 @@
     */
     //NSString *md5s = [bootstrapInfoPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.md5sums", self.packageName]];
     
-    //DLog(@"\nCreating list file '%@'...\n", listFile);
-    
-    //NSString *listString = [NSString stringWithFormat:@"/usr/bin/find %@ -type f -not -path \"*.DS_Store*\" | /usr/bin/sed \"s|%@||g\" >> \"%@\"", tmpPath, tmpPath, listFile];
-    //NSString *listOutput = [HelperClass singleLineReturnForProcess:listString];
-    
-    //DLog(@"list output: %@", listOutput);
-    
-    //[listOutput writeToFile:listFile atomically:TRUE encoding:NSASCIIStringEncoding error:nil];
-    
-    //find "$BUILD_ROOT/$WORKING_DIR/deb" -type f -not -path "$BUILD_ROOT/$WORKING_DIR/deb/DEBIAN/*" | sed "s|$BUILD_ROOT/$WORKING_DIR/deb||g" >> "$SKEL_PREFIX/var/lib/dpkg/info/$PKG_NAME.list"
-    
     //find "$BUILD_ROOT/$WORKING_DIR/deb" -type f -not -path "$BUILD_ROOT/$WORKING_DIR/deb/DEBIAN/*" -exec $MD5 {} \; |  awk '{ print $1 "  " $2 }' | sed "s|$BUILD_ROOT/$WORKING_DIR/deb/||g" >> "$SKEL_PREFIX/var/lib/dpkg/info/$PKG_NAME.md5sums"
     
     //[self.listfile writeToFile:listFile atomically:TRUE encoding:NSASCIIStringEncoding error:nil];
@@ -365,8 +356,7 @@
             
             //[controlFile replaceOccurrencesOfString:@"iphoneos-arm" withString:@"appletvos-arm64" options:NSLiteralSearch range:NSMakeRange(0, [controlFile length])];
             
-            NSString *controlString = self.unpack ? @"Status: install ok unpacked" : @"Status: install ok installed";
-            
+            NSString *controlString = self.unpack ? @"Status: install ok unpacked\n" : @"Status: install ok installed\n";
             [controlFile appendString:controlString];
             
             //DLog(@"control file: -%@-\n", controlFile);
@@ -374,9 +364,9 @@
             NSString *statusFile = [bootstrapPath relativeStatusFilePath];
             
             [FM copyItemAtPath:statusFile toPath:[statusFile stringByAppendingPathExtension:@"bak"] error:nil];
-            
-            NSMutableString *statusContents = [[NSMutableString alloc] initWithContentsOfFile:statusFile encoding:NSASCIIStringEncoding error:nil];
-            
+            NSString *statusCont = [[NSString stringWithContentsOfFile:statusFile encoding:NSASCIIStringEncoding error:nil] sanitizedControlString];
+            NSMutableString *statusContents = [statusCont mutableCopy];//[[NSMutableString alloc] initWithContentsOfFile:statusFile encoding:NSASCIIStringEncoding error:nil];
+            //[statusContents sanitizedControlString];
             if (model) { //we found the package model, just replace the old string with our new one
                 DLog(@"Updating status file for new package version...\n\n");
                 
@@ -388,6 +378,7 @@
                 
                 //DLog(@"down here??: -%@-\n", controlFile);
                 [statusContents appendFormat:@"%@\n", controlFile];
+                
                 //DLog(@"\nnew status file: %@\n", statusContents);
                 //[statusContents writeToFileWithoutAttributes:statusFile];
                 [statusContents writeToFile:statusFile atomically:TRUE encoding:NSASCIIStringEncoding error:nil];
@@ -833,7 +824,7 @@
 - (ErrorReturn *)errorReturnForBootstrap:(NSString *)bootstrapPath {
     NSArray *ignoreFiles = @[@".fauxsu", @".DS_Store"];
     NSArray *forbiddenRoots = @[@"etc", @"var", @"tmp"];
-    __block NSInteger returnValue = 0; //0 = good to go 1 = over write warning, 2 = no go
+    __block NSInteger returnValue = ErrorReturnTypeNone; //0 = good to go 1 = over write warning, 2 = no go
     __block NSMutableArray *_overwriteArray = [NSMutableArray new];
     [self.files enumerateObjectsUsingBlock:^(InputPackageFile * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         if ([obj.fileType isEqualToString:@"file"]){
@@ -842,7 +833,7 @@
                 //DLog(@"[WARNING] overwriting a file that already exists and isn't DS_Store or .fauxsu: %@", fullPath);
                 [_overwriteArray addObject:obj.path];
                 //*stop = TRUE;//return FALSE;
-                returnValue = 1;
+                returnValue = ErrorReturnTypeOverwriteWarnings;
             }
             NSArray *pathComponents = [obj.path pathComponents];
             if ([pathComponents count] > 1) {
@@ -850,7 +841,7 @@
                 if ([forbiddenRoots containsObject:rootPath]) {
                     DLog(@"\n [ERROR] package file: '%@' would overwrite symbolic link at '%@'! Exiting!\n\n", obj.path, rootPath);
                     *stop = TRUE;
-                    returnValue = 2;
+                    returnValue = ErrorReturnTypeForbiddenFailure;
                 }
             }
         }

+ 2 - 0
bootstrapTool/Classes/NSString+Additions.h

@@ -17,4 +17,6 @@
 - (NSString *)relativeInfoFolderPath;
 - (NSString *)runPathForSearchPath;
 - (NSArray *)excludeArrayFromFile;
+- (NSArray *)lineArray;
+- (NSString *)sanitizedControlString;
 @end

+ 34 - 0
bootstrapTool/Classes/NSString+Additions.m

@@ -18,6 +18,40 @@
 @end
 
 @implementation NSString (Additions)
+
+- (NSArray *)lineArray {
+    return [self componentsSeparatedByString:@"\n"];
+}
+
+- (NSString *)sanitizedControlString {
+    __block NSString *finalString = self;
+    NSArray *lines = [self lineArray];
+    NSArray *lastTwo = [lines subarrayWithRange:NSMakeRange(lines.count-2, 2)];
+    DLog(@"lastTwo: %@", lastTwo);
+    NSArray *filtered = [lastTwo filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self == ''"]];
+    DLog(@"filtered: %@", filtered);
+    if (filtered.count == 2){
+        DLog(@"double empty, only need one!");
+        NSMutableArray *_newLines = [lines mutableCopy];
+        //[_newLines removeLastObject];
+        finalString = [_newLines componentsJoinedByString:@"\n"];
+    }
+    if (filtered.count <= 1) {
+        DLog(@"insufficient empty lines!");
+        NSMutableArray *_newLines = [lines mutableCopy];
+        if (filtered.count == 0){
+            DLog(@"add 2!");
+            //[_newLines addObject:@"\n"];
+            [_newLines addObject:@"\n"];
+        } else if (filtered.count == 1){
+            DLog(@"add 1!");
+            [_newLines addObject:@"\n"];
+        }
+        finalString = [_newLines componentsJoinedByString:@"\n"];
+    }
+    return finalString;
+}
+
 //@"path-exclude=*/share/info/*\npath-exclude=*/share/doc/*\npath-exclude=*/share/man/*"
 - (NSArray *)excludeArrayFromFile {
     NSString *fileContents = [NSString stringWithContentsOfFile:self encoding:NSUTF8StringEncoding error:nil];

+ 2 - 1
bootstrapTool/Classes/StatusPackageModel.m

@@ -110,7 +110,7 @@
 
 - (NSString *)description {
     NSString *orig = [super description];
-    return [NSString stringWithFormat:@"%@ = %@ (%@)", orig, self.package, self.version];
+    return [NSString stringWithFormat:@"%@ = %@ (%@): %@", orig, self.package, self.version, self.status];
 }
 
 - (NSString*) fullDescription {
@@ -196,6 +196,7 @@
              @"osMax": @"osMax",
              @"osMin": @"osMin",
              @"banner": @"Banner",
+             @"status": @"Status",
              @"topShelfImage": @"TopShelfImage"
              };
 }

+ 6 - 2
bootstrapTool/main.m

@@ -267,9 +267,13 @@ int main(int argc, char **argv) {
             DLog(@"Choose a package from the list below to create from the bootstrap\n");
             DLog(@"%@", numered);
             DLog(@"\n");
-            NSInteger getNumber = [HelperClass getNumberFromPrompt:@"Choose a package index from above: "];
+            NSInteger getNumber = [HelperClass getNumberFromPrompt:@"Choose a package index from above (0 to exit): "];
             while (getNumber > installedPackages.count) {
-                getNumber = [HelperClass getNumberFromPrompt:@"Choose a package index from above: "];
+                getNumber = [HelperClass getNumberFromPrompt:@"Choose a package index from above (0 to exit): "];
+            }
+            if (getNumber == 0) {
+                DLog(@"\n");
+                return 1;
             }
             DLog(@"chosen package: %lu: %@", getNumber, installedPackages[getNumber-1]);
             StatusPackageModel *model = installedPackages[getNumber-1];