iPhone unix 文件访问

发布于 2024-12-28 05:12:43 字数 262 浏览 1 评论 0原文

当我使用:

int fd = open(fileNameWitPath, O_CREAT | O_RDWR);

在设备模拟器中一切正常。 但物理设备返回fd = -1errno = 0x0d (13),权限被拒绝。

目标路径是应用程序沙箱文档或tmp路径。

这可以阅读吗?不越狱,通过Unix功能在iPhone上的应用程序私有区域写入文件?

When I use:

int fd = open(fileNameWitPath, O_CREAT | O_RDWR);

in the device simulator all is right.
But physical device return fd = -1 and errno = 0x0d (13), permission denied.

The destination path is the application sandbox Documents or tmp path.

Is this possible to read & write files on iPhone, in the application private area, by Unix function without jailbreak?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

睫毛上残留的泪 2025-01-04 05:12:43

它应该可以正常工作,但您必须记住,iOS 模拟器使用与真实设备不同的文件系统。事实上,您只能从捆绑包中读取文件,而不能写入文件,并且您尝试使用读\写权限打开文件。请检查 fileNameWitPath 的值

获取文档目录的正确方法是:

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

您应该使用

NSFileManager *fileManager = [NSFileManager defaultManager]; 
[fileManager createFileAtPath: contents: attributes:]

而不是

int fd = open(fileNameWitPath, O_CREAT | O_RDWR);

It should work OK but you have to remember that the iOS Simulator uses different filesystem than a real device. And the fact that you can only READ files from the bundle, not WRITE, and you try to open with read\write permission. please check the value of fileNameWitPath

The right way to get the document directory is:

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

You should use

NSFileManager *fileManager = [NSFileManager defaultManager]; 
[fileManager createFileAtPath: contents: attributes:]

instead of

int fd = open(fileNameWitPath, O_CREAT | O_RDWR);
无敌元气妹 2025-01-04 05:12:43

Skippy 关于缺少第三个参数的评论是这里的正确问题。

如果没有第三个参数, open(...) 将第一次工作,并在后续运行中失败并返回 -1 和 EACCES,因为应用程序第一次创建了该文件,但不再具有访问该文件的权限。 (如果您在设备上重新编译和测试,并看到错误,那是因为您需要删除应用程序,该应用程序会删除权限不正确的文件。)

Skippy's comment regarding the missing third parameter is the correct issue at hand here.

Without the third parameter, open(...) will work the first time, and fail with -1 and EACCES on subsequent runs because the app created the file the first time, but no longer has permission to access the file. (If you are recompiling and testing on device, and seeing the error, it's because you need to delete the app which deletes the incorrectly-permissioned file.)

π浅易 2025-01-04 05:12:43

*完全重写答案*

以下代码有效:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    NSArray *dp = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [dp objectAtIndex:0];
    NSString *fileNameWithPath = [NSString stringWithFormat:@"%@/%@",path,@"fileName.txt"];
    int fd = open([fileNameWithPath UTF8String], O_CREAT | O_RDWR);

    NSLog(@"Open %@ for writing returned %d",fileNameWithPath,fd);

    return YES;
}

调试输出:

Re-enabling shared library breakpoint 1
(gdb) n
30      NSLog(@"Open %@ for writing returned %d",fileNameWithPath,fd);
(gdb) n
2012-01-20 11:01:23.161 FOpen[6535:707] Open /var/mobile/Applications/AF7EA358-E4AA-491F-AEA1-83080F2749E5/Documents/fileName.txt for writing returned 6
32      return YES;

关键是您必须传递 c 函数“open”ac 样式字符串,例如 [fileNameWithPath UTF8String] 。如果您使用 ARC,您可能需要做更多的事情才能让编译器满意,但我无法确认这一点。

*Compleat rewrite of answer *

The following code works:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    NSArray *dp = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [dp objectAtIndex:0];
    NSString *fileNameWithPath = [NSString stringWithFormat:@"%@/%@",path,@"fileName.txt"];
    int fd = open([fileNameWithPath UTF8String], O_CREAT | O_RDWR);

    NSLog(@"Open %@ for writing returned %d",fileNameWithPath,fd);

    return YES;
}

Debug output:

Re-enabling shared library breakpoint 1
(gdb) n
30      NSLog(@"Open %@ for writing returned %d",fileNameWithPath,fd);
(gdb) n
2012-01-20 11:01:23.161 FOpen[6535:707] Open /var/mobile/Applications/AF7EA358-E4AA-491F-AEA1-83080F2749E5/Documents/fileName.txt for writing returned 6
32      return YES;

The key is that you must pass the c function 'open' a c style string, e.g. [fileNameWithPath UTF8String]. If you are using ARC you may have to do a bit more to get the compiler happy, I can't confirm that though.

指尖上的星空 2025-01-04 05:12:43

我写的是:

int fd = open(fileNameWithPath, O_CREAT | O_RDWR);

但是“设置'O_CREAT'时调用'open'需要第三个参数”。
代码行必须是(对于 RW 权限):

 int fd = open(fileNameWithPath, O_CREAT | O_RDWR, 0x0666);

并且这可以正常工作。

[编辑]
我写的是:

int fd = open(fileNameWithPath, O_CREAT | O_RDWR, 0x0666);

这是错误,umask 必须是 666,但是八进制,而不是十六进制:

int fd = open(fileNameWithPath, O_CREAT | O_RDWR, 00666);

I was write:

int fd = open(fileNameWithPath, O_CREAT | O_RDWR);

But "Call to 'open' requires third argument when the 'O_CREAT' is set".
The line of the code must be (for RW permissions):

 int fd = open(fileNameWithPath, O_CREAT | O_RDWR, 0x0666);

and this work fine.

[edit]
I was write:

int fd = open(fileNameWithPath, O_CREAT | O_RDWR, 0x0666);

this is error, umask must be 666 but octal, not hex:

int fd = open(fileNameWithPath, O_CREAT | O_RDWR, 00666);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文