xrootd

XrdOucXAttr.hh

Go to the documentation of this file.
00001 #ifndef __XRDOUCXATTR_HH__
00002 #define __XRDOUCXATTR_HH__
00003 /******************************************************************************/
00004 /*                                                                            */
00005 /*                        X r d O u c X A t t r . h h                         */
00006 /*                                                                            */
00007 /* (c) 2010 by the Board of Trustees of the Leland Stanford, Jr., University  */
00008 /*                            All Rights Reserved                             */
00009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
00010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
00011 /******************************************************************************/
00012 
00013 #include <string.h>
00014 #include <sys/types.h>
00015 
00016 #include "XrdSys/XrdSysFAttr.hh"
00017 
00018 /* XrdOucXAttr encapsulates a simple extended attribute variable. The name of
00019    the object encapsulating the xattr definition is a class template argument.
00020    A template format is used with defined methods for effciency. This means that
00021    the template argument object must have five methods:
00022 
00023    int         postGet(int Result) - Formats, if necessary, the attribute value
00024                                      read into the object T if Result > 0.
00025                                      Result is -errno if an error occurred o/w
00026                                      it's the number of bytes read. The method
00027                                      should normaly return Result as this is
00028                                      returned to the caller as the final result
00029                                      of the corresponding XrdOucXAttr::Get().
00030 
00031    T          *preSet(T &x)        - Formats, if necessary, the attribute value
00032                                      prior to writing it out. If formating is
00033                                      required, the data members should be copied
00034                                      into the passed object 'x' and changes made
00035                                      to the copy with the address of 'x' being
00036                                      returned. If no changes are needed, simply
00037                                      return 'this' (the address of yourself).
00038                                      Data is writen from the area pointed to by
00039                                      the returned pointer.
00040 
00041    const char *Name()              - Provides the attribute name. All attribute
00042                                      names are automatically placed in the user
00043                                      namespace so it should not be qualified.
00044 
00045    int         sizeGet()           - Provides the length of the attr value for
00046                                      Get(). No more than this number of bytes
00047                                      are read.
00048 
00049    int         sizeSet()           - Provides the length of the attr value for
00050                                      Set(). This number of bytes are written.
00051 
00052 A sample class would be:
00053 
00054 class myXattr
00055 {public:
00056 
00057  char myVal[1024]; // Define data members here
00058 
00059  int         postGet(int Result)
00060                     {if (Result > 0) {<make changes to yourself>}
00061                      return Result;
00062                     }
00063 
00064  myXattr    *preSet(myXattr &outXattr)
00065                     {setXattr = *this;    // Copy   'this' if changes are needed
00066                      <change setXattr>
00067                      return &setXattr;    // Return 'this' if no changes needed
00068                     }
00069 
00070  const char *Name()    {return "myXattr";}
00071 
00072  int         sizeGet() {return sizeof(myXattr);}
00073 
00074  int         sizeSet() {return strlen(myVal)+1;}
00075 
00076              myXattr() {}
00077             ~myXattr() {}
00078 };
00079 
00080 XrdOucXAttr<myXattr> Foo;
00081 */
00082 
00083 /******************************************************************************/
00084 /*                  T e m p l a t e   X r d O u c X A t t r                   */
00085 /******************************************************************************/
00086   
00087 template<class T>
00088 class XrdOucXAttr
00089 {
00090 public:
00091 
00092 T   Attr; // The attribute value
00093 
00094 /* Del() removes this attribute from the file identified by Path or an open
00095          file with file descriptor of fd (fd must be >= 0).
00096          Success:  Zero  is returned.
00097          Failure: -errno is returned.
00098 */
00099 int Del(const char *Path, int fd=-1)
00100        {return XrdSysFAttr::Del(Attr.Name(), Path, fd);}
00101 
00102 /* Get() get this attribute from the file identified by Path or an open file
00103          with file descriptor of fd (fd must be >= 0). The attribute values are
00104          placed in the object as defined by Attr above.
00105          Success: attribute value length is returned.
00106          Failure: -errno is returned.
00107 */
00108 int Get(const char *Path, int fd=-1)
00109        {return Attr.postGet(XrdSysFAttr::Get(Attr.Name(), &Attr, Attr.sizeGet(),
00110                                              Path, fd));
00111        }
00112 
00113 /* Set() sets the extended attribute for file identified by Path or an open
00114          file with file descriptor of fd (fd must be >= 0). The values are
00115          taken from the object Attr, defined above.
00116          Success:   zero is returned.
00117          Failure: -errno is returned.
00118 */
00119 int Set(const char *Path, int fd=-1)
00120        {T xA;
00121         return XrdSysFAttr::Set(Attr.Name(), Attr.preSet(xA), Attr.sizeSet(),
00122                                 Path, fd);
00123        }
00124 
00125     XrdOucXAttr() {}
00126    ~XrdOucXAttr() {}
00127 };
00128 #endif