xrootd
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 
13 #include <string.h>
14 #include <sys/types.h>
15 
16 #include "XrdSys/XrdSysFAttr.hh"
17 
18 /* XrdOucXAttr encapsulates a simple extended attribute variable. The name of
19  the object encapsulating the xattr definition is a class template argument.
20  A template format is used with defined methods for effciency. This means that
21  the template argument object must have five methods:
22 
23  int postGet(int Result) - Formats, if necessary, the attribute value
24  read into the object T if Result > 0.
25  Result is -errno if an error occurred o/w
26  it's the number of bytes read. The method
27  should normaly return Result as this is
28  returned to the caller as the final result
29  of the corresponding XrdOucXAttr::Get().
30 
31  T *preSet(T &x) - Formats, if necessary, the attribute value
32  prior to writing it out. If formating is
33  required, the data members should be copied
34  into the passed object 'x' and changes made
35  to the copy with the address of 'x' being
36  returned. If no changes are needed, simply
37  return 'this' (the address of yourself).
38  Data is writen from the area pointed to by
39  the returned pointer.
40 
41  const char *Name() - Provides the attribute name. All attribute
42  names are automatically placed in the user
43  namespace so it should not be qualified.
44 
45  int sizeGet() - Provides the length of the attr value for
46  Get(). No more than this number of bytes
47  are read.
48 
49  int sizeSet() - Provides the length of the attr value for
50  Set(). This number of bytes are written.
51 
52 A sample class would be:
53 
54 class myXattr
55 {public:
56 
57  char myVal[1024]; // Define data members here
58 
59  int postGet(int Result)
60  {if (Result > 0) {<make changes to yourself>}
61  return Result;
62  }
63 
64  myXattr *preSet(myXattr &outXattr)
65  {setXattr = *this; // Copy 'this' if changes are needed
66  <change setXattr>
67  return &setXattr; // Return 'this' if no changes needed
68  }
69 
70  const char *Name() {return "myXattr";}
71 
72  int sizeGet() {return sizeof(myXattr);}
73 
74  int sizeSet() {return strlen(myVal)+1;}
75 
76  myXattr() {}
77  ~myXattr() {}
78 };
79 
80 XrdOucXAttr<myXattr> Foo;
81 */
82 
83 /******************************************************************************/
84 /* T e m p l a t e X r d O u c X A t t r */
85 /******************************************************************************/
86 
87 template<class T>
89 {
90 public:
91 
92 T Attr; // The attribute value
93 
94 /* Del() removes this attribute from the file identified by Path or an open
95  file with file descriptor of fd (fd must be >= 0).
96  Success: Zero is returned.
97  Failure: -errno is returned.
98 */
99 int Del(const char *Path, int fd=-1)
100  {return XrdSysFAttr::Del(Attr.Name(), Path, fd);}
101 
102 /* Get() get this attribute from the file identified by Path or an open file
103  with file descriptor of fd (fd must be >= 0). The attribute values are
104  placed in the object as defined by Attr above.
105  Success: attribute value length is returned.
106  Failure: -errno is returned.
107 */
108 int Get(const char *Path, int fd=-1)
109  {return Attr.postGet(XrdSysFAttr::Get(Attr.Name(), &Attr, Attr.sizeGet(),
110  Path, fd));
111  }
112 
113 /* Set() sets the extended attribute for file identified by Path or an open
114  file with file descriptor of fd (fd must be >= 0). The values are
115  taken from the object Attr, defined above.
116  Success: zero is returned.
117  Failure: -errno is returned.
118 */
119 int Set(const char *Path, int fd=-1)
120  {T xA;
121  return XrdSysFAttr::Set(Attr.Name(), Attr.preSet(xA), Attr.sizeSet(),
122  Path, fd);
123  }
124 
127 };
128 #endif