Fix a few more issues with context reading; if we're reading less than readSize, we need to decrement readSize too. Refs #13362.
authorZachary West <zacw@adium.im>
Thu Nov 26 10:33:53 2009 -0500 (2009-11-26)
changeset 282951687f71fc63
parent 2828 bd512b533b98
child 2830 fd43f6298e71
Fix a few more issues with context reading; if we're reading less than readSize, we need to decrement readSize too. Refs #13362.
Source/DCMessageContextDisplayPlugin.m
     1.1 --- a/Source/DCMessageContextDisplayPlugin.m	Thu Nov 26 01:15:12 2009 -0500
     1.2 +++ b/Source/DCMessageContextDisplayPlugin.m	Thu Nov 26 10:33:53 2009 -0500
     1.3 @@ -230,7 +230,6 @@
     1.4  		NSInteger readSize = 4 * getpagesize(); //Read 4 pages at a time.
     1.5  		NSMutableData *chunk = [NSMutableData dataWithLength:readSize];
     1.6  		NSInteger fd = [file fileDescriptor];
     1.7 -		char *buf = [chunk mutableBytes];
     1.8  		off_t offset = [file offsetInFile];
     1.9  		enum LMXParseResult result = LMXParsedIncomplete;
    1.10  
    1.11 @@ -238,17 +237,39 @@
    1.12  		NSAutoreleasePool *parsingAutoreleasePool = [[NSAutoreleasePool alloc] init];
    1.13  		
    1.14  		do {
    1.15 -			//Calculate the new offset. This also leaves offset where it needs to be
    1.16 -			//for the next iteration, since we modify it for the round every time.
    1.17 -			offset = (offset <= readSize) ? 0 : offset - readSize;
    1.18 +			// The location we're going to read for *this* set of reads.
    1.19 +			off_t readOffset = offset - readSize;
    1.20 +
    1.21 +			if (readOffset < 0) {
    1.22 +				// Decrease it by the amount we're over.
    1.23 +				readSize += readOffset;
    1.24 +				// Start from the beginning.
    1.25 +				readOffset = 0;
    1.26 +			}
    1.27  			
    1.28 +			if (chunk.length != readSize) {
    1.29 +				// In case we short-read last time, or we're reading a smaller amount this time.
    1.30 +				[chunk setLength:readSize];				
    1.31 +			}
    1.32 +			
    1.33 +			char *buf = [chunk mutableBytes];
    1.34 +
    1.35  			//Seek to it and read greedily until we hit readSize or run out of file.
    1.36  			NSInteger idx = 0;
    1.37 -			for (ssize_t amountRead = 0; idx < readSize; idx += amountRead) { 
    1.38 -				amountRead = pread(fd, buf + idx, readSize, offset + idx); 
    1.39 +			ssize_t amountRead = 0;
    1.40 +			for (amountRead = 0; idx < readSize; idx += amountRead) { 
    1.41 +				amountRead = pread(fd, buf + idx, readSize, readOffset + idx); 
    1.42  			   if (amountRead <= 0) break;
    1.43  			}
    1.44  			
    1.45 +			if (idx != readSize) {
    1.46 +				// If we short read, we don't want to read unknown buffer contents.
    1.47 +				[chunk setLength:idx];
    1.48 +			}
    1.49 +			
    1.50 +			// Adjust the real offset
    1.51 +			offset -= idx;
    1.52 +
    1.53  			//Parse
    1.54  			result = [parser parseChunk:chunk];
    1.55  			
    1.56 @@ -265,7 +286,7 @@
    1.57  		[outerFoundContentContexts replaceObjectsInRange:NSMakeRange(0, 0) withObjectsFromArray:foundMessages];
    1.58  		linesLeftToFind -= [outerFoundContentContexts count];
    1.59  	}
    1.60 -		
    1.61 +	
    1.62  	if (linesLeftToFind > 0) {
    1.63  		AILogWithSignature(@"Unable to find %d logs for %@", linesLeftToFind, chat);
    1.64  	}
    1.65 @@ -358,7 +379,7 @@
    1.66  				//Don't log this object
    1.67  				[message setPostProcessContent:NO];
    1.68  				[message setTrackContent:NO];
    1.69 -
    1.70 +				
    1.71  				//Add it to the array (in front, since we're working backwards, and we want the array in forward order)
    1.72  				[foundMessages insertObject:message atIndex:0];
    1.73  			} else {