Turn from: 7661024
Extended Properties (XATTRS) provides a mechanism for permanently associating a key/value pair to a file, allowing existing file systems to support functionality not provided in the original design. Extended properties are file system agnostic, and applications can manipulate them through a standard interface that does not vary by file system. Each extended property can be distinguished by a unique key, and the contents of the key must be valid utf-8, formatted as Namespace.attribute, with each key in a fully qualified form. It is important to note that the value here can be an array of any byte, not necessarily a character designators, and may not be null at the end, so you must know the size of the value when accessing it. It is also necessary to set the value size when setting it. The usefulness of an extended property: The behavior of the GUI's file management program varies according to the file type. To determine the format of a file, an operating system such as WINODWS only needs to view the file's extension, and UNIX systems often need to look at the contents of the file to determine the type. Some file management programs generate this information directly, and others cache the resulting information for next use. A better approach is to store such metadata in an extended attribute. 4 Extended attribute namespaces defined under Linux: System: Used to implement kernel functions that take advantage of extended properties, such as access control tables. Eg:system.posix_acl_access is the extended property located in this user space, and whether the user can read or write these properties depends on the security module being used. Security: Used to implement a safety module. Trusted: Store restricted information in user space. User: The standard namespace used by the generic process to control access to this namespace through the general file permission bit. Here is an example of using an extended property: Create Disk # DDif=/dev/zero OF=/OPT/TESTPTN count= +# MKE2FS Xattr_disk Mount Disk # Mount-o loop,user_xattr xattr_disk mnt extended attribute Operation # Touch test.txt set extension feature # Setfattr-N user.abc-v123test.txt Display Extension Properties # getfattr-n user.abc test.txt# file:test.txtuser.abc="123"One more example: #include<stdio.h>#include<unistd.h>#include<errno.h>#include<string.h>#include<sys/xattr.h>#include<sys/types.h>voidTestset () {Charkey[7] = {'u','s','e','R','.','#',' /'}; Charvalue[2] = {'#',' /'}; intI, ret; for(i =0; I <Ten; i++) {key[5] = value[0] ='0'+i; RET= Setxattr ("Test", key, Value,2,0); } } voidtestlist () {Charbuf[ +]; intRET, i=0, j =0; printf ("The key on test are:\n"); RET= Listxattr ("Test", BUF, +); while(I <ret) {printf ("%s\n", buf+i); I+ = strlen (buf+i); I++; } } voidTestremove () {Charkey[7] ="User.2"; intret; RET= Removexattr ("Test", key); printf ("%d\n", ret); } voidTestget () {Charkey[7] ="user.#"; Charvalue[3]; intret, I; printf ("The <key,value> on test are:\n"); for(i =0; I < One; i++) {key[5] ='0'+i; RET= Getxattr ("Test", key, Value,3); if(Ret! =-1) printf ("<%s,%s>\n", key, value); } } intMain () {testset (); Testlist (); Testremove (); Testget (); return 0; }
Linux File System Extended Properties "Go"