Xattrs provides a mechanism for permanently associating key/value pairs to files, allowing existing file systems to support functionality not provided in the original design. Extended attributes are unknown to file systems and applicationsProgramThey can be manipulated through a standard interface, which does not vary with the file system. Each extended property can be distinguished by a unique key, the content of the key must be a valid UTF-8 in the format of namespace. attribute, each key in a fully qualified form.
Note that the value here can be an array of any byte, not necessarily a character, and may not be null at last, so that the value size must be known during access. Of course, you also need to set the value size when setting.
The use of an extended property: the behavior of the GUI File Manager varies according to the file type. To determine the file format, windows and other operating systems only need to view the file extension, while UNIX systems often need to view the file content to determine the type. Some file management programs directly generate this information, and some files cache the generated information for future use. A better way is to store such metadata into extended attributes.
Four extended attribute namespaces defined in Linux:
- System: Used to implement kernel functions that utilize extended attributes, such as Access Control tables. Eg: system. posix_acl_access is an extended attribute located in the user space. Whether the user can read or write these attributes depends on the security module in use.
- Security: used to implement the security module.
- Trusted: stores restricted information into user space.
- User: The standard namespace used by a general process. Access to this namespace is controlled by the General File Permission.
The following is an example of using extended attributes:
Create Disk
# Dd If =/dev/Zero of =/opt/testptn COUNT = 512
# Mke2fs xattr_disk
Attach Disk
# Mount-o loop, user_xattr xattr_disk mnt
Scaling features
# Touch test.txt
Set extended features
# Setfattr-N user. ABC-V 123 test.txt
Show extended features
# Getfattr-N user. ABC test.txt
# File: test.txt
User. ABC = "123"
Another example:
# Include <stdio. h> # include <unistd. h> # include <errno. h> # include <string. h> # include <sys/xattr. h> # include <sys/types. h> void testset () {char key [7] = {'U', 's', 'E', 'R ','. ',' # ',' \ 0'}; char value [2] = {'#', '\ 0'}; int I, RET; for (I = 0; I <10; I ++) {key [5] = value [0] = '0' + I; ret = setxattr ("test", key, value, 2, 0) ;}} void testlist () {char Buf [1000]; int ret, I = 0, j = 0; printf ("the key on test are: \ n" ); Ret = listxattr ("test", Buf, 1000); While (I <RET) {printf ("% s \ n", BUF + I ); I + = strlen (BUF + I); I ++ ;}} void testremove () {char key [7] = "user.2"; int ret; ret = removexattr ("test", key); printf ("% d \ n", RET);} void testget () {char key [7] = "user. # "; char value [3]; int ret, I; printf (" the <key, value> on test are: \ n "); for (I = 0; I <11; I ++) {key [5] = '0' + I; ret = getxattr ("test", key, value, 3); I F (Ret! =-1) printf ("<% s, % S> \ n", key, value) ;}} int main () {testset (); testlist (); testremove (); testget (); Return 0 ;}