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 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 300 301 302 303 304 305 306
|
/*
TarArchive.m
Zipper
Copyright (C) 2012-2013 Free Software Foundation, Inc
Authors: Dirk Olmes <dirk@xanthippe.ping.de>
Riccardo Mottola <rm@gnu.org>
Sebastian Reitenbach <sebastia@l00-bugdead-prods.de>
This application is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "TarArchive.h"
#import "FileInfo.h"
#import "NSString+Custom.h"
#import "Preferences.h"
#import "NSArray+Custom.h"
@interface TarArchive (PrivateAPI)
- (NSData *)dataByRunningTar;
- (FileInfo *)fileInfoFromLine:(NSString *)line;
@end
@implementation TarArchive : Archive
/**
* register our supported file extensions with superclass.
*/
+ (void)initialize
{
[self registerFileExtension:@"tar" forArchiveClass:self];
[self registerFileExtension:@"tar.gz" forArchiveClass:self];
[self registerFileExtension:@"tgz" forArchiveClass:self];
[self registerFileExtension:@"tar.bz2" forArchiveClass:self];
[self registerFileExtension:@"tar.xz" forArchiveClass:self];
}
+ (NSString *)unarchiveExecutable
{
return [Preferences tarExecutable];
}
/**
* Tar files inherently have the full path info and can't be uncompressed flat.
*/
+ (BOOL)canExtractWithoutFullPath
{
return NO;
}
+ (ArchiveType)archiveType
{
return TAR;
}
//------------------------------------------------------------------------------
// expanding the archive
//------------------------------------------------------------------------------
- (int)expandFiles:(NSArray *)files withPathInfo:(BOOL)usePathInfo toPath:(NSString *)path
{
FileInfo *fileInfo;
NSString *compressionArg;
NSMutableArray *args;
compressionArg = [Preferences compressionArgumentForFile:[self path]];
NSParameterAssert(compressionArg != nil);
args = [NSMutableArray array];
[args addObject:@"-x"];
if (compressionArg != nil && [compressionArg length] > 0)
{
// compression method
[args addObject:compressionArg];
}
// the archive
[args addObject:@"-f"];
[args addObject:[self path]];
// destination dir
[args addObject:@"-C"];
[args addObject:path];
if (files != nil)
{
NSEnumerator *cursor = [files objectEnumerator];
while ((fileInfo = [cursor nextObject]) != nil)
{
[args addObject:[fileInfo fullPath]];
}
}
return [self runUnarchiverWithArguments:args];
}
- (NSArray *)listContents
{
NSString *line;
NSMutableArray *results = [NSMutableArray array];
NSData *data;
NSString *string;
NSArray *lines;
NSEnumerator *cursor;
data = [self dataByRunningTar];
string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
lines = [string componentsSeparatedByString:@"\n"];
[string release];
cursor = [lines objectEnumerator];
while ((line = [cursor nextObject]) != nil)
{
FileInfo *info;
// BSD tar seems to add linefeed at the end of the line. strip that
if ([line hasSuffix:@"\r"])
{
line = [line substringToIndex:[line length] - 1];
}
// we skip empty lines and plain directory entries
if (([line length] == 0) || [line hasSuffix:@"/"])
{
continue;
}
info = [self fileInfoFromLine:line];
if (info)
[results addObject:info];
}
return results;
}
//------------------------------------------------------------------------------
// creating archives
//------------------------------------------------------------------------------
+ (void)createArchive:(NSString *)archivePath withFiles:(NSArray *)filenames archiveType: (ArchiveType) archiveType
{
NSEnumerator *filenameCursor;
NSString *filename;
NSString *workdir;
NSMutableArray *arguments;
// make sure archivePath has the correct suffix
// and build the command line arguments
arguments = [NSMutableArray array];
switch (archiveType)
{
case TAR:
if ([archivePath hasSuffix:@".tar"] == NO)
{
archivePath = [archivePath stringByAppendingString:@".tar"];
}
[arguments addObject:@"-cf"];
break;
case TARGZ:
if ([archivePath hasSuffix:@".tar.gz"] == NO)
{
archivePath = [archivePath stringByAppendingString:@".tar.gz"];
}
[arguments addObject:@"-czf"];
break;
case TARBZ2:
if ([archivePath hasSuffix:@".tar.bz2"] == NO)
{
archivePath = [archivePath stringByAppendingString:@".tar.bz2"];
}
[arguments addObject:@"-cjf"];
break;
case TARXZ:
if ([Preferences isBsdTar])
{
// this is at least true on OpenBSD
NSRunAlertPanel(@"Error", @"BSD tar doesn't support creation of tar.xz archives",
@"OK", nil, nil, nil);
return;
}
if ([archivePath hasSuffix:@".tar.xz"] == NO)
{
archivePath = [archivePath stringByAppendingString:@".tar.xz"];
}
[arguments addObject:@"-cJf"];
break;
default:
NSRunAlertPanel(@"Error", @"This type of tar archive is not supported for archive creation",
@"OK", nil, nil, nil);
return;
}
[arguments addObject:archivePath];
// filenames contains absolute paths, convert them to relative paths. This works
// because you can select only files/directories below a current directory in
// GWorkspace so all the files *have* to have a common filesystem root.
filenameCursor = [filenames objectEnumerator];
while ((filename = [filenameCursor nextObject]) != nil)
{
[arguments addObject:[filename lastPathComponent]];
}
// change into this directory when running the task
workdir = [[filenames objectAtIndex:0] stringByDeletingLastPathComponent];
// create the archive. In the case of TarArchive the unarchiver can also create
// archives
[self runUnarchiverWithArguments:arguments inDirectory:workdir];
}
//------------------------------------------------------------------------------
// private API
//------------------------------------------------------------------------------
- (NSData *)dataByRunningTar
{
NSString *compressionArg;
NSMutableArray *arguments;
compressionArg = [Preferences compressionArgumentForFile:[self path]];
NSParameterAssert(compressionArg != nil);
arguments = [NSMutableArray arrayWithObject:@"-tv"];
if ([compressionArg isEqual:@""] == NO)
{
[arguments addObject:compressionArg];
}
[arguments addObject:@"-f"];
[arguments addObject:[self path]];
return [self dataByRunningUnachiverWithArguments:arguments];
}
- (FileInfo *)fileInfoFromLine:(NSString *)line
{
int index, length = -1;
NSString *path = nil;
NSString *dateString = nil;
NSString *time = nil;
NSCalendarDate *calendarDate = nil;
NSArray *components;
FileInfo *fileInfo = nil;
if (line == nil || [line length] == 0)
return nil;
if ([line hasPrefix:@"tar: "])
return nil;
components = [line componentsSeparatedByString:@" "];
components = [components arrayByRemovingEmptyStrings];
if ([Preferences isBsdTar])
{
NSArray *dateComponents;
// BSD tar
length = [[components objectAtIndex:4] intValue];
dateComponents = [components subarrayWithRange:NSMakeRange(5, 3)];
dateString = [dateComponents componentsJoinedByString:@" "];
if ([dateString rangeOfString:@":"].location != NSNotFound)
calendarDate = [NSCalendarDate dateWithString:dateString calendarFormat:@"%b %d %H:%M"];
else
calendarDate = [NSCalendarDate dateWithString:dateString calendarFormat:@"%b %d %Y"];
index = [line rangeOfString:[components objectAtIndex:7]].location;
index += [[components objectAtIndex:7] length];
}
else
{
// linux tar
NSString *date;
length = [[components objectAtIndex:2] intValue];
date = [components objectAtIndex:3];
time = [components objectAtIndex:4];
dateString = [NSString stringWithFormat:@"%@ %@", date, time];
calendarDate = [NSCalendarDate dateWithString:dateString calendarFormat:@"%Y-%m-%d %H:%M"];
if (calendarDate == nil)
calendarDate = [NSCalendarDate dateWithString:dateString calendarFormat:@"%Y-%m-%d %H:%M:%S"];
index = [line rangeOfString:dateString].location;
index += [dateString length];
}
// The path is everything after the date string. Since it can contain blanks,
// do *not* just grab any objects from components array
if (index > 0)
{
path = [[line substringFromIndex:index] stringByRemovingWhitespaceFromBeginning];
fileInfo = [FileInfo newWithPath:path date:calendarDate size:[NSNumber numberWithInt:length]];
}
return fileInfo;
}
@end
|