Skip to content

Commit

Permalink
[Darwin][BDX] Do not load all the rest of the file in memory to check…
Browse files Browse the repository at this point in the history
… if the end of file has been reached during a BDX Query Block request (#21229)
  • Loading branch information
vivien-apple authored and web-flow committed Jul 29, 2022
1 parent 079a65d commit 92477c3
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ @interface OTAProviderDelegate ()
@property NSString * mOTAFilePath;
@property NSFileHandle * mFileHandle;
@property NSNumber * mFileOffset;
@property NSNumber * mFileEndOffset;
@property DeviceSoftwareVersionModel * candidate;
@end

Expand Down Expand Up @@ -144,8 +145,19 @@ - (void)handleBDXTransferSessionBegin:(NSString * _Nonnull)fileDesignator
return;
}

uint64_t endOffset;
if (![handle seekToEndReturningOffset:&endOffset error:&seekError]) {
auto errorString = [NSString stringWithFormat:@"Error seeking file (%@) to end offset", fileDesignator];
auto error = [[NSError alloc] initWithDomain:@"OTAProviderDomain"
code:MTRErrorCodeGeneralError
userInfo:@{ NSLocalizedDescriptionKey : NSLocalizedString(errorString, nil) }];
completionHandler(error);
return;
}

_mFileHandle = handle;
_mFileOffset = offset;
_mFileEndOffset = @(endOffset);
completionHandler(nil);
}

Expand All @@ -154,6 +166,7 @@ - (void)handleBDXTransferSessionEnd:(NSError * _Nullable)error
NSLog(@"BDX TransferSession end with error: %@", error);
_mFileHandle = nil;
_mFileOffset = nil;
_mFileEndOffset = nil;
}

- (void)handleBDXQuery:(NSNumber * _Nonnull)blockSize
Expand All @@ -173,8 +186,15 @@ - (void)handleBDXQuery:(NSNumber * _Nonnull)blockSize
return;
}

NSData * data = [_mFileHandle readDataOfLength:[blockSize unsignedLongValue]];
completionHandler(data, [[_mFileHandle availableData] length] == 0);
NSData * data = [_mFileHandle readDataUpToLength:[blockSize unsignedLongValue] error:&error];
if (error != nil) {
NSLog(@"Error reading file %@", _mFileHandle);
completionHandler(nil, NO);
return;
}

BOOL isEOF = offset + [blockSize unsignedLongValue] >= [_mFileEndOffset unsignedLongLongValue];
completionHandler(data, isEOF);
}

- (void)SetOTAFilePath:(const char *)path
Expand Down

0 comments on commit 92477c3

Please sign in to comment.