Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 +?

like image 692
Jenna Maiz Avatar asked Mar 25 '16 22:03

Jenna Maiz


People also ask

What are extended file attributes Linux?

Extended file attributes are file system features that enable users to associate computer files with metadata not interpreted by the filesystem, whereas regular attributes have a purpose strictly defined by the filesystem (such as permissions or records of creation and modification times).

Which command would display the extended attributes that are set on a file?

File attributes that are stored in the Attributes file can be view, and something manipulated, with the xattr command. This command be can used to “display, modify, or remove the extended attributes of one or more files, including directories and symbolic links.

Where are extended attributes stored?

EAs are stored in the attributes B*-Tree of the HFS+ filesystem, and have a maximum size of 128KB as of OS X Lion & iOS 5.

How do I check the attributes of a file in Linux?

You can list the attribute of the contents of a particular directory with lsattr command followed with a file or directory name as the argument. As the ls -l command, the -d option with lsattr will list the attributes of the directory itself instead of the files in that directory.


2 Answers

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

like image 196
Barmar Avatar answered Sep 24 '22 20:09

Barmar


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 !

like image 42
Jenna Maiz Avatar answered Sep 25 '22 20:09

Jenna Maiz