Oolite
Loading...
Searching...
No Matches
NSData(OOExtensions) Category Reference

#include <NSDataOOExtensions.h>

Class Methods

(instancetype) + oo_dataWithOXZFile:

Detailed Description

Definition at line 32 of file NSDataOOExtensions.h.

Method Documentation

◆ oo_dataWithOXZFile:

+ (instancetype) oo_dataWithOXZFile: (NSString*) path

Definition at line 37 of file NSDataOOExtensions.m.

37 :(NSString*)path
38{
39 NSUInteger i, cl;
40 NSArray* components = [path pathComponents];
41 cl = [components count];
42 for (i = 0; i < cl; i++) {
43 NSString* component = [components objectAtIndex:i];
44 if ([[[component pathExtension] lowercaseString] isEqualToString:@"oxz"]) {
45 break;
46 }
47 }
48 // if i == cl then the path is entirely uncompressed
49 if (i == cl) {
50/* -initWithContentsOfMappedFile fails quietly under OS X if there's no file,
51 but GNUstep complains. */
52#if OOLITE_MAC_OS_X
53 return [[[NSData alloc] initWithContentsOfMappedFile:path] autorelease];
54#else
55 NSFileManager* fmgr = [NSFileManager defaultManager];
56 BOOL dir;
57
58 if ([fmgr fileExistsAtPath:path isDirectory:&dir]) {
59 if (!dir) {
60 if ([[fmgr fileAttributesAtPath:path traverseLink:NO] fileSize] == 0) {
61 OOLog(kOOLogFileNotFound, @"Expected file but found empty file at %@", path);
62 } else {
63 return [[[NSData alloc] initWithContentsOfMappedFile:path] autorelease];
64 }
65 } else {
66 OOLog(kOOLogFileNotFound, @"Expected file but found directory at %@", path);
67 }
68 }
69 return nil;
70#endif
71 }
72 // otherwise components 0..i are the OXZ path, and i+1..n are the
73 // path inside the OXZ
74 NSRange range;
75 range.location = 0;
76 range.length = i + 1;
77 NSString* zipFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
78 range.location = i + 1;
79 range.length = cl - (i + 1);
80 NSString* containedFile = [NSString pathWithComponents:[components subarrayWithRange:range]];
81
82 unzFile uf = NULL;
83 const char* zipname = [zipFile UTF8String];
84 if (zipname != NULL) {
85 uf = unzOpen64(zipname);
86 }
87 if (uf == NULL) {
88 // This is not necessarily an error - the OXZ manager tries to
89 // do this as a test for the presence of managed OXZs
90 // OOLog(kOOLogFileNotFound, @"Could not unzip OXZ at %@", zipFile);
91 return nil;
92 }
93 const char* filename = [containedFile UTF8String];
94 // unzLocateFile(*, *, 1) = case-sensitive extract
95 if (unzLocateFile(uf, filename, 1) != UNZ_OK) {
96 unzClose(uf);
97 /* Much of the time this function is called with the
98 * expectation that the file may not necessarily exist -
99 * e.g. on plist merges, config scans, etc. So don't add log
100 * entries for this failure mode */
101 // OOLog(kOOLogFileNotFound, @"Could not find %@ within OXZ at %@", containedFile, zipFile);
102 return nil;
103 }
104
105 int err = UNZ_OK;
106 unz_file_info64 file_info = { 0 };
107 err = unzGetCurrentFileInfo64(uf, &file_info, NULL, 0, NULL, 0, NULL, 0);
108 if (err != UNZ_OK) {
109 unzClose(uf);
110 OOLog(kOOLogFileNotFound, @"Could not get properties of %@ within OXZ at %@", containedFile, zipFile);
111 return nil;
112 }
113
114 err = unzOpenCurrentFile(uf);
115 if (err != UNZ_OK) {
116 unzClose(uf);
117 OOLog(kOOLogFileNotFound, @"Could not read %@ within OXZ at %@", containedFile, zipFile);
118 return nil;
119 }
120
121 NSMutableData* tmp = [NSMutableData dataWithCapacity:file_info.uncompressed_size];
122 void* buf = (void*)malloc(ZIP_BUFFER_SIZE);
123 do {
125 if (err < 0) {
126 OOLog(kOOLogFileNotFound, @"Could not read %@ within OXZ at %@ (err %d)", containedFile, zipFile, err);
127 break;
128 }
129 if (err == 0) {
130 break;
131 }
132 [tmp appendBytes:buf length:err];
133 } while (err > 0);
134 free(buf);
135
136 err = unzCloseCurrentFile(uf);
137 if (err != UNZ_OK) {
138 unzClose(uf);
139 OOLog(kOOLogFileNotFound, @"Could not close %@ within OXZ at %@", containedFile, zipFile);
140 return nil;
141 }
142
143 unzClose(uf);
144 return [[tmp retain] autorelease];
145}
#define ZIP_BUFFER_SIZE
#define OOLog(class, format,...)
Definition OOLogging.h:84
NSString *const kOOLogFileNotFound
Definition OOLogging.m:601
return nil
const char * filename
Definition ioapi.h:125
voidpf void * buf
Definition ioapi.h:126
int ZEXPORT unzLocateFile(unzFile file, const char *szFileName, int iCaseSensitivity)
Definition unzip.c:1161
int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *szFileName, uLong fileNameBufferSize, void *extraField, uLong extraFieldBufferSize, char *szComment, uLong commentBufferSize)
Definition unzip.c:1058
int ZEXPORT unzOpenCurrentFile(unzFile file)
Definition unzip.c:1558
int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len)
Definition unzip.c:1600
unzFile ZEXPORT unzOpen64(const void *path)
Definition unzip.c:758
int ZEXPORT unzCloseCurrentFile(unzFile file)
Definition unzip.c:1893
int ZEXPORT unzClose(unzFile file)
Definition unzip.c:768
struct unz_file_info64_s unz_file_info64
voidp unzFile
Definition unzip.h:72
#define UNZ_OK
Definition unzip.h:75

References kOOLogFileNotFound, nil, OOLog, UNZ_OK, unzClose(), unzCloseCurrentFile(), unzGetCurrentFileInfo64(), unzLocateFile(), unzOpen64(), unzOpenCurrentFile(), unzReadCurrentFile(), and ZIP_BUFFER_SIZE.

Here is the call graph for this function:

The documentation for this category was generated from the following files: