Re: Using NSTask to execute softwareupdate

Colin Swelin <[email protected]> Tue, 25 Jan 2011 19:02:58 -0500
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
Hi,

Sorry for the long reply. I  gave this a try and it looked promising, however, the task still hangs at the "writing files" step of few patches. It's reading the buffer correctly, as its output everything up to that point.

Anyone have any insight to why the 10.6.6 update does this?

Thanks again,
Colin

On 2011-01-08, at 1:27 PM, Tom Jones wrote:

> The problem is the use of "waitUntilExit" the buffer is filling up. 
> 
> Try something like this...
> ( You might have to clean some of it up :-) )
> 
> Tom
> 
> - (void)runTask:(NSDictionary *)patch
> {
> NSTask *task = [[NSTask alloc] init];
> [task setLaunchPath: @"/usr/sbin/softwareupdate"];
> [task setArguments:[NSArray arrayWithObjects:@"-i", [patch objectForKey:@"patch_identifier"], nil]];
> [task setEnvironment:[NSDictionary dictionaryWithObject:@"1" forKey:@"COMMAND_LINE_INSTALL"]];
> 
> NSPipe *install_pipe = [NSPipe pipe];
> [task setStandardOutput: install_pipe];
> [task setStandardError: install_pipe];
> 	
> NSFileHandle *file = [install_pipe fileHandleForReading];
> 
> 
> [[NSNotificationCenter defaultCenter] addObserver: self
> 										 selector: @selector(taskEnded:)
> 											 name: NSTaskDidTerminateNotification
> 										   object: file];
> 
> [[NSNotificationCenter defaultCenter] addObserver: self
> 										 selector: @selector(readFromTask:)
> 											 name: NSFileHandleDataAvailableNotification
> 										   object: file];
> 
> [file readInBackgroundAndNotify];
> [task launch];
> [task release];
> }
> 
> 
> - (void)readFromTask:(NSNotification *)aNotification
> {
>  NSData *data = [[aNotification userInfo] objectForKey: NSFileHandleNotificationDataItem];
>  if ([data length]) {
>  	/* Process data */
>  	[[aNotification object] readInBackgroundAndNotify];
>  }
> }
> 
> - (void)taskEnded:(NSNotification *)aNotification
> {
> 	// Task is full complete
> 	[[NSNotificationCenter defaultCenter] removeObserver:self];
> }
> 
> 
> 
> On Jan 8, 2011, at 9:59 AM, Colin Swelin wrote:
> 
>> Here's the basic NSTask stuff:
>> 
>> 
>> 			NSPipe *pipes = [NSPipe pipe];
>> 			NSTask* patchInstall = [[NSTask alloc] init];
>>                        [patchInstall setLaunchPath:@"/usr/sbin/softwareupdate"];
>> 			[patchInstall setArguments:[NSArray arrayWithObjects:@"-i", [patch objectForKey:@"patch_identifier"], nil]];
>> 			
>> 			[patchInstall setStandardOutput: pipes];
>> 
>>                        NSFileHandle *file = [pipes fileHandleForReading];
>> 			
>> 			[patchInstall launch];
>> 			
>> 			NSData *data = [file readDataToEndOfFile];
>> 			
>> 			[patchInstall waitUntilExit];
>> 
>> 
>>                          ......
>>                       [patchInstall release];
>> 
>> 
>> So i'm waiting for the task to complete, then I do my thing with the results. 
>> 
>> Thanks,
>> Colin
>> 
>> On Sat, Jan 8, 2011 at 12:48 PM, Tom Jones <[email protected]> wrote:
>> Hi Colin,
>> I would need to see your NSTask code, to really know. Are you using notifications as data becomes available or are you waiting until the task has completed?
>> 
>> 
>> Thanks,
>> tom
>> 
>> 
>> On Jan 6, 2011, at 8:54 AM, Colin Swelin wrote:
>> 
>>> Hi,
>>> 
>>> I"m currently using NSTask to execute softwareupdate to install patches, however, there's a few patches that seem to hang when trying to install. For example, the latest 10.6.6 update and a older iTunes patch, everything else seems to work perfectly fine.
>>> 
>>> Anyone have any clues to why this might happen? I've set the "COMMAND_LINE_INSTALL" environment variable to 1, so i'm a little confused to why this is happening.
>>> 
>>> Thanks._______________________________________________
>>> MacOSX-dev mailing list
>>> [email protected]
>>> http://www.omnigroup.com/mailman/listinfo/macosx-dev
>> 
>> 
>