如何获取 XCode 项目中文件的访问控制列表(POSIX ACL)
How to get Access-control list(POSIX ACL) for file in XCode project
我无法获取 XCode 中文件的 ACL。它总是失败。
我的代码:
acl_type_t opt = ACL_TYPE_ACCESS;
const char *path_p = path.fileSystemRepresentation;
acl_t acl = acl_get_file(path_p, opt);
if (acl == NULL) {
NSAssert(NO, @"acl_get_file %s", path_p);
return;
}
发现 an example 也有同样的作用,但在我的情况下不起作用。
ACL - 访问控制列表。(要了解什么是 ACL,您可以在终端应用程序中输入“man acl”或“man chmod”。)
A filesystem ACL is a data structure (usually a table) containing entries that specify individual user or group rights to specific system objects such as programs, processes, or files.
我使用 NSOpenPanel 获取访问权限:
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
openPanel.directoryURL = [NSURL fileURLWithPath:@"/"];
openPanel.canChooseDirectories = false;
openPanel.canChooseFiles = !openPanel.canChooseDirectories;
[openPanel beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse result) {
[openPanel orderOut:nil];
if (result == NSModalResponseOK && openPanel.URL != nil) {
//My code getting ACL for openPanel.URL.path
}
}];
然后我检查文件的修改访问权限(我不知道如何检查 ACL 访问权限。看起来我没有)
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error = nil;
__auto_type resourceValues = [url resourceValuesForKeys:@[NSURLIsUserImmutableKey, NSURLIsWritableKey] error:&error];
NSParameterAssert(error == nil);
NSParameterAssert(!resourceValues.fileIsImmutable);
if ([resourceValues[NSURLIsWritableKey] isKindOfClass:[NSNumber class]]) {
NSParameterAssert([(NSNumber *)resourceValues[NSURLIsWritableKey] boolValue]);
} else {
NSParameterAssert(NO);
}
NSParameterAssert([[NSFileManager defaultManager] isWritableFileAtPath:path]);
我在控制台中看到的内容:
12/14/20 5:59:15.383 PM ACLTest[915]: acl_get_file /Users/test/Desktop/untitled folder
我尝试了什么:
- 给我的应用程序“全盘访问”
- 在 10.10 和 10.15 上启动了我的应用程序
- 在“控制台项目模板”中编写了相同的代码
- 尝试使用 acl_type_t
中的每个枚举元素
- Google 搜索失败,没有任何有用的结果
我在终端应用程序中获得了我需要的数据(我知道如何从应用程序启动脚本,但假设它可能违反了一些 Apple 政策。并且给用户带来了一些额外的肤色。所以我的目标是从代码中获取信息而无需启动其他脚本):
# ls -le
total 64
drwxrwxr-x@ 8 <UserName> staff 256 Dec 14 16:29 <Folder Name>
0: group:everyone deny add_file,delete,add_subdirectory,delete_child,writeattr,writeextattr,chown
使用ACL_TYPE_EXTENDED
,因为不支持其他类型:
#import <sys/acl.h>
#import <stdio.h>
#import <sys/errno.h>
acl_t acl = acl_get_file(fn, ACL_TYPE_EXTENDED);
if (acl == NULL) {
if (errno == ENOENT) {
// either file not found, or does not have ACL attached
}
else {
perror("acl_get_file");
}
}
来自acl.h:
/* 23.2.6 Individual ACL types */
typedef enum {
ACL_TYPE_EXTENDED = 0x00000100,
/* Posix 1003.1e types - not supported */
ACL_TYPE_ACCESS = 0x00000000,
ACL_TYPE_DEFAULT = 0x00000001,
/* The following types are defined on FreeBSD/Linux - not supported */
ACL_TYPE_AFS = 0x00000002,
ACL_TYPE_CODA = 0x00000003,
ACL_TYPE_NTFS = 0x00000004,
ACL_TYPE_NWFS = 0x00000005
} acl_type_t;
相关手册:
我无法获取 XCode 中文件的 ACL。它总是失败。 我的代码:
acl_type_t opt = ACL_TYPE_ACCESS;
const char *path_p = path.fileSystemRepresentation;
acl_t acl = acl_get_file(path_p, opt);
if (acl == NULL) {
NSAssert(NO, @"acl_get_file %s", path_p);
return;
}
发现 an example 也有同样的作用,但在我的情况下不起作用。
ACL - 访问控制列表。(要了解什么是 ACL,您可以在终端应用程序中输入“man acl”或“man chmod”。)
A filesystem ACL is a data structure (usually a table) containing entries that specify individual user or group rights to specific system objects such as programs, processes, or files.
我使用 NSOpenPanel 获取访问权限:
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
openPanel.directoryURL = [NSURL fileURLWithPath:@"/"];
openPanel.canChooseDirectories = false;
openPanel.canChooseFiles = !openPanel.canChooseDirectories;
[openPanel beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse result) {
[openPanel orderOut:nil];
if (result == NSModalResponseOK && openPanel.URL != nil) {
//My code getting ACL for openPanel.URL.path
}
}];
然后我检查文件的修改访问权限(我不知道如何检查 ACL 访问权限。看起来我没有)
NSURL *url = [NSURL fileURLWithPath:path];
NSError *error = nil;
__auto_type resourceValues = [url resourceValuesForKeys:@[NSURLIsUserImmutableKey, NSURLIsWritableKey] error:&error];
NSParameterAssert(error == nil);
NSParameterAssert(!resourceValues.fileIsImmutable);
if ([resourceValues[NSURLIsWritableKey] isKindOfClass:[NSNumber class]]) {
NSParameterAssert([(NSNumber *)resourceValues[NSURLIsWritableKey] boolValue]);
} else {
NSParameterAssert(NO);
}
NSParameterAssert([[NSFileManager defaultManager] isWritableFileAtPath:path]);
我在控制台中看到的内容:
12/14/20 5:59:15.383 PM ACLTest[915]: acl_get_file /Users/test/Desktop/untitled folder
我尝试了什么:
- 给我的应用程序“全盘访问”
- 在 10.10 和 10.15 上启动了我的应用程序
- 在“控制台项目模板”中编写了相同的代码
- 尝试使用 acl_type_t 中的每个枚举元素
- Google 搜索失败,没有任何有用的结果
我在终端应用程序中获得了我需要的数据(我知道如何从应用程序启动脚本,但假设它可能违反了一些 Apple 政策。并且给用户带来了一些额外的肤色。所以我的目标是从代码中获取信息而无需启动其他脚本):
# ls -le
total 64
drwxrwxr-x@ 8 <UserName> staff 256 Dec 14 16:29 <Folder Name>
0: group:everyone deny add_file,delete,add_subdirectory,delete_child,writeattr,writeextattr,chown
使用ACL_TYPE_EXTENDED
,因为不支持其他类型:
#import <sys/acl.h>
#import <stdio.h>
#import <sys/errno.h>
acl_t acl = acl_get_file(fn, ACL_TYPE_EXTENDED);
if (acl == NULL) {
if (errno == ENOENT) {
// either file not found, or does not have ACL attached
}
else {
perror("acl_get_file");
}
}
来自acl.h:
/* 23.2.6 Individual ACL types */
typedef enum {
ACL_TYPE_EXTENDED = 0x00000100,
/* Posix 1003.1e types - not supported */
ACL_TYPE_ACCESS = 0x00000000,
ACL_TYPE_DEFAULT = 0x00000001,
/* The following types are defined on FreeBSD/Linux - not supported */
ACL_TYPE_AFS = 0x00000002,
ACL_TYPE_CODA = 0x00000003,
ACL_TYPE_NTFS = 0x00000004,
ACL_TYPE_NWFS = 0x00000005
} acl_type_t;
相关手册: