Re: Problem getting correct file URL in NSTask process
Re: Problem getting correct file URL in NSTask process
- Subject: Re: Problem getting correct file URL in NSTask process
- From: Ken Thomases <email@hidden>
- Date: Thu, 27 May 2010 05:52:01 -0500
On May 27, 2010, at 4:33 AM, Antonio Nunes wrote:
> I have a daemon that spawns NSTasks on request. A task is passed a number of paths to process. These paths are pased correctly to the task, and in the task I can correctly extract the path string from optarg.
No, they aren't and no you can't, according to what you write below.
> When I try to turn the path into a file url however, the resulting URL is not correct.
>
> For instance: I create a string from the argument:
> NSString *path = [[[NSString alloc] initWithBytes:(UInt8 *)optarg length:strlen(optarg) encoding:NSMacOSRomanStringEncoding] stringByExpandingTildeInPath];
Why do you think the string is in Mac Roman encoding? It probably isn't. In fact, I happen to know that NSTask converts all of its arguments to "file system representation". Even if I didn't know that, that's the only correct thing to assume. So, you should use the following to construct an NSString from optarg:
NSString* path = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:optarg length:strlen(optarg)];
> At this point path is "/Volumes/MyDisk/Users/sonata/Desktop/Stash/BPSystemStartup.pdf"
Well, this is ambiguous as you're about to see...
> When I subsequently call [NSURL fileURLWithPath:path] the result is " "/Volumes/MyDisk/Users/sonata/Desktop/Stash/BPSystemStartup.pdf" -- /Volumes/MyDisk/Users/sonata/Documents/Xcode Projects/builds/Debug/"
The percent-escape sequences in there show that path didn't contain what you thought it did. It starts with a space character (the ). Plus the file path has actual, literal quote characters (the "s) in it.
So, it would be more accurate to says that path is " \"/Volumes/MyDisk/Users/sonata/Desktop/Stash/BPSystemStartup.pdf\"", as it might be expressed as a C literal.
> The task launched by NSTask is a command line app that works just fine when run on its own (e.g. from the terminal). The issue only happens when launching the command line app through NSTask. Any pointers to why this happens?
You are almost certainly confused about the need to quote and escape things for the shell. The shell needs to take what it receives as a single command string and break it up into its component parts. That's what introduces the need to quote whitespace and escape special characters when working in the shell.
By contrast, NSTask is handed its arguments already neatly separated out into individual elements. Plus, it doesn't have any special characters -- it just treats all of its arguments literally. Thus, it does not require that its arguments be quoted or escaped. Indeed, if you try to do so, those characters are just passed along to the new process.
So, I guess that you have taken a file path string in the parent process and, while attempting to pass it through to a subprocess verbatim, you've done exactly the wrong thing. You've modified it, putting quote marks around it and, apparently, a space before it. If you had done nothing with the string but pass it in as one of the task's arguments, _that_ would have gotten it to the subprocess verbatim.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden