简体   繁体   English

使用Cocoa,我如何检查给定文件归当前用户所有?

[英]With Cocoa how do i check that given file is owned by current user?

Given a file path how do i check that this file is owned by current user? 给定一个文件路径,我如何检查该文件是否为当前用户所有? Currently i managed to get: 目前我设法得到:

[[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:outError] fileOwnerAccountID];

Which return NSNumber*. 返回NSNumber *。 But i can't seem to google for how to get current user account id to compare it with. 但是我似乎无法在Google上找到如何获取当前用户帐户ID进行比较的方法。 Besides all this looks messy, there seem to be a better way to do it, no? 除了所有这些看起来很凌乱之外,似乎还有更好的方法,不是吗?

You can get the current user's id with getuid(). 您可以使用getuid()获取当前用户的ID。

Presumably the reason there's no single-call way of getting the information you want is that it's uncommon for a program to care whether the user owns a file; 大概没有获得所需信息的单一调用方式的原因是,程序关心用户是否拥有文件并不常见。 OS X, like UNIX in general, is more about permissions than ownership, so usually you'd use calls like isWritableFileAtPath: or isDeletableFileAtPath: OS X与一般的UNIX一样,更重要的是权限而不是所有权,因此通常您会使用isWritableFileAtPath:或isDeletableFileAtPath之类的调用:

Instead of 代替

fileOwnerAccountID

try 尝试

fileOwnerAccountName

I'm new here, and if there's a reason this won't work maybe I've missed it. 我是新来的,如果有原因不能解决,也许我错过了。 I'm not near my mac to test. 我不在Mac上进行测试。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM