0

I want to create a file at /Libary/Logs in my Mac application.

I am using the following code,

NSError *error = nil;
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSLocalDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Logs"];
//path = [path stringByAppendingPathComponent:@"e315Updater"];

if ( ![fm fileExistsAtPath:path] )
{
    BOOL sucess = [fm createDirectoryAtPath:path
 withIntermediateDirectories:YES
                  attributes:nil
                       error:&error];

    NSLog(@"Success value = %d",sucess);
    if ( error ){
        NSLog(@"Err = %@",[error description]);
    }
}

It keeps failing because of the permissions issue as every user does NOT have read/write permissions to this directory.

I also tried to set permissions on /Libary/Logs directory by using NSFilePosixPermissions but it failes to set the Read/write permissions also.

So I need help in figuring out that 'How to create and read/write to file inside Library/Logs of main Disk not the users Library'

4

1 回答 1

0

OS X 权限模型不允许普通用户写入大多数系统目录,包括 /Library/Logs。如果您找到解决此问题的方法,您就发现了一个安全漏洞并且需要修复(然后您的应用程序将失败);如果您更改权限(如果您首先提升权限,您可以这样做),您将破坏 OS X 安全模型,这是一个非常糟糕的主意

我建议不要尝试直接写入您自己的私人日志,而是通过NSLog或可能是较低级别的接口ASL 或 syslog之一来通过系统日志记录工具。您不会获得自己的私人日志文件,但它确实可以工作。

于 2013-08-07T16:25:00.573 回答