如何获取文件的扩展属性(UNIX/C)? [英] How to get extended attributes of a file(UNIX/C)?

查看:219
本文介绍了如何获取文件的扩展属性(UNIX/C)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在命令行中键入ls -l时,有时会在文件权限旁边出现一个@+符号(顺便说一句,我在OS X上),如下所示:

When I type ls -l in the command line, sometimes an @ or + symbol comes up alongside the file permissions(btw, I am on OS X), as shown below:

-rw-r-----@  1 john  staff      6731 Sep 28 01:10 mutations.txt
drwxr-xr-x+ 71 john  staff      2414 Mar 25 18:16 ..

我知道如何使用stat结构获取权限位,但是我不认为这些扩展的权限值在那里.有人可以为我指出如何通过C或POSIX API获得这些值的正确方向吗?

I know how to get the permission bits using the stat structure, but I don't think these extended permission values are there. Can someone point me in the right direction as to how to obtain these values via a C or POSIX API?

编辑:

我尝试了以下操作:

#include <sys/types.h>
#include <sys/xattr.h>
#include <sys/types.h>

int main () {
    char  l[1024];
    listxattr("/Users/john/desktop/mutations.txt", l, 1024,  XATTR_SHOWCOMPRESSION);

    printf("%s\n", l);
}

并作为输出:

com.apple.metadata:kMDItemWhereFroms

仍然试图了解如何将其转换为@+?

Still trying to understand how to convert this to an @ or +?

推荐答案

以下是我从Apple给出的ls官方实现中摘录的一些代码,您会发现

Following is some code I scraped off of the official implementation of ls given by Apple you will find here. The code is long so do CMD + F and search for "printlong".

#include <sys/types.h>
#include <sys/xattr.h>
#include <sys/types.h>
#include <sys/acl.h>
#include <stdio.h>

int main () {
    acl_t acl = NULL;
    acl_entry_t dummy;
    ssize_t xattr = 0;
    char chr;
    char * filename = "/Users/john/desktop/mutations.txt";

    acl = acl_get_link_np(filename, ACL_TYPE_EXTENDED);
    if (acl && acl_get_entry(acl, ACL_FIRST_ENTRY, &dummy) == -1) {
        acl_free(acl);
        acl = NULL;
    }
    xattr = listxattr(filename, NULL, 0, XATTR_NOFOLLOW);
    if (xattr < 0)
        xattr = 0;

    if (xattr > 0)
        chr = '@';
    else if (acl != NULL)
        chr = '+';
    else
        chr = ' ';

    printf("%c\n", chr);
 }

根据所使用的文件,输出将为空白,@或+,并且显示方式与ls -l完全相同.希望这会有所帮助!

Depending on the file used, the output will be a blank, @, or + in exactly the same manner ls -l displays it. Hope this helps !

这篇关于如何获取文件的扩展属性(UNIX/C)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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