Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Carthage/Checkouts/PINCache/Source/PINDiskCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ extern NSErrorUserInfoKey const PINDiskCacheErrorWriteFailureCodeKey;
extern NSString * const PINDiskCachePrefix;

typedef NS_ENUM(NSInteger, PINDiskCacheError) {
PINDiskCacheErrorReadFailure = -1000,
PINDiskCacheErrorWriteFailure = -1001,
PINDiskCacheErrorReadFailure = -1000,
PINDiskCacheErrorWriteFailure = -1001,
Comment on lines +22 to +23
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could revert this formatting change, too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i could but previously they're not right indented?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine then. It's just not related to the rest of this change.

};

/**
Expand Down
40 changes: 24 additions & 16 deletions Carthage/Checkouts/PINCache/Source/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#import <PINOperation/PINOperation.h>
#endif

#define PINDiskCacheError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \
#define PINDiskCacheLogError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \
[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \
__LINE__, [error localizedDescription]); }

Expand Down Expand Up @@ -332,7 +332,7 @@ - (PINDiskCacheSerializerBlock)defaultSerializer
if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) {
NSError *error = nil;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object requiringSecureCoding:NO error:&error];
PINDiskCacheError(error);
PINDiskCacheLogError(error);
return data;
} else {
return [NSKeyedArchiver archivedDataWithRootObject:object];
Expand All @@ -343,7 +343,15 @@ - (PINDiskCacheSerializerBlock)defaultSerializer
- (PINDiskCacheDeserializerBlock)defaultDeserializer
{
return ^id(NSData * data, NSString *key){
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) {
NSError *error = nil;
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error];
PINDiskCacheLogError(error);
unarchiver.requiresSecureCoding = NO;
return [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey];
} else {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
};
}

Expand Down Expand Up @@ -436,7 +444,7 @@ + (NSURL *)sharedTrashURL
withIntermediateDirectories:YES
attributes:nil
error:&error];
PINDiskCacheError(error);
PINDiskCacheLogError(error);
}
trashURL = _sharedTrashURL;
[[PINDiskCache sharedLock] unlock];
Expand All @@ -453,7 +461,7 @@ + (BOOL)moveItemAtURLToTrash:(NSURL *)itemURL
NSString *uniqueString = [[NSProcessInfo processInfo] globallyUniqueString];
NSURL *uniqueTrashURL = [[PINDiskCache sharedTrashURL] URLByAppendingPathComponent:uniqueString isDirectory:NO];
BOOL moved = [[NSFileManager defaultManager] moveItemAtURL:itemURL toURL:uniqueTrashURL error:&error];
PINDiskCacheError(error);
PINDiskCacheLogError(error);
return moved;
}

Expand All @@ -475,7 +483,7 @@ + (void)emptyTrash
if (trashURL != nil) {
NSError *removeTrashedItemError = nil;
[[NSFileManager defaultManager] removeItemAtURL:trashURL error:&removeTrashedItemError];
PINDiskCacheError(removeTrashedItemError);
PINDiskCacheLogError(removeTrashedItemError);
}
});
}
Expand All @@ -491,7 +499,7 @@ - (BOOL)_locked_createCacheDirectory
withIntermediateDirectories:YES
attributes:nil
error:&error];
PINDiskCacheError(error);
PINDiskCacheLogError(error);
}


Expand Down Expand Up @@ -523,7 +531,7 @@ - (NSUInteger)_locked_initializeDiskPropertiesForFile:(NSURL *)fileURL fileKey:(
NSError *error = nil;

NSDictionary *dictionary = [fileURL resourceValuesForKeys:[PINDiskCache resourceKeys] error:&error];
PINDiskCacheError(error);
PINDiskCacheLogError(error);

if (_metadata[fileKey] == nil) {
_metadata[fileKey] = [[PINDiskCacheMetadata alloc] init];
Expand Down Expand Up @@ -552,7 +560,7 @@ - (NSUInteger)_locked_initializeDiskPropertiesForFile:(NSURL *)fileURL fileKey:(
if (errno != ENOATTR) {
NSDictionary<NSErrorUserInfoKey, id> *userInfo = @{ PINDiskCacheErrorReadFailureCodeKey : @(errno)};
error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorReadFailure userInfo:userInfo];
PINDiskCacheError(error);
PINDiskCacheLogError(error);
}
}
}
Expand All @@ -573,7 +581,7 @@ - (void)initializeDiskProperties
error:&error];
[self unlock];

PINDiskCacheError(error);
PINDiskCacheLogError(error);

for (NSURL *fileURL in files) {
NSString *fileKey = [self keyForEncodedFileURL:fileURL];
Expand Down Expand Up @@ -619,7 +627,7 @@ - (BOOL)_locked_setFileModificationDate:(NSDate *)date forURL:(NSURL *)fileURL
BOOL success = [[NSFileManager defaultManager] setAttributes:@{ NSFileModificationDate: date }
ofItemAtPath:[fileURL path]
error:&error];
PINDiskCacheError(error);
PINDiskCacheLogError(error);

return success;
}
Expand All @@ -646,14 +654,14 @@ - (BOOL)_locked_setAgeLimit:(NSTimeInterval)ageLimit forURL:(NSURL *)fileURL
if (errno != ENOATTR) {
NSDictionary<NSErrorUserInfoKey, id> *userInfo = @{ PINDiskCacheErrorWriteFailureCodeKey : @(errno)};
error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorWriteFailure userInfo:userInfo];
PINDiskCacheError(error);
PINDiskCacheLogError(error);
}
}
} else {
if (setxattr(PINDiskCacheFileSystemRepresentation(fileURL), PINDiskCacheAgeLimitAttributeName, &ageLimit, sizeof(NSTimeInterval), 0, 0) != 0) {
NSDictionary<NSErrorUserInfoKey, id> *userInfo = @{ PINDiskCacheErrorWriteFailureCodeKey : @(errno)};
error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorWriteFailure userInfo:userInfo];
PINDiskCacheError(error);
PINDiskCacheLogError(error);
}
}

Expand Down Expand Up @@ -1102,7 +1110,7 @@ - (id)objectForKeyedSubscript:(NSString *)key
[self lock];
[[NSFileManager defaultManager] removeItemAtPath:[fileURL path] error:&error];
[self unlock];
PINDiskCacheError(error)
PINDiskCacheLogError(error)
PINDiskCacheException(exception);
}
[self lock];
Expand Down Expand Up @@ -1220,7 +1228,7 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key withAgeLimit:(NST

NSError *writeError = nil;
BOOL written = [data writeToURL:fileURL options:writeOptions error:&writeError];
PINDiskCacheError(writeError);
PINDiskCacheLogError(writeError);

if (written) {
if (_metadata[key] == nil) {
Expand All @@ -1229,7 +1237,7 @@ - (void)setObject:(id <NSCoding>)object forKey:(NSString *)key withAgeLimit:(NST

NSError *error = nil;
NSDictionary *values = [fileURL resourceValuesForKeys:@[ NSURLCreationDateKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey ] error:&error];
PINDiskCacheError(error);
PINDiskCacheLogError(error);

NSNumber *diskFileSize = [values objectForKey:NSURLTotalFileAllocatedSizeKey];
if (diskFileSize) {
Expand Down
5 changes: 4 additions & 1 deletion Carthage/Checkouts/PINCache/Tests/PINCacheTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ - (void)testCustomEncoderDecoder {

[testCache setObject:@(1) forKey:@"test_key"];

XCTAssertNotNil([testCache objectForKey:@"test_key"], @"Object should not be nil");
XCTAssertEqualObjects([testCache objectForKey:@"test_key"], @1);

NSString *encodedKey = [[testCache fileURLForKey:@"test_key"] lastPathComponent];
XCTAssertEqualObjects(@"test_key", encodedKey, @"Encoded key should be equal to decoded one");
Expand All @@ -1407,6 +1407,9 @@ - (void)testTTLCacheIsSet {
PINCache *cache = [[PINCache alloc] initWithName:@"test" rootPath:PINDiskCachePrefix serializer:nil deserializer:nil keyEncoder:nil keyDecoder:nil ttlCache:YES];
XCTAssert(cache.diskCache.isTTLCache);
XCTAssert(cache.memoryCache.isTTLCache);

[cache setObject:@(1) forKey:@"test_key"];
XCTAssertNotNil([cache objectForKey:@"test_key"], @"Object should not be nil");
}

@end
18 changes: 15 additions & 3 deletions Source/Classes/PINRemoteImageManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ @interface PINRemoteImageManager () <PINURLSessionManagerDelegate>
PINAlternateRepresentationProvider *_defaultAlternateRepresentationProvider;
__weak PINAlternateRepresentationProvider *_alternateRepProvider;
NSURLSessionConfiguration *_sessionConfiguration;

}

@property (nonatomic, strong) id<PINRemoteImageCaching> cache;
Expand All @@ -158,6 +157,7 @@ @interface PINRemoteImageManager () <PINURLSessionManagerDelegate>
@property (nonatomic, strong) NSMutableDictionary <NSString *, NSString *> *httpHeaderFields;
@property (nonatomic, readonly) BOOL diskCacheTTLIsEnabled;
@property (nonatomic, readonly) BOOL memoryCacheTTLIsEnabled;

#if DEBUG
@property (nonatomic, assign) NSUInteger totalDownloads;
#endif
Expand Down Expand Up @@ -328,12 +328,24 @@ - (void)dealloc
PINCache *pinCache = [[PINCache alloc] initWithName:kPINRemoteImageDiskCacheName rootPath:cacheURLRoot serializer:^NSData * _Nonnull(id<NSCoding> _Nonnull object, NSString * _Nonnull key) {
id <NSCoding, NSObject> obj = (id <NSCoding, NSObject>)object;
if ([key hasPrefix:PINRemoteImageCacheKeyResumePrefix]) {
return [NSKeyedArchiver archivedDataWithRootObject:obj];
if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) {
return [NSKeyedArchiver archivedDataWithRootObject:obj requiringSecureCoding:NO error:nil];
} else {
return [NSKeyedArchiver archivedDataWithRootObject:object];
}
}
return (NSData *)object;
} deserializer:^id<NSCoding> _Nonnull(NSData * _Nonnull data, NSString * _Nonnull key) {
if ([key hasPrefix:PINRemoteImageCacheKeyResumePrefix]) {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) {
NSError *error = nil;
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error];
NSAssert(!error, @"unarchiver init failed with error");
unarchiver.requiresSecureCoding = NO;
return [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey];
} else {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
}
return data;
} keyEncoder:nil keyDecoder:nil ttlCache:enableTtl];
Expand Down
14 changes: 12 additions & 2 deletions Tests/PINRemoteImageTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,19 @@ - (void)testInvalidObject
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

PINDiskCache *tempDiskCache = [[PINDiskCache alloc] initWithName:kPINRemoteImageDiskCacheName rootPath:cachePath serializer:^NSData * _Nonnull(id<NSCoding> _Nonnull object, NSString * _Nonnull key) {
return [NSKeyedArchiver archivedDataWithRootObject:object];
if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) {
return [NSKeyedArchiver archivedDataWithRootObject:object requiringSecureCoding:NO error:nil];
} else {
return [NSKeyedArchiver archivedDataWithRootObject:object];
}
} deserializer:^id<NSCoding> _Nonnull(NSData * _Nonnull data, NSString * _Nonnull key) {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:nil];
unarchiver.requiresSecureCoding = NO;
return [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey];
} else {
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
}];

[tempDiskCache setObject:@"invalid" forKey:[self.imageManager cacheKeyForURL:[self JPEGURL] processorKey:nil]];
Expand Down