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 /* This file is part of the XRootD software suite.                            */
00013 /*                                                                            */
00014 /* XRootD is free software: you can redistribute it and/or modify it under    */
00015 /* the terms of the GNU Lesser General Public License as published by the     */
00016 /* Free Software Foundation, either version 3 of the License, or (at your     */
00017 /* option) any later version.                                                 */
00018 /*                                                                            */
00019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
00020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
00021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
00022 /* License for more details.                                                  */
00023 /*                                                                            */
00024 /* You should have received a copy of the GNU Lesser General Public License   */
00025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
00026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
00027 /*                                                                            */
00028 /* The copyright holder's institutional names and contributor's names may not */
00029 /* be used to endorse or promote products derived from this software without  */
00030 /* specific prior written permission of the institution or contributor.       */
00031 /******************************************************************************/
00032 
00033 #include <string.h>
00034 #include <sys/types.h>
00035 
00036 #include "XrdSys/XrdSysFAttr.hh"
00037 
00038 /* XrdOucXAttr encapsulates a simple extended attribute variable. The name of
00039    the object encapsulating the xattr definition is a class template argument.
00040    A template format is used with defined methods for effciency. This means that
00041    the template argument object must have five methods:
00042 
00043    int         postGet(int Result) - Formats, if necessary, the attribute value
00044                                      read into the object T if Result > 0.
00045                                      Result is -errno if an error occurred o/w
00046                                      it's the number of bytes read. The method
00047                                      should normaly return Result as this is
00048                                      returned to the caller as the final result
00049                                      of the corresponding XrdOucXAttr::Get().
00050 
00051    T          *preSet(T &x)        - Formats, if necessary, the attribute value
00052                                      prior to writing it out. If formating is
00053                                      required, the data members should be copied
00054                                      into the passed object 'x' and changes made
00055                                      to the copy with the address of 'x' being
00056                                      returned. If no changes are needed, simply
00057                                      return 'this' (the address of yourself).
00058                                      Data is writen from the area pointed to by
00059                                      the returned pointer.
00060 
00061    const char *Name()              - Provides the attribute name. All attribute
00062                                      names are automatically placed in the user
00063                                      namespace so it should not be qualified.
00064 
00065    int         sizeGet()           - Provides the length of the attr value for
00066                                      Get(). No more than this number of bytes
00067                                      are read.
00068 
00069    int         sizeSet()           - Provides the length of the attr value for
00070                                      Set(). This number of bytes are written.
00071 
00072 A sample class would be:
00073 
00074 class myXattr
00075 {public:
00076 
00077  char myVal[1024]; // Define data members here
00078 
00079  int         postGet(int Result)
00080                     {if (Result > 0) {<make changes to yourself>}
00081                      return Result;
00082                     }
00083 
00084  myXattr    *preSet(myXattr &outXattr)
00085                     {setXattr = *this;    // Copy   'this' if changes are needed
00086                      <change setXattr>
00087                      return &setXattr;    // Return 'this' if no changes needed
00088                     }
00089 
00090  const char *Name()    {return "myXattr";}
00091 
00092  int         sizeGet() {return sizeof(myXattr);}
00093 
00094  int         sizeSet() {return strlen(myVal)+1;}
00095 
00096              myXattr() {}
00097             ~myXattr() {}
00098 };
00099 
00100 XrdOucXAttr<myXattr> Foo;
00101 */
00102 
00103 /******************************************************************************/
00104 /*                  T e m p l a t e   X r d O u c X A t t r                   */
00105 /******************************************************************************/
00106   
00107 template<class T>
00108 class XrdOucXAttr
00109 {
00110 public:
00111 
00112 T   Attr; // The attribute value
00113 
00114 /* Del() removes this attribute from the file identified by Path or an open
00115          file with file descriptor of fd (fd must be >= 0).
00116          Success:  Zero  is returned.
00117          Failure: -errno is returned.
00118 */
00119 int Del(const char *Path, int fd=-1)
00120        {return XrdSysFAttr::Del(Attr.Name(), Path, fd);}
00121 
00122 /* Get() get this attribute from the file identified by Path or an open file
00123          with file descriptor of fd (fd must be >= 0). The attribute values are
00124          placed in the object as defined by Attr above.
00125          Success: attribute value length is returned.
00126          Failure: -errno is returned.
00127 */
00128 int Get(const char *Path, int fd=-1)
00129        {return Attr.postGet(XrdSysFAttr::Get(Attr.Name(), &Attr, Attr.sizeGet(),
00130                                              Path, fd));
00131        }
00132 
00133 /* Set() sets the extended attribute for file identified by Path or an open
00134          file with file descriptor of fd (fd must be >= 0). The values are
00135          taken from the object Attr, defined above.
00136          Success:   zero is returned.
00137          Failure: -errno is returned.
00138 */
00139 int Set(const char *Path, int fd=-1)
00140        {T xA;
00141         return XrdSysFAttr::Set(Attr.Name(), Attr.preSet(xA), Attr.sizeSet(),
00142                                 Path, fd);
00143        }
00144 
00145     XrdOucXAttr() {}
00146    ~XrdOucXAttr() {}
00147 };
00148 #endif