XRootD
XrdSysFAttrLnx.icc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d S y s F A t t r L n x . i c c */
4 /* */
5 /* (c) 2010 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #include <cerrno>
32 #include <cstdio>
33 #include <cstring>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/xattr.h>
37 
38 #include "XrdSys/XrdSysFAttr.hh"
39 
40 /******************************************************************************/
41 /* L o c a l D e f i n i t i o n s */
42 /******************************************************************************/
43 
44 #define AttrName(Aname, Abuff, Ablen) snprintf(Abuff, Ablen, "user.%s", Aname);
45 
46 /******************************************************************************/
47 /* X r d S y s F A t t r : : D e l */
48 /******************************************************************************/
49 
50 int XrdSysFAttr::Del(const char *Aname, const char *Path, int fd)
51 {
52  char Avar[512];
53  int ec;
54 
55 // Qualify the name
56 //
57  AttrName(Aname, Avar, sizeof(Avar));
58 
59 // Remove the attribute
60 //
61  ec = (fd < 0 ? removexattr(Path, Avar) : fremovexattr(fd, Avar));
62 
63 // Diagnose errors.
64 //
65  if (ec) ec = Diagnose("remove", Aname, Path, errno);
66  return ec;
67 }
68 
69 /******************************************************************************/
70 /* X r d S y s F A t t r : : G e t */
71 /******************************************************************************/
72 
73 int XrdSysFAttr::Get(const char *Aname, void *Aval, int Avsz,
74  const char *Path, int fd)
75 {
76  char Avar[512];
77  int ec;
78 
79 // Qualify the name
80 //
81  AttrName(Aname, Avar, sizeof(Avar));
82 
83 // Get the attribute
84 //
85  ec = (fd < 0 ? getxattr(Path, Avar, Aval, Avsz)
86  : fgetxattr(fd, Avar, Aval, Avsz));
87 
88 // Diagnose errors.
89 //
90  if (ec < 0) ec = Diagnose("get", Aname, Path, errno);
91  return ec;
92 }
93 
94 /******************************************************************************/
95 /* X r d S y s F A t t r : : L i s t */
96 /******************************************************************************/
97 
98 int XrdSysFAttr::List(AList **aPL, const char *Path, int fd, int getSz)
99 {
100  AList *aNew;
101  char *Buff = 0, *bP, *bEnd;
102  int ec, Tlen, maxSz = 0, *msP = (getSz ? &maxSz : 0);
103 
104 // First obtain the amount of storage we will need for the whole list
105 //
106  *aPL = 0;
107  Tlen = (fd < 0 ? listxattr(Path, Buff, 0) : flistxattr(fd, Buff, 0));
108  if (Tlen < 0)
109  {if ((ec = Diagnose("list", "*", Path, errno)) == -ENOATTR) ec = 0;
110  return ec;
111  }
112 
113 // If we don't have any then just return 0. Otherwise, add 4K to the buffer
114 // size just in case some one is adding attributes while we get the list.
115 //
116  if (!Tlen) return 0;
117  Tlen += 4096;
118 
119 // Allocate storage to get the whole list
120 //
121  if (!(Buff = (char *)malloc(Tlen))) return -ENOMEM;
122 
123 // Now get the actual list. We will not recover if someone added an attribute
124 // since the time we actual determined the size of the buffer we need.
125 //
126  Tlen = (fd < 0 ? listxattr(Path, Buff, Tlen) : flistxattr(fd, Buff, Tlen));
127  if (Tlen < 0)
128  {if ((ec = Diagnose("list", "*", Path, errno)) == -ENOATTR) ec = 0;
129  free(Buff);
130  return ec;
131  }
132  if (!Tlen) return 0;
133 
134 // Run through the memory and allocate an AList entry for each. Note that we
135 // strip off the name space prefix.
136 //
137  bP = Buff; bEnd = Buff+Tlen;
138  while(bP < bEnd)
139  {if (!strncmp("user.", bP, 5) && bP[5]
140  && (aNew = getEnt(Path, fd, bP+5, *aPL, msP))) *aPL = aNew;
141  bP = bP + strlen(bP) + 1;
142  }
143 
144 // All done
145 //
146  free(Buff);
147  return maxSz;
148 }
149 
150 /******************************************************************************/
151 /* X r d S y s F A t t r : : S e t */
152 /******************************************************************************/
153 
154 int XrdSysFAttr::Set(const char *Aname, const void *Aval, int Avsz,
155  const char *Path, int fd, int isNew)
156 {
157  char Avar[512];
158  int ec, xFlag = (isNew ? XATTR_CREATE : 0);
159 
160 // Qualify the name
161 //
162  AttrName(Aname, Avar, sizeof(Avar));
163 
164 // Set the attribute
165 //
166  ec = (fd < 0 ? setxattr(Path, Avar, Aval, Avsz, xFlag)
167  : fsetxattr(fd, Avar, Aval, Avsz, xFlag));
168 
169 // Diagnose any errors
170 //
171  if (ec < 0) ec = Diagnose("set", Aname, Path, errno);
172  return ec;
173 }
#define ENOATTR
Definition: XProtocol.hh:1342
XrdOucString Path
#define AttrName(Aname, Abuff, Ablen)