How to get extended attributes of a file(UNIX/C)?
Asked Answered
A

2

5

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 ..

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?

EDIT:

I attempted the following:

#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);
}

and got as output:

com.apple.metadata:kMDItemWhereFroms

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

Antilogism answered 25/3, 2016 at 22:46 Comment(1)
Also, just to let you know, you included <sys/types.h> twice. Not sure if this is by design.Valerie
A
3

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);
 }

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

Antilogism answered 26/3, 2016 at 3:40 Comment(0)
L
6

@ means the file has extended attributes. Use listxattr() to get a list of the names of all the extended attributes, and getxattr() to get the value of a particular attribute. If listxattr returns a non-zero result, you would display @ to indicate this.

Extended attributes are not in POSIX, but this API is available in Linux and OS X, at least.

You can find an example of how to use these functions here.

+ means the file has an access control list. In some filesystems, this is stored as a special extended attribute; in others it's stored separately. For access control lists, see acl(5) for a reference, and you can find an example program that displays it here.

Leahleahey answered 25/3, 2016 at 22:50 Comment(11)
Can you give an example of how to get the @ or the + symbol ? I read the documentation, and tried the link and I got: com.apple.metadata:kMDItemWhereFroms. Any ideas on what this means ?Antilogism
@ and + don't come from anywhere. They're just displayed by ls if it detects that the file has extended attributes or an ACL.Leahleahey
To get the meaning of any specific attribute, you'll have to search for it. I don't know of a comprehensive list.Leahleahey
I updated the question with an attempt. So in a case like above, where I get some output, does that mean the file has extended attribute ? what about ACL ?Antilogism
I found the information in my answer by googling. Are you really so feeble that you can't do a little investigation on your own?Leahleahey
My guess is that kMDItemWhereFrom is used for items that are moved to the desktop to remember the folder it originally came from, so you can use the Put Back menu to move it back there.Leahleahey
I've added more information to my answer, including links to sample programs that display extended attributes and ACLs.Leahleahey
Unfortunately, I don't think these examples work on OS X. But thanks for you assistance. Regarding "are you so feeble that you can't do a little investigation", why do people on this site provoke others ? Is that like a price for getting free help ?Antilogism
Yeah, occasionally I get feisty when it seems like the questioner is acting too helpless. There's only so much spoon-feeding we should have to do, you need to do some of your own thinking.Leahleahey
The question is tagged "linux", why are you looking for OS X solutions? But the OS X documentation for the *xattr() functions looks the same as the Linux versions, so I'm not sure why the examples wouldn't work.Leahleahey
Thanks, I noticed the tag, not the question text, so I was understandably confused. I've edited the tag.Leahleahey
A
3

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);
 }

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

Antilogism answered 26/3, 2016 at 3:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.