From 19f0e3e178b2a1eb8d8ea2ca36258fcc8dafb695 Mon Sep 17 00:00:00 2001 From: Liang Ma Date: Tue, 31 Aug 2021 17:58:49 -0700 Subject: [PATCH 1/5] initial commit --- Carthage/Checkouts/PINCache/Source/PINDiskCache.m | 6 +++++- Carthage/Checkouts/PINCache/Tests/PINCacheTests.m | 5 ++++- Source/Classes/PINRemoteImageManager.m | 15 +++++++++++++-- Tests/PINRemoteImageTests.m | 15 +++++++++++++-- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m index f30a5ad7..2d70c8f3 100644 --- a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m +++ b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m @@ -343,7 +343,11 @@ - (PINDiskCacheSerializerBlock)defaultSerializer - (PINDiskCacheDeserializerBlock)defaultDeserializer { return ^id(NSData * data, NSString *key){ - return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + if (@available(iOS 11.0, tvOS 11.0, *)) { + return [NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:data error:nil]; + } else { + return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + } }; } diff --git a/Carthage/Checkouts/PINCache/Tests/PINCacheTests.m b/Carthage/Checkouts/PINCache/Tests/PINCacheTests.m index 0a79a3ed..7a4bb370 100644 --- a/Carthage/Checkouts/PINCache/Tests/PINCacheTests.m +++ b/Carthage/Checkouts/PINCache/Tests/PINCacheTests.m @@ -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"); @@ -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 diff --git a/Source/Classes/PINRemoteImageManager.m b/Source/Classes/PINRemoteImageManager.m index 57adce8b..8384369c 100644 --- a/Source/Classes/PINRemoteImageManager.m +++ b/Source/Classes/PINRemoteImageManager.m @@ -328,12 +328,23 @@ - (void)dealloc PINCache *pinCache = [[PINCache alloc] initWithName:kPINRemoteImageDiskCacheName rootPath:cacheURLRoot serializer:^NSData * _Nonnull(id _Nonnull object, NSString * _Nonnull key) { id obj = (id )object; if ([key hasPrefix:PINRemoteImageCacheKeyResumePrefix]) { - return [NSKeyedArchiver archivedDataWithRootObject:obj]; + 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); + return data; + } else { + return [NSKeyedArchiver archivedDataWithRootObject:object]; + } } return (NSData *)object; } deserializer:^id _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, *)) { + return [NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:data error:nil]; + } else { + return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + } } return data; } keyEncoder:nil keyDecoder:nil ttlCache:enableTtl]; diff --git a/Tests/PINRemoteImageTests.m b/Tests/PINRemoteImageTests.m index 3a772630..d10107a0 100644 --- a/Tests/PINRemoteImageTests.m +++ b/Tests/PINRemoteImageTests.m @@ -795,9 +795,20 @@ - (void)testInvalidObject NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; PINDiskCache *tempDiskCache = [[PINDiskCache alloc] initWithName:kPINRemoteImageDiskCacheName rootPath:cachePath serializer:^NSData * _Nonnull(id _Nonnull object, NSString * _Nonnull key) { - return [NSKeyedArchiver archivedDataWithRootObject:object]; + 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); + return data; + } else { + return [NSKeyedArchiver archivedDataWithRootObject:object]; + } } deserializer:^id _Nonnull(NSData * _Nonnull data, NSString * _Nonnull key) { - return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + if (@available(iOS 11.0, tvOS 11.0, *)) { + return [NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:data error:nil]; + } else { + return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + } }]; [tempDiskCache setObject:@"invalid" forKey:[self.imageManager cacheKeyForURL:[self JPEGURL] processorKey:nil]]; From 460d8b6a9e112317b5f2fa37ce7a4e153b8138cb Mon Sep 17 00:00:00 2001 From: Liang Ma Date: Wed, 1 Sep 2021 11:07:10 -0700 Subject: [PATCH 2/5] PINDiskCacheError is both enum type and a function that confuses compiler, but not sure why only issue on iphone 8 --- .../Checkouts/PINCache/Source/PINDiskCache.h | 16 +++--- .../Checkouts/PINCache/Source/PINDiskCache.m | 50 +++++++++---------- Source/Classes/PINRemoteImageManager.m | 2 +- Tests/PINRemoteImageTests.m | 2 +- 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/Carthage/Checkouts/PINCache/Source/PINDiskCache.h b/Carthage/Checkouts/PINCache/Source/PINDiskCache.h index 81bb26a4..f53fcf0c 100644 --- a/Carthage/Checkouts/PINCache/Source/PINDiskCache.h +++ b/Carthage/Checkouts/PINCache/Source/PINDiskCache.h @@ -13,16 +13,20 @@ NS_ASSUME_NONNULL_BEGIN @class PINDiskCache; @class PINOperationQueue; -extern NSString * const PINDiskCacheErrorDomain; -extern NSErrorUserInfoKey const PINDiskCacheErrorReadFailureCodeKey; -extern NSErrorUserInfoKey const PINDiskCacheErrorWriteFailureCodeKey; +extern NSString * const PINDiskCacheLogErrorDomain; +extern NSErrorUserInfoKey const PINDiskCacheLogErrorReadFailureCodeKey; +extern NSErrorUserInfoKey const PINDiskCacheLogErrorWriteFailureCodeKey; extern NSString * const PINDiskCachePrefix; -typedef NS_ENUM(NSInteger, PINDiskCacheError) { - PINDiskCacheErrorReadFailure = -1000, - PINDiskCacheErrorWriteFailure = -1001, +typedef NS_ENUM(NSInteger, PINDiskCacheErrorType) { + PINDiskCacheLogErrorReadFailure = -1000, + PINDiskCacheLogErrorWriteFailure = -1001, }; +#define PINDiskCacheLogError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \ +[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \ +__LINE__, [error localizedDescription]); } + /** A callback block which provides the cache, key and object as arguments */ diff --git a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m index 2d70c8f3..307efc2c 100644 --- a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m +++ b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m @@ -17,16 +17,12 @@ #import #endif -#define PINDiskCacheError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \ -[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \ -__LINE__, [error localizedDescription]); } - #define PINDiskCacheException(exception) if (exception) { NSAssert(NO, [exception reason]); } const char * PINDiskCacheAgeLimitAttributeName = "com.pinterest.PINDiskCache.ageLimit"; -NSString * const PINDiskCacheErrorDomain = @"com.pinterest.PINDiskCache"; -NSErrorUserInfoKey const PINDiskCacheErrorReadFailureCodeKey = @"PINDiskCacheErrorReadFailureCodeKey"; -NSErrorUserInfoKey const PINDiskCacheErrorWriteFailureCodeKey = @"PINDiskCacheErrorWriteFailureCodeKey"; +NSString * const PINDiskCacheLogErrorDomain = @"com.pinterest.PINDiskCache"; +NSErrorUserInfoKey const PINDiskCacheLogErrorReadFailureCodeKey = @"PINDiskCacheLogErrorReadFailureCodeKey"; +NSErrorUserInfoKey const PINDiskCacheLogErrorWriteFailureCodeKey = @"PINDiskCacheLogErrorWriteFailureCodeKey"; NSString * const PINDiskCachePrefix = @"com.pinterest.PINDiskCache"; static NSString * const PINDiskCacheSharedName = @"PINDiskCacheShared"; @@ -332,7 +328,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]; @@ -440,7 +436,7 @@ + (NSURL *)sharedTrashURL withIntermediateDirectories:YES attributes:nil error:&error]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); } trashURL = _sharedTrashURL; [[PINDiskCache sharedLock] unlock]; @@ -457,7 +453,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; } @@ -479,7 +475,7 @@ + (void)emptyTrash if (trashURL != nil) { NSError *removeTrashedItemError = nil; [[NSFileManager defaultManager] removeItemAtURL:trashURL error:&removeTrashedItemError]; - PINDiskCacheError(removeTrashedItemError); + PINDiskCacheLogError(removeTrashedItemError); } }); } @@ -495,7 +491,7 @@ - (BOOL)_locked_createCacheDirectory withIntermediateDirectories:YES attributes:nil error:&error]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); } @@ -527,7 +523,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]; @@ -554,9 +550,9 @@ - (NSUInteger)_locked_initializeDiskPropertiesForFile:(NSURL *)fileURL fileKey:( } else if (res == -1) { // Ignore if the extended attribute was never recorded for this file. if (errno != ENOATTR) { - NSDictionary *userInfo = @{ PINDiskCacheErrorReadFailureCodeKey : @(errno)}; - error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorReadFailure userInfo:userInfo]; - PINDiskCacheError(error); + NSDictionary *userInfo = @{ PINDiskCacheLogErrorReadFailureCodeKey : @(errno)}; + error = [NSError errorWithDomain:PINDiskCacheLogErrorDomain code:PINDiskCacheLogErrorReadFailure userInfo:userInfo]; + PINDiskCacheLogError(error); } } } @@ -577,7 +573,7 @@ - (void)initializeDiskProperties error:&error]; [self unlock]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); for (NSURL *fileURL in files) { NSString *fileKey = [self keyForEncodedFileURL:fileURL]; @@ -623,7 +619,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; } @@ -648,16 +644,16 @@ - (BOOL)_locked_setAgeLimit:(NSTimeInterval)ageLimit forURL:(NSURL *)fileURL if (removexattr(PINDiskCacheFileSystemRepresentation(fileURL), PINDiskCacheAgeLimitAttributeName, 0) != 0) { // Ignore if the extended attribute was never recorded for this file. if (errno != ENOATTR) { - NSDictionary *userInfo = @{ PINDiskCacheErrorWriteFailureCodeKey : @(errno)}; - error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorWriteFailure userInfo:userInfo]; - PINDiskCacheError(error); + NSDictionary *userInfo = @{ PINDiskCacheLogErrorWriteFailureCodeKey : @(errno)}; + error = [NSError errorWithDomain:PINDiskCacheLogErrorDomain code:PINDiskCacheLogErrorWriteFailure userInfo:userInfo]; + PINDiskCacheLogError(error); } } } else { if (setxattr(PINDiskCacheFileSystemRepresentation(fileURL), PINDiskCacheAgeLimitAttributeName, &ageLimit, sizeof(NSTimeInterval), 0, 0) != 0) { - NSDictionary *userInfo = @{ PINDiskCacheErrorWriteFailureCodeKey : @(errno)}; - error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorWriteFailure userInfo:userInfo]; - PINDiskCacheError(error); + NSDictionary *userInfo = @{ PINDiskCacheLogErrorWriteFailureCodeKey : @(errno)}; + error = [NSError errorWithDomain:PINDiskCacheLogErrorDomain code:PINDiskCacheLogErrorWriteFailure userInfo:userInfo]; + PINDiskCacheLogError(error); } } @@ -1106,7 +1102,7 @@ - (id)objectForKeyedSubscript:(NSString *)key [self lock]; [[NSFileManager defaultManager] removeItemAtPath:[fileURL path] error:&error]; [self unlock]; - PINDiskCacheError(error) + PINDiskCacheLogError(error) PINDiskCacheException(exception); } [self lock]; @@ -1224,7 +1220,7 @@ - (void)setObject:(id )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) { @@ -1233,7 +1229,7 @@ - (void)setObject:(id )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) { diff --git a/Source/Classes/PINRemoteImageManager.m b/Source/Classes/PINRemoteImageManager.m index 8384369c..809607bd 100644 --- a/Source/Classes/PINRemoteImageManager.m +++ b/Source/Classes/PINRemoteImageManager.m @@ -331,7 +331,7 @@ - (void)dealloc 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]; diff --git a/Tests/PINRemoteImageTests.m b/Tests/PINRemoteImageTests.m index d10107a0..0c90852e 100644 --- a/Tests/PINRemoteImageTests.m +++ b/Tests/PINRemoteImageTests.m @@ -798,7 +798,7 @@ - (void)testInvalidObject 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]; From 73f34845a07825280c0c30879733e96f50c3cc4a Mon Sep 17 00:00:00 2001 From: Liang Ma Date: Wed, 1 Sep 2021 13:03:14 -0700 Subject: [PATCH 3/5] not use PINDiskCacheLog as it doesn't use PINCache --- Source/Classes/PINRemoteImageManager.m | 5 +---- Tests/PINRemoteImageTests.m | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/Source/Classes/PINRemoteImageManager.m b/Source/Classes/PINRemoteImageManager.m index 809607bd..403ec38e 100644 --- a/Source/Classes/PINRemoteImageManager.m +++ b/Source/Classes/PINRemoteImageManager.m @@ -329,10 +329,7 @@ - (void)dealloc id obj = (id )object; if ([key hasPrefix:PINRemoteImageCacheKeyResumePrefix]) { 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]; - PINDiskCacheLogError(error); - return data; + return [NSKeyedArchiver archivedDataWithRootObject:obj requiringSecureCoding:NO error:nil]; } else { return [NSKeyedArchiver archivedDataWithRootObject:object]; } diff --git a/Tests/PINRemoteImageTests.m b/Tests/PINRemoteImageTests.m index 0c90852e..9895e7e5 100644 --- a/Tests/PINRemoteImageTests.m +++ b/Tests/PINRemoteImageTests.m @@ -796,10 +796,7 @@ - (void)testInvalidObject PINDiskCache *tempDiskCache = [[PINDiskCache alloc] initWithName:kPINRemoteImageDiskCacheName rootPath:cachePath serializer:^NSData * _Nonnull(id _Nonnull object, NSString * _Nonnull key) { 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]; - PINDiskCacheLogError(error); - return data; + return [NSKeyedArchiver archivedDataWithRootObject:object requiringSecureCoding:NO error:nil]; } else { return [NSKeyedArchiver archivedDataWithRootObject:object]; } From 0d23b577218ce849da4131e2999b261fde550ffa Mon Sep 17 00:00:00 2001 From: Liang Ma Date: Wed, 1 Sep 2021 15:54:30 -0700 Subject: [PATCH 4/5] use requiresSecureCoding:NO for unarchiving as many classes not comform to NSSecureCoding --- Carthage/Checkouts/PINCache/Source/PINDiskCache.m | 9 +++++++-- Source/Classes/PINRemoteImageManager.m | 9 +++++++-- Tests/PINRemoteImageTests.m | 7 +++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m index 307efc2c..b2da57b4 100644 --- a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m +++ b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m @@ -339,8 +339,13 @@ - (PINDiskCacheSerializerBlock)defaultSerializer - (PINDiskCacheDeserializerBlock)defaultDeserializer { return ^id(NSData * data, NSString *key){ - if (@available(iOS 11.0, tvOS 11.0, *)) { - return [NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:data error:nil]; + 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; + id obj = [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; + return obj; } else { return [NSKeyedUnarchiver unarchiveObjectWithData:data]; } diff --git a/Source/Classes/PINRemoteImageManager.m b/Source/Classes/PINRemoteImageManager.m index 403ec38e..a18dfd27 100644 --- a/Source/Classes/PINRemoteImageManager.m +++ b/Source/Classes/PINRemoteImageManager.m @@ -133,7 +133,6 @@ @interface PINRemoteImageManager () PINAlternateRepresentationProvider *_defaultAlternateRepresentationProvider; __weak PINAlternateRepresentationProvider *_alternateRepProvider; NSURLSessionConfiguration *_sessionConfiguration; - } @property (nonatomic, strong) id cache; @@ -158,6 +157,7 @@ @interface PINRemoteImageManager () @property (nonatomic, strong) NSMutableDictionary *httpHeaderFields; @property (nonatomic, readonly) BOOL diskCacheTTLIsEnabled; @property (nonatomic, readonly) BOOL memoryCacheTTLIsEnabled; + #if DEBUG @property (nonatomic, assign) NSUInteger totalDownloads; #endif @@ -338,7 +338,12 @@ - (void)dealloc } deserializer:^id _Nonnull(NSData * _Nonnull data, NSString * _Nonnull key) { if ([key hasPrefix:PINRemoteImageCacheKeyResumePrefix]) { if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) { - return [NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:data error:nil]; + NSError *error = nil; + NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error]; + NSAssert(!error, @"unarchiver init failed with error"); + unarchiver.requiresSecureCoding = NO; + id obj = [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; + return obj; } else { return [NSKeyedUnarchiver unarchiveObjectWithData:data]; } diff --git a/Tests/PINRemoteImageTests.m b/Tests/PINRemoteImageTests.m index 9895e7e5..fedffb59 100644 --- a/Tests/PINRemoteImageTests.m +++ b/Tests/PINRemoteImageTests.m @@ -801,8 +801,11 @@ - (void)testInvalidObject return [NSKeyedArchiver archivedDataWithRootObject:object]; } } deserializer:^id _Nonnull(NSData * _Nonnull data, NSString * _Nonnull key) { - if (@available(iOS 11.0, tvOS 11.0, *)) { - return [NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:data error:nil]; + 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; + id obj = [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; + return obj; } else { return [NSKeyedUnarchiver unarchiveObjectWithData:data]; } From 9472e1cc131e1805451b9830469ab630ac8cb07e Mon Sep 17 00:00:00 2001 From: Liang Ma Date: Wed, 1 Sep 2021 16:58:22 -0700 Subject: [PATCH 5/5] revert renames and address other comments --- .../Checkouts/PINCache/Source/PINDiskCache.h | 16 +++++------- .../Checkouts/PINCache/Source/PINDiskCache.m | 25 +++++++++++-------- Source/Classes/PINRemoteImageManager.m | 3 +-- Tests/PINRemoteImageTests.m | 3 +-- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/Carthage/Checkouts/PINCache/Source/PINDiskCache.h b/Carthage/Checkouts/PINCache/Source/PINDiskCache.h index f53fcf0c..3a4985b4 100644 --- a/Carthage/Checkouts/PINCache/Source/PINDiskCache.h +++ b/Carthage/Checkouts/PINCache/Source/PINDiskCache.h @@ -13,20 +13,16 @@ NS_ASSUME_NONNULL_BEGIN @class PINDiskCache; @class PINOperationQueue; -extern NSString * const PINDiskCacheLogErrorDomain; -extern NSErrorUserInfoKey const PINDiskCacheLogErrorReadFailureCodeKey; -extern NSErrorUserInfoKey const PINDiskCacheLogErrorWriteFailureCodeKey; +extern NSString * const PINDiskCacheErrorDomain; +extern NSErrorUserInfoKey const PINDiskCacheErrorReadFailureCodeKey; +extern NSErrorUserInfoKey const PINDiskCacheErrorWriteFailureCodeKey; extern NSString * const PINDiskCachePrefix; -typedef NS_ENUM(NSInteger, PINDiskCacheErrorType) { - PINDiskCacheLogErrorReadFailure = -1000, - PINDiskCacheLogErrorWriteFailure = -1001, +typedef NS_ENUM(NSInteger, PINDiskCacheError) { + PINDiskCacheErrorReadFailure = -1000, + PINDiskCacheErrorWriteFailure = -1001, }; -#define PINDiskCacheLogError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \ -[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \ -__LINE__, [error localizedDescription]); } - /** A callback block which provides the cache, key and object as arguments */ diff --git a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m index b2da57b4..61128c36 100644 --- a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m +++ b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m @@ -17,12 +17,16 @@ #import #endif +#define PINDiskCacheLogError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \ +[[NSString stringWithUTF8String:__FILE__] lastPathComponent], \ +__LINE__, [error localizedDescription]); } + #define PINDiskCacheException(exception) if (exception) { NSAssert(NO, [exception reason]); } const char * PINDiskCacheAgeLimitAttributeName = "com.pinterest.PINDiskCache.ageLimit"; -NSString * const PINDiskCacheLogErrorDomain = @"com.pinterest.PINDiskCache"; -NSErrorUserInfoKey const PINDiskCacheLogErrorReadFailureCodeKey = @"PINDiskCacheLogErrorReadFailureCodeKey"; -NSErrorUserInfoKey const PINDiskCacheLogErrorWriteFailureCodeKey = @"PINDiskCacheLogErrorWriteFailureCodeKey"; +NSString * const PINDiskCacheErrorDomain = @"com.pinterest.PINDiskCache"; +NSErrorUserInfoKey const PINDiskCacheErrorReadFailureCodeKey = @"PINDiskCacheErrorReadFailureCodeKey"; +NSErrorUserInfoKey const PINDiskCacheErrorWriteFailureCodeKey = @"PINDiskCacheErrorWriteFailureCodeKey"; NSString * const PINDiskCachePrefix = @"com.pinterest.PINDiskCache"; static NSString * const PINDiskCacheSharedName = @"PINDiskCacheShared"; @@ -344,8 +348,7 @@ - (PINDiskCacheDeserializerBlock)defaultDeserializer NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error]; PINDiskCacheLogError(error); unarchiver.requiresSecureCoding = NO; - id obj = [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; - return obj; + return [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; } else { return [NSKeyedUnarchiver unarchiveObjectWithData:data]; } @@ -555,8 +558,8 @@ - (NSUInteger)_locked_initializeDiskPropertiesForFile:(NSURL *)fileURL fileKey:( } else if (res == -1) { // Ignore if the extended attribute was never recorded for this file. if (errno != ENOATTR) { - NSDictionary *userInfo = @{ PINDiskCacheLogErrorReadFailureCodeKey : @(errno)}; - error = [NSError errorWithDomain:PINDiskCacheLogErrorDomain code:PINDiskCacheLogErrorReadFailure userInfo:userInfo]; + NSDictionary *userInfo = @{ PINDiskCacheErrorReadFailureCodeKey : @(errno)}; + error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorReadFailure userInfo:userInfo]; PINDiskCacheLogError(error); } } @@ -649,15 +652,15 @@ - (BOOL)_locked_setAgeLimit:(NSTimeInterval)ageLimit forURL:(NSURL *)fileURL if (removexattr(PINDiskCacheFileSystemRepresentation(fileURL), PINDiskCacheAgeLimitAttributeName, 0) != 0) { // Ignore if the extended attribute was never recorded for this file. if (errno != ENOATTR) { - NSDictionary *userInfo = @{ PINDiskCacheLogErrorWriteFailureCodeKey : @(errno)}; - error = [NSError errorWithDomain:PINDiskCacheLogErrorDomain code:PINDiskCacheLogErrorWriteFailure userInfo:userInfo]; + NSDictionary *userInfo = @{ PINDiskCacheErrorWriteFailureCodeKey : @(errno)}; + error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorWriteFailure userInfo:userInfo]; PINDiskCacheLogError(error); } } } else { if (setxattr(PINDiskCacheFileSystemRepresentation(fileURL), PINDiskCacheAgeLimitAttributeName, &ageLimit, sizeof(NSTimeInterval), 0, 0) != 0) { - NSDictionary *userInfo = @{ PINDiskCacheLogErrorWriteFailureCodeKey : @(errno)}; - error = [NSError errorWithDomain:PINDiskCacheLogErrorDomain code:PINDiskCacheLogErrorWriteFailure userInfo:userInfo]; + NSDictionary *userInfo = @{ PINDiskCacheErrorWriteFailureCodeKey : @(errno)}; + error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorWriteFailure userInfo:userInfo]; PINDiskCacheLogError(error); } } diff --git a/Source/Classes/PINRemoteImageManager.m b/Source/Classes/PINRemoteImageManager.m index a18dfd27..11515596 100644 --- a/Source/Classes/PINRemoteImageManager.m +++ b/Source/Classes/PINRemoteImageManager.m @@ -342,8 +342,7 @@ - (void)dealloc NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error]; NSAssert(!error, @"unarchiver init failed with error"); unarchiver.requiresSecureCoding = NO; - id obj = [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; - return obj; + return [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; } else { return [NSKeyedUnarchiver unarchiveObjectWithData:data]; } diff --git a/Tests/PINRemoteImageTests.m b/Tests/PINRemoteImageTests.m index fedffb59..13063de9 100644 --- a/Tests/PINRemoteImageTests.m +++ b/Tests/PINRemoteImageTests.m @@ -804,8 +804,7 @@ - (void)testInvalidObject 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; - id obj = [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; - return obj; + return [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; } else { return [NSKeyedUnarchiver unarchiveObjectWithData:data]; }