XRootD
XrdSysFAttrMac.icc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d S y s F A t t r M a c . 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/XrdSysError.hh"
39 #include "XrdSys/XrdSysFAttr.hh"
40 
41 /******************************************************************************/
42 /* X r d S y s F A t t r : : D e l */
43 /******************************************************************************/
44 
45 int XrdSysFAttr::Del(const char *Aname, const char *Path, int fd)
46 {
47  int ec;
48 
49 // Remove the attrbiute but ignore errors if it doesn't exist
50 //
51  ec = (fd < 0 ? removexattr(Path,Aname,0) : fremovexattr(fd, Aname, 0));
52 
53 // Diagnose errors.
54 //
55  if (ec) ec = Diagnose("remove", Aname, Path, errno);
56  return ec;
57 }
58 
59 /******************************************************************************/
60 /* X r d S y s F A t t r : : G e t */
61 /******************************************************************************/
62 
63 int XrdSysFAttr::Get(const char *Aname, void *Aval, int Avsz,
64  const char *Path, int fd)
65 {
66  int ec;
67 
68 // Obtain the attribute.
69 //
70  ec = (fd < 0 ? getxattr(Path, Aname, Aval, Avsz, 0, 0)
71  : fgetxattr(fd, Aname, Aval, Avsz, 0, 0));
72 
73 // Diagnose errors.
74 //
75  if (ec < 0) ec = Diagnose("get", Aname, Path, errno);
76  return ec;
77 }
78 
79 /******************************************************************************/
80 /* X r d S y s F A t t r : : L i s t */
81 /******************************************************************************/
82 
83 int XrdSysFAttr::List(AList **aPL, const char *Path, int fd, int getSz)
84 {
85  AList *aNew;
86  char *Buff, *bP, *bEnd;
87  int ec, Tlen, maxSz = 0, *msP = (getSz ? &maxSz : 0);
88 
89 // First obtain the amount of storage we will need for the whole list
90 //
91  *aPL = 0;
92  Tlen = (fd < 0 ? listxattr(Path, 0, 0, 0) : flistxattr(fd, 0, 0, 0));
93  if (Tlen < 0)
94  {if ((ec = Diagnose("list", "*", Path, errno)) == -ENOATTR) ec = 0;
95  return ec;
96  }
97 
98 // If we don't have any then just return 0. Otherwise, add 4K to the buffer
99 // size just in case some one is adding attributes while we get the list.
100 //
101  if (!Tlen) return 0;
102  Tlen += 4096;
103 
104 // Allocate storage to get the whole list
105 //
106  if (!(Buff = (char *)malloc(Tlen))) return -ENOMEM;
107 
108 // Now get the actual list. We will not recover if someone added an attribute
109 // since the time we actual determined the size of the buffer we need.
110 //
111  Tlen = (fd < 0 ? listxattr(Path,Buff,Tlen,0) : flistxattr(fd,Buff,Tlen,0));
112  if (Tlen < 0)
113  {if ((ec = Diagnose("list", "*", Path, errno)) == -ENOATTR) ec = 0;
114  free(Buff);
115  return ec;
116  }
117  if (!Tlen) return 0;
118 
119 // Run through the memory and allocate an AList entry for each.
120 //
121  bP = Buff; bEnd = Buff+Tlen;
122  while(bP < bEnd)
123  {if ((aNew = getEnt(Path, fd, bP, *aPL, msP))) *aPL = aNew;
124  bP = bP + strlen(bP) + 1;
125  }
126 
127 // All done
128 //
129  free(Buff);
130  return maxSz;
131 }
132 
133 /******************************************************************************/
134 /* X r d S y s F A t t r : : S e t */
135 /******************************************************************************/
136 
137 int XrdSysFAttr::Set(const char *Aname, const void *Aval, int Avsz,
138  const char *Path, int fd, int isNew)
139 {
140  int ec, xFlag = (isNew ? XATTR_CREATE : 0);
141 
142 // Set the attribute
143 //
144  ec = (fd < 0 ? setxattr(Path, Aname, Aval, Avsz, 0, xFlag)
145  : fsetxattr(fd, Aname, Aval, Avsz, 0, xFlag));
146 
147 // Diagnose any errors
148 //
149  if (ec < 0) ec = Diagnose("set", Aname, Path, errno);
150  return ec;
151 }
#define ENOATTR
Definition: XProtocol.hh:1342
XrdOucString Path