Rajendra's Blog
Named Attributes
Recently a new Java language API 'java.nio.flile.attribute.NamedAttributeView' has been added to JSR-203 specification. This allows to manipulate the extended attributes associated with a file in the file system. Operating Systems like Solaris, Linux and Windows support extended attributes in different ways. I will give an overview of what is an extended attribute and how can we add or delete the attributes in different file systems. Having known this file system feature makes you easier to play with the new Java technology APIs.
Every file system defines a set of file system specific attributes that describe the state of the file. These attributes are stored as part of metadata of the file. The attributes like filename, last modified time, size etc. are set by the file system when you create or modify a file. Extended attribute is a new file system feature that enables user to add an attribute to a file which is maintained in the metadata of the file.
To give a usage of named attribute, assume you have a collection of mp3 files which are hundred in number stored in a directory 'my_favorite_collection'. If you want to list out all the songs of a particular singer or songs of specific type( emotional, love, inspirational ) then there is no way if you depend on file system specific attributes. Extended attributes helps you to associate an attribute with the file. So you can add attributes like 'singer','type' etc. to the files and you can list out files of your choice by specifying a criteria.The way you associate an attribute to a file is different in different file systems. Lets first look at how we can add named attributes in Windows.
Windows NTFS file system supports alternate data streams. You can associate an alternate data streams to file as follows.
To display the contents of the named attributes use the following toolC:\> echo this content will be stored in the file sub stream > demo.txt:attr.1
In Windows Vista you can use DIR /R to display the alternate data streams of the files. You can add named attributes to symbolic links in Vista.C:\> more < demo.txt:attr.1
You can add a data stream to a file using 'type' command.
which copies the contents of demofile.txt as an alternate data stream to file demo.txt as an attribute name 'attr3'.C:\> type demofile.txt > demo.txt:attr3
The above type command does not recognize data streams instead you have to use 'more' command. There is no tool as such in windows to manipulate the alternate data streams. Now using JSR-203 API you can list all the attributes, add and delete attributes irrespective of the way they defined in file system.C:\> type demo.txt:attr3
Lets see how we can add extended attributes in Solaris:
In Solaris you can associate regular files as an extended attribute to a file or directory. To add a sub file to a file/directory you can use 'runat' tool which executes a command in extended attribute name space.
Remember that you can add named attributes only for a file or a directory but not for a symbolic link.To add a sub file /home/rajendra/filenotes to a file file.1, execute the following command.
To remove an extended attribute:user% runat file.1 cp /home/rajendra/filenotes attr.1
To open shell in the extended attribute name space for the file 'file.1', you can use the following commanduser% runat file.1 rm attr.1
Extended attributes in Linux:user% runat file.1 /bin/sh $
Linux supports extended attributes as name -value pairs. You can tag a file with a name value pairs. In order to add an extend attributes to files you have to enable this feature in the file system (ext2,ext3 or Reiser file systems)
you can enable this feature by remounting the partition as follows'setfattr' and 'getfattr' tools comes in handy to manipulate extended attributes in Linux. If you don't find this tools (in any Linux flavour) then you have to install the attr package in Ubuntu as follows.#sudo mount -o remount,user_xattr /
That's it. Now we can add the attributes. Final thing to remember is we have to add extended attributes in the 'user' name space. To add exteded attribute user.songs.title to the file songs1.mp3# apt-get install attr
To retrieve the attributes for the file song1.mp3$ setfattr -n user.songs.title -v “ My heart will go on” song1.mp3
To delete an attribute:$ getfattr -n user.songs.title song1.mp3 # file: song1.mp3 user.songs.title = song1.mp3.
That's it for now. Next we will see how easy to access extended attributes using JSR-203 APIs.$ setfattr -x user.songs.title song1.mp3
Posted at 09:22PM Jun 13, 2008 by Rajendra Gutupalli in Sun | Comments[2]
Hi Rajendra,
I am working with the ntfs-3g project, aiming at providing NTFS storage
for multiple OS (Linux, MacOSX, etc.)
Now, making the NamedAttributeView API portable would also mean a
named attribute be stored the same way on NTFS whatever the OS used
(just think of a pluggable storage device being plugged into several
computers).
From what I have read, a named attribute "myattrib" would be stored on
Windows XP as an alternate data stream just named "myattrib". From your
post, I infer that an extended attribute would be used on Linux, but it
is not clear to me if its name would be "user.myattrib".
Can you give some details about the low-level attributes to which a
named attribute would be mapped (Windows, Solaris, Linux MacOSX) ?
Regards
Jean-Pierre
Posted by Jean-Pierre Andre on January 16, 2009 at 02:34 PM IST #
my answer
Posted by vikram jadhav on August 08, 2009 at 01:11 PM IST #