Extended attributes & filesystems

hacker_compEvery filesystem provide for commonplace attributes like read, write, execute, access and modification time etc. However, when if comes to a filesystem, every measure should be taken so that it can be extended in future. Filesystems like ext2, ext3, ext4, XFS, JFS and reiserfs allow extended attributes which can be used to add tags to files as name-value pairs. This is by design and different from tagging in TMSU. Probably Tracker takes the benefit of this feature.

Extended attributes are used to implement ACLs (Access Control List) on Linux, which add to the basic permission handling by a filesystem as well as the kernel security modules. They can also be used by userland applications to determine the character set to use to open a file or to show a thumbnail or a high-resolution image.

Extended attributes (user_xattr) have to be enabled per volume. To check if an extn volume supports it, run:

$ sudo tune2fs -l /dev/sda2
...
Default mount options: user_xattr acl
...

If it is not enabled, you can specify user_xattr as a mount option in /etc/fstab:

/dev/sda2  /media/mount_point  ext4  auto,users,user_xattr  0  2

Or use the tun2fs utility:

$ sudo tune2fs -O user_xattr /dev/sda2

attr

The userspace utility attr can be used to set, remove and list extended attributes. To install it on Ubuntu, run:

$ sudo apt-get install attr

The following set of operations show how to manipulate attributes:

$ attr -s imgtype -V png myimage.png 
Attribute "imgtype" set to a 3 byte value for myimage.png:
png
$ attr -s res -V "high resolution" myimage.png 
Attribute "res" set to a 15 byte value for myimage.png:
high resolution
$ attr -l myimage.png 
Attribute "imgtype" has a 3 byte value for myimage.png
Attribute "res" has a 15 byte value for myimage.png
$ attr -r imgtype myimage.png 
$ attr -r res myimage.png 
$ attr -l myimage.png
$

The package attr also installs 2 more utilities: getfattr and setfattr which provides similar funtionality. Refer to the man pages.

APIs

You can manipulate extended attributes from your C program using a set of userspace APIs exposed:

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

/* GETTY */
/* Get xattr by name in buffer value of size specified by path */
ssize_t getxattr(const char *path, const char *name,
                void *value, size_t size);

/* Get xattr for a symbolic link (and not the target) */
ssize_t lgetxattr(const char *path, const char *name,
                void *value, size_t size);

/* Same as getxattr, uses open file descriptor */
ssize_t fgetxattr(int fd, const char *name,
                void *value, size_t size);

/* SETTY */
int setxattr(const char *path, const char *name,
                const void *value, size_t size, int flags);
int lsetxattr(const char *path, const char *name,
                const void *value, size_t size, int flags);
int fsetxattr(int fd, const char *name,
                const void *value, size_t size, int flags);

/* LISTING */
ssize_t listxattr(const char *path, char *list, size_t size);
ssize_t llistxattr(const char *path, char *list, size_t size);
ssize_t flistxattr(int fd, char *list, size_t size);

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s