Skip to content

Commit

Permalink
About to do some damage.
Browse files Browse the repository at this point in the history
  • Loading branch information
sammcd committed Feb 23, 2010
1 parent 65366a0 commit 2f3cf10
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions FTHostsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ - (void)dealloc {
}

+ (void)flushDNS {
// A poor man's dns flush.
NSArray* arguments = [NSArray arrayWithObjects:@"-flushcache",nil];
[NSTask launchedTaskWithLaunchPath:@"/usr/bin/dscacheutil" arguments:arguments];
}
Expand All @@ -39,27 +40,32 @@ - (NSString*)readHostsFile {
return [NSString stringWithContentsOfFile:@"/Users/sammcd/Desktop/hosts" encoding:NSASCIIStringEncoding error:NULL];
}


- (void)writeHostsToFile {
- (void)writeArrayToHosts:(NSArray*)arrayToWrite {
NSString* sectionStart;
NSString* sectionEnd;
NSString* hostsString;
NSMutableArray* hostsArray;
NSMutableArray* addedContent;
int startLine = -1;
int stopLine = -1;
int indexToAdd = 0;

hostsArray = [[NSMutableArray alloc] initWithArray:[[self readHostsFile] componentsSeparatedByString:@"\n"]];
addedContent = [[NSMutableArray alloc] init];

sectionStart = [NSString stringWithFormat:@"# %@ START",uniqueName];
sectionEnd = [NSString stringWithFormat:@"# %@ END",uniqueName];



// Loop through the array looking for the line end and beginning.
int i;
NSLog(@"%d host count",[hostsArray count]);
for (i = 0; i < [hostsArray count]; i++ ) {
NSString* line = [hostsArray objectAtIndex:i];

NSLog(@"Line: %@",line);
NSLog(@"Suffix: %@",sectionStart);

if ( [line hasSuffix:sectionStart] ) {
startLine = i;
break;
Expand All @@ -73,9 +79,12 @@ - (void)writeHostsToFile {
break;
}
}



NSLog(@"start: %d stop: %d",startLine,stopLine);


if ( startLine == -1 ) {
NSLog(@" there is no section");
// There is no section so create it.
[hostsArray addObject:[NSString stringWithFormat:@"%@",sectionStart]];
indexToAdd = [hostsArray count];
Expand All @@ -89,24 +98,41 @@ - (void)writeHostsToFile {
removeRange.length = stopLine - removeRange.location;
[hostsArray removeObjectsInRange:removeRange];
indexToAdd = startLine + 1;
NSLog(@"Remove Range: %d %d",removeRange.location, removeRange.length);

}

// Let create the array of lines that
// Should be in the middle
for ( FTHost* host in hosts ) {
[hostsArray insertObject:[NSString stringWithFormat:@"%@ %@",host.ip, host.name] atIndex:indexToAdd];
indexToAdd++;
for ( FTHost* host in arrayToWrite ) {
[addedContent addObject:[NSString stringWithFormat:@"%@ %@",host.ip, host.name]];
}

// Bring array back to string.
hostsString = [hostsArray componentsJoinedByString:@"\n"];

NSRange beforeNewContent;
beforeNewContent.location = 0;
beforeNewContent.length = indexToAdd;
NSRange afterNewContent;
afterNewContent.location = indexToAdd;
afterNewContent.length = [hostsArray count] - indexToAdd;

NSString* beginingString = [[hostsArray subarrayWithRange:beforeNewContent] componentsJoinedByString:@"\n"];
NSString* stringWithContent = [addedContent componentsJoinedByString:@"\n"];
NSString* endingString = [[hostsArray subarrayWithRange:afterNewContent] componentsJoinedByString:@"\n"];
hostsString = [NSString stringWithFormat:@"%@\n%@\n%@",beginingString,stringWithContent,endingString];


NSError* fileError;
NSString* tmpPath = [[NSFileManager defaultManager] newTmpFilePath];
[hostsString writeToFile:tmpPath atomically:YES encoding:NSASCIIStringEncoding error:&fileError];
[[NSFileManager defaultManager] authorizedMovePath:tmpPath toPath:@"/etc/hosts"];

[hostsArray release];
[addedContent release];

}

- (void)writeHostsToFile {
[self writeArrayToHosts:hosts];
}

- (void)removeHostsFromFile {
Expand All @@ -124,7 +150,6 @@ - (void)removeHostsFromFile {
sectionEnd = [NSString stringWithFormat:@"# %@ END",uniqueName];



// Loop through the array looking for the line end and beginning.
int i;
for (i = 0; i < [hostsArray count]; i++ ) {
Expand Down Expand Up @@ -160,8 +185,6 @@ - (void)removeHostsFromFile {
indexToAdd = startLine + 1;
}



// Bring array back to string.
hostsString = [hostsArray componentsJoinedByString:@"\n"];

Expand Down

0 comments on commit 2f3cf10

Please sign in to comment.