xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdOucXAttr.hh
Go to the documentation of this file.
1 #ifndef __XRDOUCXATTR_HH__
2 #define __XRDOUCXATTR_HH__
3 /******************************************************************************/
4 /* */
5 /* X r d O u c X A t t r . h h */
6 /* */
7 /* (c) 2010 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* All Rights Reserved */
9 /* Produced by Andrew Hanushevsky for Stanford University under contract */
10 /* DE-AC02-76-SFO0515 with the Department of Energy */
11 /* */
12 /* This file is part of the XRootD software suite. */
13 /* */
14 /* XRootD is free software: you can redistribute it and/or modify it under */
15 /* the terms of the GNU Lesser General Public License as published by the */
16 /* Free Software Foundation, either version 3 of the License, or (at your */
17 /* option) any later version. */
18 /* */
19 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
20 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
21 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
22 /* License for more details. */
23 /* */
24 /* You should have received a copy of the GNU Lesser General Public License */
25 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
26 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
27 /* */
28 /* The copyright holder's institutional names and contributor's names may not */
29 /* be used to endorse or promote products derived from this software without */
30 /* specific prior written permission of the institution or contributor. */
31 /******************************************************************************/
32 
33 #include <cstring>
34 #include <sys/types.h>
35 
36 #include "XrdSys/XrdSysFAttr.hh"
37 
38 /* XrdOucXAttr encapsulates a simple extended attribute variable. The name of
39  the object encapsulating the xattr definition is a class template argument.
40  A template format is used with defined methods for effciency. This means that
41  the template argument object must have five methods:
42 
43  int postGet(int Result) - Formats, if necessary, the attribute value
44  read into the object T if Result > 0.
45  Result is -errno if an error occurred o/w
46  it's the number of bytes read. The method
47  should normaly return Result as this is
48  returned to the caller as the final result
49  of the corresponding XrdOucXAttr::Get().
50 
51  T *preSet(T &x) - Formats, if necessary, the attribute value
52  prior to writing it out. If formating is
53  required, the data members should be copied
54  into the passed object 'x' and changes made
55  to the copy with the address of 'x' being
56  returned. If no changes are needed, simply
57  return 'this' (the address of yourself).
58  Data is writen from the area pointed to by
59  the returned pointer.
60 
61  const char *Name() - Provides the attribute name. All attribute
62  names are automatically placed in the user
63  namespace so it should not be qualified.
64 
65  int sizeGet() - Provides the length of the attr value for
66  Get(). No more than this number of bytes
67  are read.
68 
69  int sizeSet() - Provides the length of the attr value for
70  Set(). This number of bytes are written.
71 
72 A sample class would be:
73 
74 class myXattr
75 {public:
76 
77  char myVal[1024]; // Define data members here
78 
79  int postGet(int Result)
80  {if (Result > 0) {<make changes to yourself>}
81  return Result;
82  }
83 
84  myXattr *preSet(myXattr &outXattr)
85  {setXattr = *this; // Copy 'this' if changes are needed
86  <change setXattr>
87  return &setXattr; // Return 'this' if no changes needed
88  }
89 
90  const char *Name() {return "myXattr";}
91 
92  int sizeGet() {return sizeof(myXattr);}
93 
94  int sizeSet() {return strlen(myVal)+1;}
95 
96  myXattr() {}
97  ~myXattr() {}
98 };
99 
100 XrdOucXAttr<myXattr> Foo;
101 */
102 
103 /******************************************************************************/
104 /* T e m p l a t e X r d O u c X A t t r */
105 /******************************************************************************/
106 
107 template<class T>
109 {
110 public:
111 
112 T Attr; // The attribute value
113 
114 /* Del() removes this attribute from the file identified by Path or an open
115  file with file descriptor of fd (fd must be >= 0).
116  Success: Zero is returned.
117  Failure: -errno is returned.
118 */
119 int Del(const char *Path, int fd=-1)
120  {return XrdSysFAttr::Xat->Del(Attr.Name(), Path, fd);}
121 
122 /* Get() get this attribute from the file identified by Path or an open file
123  with file descriptor of fd (fd must be >= 0). The attribute values are
124  placed in the object as defined by Attr above.
125  Success: attribute value length is returned.
126  Failure: -errno is returned.
127 */
128 int Get(const char *Path, int fd=-1)
129  {return Attr.postGet(XrdSysFAttr::Xat->Get(Attr.Name(), &Attr,
130  Attr.sizeGet(), Path, fd));
131  }
132 
133 /* Set() sets the extended attribute for file identified by Path or an open
134  file with file descriptor of fd (fd must be >= 0). The values are
135  taken from the object Attr, defined above.
136  Success: zero is returned.
137  Failure: -errno is returned.
138 */
139 int Set(const char *Path, int fd=-1)
140  {T xA;
141  return XrdSysFAttr::Xat->Set(Attr.Name(), Attr.preSet(xA),
142  Attr.sizeSet(), Path, fd);
143  }
144 
147 };
148 #endif
int Del(const char *Path, int fd=-1)
Definition: XrdOucXAttr.hh:119
virtual int Set(const char *Aname, const void *Aval, int Avsz, const char *Path, int fd=-1, int isNew=0)=0
Definition: XrdOucXAttr.hh:108
static XrdSysXAttr * Xat
Definition: XrdSysFAttr.hh:51
~XrdOucXAttr()
Definition: XrdOucXAttr.hh:146
int Set(const char *Path, int fd=-1)
Definition: XrdOucXAttr.hh:139
T Attr
Definition: XrdOucXAttr.hh:112
virtual int Del(const char *Aname, const char *Path, int fd=-1)=0
int Get(const char *Path, int fd=-1)
Definition: XrdOucXAttr.hh:128
XrdOucXAttr()
Definition: XrdOucXAttr.hh:145