Skip to content

Commit 6637e47

Browse files
committed
Merge pull request #98 from DeskConnect/master
Make file protection availability check more robust
2 parents 8165e0a + 69cab76 commit 6637e47

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

FastImageCache/FastImageCache/FastImageCache/FICImageTable.m

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -513,23 +513,29 @@ - (void)_setEntryCount:(NSInteger)entryCount {
513513
// accessible and when you try to use that data. Sidestep this issue altogether
514514
// by using NSFileProtectionNone
515515
- (BOOL)canAccessEntryData {
516-
BOOL result = YES;
517-
if ([_fileDataProtectionMode isEqualToString:NSFileProtectionComplete]) {
518-
result = [[UIApplication sharedApplication] isProtectedDataAvailable];
519-
} else if ([_fileDataProtectionMode isEqualToString:NSFileProtectionCompleteUntilFirstUserAuthentication]) {
520-
// For "complete until first auth", if we were previously able to access data, then we'll still be able to
521-
// access it. If we haven't yet been able to access data, we'll need to try until we are successful.
522-
if (_canAccessData == NO) {
523-
if ([[UIApplication sharedApplication] isProtectedDataAvailable]) {
524-
// we are unlocked, so we're good to go.
525-
_canAccessData = YES;
526-
} else {
527-
// we are locked, so try to access data.
528-
_canAccessData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:_filePath] options:NSDataReadingMappedAlways error:NULL] != nil;
529-
}
530-
}
516+
if ([_fileDataProtectionMode isEqualToString:NSFileProtectionNone])
517+
return YES;
518+
519+
if ([_fileDataProtectionMode isEqualToString:NSFileProtectionCompleteUntilFirstUserAuthentication] && _canAccessData)
520+
return YES;
521+
522+
// -[UIApplication isProtectedDataAvailable] checks whether the keybag is locked or not
523+
UIApplication *application = [UIApplication performSelector:@selector(sharedApplication)];
524+
if (application) {
525+
_canAccessData = [application isProtectedDataAvailable];
526+
}
527+
528+
// We have to fallback to a direct check on the file if either:
529+
// - The application doesn't exist (happens in some extensions)
530+
// - The keybag is locked, but the file might still be accessible because the mode is "until first user authentication"
531+
if (!application || (!_canAccessData && [_fileDataProtectionMode isEqualToString:NSFileProtectionCompleteUntilFirstUserAuthentication])) {
532+
int fd;
533+
_canAccessData = ((fd = open([_filePath fileSystemRepresentation], O_RDONLY)) != -1);
534+
if (_canAccessData)
535+
close(fd);
531536
}
532-
return result;
537+
538+
return _canAccessData;
533539
}
534540

535541
- (FICImageTableEntry *)_entryDataAtIndex:(NSInteger)index {

0 commit comments

Comments
 (0)