从沙盒命令行应用程序获取当前工作目录 [英] Getting current working directory from a Sandboxed command-line app

查看:52
本文介绍了从沙盒命令行应用程序获取当前工作目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在尝试对用Objective-C编写的命令行应用进行沙箱测试.

I'm now trying to sandbox my command-line app which is written in Objective-C.

未对应用程序进行沙箱处理时,我可以使用 [[NSFileManager defaultManager] currentDirectoryPath] 获取用户调用应用程序的目录.但是,沙盒化之后,无论从何处调用命令行应用程序,该应用程序始终会返回当前目录/Users/{MY_ACCOUNT}/Library/Containers/{BUNDLE_IDENTIFIER}/Data .

When the app was not sandboxed, I could get the directory where the user invoked the app using [[NSFileManager defaultManager] currentDirectoryPath]. However, after sandboxing, the app always returns for current directory /Users/{MY_ACCOUNT}/Library/Containers/{BUNDLE_IDENTIFIER}/Data no matter from where the command-line app was invoked.

实际上,我想知道用户作为命令参数传递的文件的绝对路径.

Actually, I wanna know the absolute path to the files which the user passed as command arguments.

例如,当我这样调用命令行应用程序时:

So when, for example, I invoke my command-line app like this:

$ pwd
/Users/MY_ACCOUNT/Desktop
$ ./foo/my_executable subfolder/myfile.txt

以下部分:

NSURL *url = [NSURL fileURLWithFileSystemRepresentation:argv[1] isDirectory:NO relativeToURL:nil];
printf("path: %s\n", [[url absoluteURL] fileSystemRepresentation]);

正确返回非沙盒应用:

path: /Users/MY_ACCOUNT/Desktop/subfolder/myfile.txt

但在沙盒应用中:

path: /Users/MY_ACCOUNT/Library/Containers/BUNDLE_IDENTIFIER/Data/subfolder/myfile.txt

这当然是不正确的.

是否有其他方法可以在沙盒命令行应用程序中获取传递的文件路径参数的正确完整路径?还是只是不可能,最好还是放弃我的应用程序的沙箱?

Is there any alternative way to get the correct full paths of the passed file path arguments in a Sandboxed command-line app? Or is it just impossible and it's better I just give up to sandbox my app?

最后,我自己找到了一种解决方案.

Finally, I found one of the solution by myself.

[NSProcessInfo processInfo] 中的 environment 属性具有正确的当前用户工作目录,即使该应用已被沙盒化.

The environment property in [NSProcessInfo processInfo] has the correct current user working directory even the app is Sandboxed.

NSDictionary *env = [[NSProcessInfo processInfo] environment];
NSString *currentPath = env[@"PWD"];

至少,当我在Terminal.app中调用命令时,它可以工作.我不确定此 PWD 是否一直存在.

At least, when I call the command in Terminal.app, it works. I'm not sure whether this PWD exists always.

此外,正如下面的@ daij-djan所指出的,我们甚至没有给定参数的读取权限.但是,无论如何,这个问题的问题一旦得到解决.

Furthermore, As @daij-djan pointed out below, we don't have even the read permission for the given arguments. However, anyway the matter of this question is once resolved.

(如果有人知道读取作为参数传递路径的文件的方法,请告诉我答案!)

(If there is someone who know the way to read the file of which path was passed as an argument, please tell me as an answer!)

推荐答案

AFAIK(我前一阵子尝试过,所以可能发生了变化-我不认为)

AFAIK (I tried this a while ago, so things MIGHT have changed -- which I don't think though)

即使知道了它们的实际位置,也无法访问通过命令行args传递的路径:/

The paths passed via command line args wouldn't be accessible even if you figured out where they really are :/

===>这样:多数民众赞成在我的答案..这毫无意义,我担心,因为它不起作用

===> so: thats my answer.. it is pointless Im afraid because it doesn't work

这篇关于从沙盒命令行应用程序获取当前工作目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆