Overview
Comment: | Refactor OFZIP
This splits out ZIP archive handling from the main file in preparation |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
4300366b0bd01b696a851a942b142524 |
User & Date: | js on 2016-04-24 02:01:37 |
Other Links: | manifest | tags |
Context
2016-04-24
| ||
02:32 | OFZIP: Add GZIP support check-in: 34e0dcaf87 user: js tags: trunk | |
02:01 | Refactor OFZIP check-in: 4300366b0b user: js tags: trunk | |
2016-04-23
| ||
19:55 | Add OFGZIPStream check-in: 7c3f557d94 user: js tags: trunk | |
Changes
Added utils/ofzip/Archive.h version [138de6bf97].
> > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 * Jonathan Schleifer <js@heap.zone> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" #import "OFFile.h" #import "OFArray.h" @protocol Archive <OFObject> + (instancetype)archiveWithFile: (OFFile*)file; - initWithFile: (OFFile*)file; - (void)listFiles; - (void)extractFiles: (OFArray OF_GENERIC(OFString*)*)files; @end |
Modified utils/ofzip/Makefile from [91db3fb8a7] to [dc2fb4a588].
1 2 3 | include ../../extra.mk PROG = ofzip${PROG_SUFFIX} | | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | include ../../extra.mk PROG = ofzip${PROG_SUFFIX} SRCS = OFZIP.m \ ZIPArchive.m include ../../buildsys.mk ${PROG}: ${LIBOBJFW_DEP_LVL2} CPPFLAGS += -I../../src -I../../src/runtime -I../../src/exceptions -I../.. LIBS := -L../../src -lobjfw ${LIBS} LD = ${OBJC} LDFLAGS += ${LDFLAGS_RPATH} |
Added utils/ofzip/OFZIP.h version [991ecbd6f2].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 * Jonathan Schleifer <js@heap.zone> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFObject.h" #import "Archive.h" @interface OFZIP: OFObject { int8_t _override; @public int8_t _outputLevel; int _exitStatus; } - (id <Archive>)openArchiveWithPath: (OFString*)path; - (bool)shouldExtractFile: (OFString*)fileName outFileName: (OFString*)outFileName; - (ssize_t)copyBlockFromStream: (OFStream*)input toStream: (OFStream*)output fileName: (OFString*)fileName; @end |
Modified utils/ofzip/OFZIP.m from [7f837623bc] to [f64cd875b8].
︙ | ︙ | |||
16 17 18 19 20 21 22 | #include "config.h" #include <string.h> #import "OFApplication.h" #import "OFArray.h" | < | | | | < < < < < < < < < < < < < < < < < < < | 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include "config.h" #include <string.h> #import "OFApplication.h" #import "OFArray.h" #import "OFFile.h" #import "OFFileManager.h" #import "OFOptionsParser.h" #import "OFStdIOStream.h" #import "OFZIP.h" #import "ZIPArchive.h" #import "OFCreateDirectoryFailedException.h" #import "OFInvalidFormatException.h" #import "OFOpenItemFailedException.h" #import "OFReadFailedException.h" #import "OFWriteFailedException.h" #define BUFFER_SIZE 4096 OF_APPLICATION_DELEGATE(OFZIP) static void help(OFStream *stream, bool full, int status) { [stream writeFormat: @"Usage: %@ -[fhlnqvx] archive.zip [file1 file2 ...]\n", |
︙ | ︙ | |||
86 87 88 89 90 91 92 | { [of_stderr writeFormat: @"Error: -%C / --%@ and -%C / --%@ are mutually exclusive!\n", shortOption1, longOption1, shortOption2, longOption2]; [OFApplication terminateWithStatus: 1]; } | < < < < < < < < < < < < < < < < < < | | 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | { [of_stderr writeFormat: @"Error: -%C / --%@ and -%C / --%@ are mutually exclusive!\n", shortOption1, longOption1, shortOption2, longOption2]; [OFApplication terminateWithStatus: 1]; } @implementation OFZIP - (void)applicationDidFinishLaunching { const of_options_parser_option_t options[] = { { 'f', @"force", 0, NULL, NULL }, { 'h', @"help", 0, NULL, NULL }, { 'l', @"list", 0, NULL, NULL }, { 'n', @"no-clobber", 0, NULL, NULL }, { 'q', @"quiet", 0, NULL, NULL }, { 'v', @"verbose", 0, NULL, NULL }, { 'x', @"extract", 0, NULL, NULL }, { '\0', nil, 0, NULL, NULL } }; OFOptionsParser *optionsParser = [OFOptionsParser parserWithOptions: options]; of_unichar_t option, mode = '\0'; OFArray OF_GENERIC(OFString*) *remainingArguments, *files; id <Archive> archive; while ((option = [optionsParser nextOption]) != '\0') { switch (option) { case 'f': if (_override < 0) mutuallyExclusiveError( 'f', @"force", 'n', @"no-clobber"); |
︙ | ︙ | |||
189 190 191 192 193 194 195 196 197 198 199 200 201 | [optionsParser lastOption]]; [OFApplication terminateWithStatus: 1]; } } remainingArguments = [optionsParser remainingArguments]; switch (mode) { case 'l': if ([remainingArguments count] != 1) help(of_stderr, false, 1); | > < < < | < < | < | 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | [optionsParser lastOption]]; [OFApplication terminateWithStatus: 1]; } } remainingArguments = [optionsParser remainingArguments]; archive = [self openArchiveWithPath: [remainingArguments firstObject]]; switch (mode) { case 'l': if ([remainingArguments count] != 1) help(of_stderr, false, 1); [archive listFiles]; break; case 'x': if ([remainingArguments count] < 1) help(of_stderr, false, 1); files = [remainingArguments objectsInRange: of_range(1, [remainingArguments count] - 1)]; @try { [archive extractFiles: files]; } @catch (OFCreateDirectoryFailedException *e) { [of_stderr writeFormat: @"\rFailed to create directory %@: %s\n", [e path], strerror([e errNo])]; _exitStatus = 1; } @catch (OFOpenItemFailedException *e) { [of_stderr writeFormat: |
︙ | ︙ | |||
233 234 235 236 237 238 239 | help(of_stderr, true, 1); break; } [OFApplication terminateWithStatus: _exitStatus]; } | | > | | > > > > > < < < < | | | < | < < < < < < < < | < < | < < < < < < < | < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | | | < | | | < | | | | | | | < | | | | | | > | < | < | > | | > | | > | < | | < > | | | | | | < | | < < | | | | | | < | | < < | | < < < < < < < < < < < < < < < < < < < < < < < < < < < | 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | help(of_stderr, true, 1); break; } [OFApplication terminateWithStatus: _exitStatus]; } - (id <Archive>)openArchiveWithPath: (OFString*)path { OFFile *file; id <Archive> archive; @try { file = [OFFile fileWithPath: path mode: @"rb"]; } @catch (OFOpenItemFailedException *e) { [of_stderr writeFormat: @"Failed to open file %@: %s\n", [e path], strerror([e errNo])]; [OFApplication terminateWithStatus: 1]; } @try { archive = [ZIPArchive archiveWithFile: file]; } @catch (OFReadFailedException *e) { [of_stderr writeFormat: @"Failed to read file %@: %s\n", path, strerror([e errNo])]; [OFApplication terminateWithStatus: 1]; } @catch (OFInvalidFormatException *e) { [of_stderr writeFormat: @"File %@ is not a valid archive!\n", path]; [OFApplication terminateWithStatus: 1]; } return archive; } - (bool)shouldExtractFile: (OFString*)fileName outFileName: (OFString*)outFileName { OFString *line; if (_override == 1 || ![[OFFileManager defaultManager] fileExistsAtPath: outFileName]) return true; if (_override == -1) { if (_outputLevel >= 0) [of_stdout writeLine: @" skipped"]; return false; } do { [of_stderr writeFormat: @"\rOverride %@? [ynAN?] ", fileName]; line = [of_stdin readLine]; if ([line isEqual: @"?"]) [of_stderr writeString: @" y: yes\n" @" n: no\n" @" A: always\n" @" N: never\n"]; } while (![line isEqual: @"y"] && ![line isEqual: @"n"] && ![line isEqual: @"N"] && ![line isEqual: @"A"]); if ([line isEqual: @"A"]) _override = 1; else if ([line isEqual: @"N"]) _override = -1; if ([line isEqual: @"n"] || [line isEqual: @"N"]) { if (_outputLevel >= 0) [of_stdout writeFormat: @"Skipping %@...\n", fileName]; return false; } if (_outputLevel >= 0) [of_stdout writeFormat: @"Extracting %@...", fileName]; return true; } - (ssize_t)copyBlockFromStream: (OFStream*)input toStream: (OFStream*)output fileName: (OFString*)fileName { char buffer[BUFFER_SIZE]; size_t length; @try { length = [input readIntoBuffer: buffer length: BUFFER_SIZE]; } @catch (OFReadFailedException *e) { [of_stderr writeFormat: @"\rFailed to read file %@: %s\n", fileName, strerror([e errNo])]; return -1; } @try { [output writeBuffer: buffer length: length]; } @catch (OFWriteFailedException *e) { [of_stderr writeFormat: @"\rFailed to write file %@: %s\n", fileName, strerror([e errNo])]; return -1; } return length; } @end |
Added utils/ofzip/ZIPArchive.h version [c7708d9e69].
> > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 * Jonathan Schleifer <js@heap.zone> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #import "OFZIPArchive.h" #import "Archive.h" @interface ZIPArchive: OFObject <Archive> { OFZIPArchive *_archive; } @end |
Added utils/ofzip/ZIPArchive.m version [39fb4a5f77].
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | /* * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 * Jonathan Schleifer <js@heap.zone> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "OFDate.h" #import "OFSet.h" #import "OFApplication.h" #import "OFFileManager.h" #import "OFStdIOStream.h" #import "ZIPArchive.h" #import "OFZIP.h" #import "OFInvalidFormatException.h" #ifndef S_IRWXG # define S_IRWXG 0 #endif #ifndef S_IRWXO # define S_IRWXO 0 #endif static OFZIP *app; static void setPermissions(OFString *path, OFZIPArchiveEntry *entry) { #ifdef OF_HAVE_CHMOD if (([entry versionMadeBy] >> 8) == OF_ZIP_ARCHIVE_ENTRY_ATTR_COMPAT_UNIX) { uint32_t mode = [entry versionSpecificAttributes] >> 16; /* Only allow modes that are safe */ mode &= (S_IRWXU | S_IRWXG | S_IRWXO); [[OFFileManager defaultManager] changePermissionsOfItemAtPath: path permissions: mode]; } #endif } @implementation ZIPArchive + (void)initialize { if (self == [ZIPArchive class]) app = [[OFApplication sharedApplication] delegate]; } + (instancetype)archiveWithFile: (OFFile*)file { return [[[self alloc] initWithFile: file] autorelease]; } - initWithFile: (OFFile*)file { self = [super init]; @try { _archive = [OFZIPArchive archiveWithSeekableStream: file]; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { [_archive release]; [super dealloc]; } - (void)listFiles { for (OFZIPArchiveEntry *entry in [_archive entries]) { void *pool = objc_autoreleasePoolPush(); [of_stdout writeLine: [entry fileName]]; if (app->_outputLevel >= 1) { OFString *date = [[entry modificationDate] localDateStringWithFormat: @"%Y-%m-%d %H:%M:%S"]; [of_stdout writeFormat: @"\tCompressed: %" PRIu64 @" bytes\n" @"\tUncompressed: %" PRIu64 @" bytes\n" @"\tCRC32: %08X\n" @"\tModification date: %@\n", [entry compressedSize], [entry uncompressedSize], [entry CRC32], date]; if (app->_outputLevel >= 2) { uint16_t versionMadeBy = [entry versionMadeBy]; [of_stdout writeFormat: @"\tVersion made by: %@\n" @"\tMinimum version needed: %@\n", of_zip_archive_entry_version_to_string( versionMadeBy), of_zip_archive_entry_version_to_string( [entry minVersionNeeded])]; if ((versionMadeBy >> 8) == OF_ZIP_ARCHIVE_ENTRY_ATTR_COMPAT_UNIX) { uint32_t mode = [entry versionSpecificAttributes] >> 16; [of_stdout writeFormat: @"\tMode: %06o\n", mode]; } } if (app->_outputLevel >= 3) [of_stdout writeFormat: @"\tExtra field: %@\n", [entry extraField]]; if ([[entry fileComment] length] > 0) [of_stdout writeFormat: @"\tComment: %@\n", [entry fileComment]]; } objc_autoreleasePoolPop(pool); } } - (void)extractFiles: (OFArray OF_GENERIC(OFString*)*)files { OFFileManager *fileManager = [OFFileManager defaultManager]; bool all = ([files count] == 0); OFMutableSet OF_GENERIC(OFString*) *missing = [OFMutableSet setWithArray: files]; for (OFZIPArchiveEntry *entry in [_archive entries]) { void *pool = objc_autoreleasePoolPush(); OFString *fileName = [entry fileName]; OFString *outFileName = [fileName stringByStandardizingPath]; OFArray OF_GENERIC(OFString*) *pathComponents; OFString *directory; OFStream *stream; OFFile *output; uint64_t written = 0, size = [entry uncompressedSize]; int8_t percent = -1, newPercent; if (!all && ![files containsObject: fileName]) continue; [missing removeObject: fileName]; #if !defined(OF_WINDOWS) && !defined(OF_MSDOS) if ([outFileName hasPrefix: @"/"]) { #else if ([outFileName hasPrefix: @"/"] || [outFileName containsString: @":"]) { #endif [of_stderr writeFormat: @"Refusing to extract %@!\n", fileName]; app->_exitStatus = 1; goto outer_loop_end; } pathComponents = [outFileName pathComponents]; for (OFString *component in pathComponents) { if ([component isEqual: OF_PATH_PARENT_DIRECTORY]) { [of_stderr writeFormat: @"Refusing to extract %@!\n", fileName]; app->_exitStatus = 1; goto outer_loop_end; } } outFileName = [OFString pathWithComponents: pathComponents]; if (app->_outputLevel >= 0) [of_stdout writeFormat: @"Extracting %@...", fileName]; if ([fileName hasSuffix: @"/"]) { [fileManager createDirectoryAtPath: outFileName createParents: true]; setPermissions(outFileName, entry); if (app->_outputLevel >= 0) [of_stdout writeLine: @" done"]; goto outer_loop_end; } directory = [outFileName stringByDeletingLastPathComponent]; if (![fileManager directoryExistsAtPath: directory]) [fileManager createDirectoryAtPath: directory createParents: true]; if (![app shouldExtractFile: fileName outFileName: outFileName]) goto outer_loop_end; stream = [_archive streamForReadingFile: fileName]; output = [OFFile fileWithPath: outFileName mode: @"wb"]; setPermissions(outFileName, entry); while (![stream isAtEndOfStream]) { ssize_t length = [app copyBlockFromStream: stream toStream: output fileName: fileName]; if (length < 0) { app->_exitStatus = 1; goto outer_loop_end; } written += length; newPercent = (written == size ? 100 : (int8_t)(written * 100 / size)); if (app->_outputLevel >= 0 && percent != newPercent) { percent = newPercent; [of_stdout writeFormat: @"\rExtracting %@... %3u%%", fileName, percent]; } } if (app->_outputLevel >= 0) [of_stdout writeFormat: @"\rExtracting %@... done\n", fileName]; outer_loop_end: objc_autoreleasePoolPop(pool); } if ([missing count] > 0) { for (OFString *file in missing) [of_stderr writeFormat: @"File %@ is not in the archive!\n", file]; app->_exitStatus = 1; } } @end |