xrootd
XrdOucErrInfo.hh
Go to the documentation of this file.
1 #ifndef __OUC_ERRINFO_H__
2 #define __OUC_ERRINFO_H__
3 /******************************************************************************/
4 /* */
5 /* X r d O u c E r r I n f o . h h */
6 /* */
7 /* (c) 2043 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /* */
31 /******************************************************************************/
32 
33 #include <string.h> // For strlcpy()
34 #include <sys/types.h>
35 
36 #include "XrdSys/XrdSysPlatform.hh"
37 
38 /******************************************************************************/
39 /* X r d O u c E I */
40 /******************************************************************************/
41 
42 struct XrdOucEI // Err information structure
43 {
44  static const size_t Max_Error_Len = 2048;
45  static const int Path_Offset = 1024;
46 
47 const char *user;
48  int code;
50 
51  void clear(const char *usr=0)
52  {code=0; message[0]='\0'; user = (usr ? usr : "?");}
53 
55  {code = rhs.code;
56  user = rhs.user;
57  strcpy(message, rhs.message);
58  return *this;
59  }
60  XrdOucEI(const char *usr) {clear(usr);}
61 };
62 
63 /******************************************************************************/
64 /* X r d O u c E r r I n f o */
65 /******************************************************************************/
66 
67 class XrdOucEICB;
68 class XrdOucEnv;
69 class XrdSysSemaphore;
70 
72 {
73 public:
74  void clear() {ErrInfo.clear();}
75 
76 inline void setErrArg(unsigned long long cbarg=0) {ErrCBarg = cbarg;}
77 inline void setErrCB(XrdOucEICB *cb, unsigned long long cbarg=0)
78  {ErrCB = cb; ErrCBarg = cbarg;}
79 inline int setErrCode(int code) {return ErrInfo.code = code;}
80 inline int setErrInfo(int code, const char *message)
81  {strlcpy(ErrInfo.message, message, sizeof(ErrInfo.message));
82  return ErrInfo.code = code;
83  }
84 inline int setErrInfo(int code, const char *txtlist[], int n)
85  {int i, j = 0, k = sizeof(ErrInfo.message), l;
86  for (i = 0; i < n && k > 1; i++)
87  {l = strlcpy(&ErrInfo.message[j], txtlist[i], k);
88  j += l; k -= l;
89  }
90  return ErrInfo.code = code;
91  }
92 inline void setErrUser(const char *user) {ErrInfo.user = (user ? user : "?");}
93 
94 inline unsigned long long getErrArg() {return ErrCBarg;}
95 
96 inline char *getMsgBuff(int &mblen)
97  {mblen = sizeof(ErrInfo.message);
98  return ErrInfo.message;
99  }
100 inline XrdOucEICB *getErrCB() {return ErrCB;}
101 inline XrdOucEICB *getErrCB(unsigned long long &ap)
102  {ap = ErrCBarg; return ErrCB;}
103 inline int getErrInfo() {return ErrInfo.code;}
104 inline int getErrInfo(XrdOucEI &errorParm)
105  {errorParm = ErrInfo; return ErrInfo.code;}
106 inline const char *getErrText()
107  {return (const char *)ErrInfo.message;}
108 inline const char *getErrText(int &ecode)
109  {ecode = ErrInfo.code;
110  return (const char *)ErrInfo.message;}
111 inline const char *getErrUser() {return ErrInfo.user;}
112 
113 inline XrdOucEnv *getEnv() {return (ErrCB ? 0 : ErrEnv);}
114 
115 inline XrdOucEnv *setEnv(XrdOucEnv *newEnv)
116  {XrdOucEnv *oldEnv = (ErrCB ? 0 : ErrEnv);
117  ErrEnv = newEnv;
118  ErrCB = 0;
119  return oldEnv;
120  }
121 
122 inline const char *getErrData()
123  {return (dOff < 0 ? 0 : ErrInfo.message+dOff);}
124 
125 inline void setErrData(const char *Data, int Offs=0)
126  {if (!Data) dOff = -1;
127  else {strlcpy(ErrInfo.message+Offs, Data,
128  sizeof(ErrInfo.message)-Offs);
129  dOff = Offs;
130  }
131  }
132 
133 inline int getErrMid() {return mID;}
134 
135 inline void setErrMid(int mid) {mID = mid;}
136 
138  {ErrInfo = rhs.ErrInfo;
139  ErrCB = rhs.ErrCB;
140  ErrCBarg= rhs.ErrCBarg;
141  mID = rhs.mID;
142  dOff = -1;
143  return *this;
144  }
145 
146  XrdOucErrInfo(const char *user=0,XrdOucEICB *cb=0,
147  unsigned long long ca=0, int mid=0)
148  : ErrInfo(user), ErrCB(cb), ErrCBarg(ca), mID(mid),
149  dOff(-1), Reserved0(0), Reserved1(0) {}
150 
151  XrdOucErrInfo(const char *user,XrdOucEnv *envp)
152  : ErrInfo(user), ErrCB(0), ErrEnv(envp), mID(0),
153  dOff(-1), Reserved0(0), Reserved1(0) {}
154 
155  XrdOucErrInfo(const char *user, int MonID)
156  : ErrInfo(user), ErrCB(0), ErrCBarg(0), mID(MonID),
157  dOff(-1), Reserved0(0), Reserved1(0) {}
158 
159 virtual ~XrdOucErrInfo() {}
160 
161 protected:
162 
165 union {
166 unsigned long long ErrCBarg;
168  };
169 int mID;
170 short dOff;
171 short Reserved0;
172 void *Reserved1;
173 };
174 
175 /******************************************************************************/
176 /* X r d O u c E I C B */
177 /******************************************************************************/
178 
180 {
181 public:
182 
183 // Done() is invoked when the requested operation completes. Arguments are:
184 // Result - the original function's result (may be changed).
185 // eInfo - Associated error information. The eInfo object may not be
186 // modified until it's own callback Done() method is called, if
187 // supplied. If the callback function in eInfo is zero, then the
188 // eInfo object is deleted by the invoked callback. Otherwise,
189 // that method must be invoked by this callback function after
190 // the actual callback message is sent. This allows the callback
191 // requestor to do post-processing and be asynchronous.
192 // Path - Optionally, the path related to thid request. It is used
193 // for tracing and detailed monitoring purposes.
194 //
195 //
196 virtual void Done(int &Result, //I/O: Function result
197  XrdOucErrInfo *eInfo, // In: Error Info
198  const char *Path=0)=0;// In: Relevant path
199 
200 // Same() is invoked to determine if two arguments refer to the same user.
201 // True is returned if so, false, otherwise.
202 //
203 virtual int Same(unsigned long long arg1, unsigned long long arg2)=0;
204 
206 virtual ~XrdOucEICB() {}
207 };
208 #endif