mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-27 05:04:27 +00:00
These fixes remove the use of the File Manager API that was causing crashes on Lion. This should resolve issue #827357. We are now using NSFileManager instead of the older File Manager API, but only on 10.6 and later. These changes should work for those targeting 10.4, but will allow users running 10.7 to still update without crashing.
112 lines
3.6 KiB
Objective-C
112 lines
3.6 KiB
Objective-C
//
|
|
// SUDiskImageUnarchiver.m
|
|
// Sparkle
|
|
//
|
|
// Created by Andy Matuschak on 6/16/08.
|
|
// Copyright 2008 Andy Matuschak. All rights reserved.
|
|
//
|
|
|
|
#import "SUDiskImageUnarchiver.h"
|
|
#import "SUUnarchiver_Private.h"
|
|
#import "NTSynchronousTask.h"
|
|
#import "SULog.h"
|
|
#import <CoreServices/CoreServices.h>
|
|
|
|
|
|
@implementation SUDiskImageUnarchiver
|
|
|
|
+ (BOOL)canUnarchivePath:(NSString *)path
|
|
{
|
|
return [[path pathExtension] isEqualToString:@"dmg"];
|
|
}
|
|
|
|
- (void)extractDMG
|
|
{
|
|
// GETS CALLED ON NON-MAIN THREAD!!!
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
BOOL mountedSuccessfully = NO;
|
|
|
|
SULog(@"Extracting %@ as a DMG", archivePath);
|
|
|
|
// get a unique mount point path
|
|
NSString *mountPointName = nil;
|
|
NSString *mountPoint = nil;
|
|
FSRef tmpRef;
|
|
do
|
|
{
|
|
CFUUIDRef uuid = CFUUIDCreate(NULL);
|
|
if (uuid)
|
|
{
|
|
CFStringRef uuidString = CFUUIDCreateString(NULL, uuid);
|
|
if (uuidString)
|
|
{
|
|
mountPoint = [@"/Volumes" stringByAppendingPathComponent:(NSString*)uuidString];
|
|
CFRelease(uuidString);
|
|
}
|
|
CFRelease(uuid);
|
|
}
|
|
}
|
|
while (noErr == FSPathMakeRefWithOptions((UInt8 *)[mountPoint fileSystemRepresentation], kFSPathMakeRefDoNotFollowLeafSymlink, &tmpRef, NULL));
|
|
|
|
NSArray* arguments = [NSArray arrayWithObjects:@"attach", archivePath, @"-mountpoint", mountPoint, /*@"-noverify",*/ @"-nobrowse", @"-noautoopen", nil];
|
|
// set up a pipe and push "yes" (y works too), this will accept any license agreement crap
|
|
// not every .dmg needs this, but this will make sure it works with everyone
|
|
NSData* yesData = [[[NSData alloc] initWithBytes:"yes\n" length:4] autorelease];
|
|
|
|
NSData *output = nil;
|
|
int returnCode = [NTSynchronousTask task:@"/usr/bin/hdiutil" directory:@"/" withArgs:arguments input:yesData output: &output];
|
|
if ( returnCode != 0 )
|
|
{
|
|
NSString* resultStr = output ? [[[NSString alloc] initWithData: output encoding: NSUTF8StringEncoding] autorelease] : nil;
|
|
SULog( @"hdiutil failed with code: %d data: <<%@>>", returnCode, resultStr );
|
|
goto reportError;
|
|
}
|
|
mountedSuccessfully = YES;
|
|
|
|
// Now that we've mounted it, we need to copy out its contents.
|
|
if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5) {
|
|
// On 10.6 and later we don't want to use the File Manager API and instead want to use NSFileManager (fixes #827357).
|
|
NSFileManager *manager = [[NSFileManager alloc] init];
|
|
if (![manager copyItemAtPath:mountPoint toPath:[archivePath stringByDeletingLastPathComponent] error:NULL]) {
|
|
goto reportError;
|
|
}
|
|
}
|
|
else {
|
|
FSRef srcRef, dstRef;
|
|
OSStatus err;
|
|
err = FSPathMakeRef((UInt8 *)[mountPoint fileSystemRepresentation], &srcRef, NULL);
|
|
if (err != noErr) goto reportError;
|
|
err = FSPathMakeRef((UInt8 *)[[archivePath stringByDeletingLastPathComponent] fileSystemRepresentation], &dstRef, NULL);
|
|
if (err != noErr) goto reportError;
|
|
|
|
err = FSCopyObjectSync(&srcRef, &dstRef, (CFStringRef)mountPointName, NULL, kFSFileOperationSkipSourcePermissionErrors);
|
|
if (err != noErr) goto reportError;
|
|
}
|
|
|
|
[self performSelectorOnMainThread:@selector(notifyDelegateOfSuccess) withObject:nil waitUntilDone:NO];
|
|
goto finally;
|
|
|
|
reportError:
|
|
[self performSelectorOnMainThread:@selector(notifyDelegateOfFailure) withObject:nil waitUntilDone:NO];
|
|
|
|
finally:
|
|
if (mountedSuccessfully)
|
|
[NSTask launchedTaskWithLaunchPath:@"/usr/bin/hdiutil" arguments:[NSArray arrayWithObjects:@"detach", mountPoint, @"-force", nil]];
|
|
else
|
|
SULog(@"Can't mount DMG %@",archivePath);
|
|
[pool drain];
|
|
}
|
|
|
|
- (void)start
|
|
{
|
|
[NSThread detachNewThreadSelector:@selector(extractDMG) toTarget:self withObject:nil];
|
|
}
|
|
|
|
+ (void)load
|
|
{
|
|
[self registerImplementation:self];
|
|
}
|
|
|
|
@end
|