XRootD
XrdXrootdPrepare Class Reference

#include <XrdXrootdPrepare.hh>

+ Inheritance diagram for XrdXrootdPrepare:
+ Collaboration diagram for XrdXrootdPrepare:

Public Member Functions

 XrdXrootdPrepare (XrdSysError *lp, XrdScheduler *sp, bool nomsg)
 
 ~XrdXrootdPrepare ()
 
void DoIt ()
 
- Public Member Functions inherited from XrdJob
 XrdJob (const char *desc="")
 
virtual ~XrdJob ()
 

Static Public Member Functions

static int Close (int fd)
 
static int List (XrdXrootdPrepArgs &pargs, char *resp, int resplen)
 
static void Log (XrdXrootdPrepArgs &pargs)
 
static void Logdel (char *reqid)
 
static int Open (const char *reqid, int &fsz)
 
static void Scrub ()
 
static int setParms (char *ldir)
 
static int setParms (int stime, int skeep)
 

Additional Inherited Members

- Public Attributes inherited from XrdJob
const char * Comment
 
XrdJobNextJob
 

Detailed Description

Definition at line 83 of file XrdXrootdPrepare.hh.

Constructor & Destructor Documentation

◆ XrdXrootdPrepare()

XrdXrootdPrepare::XrdXrootdPrepare ( XrdSysError lp,
XrdScheduler sp,
bool  nomsg 
)

Definition at line 79 of file XrdXrootdPrepare.cc.

80  : XrdJob("Prep log scrubber")
81 {eDest = errp;
82  SchedP = sp;
83  if (LogDir) SchedP->Schedule((XrdJob *)this, scrubtime+time(0));
84 // else if (!nomsg) eDest->Say("Config warning: 'xrootd.prepare logdir' "
85 // "not specified; prepare tracking disabled.");
86 }
Definition: XrdJob.hh:43
XrdJob(const char *desc="")
Definition: XrdJob.hh:51
void Schedule(XrdJob *jp)

References XrdScheduler::Schedule().

+ Here is the call graph for this function:

◆ ~XrdXrootdPrepare()

XrdXrootdPrepare::~XrdXrootdPrepare ( )
inline

Definition at line 108 of file XrdXrootdPrepare.hh.

108 {} // Never gets deleted

Member Function Documentation

◆ Close()

static int XrdXrootdPrepare::Close ( int  fd)
inlinestatic

Definition at line 87 of file XrdXrootdPrepare.hh.

87 {return close(fd);}
#define close(a)
Definition: XrdPosix.hh:48

References close.

◆ DoIt()

void XrdXrootdPrepare::DoIt ( )
inlinevirtual

Implements XrdJob.

Definition at line 89 of file XrdXrootdPrepare.hh.

89  {Scrub();
90  SchedP->Schedule((XrdJob *)this, scrubtime+time(0));
91  }
static void Scrub()

References XrdScheduler::Schedule(), and Scrub().

+ Here is the call graph for this function:

◆ List()

int XrdXrootdPrepare::List ( XrdXrootdPrepArgs pargs,
char *  resp,
int  resplen 
)
static

Definition at line 92 of file XrdXrootdPrepare.cc.

93 {
94  char *up, path[2048];
95  struct dirent *dp;
96  struct stat buf;
97  int rc;
98 
99 // If logging is not supported, return eof
100 //
101  if (!LogDir) return -1;
102 
103 // Check if this is the first call
104 //
105  if (!pargs.dirP)
106  {if (!(pargs.dirP = opendir((const char *)LogDir)))
107  {eDest->Emsg("List", errno, "open prep log directory", LogDir);
108  return -1;
109  }
110  if (pargs.reqid) pargs.reqlen = strlen(pargs.reqid);
111  if (pargs.user) pargs.usrlen = strlen(pargs.user);
112  }
113 
114 // Find the next entry that satisfies the search criteria
115 //
116  errno = 0;
117  while((dp = readdir(pargs.dirP)))
118  {if (!(up = (char *) index((const char *)dp->d_name, '_'))) continue;
119  if (pargs.reqlen && pargs.reqid
120  && strncmp(dp->d_name, pargs.reqid, pargs.reqlen))
121  continue;
122  if (pargs.usrlen && pargs.user)
123  if (!up || strcmp((const char *)up+1,(const char *)pargs.user))
124  continue;
125  strcpy(path, (const char *)LogDir);
126  strcpy(path+LogDirLen, (const char *)dp->d_name);
127  if (stat((const char *)path, &buf)) continue;
128  *up = ' ';
129  if ((up = (char *) index((const char *)(up+1), (int)'_'))) *up = ' ';
130  else continue;
131  if ((up = (char *) index((const char *)(up+1), (int)'_'))) *up = ' ';
132  else continue;
133  return snprintf(resp, resplen-1, "%s %lld",
134  dp->d_name, (long long) buf.st_mtime);
135  }
136 
137 // Completed
138 //
139  if ((rc = errno))
140  eDest->Emsg("List", errno, "read prep log directory", LogDir);
141  closedir(pargs.dirP);
142  pargs.dirP = 0;
143  return (rc ? -1 : 0);
144 }
static XrdSysError eDest(0,"crypto_")
int closedir(DIR *dirp)
DIR * opendir(const char *path)
#define stat(a, b)
Definition: XrdPosix.hh:101
#define readdir(a)
Definition: XrdPosix.hh:86
int Emsg(const char *esfx, int ecode, const char *text1, const char *text2=0)
Definition: XrdSysError.cc:95

References closedir(), eDest, XrdSysError::Emsg(), opendir(), readdir, XrdXrootdPrepArgs::reqid, stat, and XrdXrootdPrepArgs::user.

+ Here is the call graph for this function:

◆ Log()

void XrdXrootdPrepare::Log ( XrdXrootdPrepArgs pargs)
static

Definition at line 150 of file XrdXrootdPrepare.cc.

151 {
152  int rc, pnum = 0, xfd;
153  XrdOucTList *tp = pargs.paths;
154  char buff[2048], blink[2048];
155  struct iovec iovec[2];
156 
157 // If logging not enabled, return
158 //
159  if (!LogDir) return;
160 
161 // Count number of paths in the list
162 //
163  while(tp) {pnum++; tp = tp->next;}
164 
165 // Construct the file name: <reqid>_<user>_<prty>_<numpaths>
166 //
167  snprintf(buff, sizeof(buff)-1, "%s%s_%s_%d_%d", LogDir,
168  pargs.reqid, pargs.user, pargs.prty, pnum);
169 
170 // Create the file
171 //
172  if ((xfd = open(buff, O_WRONLY|O_CREAT|O_TRUNC,0644)) < 0)
173  {eDest->Emsg("Log", errno, "open prep log file", buff);
174  return;
175  }
176 
177 // Write all the paths into the file, separating each by a space
178 //
179  iovec[1].iov_base = (char *)" ";
180  iovec[1].iov_len = 1;
181  tp = pargs.paths;
182  while(tp)
183  {if (tp->next == 0) iovec[1].iov_base = (char *)"\n";
184  iovec[0].iov_base = tp->text;
185  iovec[0].iov_len = strlen(tp->text);
186  do {rc = writev(xfd, (const struct iovec *)iovec, 2);}
187  while(rc < 0 && errno == EINTR);
188  if (rc < 0)
189  {eDest->Emsg("Log", errno, "write prep log file", buff);
190  close(xfd);
191  return;
192  }
193  tp = tp->next;
194  }
195 
196 // Create a symlink to the file
197 //
198  close(xfd);
199  strcpy(blink, LogDir);
200  strlcpy(blink+LogDirLen, pargs.reqid, sizeof(blink)-1);
201  if (symlink((const char *)buff, (const char *)blink))
202  {eDest->Emsg("Log", errno, "create symlink to prep log file", buff);
203  return;
204  }
205 }
ssize_t writev(int fildes, const struct iovec *iov, int iovcnt)
#define open
Definition: XrdPosix.hh:76
size_t strlcpy(char *dst, const char *src, size_t sz)
XrdOucTList * next
Definition: XrdOucTList.hh:45
char * text
Definition: XrdOucTList.hh:46
XrdOucTList * paths

References close, eDest, XrdSysError::Emsg(), XrdOucTList::next, open, XrdXrootdPrepArgs::paths, XrdXrootdPrepArgs::prty, XrdXrootdPrepArgs::reqid, strlcpy(), XrdOucTList::text, XrdXrootdPrepArgs::user, and writev().

+ Here is the call graph for this function:

◆ Logdel()

void XrdXrootdPrepare::Logdel ( char *  reqid)
static

Definition at line 211 of file XrdXrootdPrepare.cc.

212 {
213  int rc;
214  char path[MAXPATHLEN+256], buff[MAXPATHLEN+1];
215 
216 // If logging not enabled, return
217 //
218  if (!LogDir || strlen(reqid) > 255) return;
219 
220 // Construct the file name of the symlink
221 //
222  strcpy(path, (const char *)LogDir);
223  strcpy(&path[LogDirLen], (const char *)reqid);
224 
225 // Read the symlink contents for this request
226 //
227  if ((rc = readlink((const char *)path, buff, sizeof(buff)-1)) < 0)
228  {if (errno != ENOENT) eDest->Emsg("Logdel",errno,"read symlink",path);
229  return;
230  }
231 
232 // Delete the file, then the symlink
233 //
234  buff[rc] = '\0';
235  if (unlink((const char *)buff)
236  && errno != ENOENT) eDest->Emsg("Logdel",errno,"remove",buff);
237  else TRACE(DEBUG, "Logdel removed " <<buff);
238  if (unlink((const char *)path)
239  && errno != ENOENT) eDest->Emsg("Logdel", errno, "remove", path);
240  else TRACE(DEBUG, "Logdel removed " <<path);
241 }
#define DEBUG(x)
Definition: XrdBwmTrace.hh:54
int unlink(const char *path)
#define TRACE(act, x)
Definition: XrdTrace.hh:63

References DEBUG, eDest, XrdSysError::Emsg(), TRACE, and unlink().

+ Here is the call graph for this function:

◆ Open()

int XrdXrootdPrepare::Open ( const char *  reqid,
int &  fsz 
)
static

Definition at line 247 of file XrdXrootdPrepare.cc.

248 {
249  int fd;
250  char path[MAXPATHLEN+264];
251  struct stat buf;
252 
253 // If logging is not supported, indicate so
254 //
255  if (!LogDir) return -ENOTSUP;
256 
257 // Construct the file name
258 //
259  strcpy(path, (const char *)LogDir);
260  strcpy(path+LogDirLen, reqid);
261 
262 // Open the file and return the file descriptor
263 //
264  if ((fd = open((const char *)path, O_RDONLY)) < 0) return -errno;
265 
266 // Get the file size
267 //
268  if (fstat(fd, &buf) != 0) {
269  close(fd);
270  return -errno;
271  }
272 
273  fsz = buf.st_size;
274 
275  return fd;
276 }
#define fstat(a, b)
Definition: XrdPosix.hh:62

References close, fstat, open, and stat.

◆ Scrub()

void XrdXrootdPrepare::Scrub ( )
static

Definition at line 282 of file XrdXrootdPrepare.cc.

283 {
284  DIR *prepD;
285  time_t stale = time(0) - scrubkeep;
286  char *up, path[2048], *fn = path+LogDirLen;
287  struct dirent *dp;
288  struct stat buf;
289 
290 // If logging is not supported, return eof
291 //
292  if (!LogDir) return;
293 
294 // Open the log directory
295 //
296  if (!(prepD = opendir((const char *)LogDir)))
297  {eDest->Emsg("Scrub", errno, "open prep log directory", LogDir);
298  return;
299  }
300  strcpy(path, (const char *)LogDir);
301 
302 // Delete all stale entries
303 //
304  errno = 0;
305  while((dp = readdir(prepD)))
306  {if (!(up = (char *) index((const char *)dp->d_name, '_'))) continue;
307  strcpy(fn, (const char *)dp->d_name);
308  if (stat((const char *)path, &buf)) continue;
309  if (buf.st_mtime <= stale)
310  {TRACE(DEBUG, "Scrub removed stale prep log " <<path);
311  unlink((const char *)path);
312  *(fn+(up-dp->d_name)) = '\0';
313  unlink((const char *)path);
314  errno = 0;
315  }
316  }
317 
318 // All done
319 //
320  if (errno)
321  eDest->Emsg("List", errno, "read prep log directory", LogDir);
322  closedir(prepD);
323 }

References closedir(), DEBUG, eDest, XrdSysError::Emsg(), opendir(), readdir, stat, TRACE, and unlink().

Referenced by DoIt().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setParms() [1/2]

int XrdXrootdPrepare::setParms ( char *  ldir)
static

Definition at line 335 of file XrdXrootdPrepare.cc.

336 {
337  char path[2048];
338  struct stat buf;
339  int plen;
340 
341 // If parm not supplied, ignore call
342 //
343  if (!ldir) return 0;
344 
345 // Make sure we have appropriate permissions for this directory
346 //
347  if (access((const char *)ldir, X_OK | W_OK | R_OK) || stat(ldir, &buf))
348  return -errno;
349  if ((buf.st_mode & S_IFMT) != S_IFDIR) return -ENOTDIR;
350 
351 // Create the path name
352 //
353  if (LogDir) free(LogDir);
354  LogDir = 0;
355  plen = strlen(ldir);
356  strcpy(path, ldir);
357  if (path[plen-1] != '/') path[plen++] = '/';
358  path[plen] = '\0';
359 
360 // Save the path and return
361 //
362  LogDir = strdup(path);
363  LogDirLen = strlen(LogDir);
364  return 0;
365 }
int access(const char *path, int amode)

References access(), and stat.

+ Here is the call graph for this function:

◆ setParms() [2/2]

int XrdXrootdPrepare::setParms ( int  stime,
int  skeep 
)
static

Definition at line 329 of file XrdXrootdPrepare.cc.

330 {if (stime > 0) scrubtime = stime;
331  if (keep > 0) scrubkeep = keep;
332  return 0;
333 }

The documentation for this class was generated from the following files: