123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- #import "HelperClass.h"
- #import "StatusPackageModel.h"
- @implementation HelperClass
- + (NSArray <StatusPackageModel*>*)statusInstalledPackagesFromFile:(NSString *)statusFile
- {
-
- if (![FM fileExistsAtPath:statusFile]) {
- return nil;
- }
- NSString *packageString = [NSString stringWithContentsOfFile:statusFile encoding:NSUTF8StringEncoding error:nil];
- NSArray *lineArray = [packageString componentsSeparatedByString:@"\n\n"];
- //DDLogInfo(@"lineArray: %@", lineArray);
- NSMutableArray *mutableList = [[NSMutableArray alloc] init];
- //NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init];
- for (id currentItem in lineArray)
- {
-
- StatusPackageModel *debModel = [[StatusPackageModel alloc] initWithRawControlString:currentItem];
- if (debModel != nil)
- [mutableList addObject:debModel];
- }
-
- NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES
- selector:@selector(localizedCaseInsensitiveCompare:)];
- NSSortDescriptor *packageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"package" ascending:YES
- selector:@selector(localizedCaseInsensitiveCompare:)];
- NSArray *descriptors = [NSArray arrayWithObjects:nameDescriptor, packageDescriptor, nil];
- NSArray *sortedArray = [mutableList sortedArrayUsingDescriptors:descriptors];
-
-
- mutableList = nil;
-
- return sortedArray;
- }
- + (BOOL)shouldContinueWithError:(NSString *)errorMessage {
-
- NSString *errorString = [NSString stringWithFormat:@"\n%@ Are you sure you want to continue? [y/n]?", errorMessage];
-
- char c;
- printf("%s", [errorString UTF8String] );
- c=getchar();
- while(c!='y' && c!='n')
- {
- if (c!='\n'){
- printf("[y/n]");
- }
- c=getchar();
- }
-
- if (c == 'n')
- {
- DLog(@"\n smart move... exiting\n\n");
- return FALSE;
- } else if (c == 'y') {
- DLog(@"\n its your funeral....\n\n");
- }
-
- return TRUE;
-
- }
- + (NSArray *)returnForProcess:(NSString *)call
- {
- if (call==nil)
- return 0;
- char line[200];
- DLog(@"\nRunning process: %@\n", call);
- FILE* fp = popen([call UTF8String], "r");
- NSMutableArray *lines = [[NSMutableArray alloc]init];
- if (fp)
- {
- while (fgets(line, sizeof line, fp))
- {
- NSString *s = [NSString stringWithCString:line encoding:NSUTF8StringEncoding];
- s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- [lines addObject:s];
- }
- }
- pclose(fp);
- return lines;
- }
- + (InputPackageFile *)packageFileFromLine:(NSString *)inputLine {
- // "-rwxr-xr-x 0 root wheel 69424 Oct 22 03:56 ./Library/MobileSubstrate/DynamicLibraries/beigelist7.dylib\n",
-
- //-rwxr-xr-x root/staff 10860 2011-02-02 03:55 ./Library/Frameworks/CydiaSubstrate.framework/Commands/cycc
-
- inputLine = [inputLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
- inputLine = [inputLine stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\t"]];
- NSMutableString *newString = [[NSMutableString alloc] initWithString:inputLine];
- [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
- [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
- [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
- [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
- [newString replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [newString length])];
-
- NSArray *lineObjects = [newString componentsSeparatedByString:@" "];
-
- //NSLog(@"lineObjects: %@", lineObjects);
-
-
- /*
-
- "drwxr-xr-x",
- "root/wheel",
- 0,
- "2018-06-27",
- "01:21",
- "./"
- */
-
-
- NSString *permissionsAndType = [lineObjects objectAtIndex:0];
- NSString *userGroup = [lineObjects objectAtIndex:1];
- NSString *size = [lineObjects objectAtIndex:2];
- NSString *date = [lineObjects objectAtIndex:3];
- NSString *time = [lineObjects objectAtIndex:4];
- NSString *path = [lineObjects objectAtIndex:5];
-
- //@"drwxr-xr-x"
- NSString *fileTypeChar = [permissionsAndType substringWithRange:NSMakeRange(0, 1)];
-
- NSString *octalPermissions = [self octalFromSymbols:permissionsAndType];
- NSString *octalUG = [self octalFromGroupSymbols:userGroup];
- NSString *fileName = [path lastPathComponent];
- //NSString *fullPath = [NSString stringWithFormat:@"/%@", path];
- NSString *fullPath = [path substringFromIndex:1];
-
- InputPackageFile *pf = [InputPackageFile new];
- [pf _setFileTypeFromRaw:fileTypeChar];
-
- switch (pf.type) {
- case BSPackageFileTypeLink:
- {
-
- fullPath = [lineObjects objectAtIndex:7];
- NSString *linkDest = [NSString stringWithFormat:@"/%@", path];
- pf.permissions = octalPermissions;
- pf.owner = octalUG;
- pf.size = size;
- pf.time = time;
- pf.date = date;
- pf.path = fullPath;
- pf.basename = fileName;
- pf.linkDestination = linkDest;
-
- return pf;
- }
- break;
-
- case BSPackageFileTypeDirectory: //return for now
-
- //DLog(@"we dont want directory entries do we %@", lineObjects);
- pf.permissions = octalPermissions;
- pf.owner = octalUG;
- pf.size = size;
- pf.time = time;
- pf.date = date;
- pf.path = fullPath;
- pf.basename = fileName;
- return pf;
- break;
-
- default:
- break;
- }
-
-
- pf.permissions = octalPermissions;
- pf.owner = octalUG;
- pf.size = size;
- pf.time = time;
- pf.date = date;
- pf.path = fullPath;
- pf.basename = fileName;
- return pf;
- // return [NSDictionary dictionaryWithObjectsAndKeys:fileType, @"fileType",octalPermissions, @"octalPermissions", octalUG, @"octalUG", size, @"size", date, @"date", time, @"time", fileName, @"fileName", fullPath, @"fullPath", nil];
-
- }
- + (NSString *)octalFromGroupSymbols:(NSString *)theSymbols
- {
- NSArray *groupArray = [theSymbols componentsSeparatedByString:@"/"];
- NSString *user = [groupArray objectAtIndex:0];
- NSString *group = [groupArray objectAtIndex:1];
-
- NSString *octalUser = nil;
- NSString *octalGroup = nil;
- //uid=0(root) gid=0(wheel) groups=0(wheel),1(daemon),2(kmem),3(sys),4(tty),5(operator),8(procview),9(procmod),20(staff),29(certusers),80(admin)
- if ([user isEqualToString:@"root"])
- {
- octalUser = @"0";
- } else if ([user isEqualToString:@"mobile"])
- {
- octalUser = @"501";
- }
- //obviously more cases!! FIXME:
-
- if ([group isEqualToString:@"staff"])
- {
- octalGroup = @"20";
- } else if ([group isEqualToString:@"admin"])
- {
- octalGroup = @"80";
- } else if ([group isEqualToString:@"wheel"])
- {
- octalGroup = @"0";
- } else if ([group isEqualToString:@"daemon"])
- {
- octalGroup = @"1";
- } else if ([group isEqualToString:@"kmem"])
- {
- octalGroup = @"2";
- } else if ([group isEqualToString:@"sys"])
- {
- octalGroup = @"3";
- } else if ([group isEqualToString:@"tty"])
- {
- octalGroup = @"4";
- } else if ([group isEqualToString:@"operator"])
- {
- octalGroup = @"5";
- } else if ([group isEqualToString:@"procview"])
- {
- octalGroup = @"8";
- } else if ([group isEqualToString:@"procmod"])
- {
- octalGroup = @"9";
- } else if ([group isEqualToString:@"certusers"])
- {
- octalGroup = @"29";
- } else
- {
- octalGroup = @"501"; //default to mobile
- }
- //uid=0(root) gid=0(wheel) groups=0(wheel),1(daemon),2(kmem),3(sys),4(tty),5(operator),8(procview),9(procmod),20(staff),29(certusers),80(admin)
- return [NSString stringWithFormat:@"%@:%@", octalUser, octalGroup];
-
- }
- + (InputPackage *)packageForDeb:(NSString *)debFile {
-
- NSString *packageName = [[self returnForProcess:[NSString stringWithFormat:@"/usr/local/bin/dpkg -f %@ Package", debFile]] componentsJoinedByString:@"\n"];
- NSString *packageVersion = [[self returnForProcess:[NSString stringWithFormat:@"/usr/local/bin/dpkg -f %@ Version", debFile]] componentsJoinedByString:@"\n"];
- NSArray <InputPackageFile *> *fileList = [self returnForProcess:[NSString stringWithFormat:@"/usr/local/bin/dpkg -c %@", debFile]];
-
- __block NSMutableArray *finalArray = [NSMutableArray new];
-
- [fileList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
-
- InputPackageFile *file = [self packageFileFromLine:obj];
- if (file) {
- //DLog(@"%@", file);
- [finalArray addObject:file];
- }
-
- }];
-
- InputPackage *pkg = [InputPackage new];
- pkg.files = finalArray;
- pkg.path = debFile;
- pkg.packageName = packageName;
- pkg.version = packageVersion;
- return pkg;
-
- }
- + (NSString *)octalFromSymbols:(NSString *)theSymbols
- {
- //NSLog(@"%@ %s", self, _cmd);
- NSString *U = [theSymbols substringWithRange:NSMakeRange(1, 3)];
- NSString *G = [theSymbols substringWithRange:NSMakeRange(4, 3)];
- NSString *O = [theSymbols substringWithRange:NSMakeRange(7, 3)];
- //NSLog(@"fileTypeChar: %@", fileTypeChar);
- //NSLog(@"U; %@", U);
- //NSLog(@"G; %@", G);
- //NSLog(@"O; %@", O);
-
- //USER
-
- int sIdBit = 0;
-
- int uOctal = 0;
-
- const char *uArray = [U cStringUsingEncoding:NSASCIIStringEncoding];
- int stringLength = [U length];
-
- int x;
- for( x=0; x<stringLength; x++ )
- {
- unsigned int aCharacter = uArray[x];
- if (aCharacter == 'r')
- {
- uOctal += 4;
- } else if (aCharacter == 'w')
- {
- uOctal += 2;
- } else if (aCharacter == 'x')
- {
- uOctal += 1;
- } else if (aCharacter == 's')
- {
- sIdBit += 4;
- }
- }
-
- //GROUP
-
- int gOctal = 0;
- const char *gArray = [G cStringUsingEncoding:NSASCIIStringEncoding];
- stringLength = [G length];
-
- int y;
- for( y=0; y<stringLength; y++ )
- {
- unsigned int aCharacter = gArray[y];
- if (aCharacter == 'r')
- {
- gOctal += 4;
- } else if (aCharacter == 'w')
- {
- gOctal += 2;
- } else if (aCharacter == 'x')
- {
- gOctal += 1;
- } else if (aCharacter == 's')
- {
- gOctal += 2;
- }
- }
-
- //OTHERS
- int z;
- int oOctal = 0;
- const char *oArray = [O cStringUsingEncoding:NSASCIIStringEncoding];
- stringLength = [O length];
-
-
- for( z=0; z<stringLength; z++ )
- {
- unsigned int aCharacter = oArray[z];
- if (aCharacter == 'r')
- {
- oOctal += 4;
- } else if (aCharacter == 'w')
- {
- oOctal += 2;
- } else if (aCharacter == 'x')
- {
- oOctal += 1;
- }
- }
-
-
- return [NSString stringWithFormat:@"%i%i%i%i", sIdBit, uOctal, gOctal, oOctal];
-
- }
- @end
|