Skip to content

Commit

Permalink
Merge branch 'release/0.12.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimme committed May 27, 2016
2 parents ef6e9dd + 33e9f41 commit 2e08faa
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 241 deletions.
2 changes: 1 addition & 1 deletion AFCache.podspec
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "AFCache"
s.version = "0.11.5"
s.version = "0.12.0"
s.summary = "AFCache is an HTTP disk cache for use on iPhone/iPad and OSX."

s.description = <<-DESC
Expand Down
36 changes: 18 additions & 18 deletions AFCache.xcodeproj/project.pbxproj
Expand Up @@ -78,8 +78,10 @@
C7503D47198640AA0032E451 /* AFDownloadOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = C7503D44198640AA0032E451 /* AFDownloadOperation.h */; };
C7503D48198640AA0032E451 /* AFDownloadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = C7503D45198640AA0032E451 /* AFDownloadOperation.m */; };
C7503D49198640AA0032E451 /* AFDownloadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = C7503D45198640AA0032E451 /* AFDownloadOperation.m */; };
E33490CD199E24B200DE6D82 /* NSFileHandle+AFCache.h in Headers */ = {isa = PBXBuildFile; fileRef = E33490CB199E24B200DE6D82 /* NSFileHandle+AFCache.h */; };
E33490CE199E24B200DE6D82 /* NSFileHandle+AFCache.m in Sources */ = {isa = PBXBuildFile; fileRef = E33490CC199E24B200DE6D82 /* NSFileHandle+AFCache.m */; };
C765AB591CEB39F200A47B4A /* AFCache+FileAttributes.h in Headers */ = {isa = PBXBuildFile; fileRef = C765AB571CEB39F200A47B4A /* AFCache+FileAttributes.h */; };
C765AB5A1CEB39F200A47B4A /* AFCache+FileAttributes.m in Sources */ = {isa = PBXBuildFile; fileRef = C765AB581CEB39F200A47B4A /* AFCache+FileAttributes.m */; };
C765AB5D1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.h in Headers */ = {isa = PBXBuildFile; fileRef = C765AB5B1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.h */; };
C765AB5E1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.m in Sources */ = {isa = PBXBuildFile; fileRef = C765AB5C1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.m */; };
E369E20919B0711700EAC9FE /* AFCache+DeprecatedAPI.h in Headers */ = {isa = PBXBuildFile; fileRef = E369E20719B0711700EAC9FE /* AFCache+DeprecatedAPI.h */; };
E369E20A19B0711700EAC9FE /* AFCache+DeprecatedAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = E369E20819B0711700EAC9FE /* AFCache+DeprecatedAPI.m */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -194,8 +196,10 @@
C73C71CB19816F13008EDA23 /* AFRequestConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFRequestConfiguration.m; path = src/shared/AFRequestConfiguration.m; sourceTree = "<group>"; };
C7503D44198640AA0032E451 /* AFDownloadOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AFDownloadOperation.h; path = src/shared/AFDownloadOperation.h; sourceTree = "<group>"; };
C7503D45198640AA0032E451 /* AFDownloadOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFDownloadOperation.m; path = src/shared/AFDownloadOperation.m; sourceTree = "<group>"; };
E33490CB199E24B200DE6D82 /* NSFileHandle+AFCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSFileHandle+AFCache.h"; path = "src/shared/NSFileHandle+AFCache.h"; sourceTree = "<group>"; };
E33490CC199E24B200DE6D82 /* NSFileHandle+AFCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSFileHandle+AFCache.m"; path = "src/shared/NSFileHandle+AFCache.m"; sourceTree = "<group>"; };
C765AB571CEB39F200A47B4A /* AFCache+FileAttributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "AFCache+FileAttributes.h"; path = "src/shared/AFCache+FileAttributes.h"; sourceTree = "<group>"; };
C765AB581CEB39F200A47B4A /* AFCache+FileAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "AFCache+FileAttributes.m"; path = "src/shared/AFCache+FileAttributes.m"; sourceTree = "<group>"; };
C765AB5B1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "AFCacheableItem+FileAttributes.h"; path = "src/shared/AFCacheableItem+FileAttributes.h"; sourceTree = "<group>"; };
C765AB5C1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "AFCacheableItem+FileAttributes.m"; path = "src/shared/AFCacheableItem+FileAttributes.m"; sourceTree = "<group>"; };
E369E20719B0711700EAC9FE /* AFCache+DeprecatedAPI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "AFCache+DeprecatedAPI.h"; path = "src/shared/AFCache+DeprecatedAPI.h"; sourceTree = "<group>"; };
E369E20819B0711700EAC9FE /* AFCache+DeprecatedAPI.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "AFCache+DeprecatedAPI.m"; path = "src/shared/AFCache+DeprecatedAPI.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -354,9 +358,13 @@
children = (
05C9BADD132A291B0087CEA1 /* AFCache.h */,
05C9BADE132A291B0087CEA1 /* AFCache.m */,
C765AB571CEB39F200A47B4A /* AFCache+FileAttributes.h */,
C765AB581CEB39F200A47B4A /* AFCache+FileAttributes.m */,
05C9BAE3132A291B0087CEA1 /* AFCache+PrivateAPI.h */,
05C9BAE4132A291B0087CEA1 /* AFCacheableItem.h */,
05C9BAE5132A291B0087CEA1 /* AFCacheableItem.m */,
C765AB5B1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.h */,
C765AB5C1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.m */,
05C9BAE8132A291B0087CEA1 /* AFCacheableItemInfo.h */,
05C9BAE9132A291B0087CEA1 /* AFCacheableItemInfo.m */,
C73C71CA19816F13008EDA23 /* AFRequestConfiguration.h */,
Expand Down Expand Up @@ -391,7 +399,6 @@
isa = PBXGroup;
children = (
E369E20619B070FB00EAC9FE /* Deprecated */,
E33490CA199E182A00DE6D82 /* Categories */,
05238D15151B6E5C0015D70E /* util */,
05238D14151B6E470015D70E /* core */,
05238D13151B6E2D0015D70E /* MimeTypes */,
Expand Down Expand Up @@ -493,15 +500,6 @@
name = "Supporting Files";
sourceTree = "<group>";
};
E33490CA199E182A00DE6D82 /* Categories */ = {
isa = PBXGroup;
children = (
E33490CB199E24B200DE6D82 /* NSFileHandle+AFCache.h */,
E33490CC199E24B200DE6D82 /* NSFileHandle+AFCache.m */,
);
name = Categories;
sourceTree = "<group>";
};
E369E20619B070FB00EAC9FE /* Deprecated */ = {
isa = PBXGroup;
children = (
Expand All @@ -520,6 +518,7 @@
files = (
05C9BB6F132A30E60087CEA1 /* AFCacheLib.h in Headers */,
046BEFAB152D180A00FE16B8 /* AFCache.h in Headers */,
C765AB5D1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.h in Headers */,
046BEFAC152D180A00FE16B8 /* AFCacheableItem.h in Headers */,
046BEFAD152D180A00FE16B8 /* AFCacheableItemInfo.h in Headers */,
046BEFAE152D180A00FE16B8 /* AFMediaTypeParser.h in Headers */,
Expand All @@ -530,7 +529,7 @@
C73C71CC19816F13008EDA23 /* AFRequestConfiguration.h in Headers */,
04007352153242D400335735 /* AFCache+Mimetypes.h in Headers */,
E369E20919B0711700EAC9FE /* AFCache+DeprecatedAPI.h in Headers */,
E33490CD199E24B200DE6D82 /* NSFileHandle+AFCache.h in Headers */,
C765AB591CEB39F200A47B4A /* AFCache+FileAttributes.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -594,7 +593,7 @@
name = AFCacheTests;
productName = AFCacheTests;
productReference = 050D30C6132A276A003809FC /* AFCacheTests.octest */;
productType = "com.apple.product-type.bundle";
productType = "com.apple.product-type.bundle.ocunit-test";
};
05FA23391357515400050BCB /* afcpkg */ = {
isa = PBXNativeTarget;
Expand Down Expand Up @@ -649,7 +648,7 @@
name = AFCacheOSXStaticTests;
productName = AFCacheOSXStaticTests;
productReference = 05FA23691357539C00050BCB /* AFCacheOSXStaticTests.octest */;
productType = "com.apple.product-type.bundle";
productType = "com.apple.product-type.bundle.ocunit-test";
};
/* End PBXNativeTarget section */

Expand Down Expand Up @@ -778,6 +777,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C765AB5E1CEB3BD500A47B4A /* AFCacheableItem+FileAttributes.m in Sources */,
C765AB5A1CEB39F200A47B4A /* AFCache+FileAttributes.m in Sources */,
05C9BAF3132A291B0087CEA1 /* AFCache.m in Sources */,
05C9BAF5132A291B0087CEA1 /* AFCache+Mimetypes.m in Sources */,
05C9BAFA132A291B0087CEA1 /* AFCacheableItem.m in Sources */,
Expand All @@ -789,7 +790,6 @@
C7503D48198640AA0032E451 /* AFDownloadOperation.m in Sources */,
05C9BB05132A291B0087CEA1 /* DateParser.m in Sources */,
05C9BB19132A29370087CEA1 /* AFRegexString.m in Sources */,
E33490CE199E24B200DE6D82 /* NSFileHandle+AFCache.m in Sources */,
05C491FB150F9CB1009EDA8F /* AFMediaTypeParser.m in Sources */,
05C491FF150F9CBA009EDA8F /* AFHTTPURLProtocol.m in Sources */,
C73C71CE19816F13008EDA23 /* AFRequestConfiguration.m in Sources */,
Expand Down
13 changes: 13 additions & 0 deletions src/shared/AFCache+FileAttributes.h
@@ -0,0 +1,13 @@
//
// AFCache+FileAttributes.h
// AFCache
//
// Created by Sebastian Grimme on 17.05.16.
// Copyright © 2016 Artifacts - Fine Software Development. All rights reserved.
//

#import <AFCache/AFCache.h>

@interface AFCache (FileAttributes)
- (uint64_t)setContentLengthForFileAtPath:(NSString*)filePath;
@end
33 changes: 33 additions & 0 deletions src/shared/AFCache+FileAttributes.m
@@ -0,0 +1,33 @@
//
// AFCache+FileAttributes.m
// AFCache
//
// Created by Sebastian Grimme on 17.05.16.
// Copyright © 2016 Artifacts - Fine Software Development. All rights reserved.
//

#import "AFCache+FileAttributes.h"

#import "AFCacheableItem+FileAttributes.h"
#import "AFCache+PrivateAPI.h"
#import "AFCache_Logging.h"
#include <sys/xattr.h>

@implementation AFCache (FileAttributes)

- (uint64_t)setContentLengthForFileAtPath:(NSString*)filePath {
NSError* err = nil;
NSDictionary* attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&err];
if (err) {
AFLog(@"Could not get file attributes for %@", filename);
return 0;
}
uint64_t fileSize = [attrs fileSize];
if (0 != setxattr(filePath.fileSystemRepresentation, kAFCacheContentLengthFileAttribute, &fileSize, sizeof(fileSize), 0, 0)) {
AFLog(@"Could not set content length for file %@", filename);
return 0;
}
return fileSize;
}

@end
11 changes: 7 additions & 4 deletions src/shared/AFCache+Packaging.h
Expand Up @@ -13,11 +13,14 @@
// TODO: Is this a real category? It relays on the existence of properties (e.g. packageArchiveQueue) that are only used by this category
@interface AFCache (Packaging)


- (BOOL)importCacheableItem:(AFCacheableItem*)cacheableItem withData:(NSData*)theData;
- (AFCacheableItem *)importObjectForURL:(NSURL *)url data:(NSData *)data;
- (AFCacheableItem *)requestPackageArchive: (NSURL *) url delegate: (id) aDelegate;
- (AFCacheableItem *)requestPackageArchive: (NSURL *) url delegate: (id) aDelegate username: (NSString*) username password: (NSString*) password;
- (BOOL)importCacheableItem:(AFCacheableItem*)cacheableItem dataWithFileAtURL:(NSURL*)URL;
- (AFCacheableItem*)importObjectForURL:(NSURL*)url data:(NSData*)data;
- (AFCacheableItem*)importObjectForURL:(NSURL*)url dataWithFileAtURL:(NSURL*)URL;

- (AFCacheableItem *)requestPackageArchive:(NSURL*)url delegate:(id)aDelegate;
- (AFCacheableItem *)requestPackageArchive:(NSURL*)url delegate:(id)aDelegate username:(NSString*)username password:(NSString*)password;

- (void)packageArchiveDidFinishLoading: (AFCacheableItem *) cacheableItem;
- (NSString*)userDataPathForPackageArchiveKey:(NSString*)archiveKey;
- (AFPackageInfo*)packageInfoForURL:(NSURL*)url;
Expand Down
75 changes: 47 additions & 28 deletions src/shared/AFCache+Packaging.m
Expand Up @@ -6,9 +6,10 @@
// Copyright 2010 Artifacts - Fine Software Development. All rights reserved.
//

#include <sys/xattr.h>
#import "AFCache+PrivateAPI.h"
#import "AFCache+FileAttributes.h"
#import "AFCacheableItem+Packaging.h"
#import "AFCacheableItem+FileAttributes.h"
#import "ZipArchive.h"
#import "DateParser.h"
#import "AFPackageInfo.h"
Expand Down Expand Up @@ -226,7 +227,7 @@ - (AFPackageInfo*)newPackageInfoByImportingCacheManifestAtPath:(NSString*)manife
NSLog(@"No filename given for entry in line %d: %@", line, entry);
}

uint64_t contentLength = [self setContentLengthForFile:[urlCacheStorePath stringByAppendingPathComponent: filename]];
uint64_t contentLength = [self setContentLengthForFileAtPath:[urlCacheStorePath stringByAppendingPathComponent: filename]];

info.contentLength = contentLength;

Expand All @@ -252,26 +253,6 @@ - (AFPackageInfo*)newPackageInfoByImportingCacheManifestAtPath:(NSString*)manife
return packageInfo;
}

// TODO: Move to NSFileHandle+AFCache, merge with method #flagAsDownloadStartedWithContentLength:
- (uint64_t)setContentLengthForFile:(NSString*)filename
{
const char* cfilename = [filename fileSystemRepresentation];

NSError* err = nil;
NSDictionary* attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filename error:&err];
if (err) {
AFLog(@"Could not get file attributes for %@", filename);
return 0;
}
uint64_t fileSize = [attrs fileSize];
if (0 != setxattr(cfilename, kAFCacheContentLengthFileAttribute, &fileSize, sizeof(fileSize), 0, 0)) {
AFLog(@"Could not set content length for file %@", filename);
return 0;
}

return fileSize;
}

- (void)storeCacheInfo:(NSDictionary*)dictionary {
@synchronized(self) {
for (NSString* key in dictionary) {
Expand All @@ -293,26 +274,64 @@ - (void)performUnarchivingFailedWithItem:(AFCacheableItem*)cacheableItem
cacheableItem.info.packageArchiveStatus = kAFCachePackageArchiveStatusUnarchivingFailed;
}

#pragma mark - import

// import and optionally overwrite a cacheableitem. might fail if a download with the very same url is in progress.
- (BOOL)importCacheableItem:(AFCacheableItem*)cacheableItem withData:(NSData*)theData {
if (cacheableItem==nil || [self isQueuedOrDownloadingURL:cacheableItem.url]) return NO;
[cacheableItem setDataAndFile:theData];
if (cacheableItem == nil || [self isQueuedOrDownloadingURL:cacheableItem.url]) {
return NO;
}

[cacheableItem setDataAndFile:theData];
[self.cachedItemInfos setObject:cacheableItem.info forKey:[cacheableItem.url absoluteString]];
[self archive];

return YES;
}

- (AFCacheableItem *)importObjectForURL:(NSURL *)url data:(NSData *)data
{
- (BOOL)importCacheableItem:(AFCacheableItem*)cacheableItem dataWithFileAtURL:(NSURL *)URL {
if (cacheableItem == nil || [self isQueuedOrDownloadingURL:cacheableItem.url]) {
return NO;
}

NSString *fullPathForCacheableItem = [self fullPathForCacheableItem:cacheableItem];

NSError *error = nil;
BOOL didMoveItemAtPath = [[NSFileManager defaultManager] moveItemAtPath:URL.path toPath:fullPathForCacheableItem error:&error];
if (!didMoveItemAtPath) {
return NO;
}

NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:fullPathForCacheableItem] options:NSDataReadingMappedIfSafe error:nil];
cacheableItem.data = data;
cacheableItem.info.contentLength = [data length];

[self.cachedItemInfos setObject:cacheableItem.info forKey:[cacheableItem.url absoluteString]];
[self archive];

return YES;
}

- (AFCacheableItem *)importObjectForURL:(NSURL *)url data:(NSData *)data {
AFCacheableItem *cachedItem = [self cacheableItemFromCacheStore:url];
if (cachedItem) {
return cachedItem;
}
else {
AFCacheableItem *item = [[AFCacheableItem alloc] initWithURL:url lastModified:[NSDate date] expireDate:nil];

[self importCacheableItem:item withData:data];

return item;
}
}

- (AFCacheableItem *)importObjectForURL:(NSURL *)url dataWithFileAtURL:(NSURL*)URL {
AFCacheableItem *cachedItem = [self cacheableItemFromCacheStore:url];
if (cachedItem) {
return cachedItem;
}
else {
AFCacheableItem *item = [[AFCacheableItem alloc] initWithURL:url lastModified:[NSDate date] expireDate:nil];
[self importCacheableItem:item dataWithFileAtURL:URL];
return item;
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/shared/AFCache+PrivateAPI.h
Expand Up @@ -27,12 +27,11 @@

- (void)updateModificationDataAndTriggerArchiving:(AFCacheableItem *)obj;


- (void)setConnectedToNetwork:(BOOL)connected;
- (void)reinitialize;
- (void)removeCacheEntryWithFilePath:(NSString*)filePath fileOnly:(BOOL) fileOnly;

- (NSFileHandle*)createFileForItem:(AFCacheableItem*)cacheableItem;
- (NSOutputStream*)createOutputStreamForItem:(AFCacheableItem*)cacheableItem;
- (void)addItemToDownloadQueue:(AFCacheableItem*)item;
- (BOOL)isQueuedURL:(NSURL*)url;
- (BOOL)_fileExistsOrPendingForCacheableItem:(AFCacheableItem*)item;
Expand All @@ -45,20 +44,14 @@
@end

@interface AFCacheableItem (PrivateAPI)

- (BOOL)isQueuedOrDownloading;
- (BOOL)hasDownloadFileAttribute;
- (BOOL)hasValidContentLength;
- (uint64_t)getContentLengthFromFile;

// Making synthesized getter and setter for private property public for private API
- (void)setHasReturnedCachedItemBeforeRevalidation:(BOOL)value;
- (BOOL)hasReturnedCachedItemBeforeRevalidation;

@end

@interface AFCacheableItemInfo (PrivateAPI)

- (NSString*)newUniqueFilename;

@end
2 changes: 0 additions & 2 deletions src/shared/AFCache.h
Expand Up @@ -65,8 +65,6 @@
#define AFCachingURLHeader @"X-AFCache"
#define AFCacheInternalRequestHeader @"X-AFCache-IntReq"

extern const char* kAFCacheContentLengthFileAttribute;
extern const char* kAFCacheDownloadingFileAttribute;
extern const double kAFCacheInfiniteFileSize;

enum {
Expand Down

0 comments on commit 2e08faa

Please sign in to comment.