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:43

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 && strncmp(dp->d_name, pargs.reqid, pargs.reqlen))
120  continue;
121  if (pargs.usrlen)
122  if (!up || strcmp((const char *)up+1,(const char *)pargs.user))
123  continue;
124  strcpy(path, (const char *)LogDir);
125  strcpy(path+LogDirLen, (const char *)dp->d_name);
126  if (stat((const char *)path, &buf)) continue;
127  *up = ' ';
128  if ((up = (char *) index((const char *)(up+1), (int)'_'))) *up = ' ';
129  else continue;
130  if ((up = (char *) index((const char *)(up+1), (int)'_'))) *up = ' ';
131  else continue;
132  return snprintf(resp, resplen-1, "%s %lld",
133  dp->d_name, (long long) buf.st_mtime);
134  }
135 
136 // Completed
137 //
138  if ((rc = errno))
139  eDest->Emsg("List", errno, "read prep log directory", LogDir);
140  closedir(pargs.dirP);
141  pargs.dirP = 0;
142  return (rc ? -1 : 0);
143 }
static XrdSysError eDest(0,"crypto_")
int stat(const char *path, struct stat *buf)
struct dirent * readdir(DIR *dirp)
int closedir(DIR *dirp)
DIR * opendir(const char *path)
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 149 of file XrdXrootdPrepare.cc.

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

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

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

References close, fstat(), open(), and stat().

+ Here is the call graph for this function:

◆ Scrub()

void XrdXrootdPrepare::Scrub ( )
static

Definition at line 281 of file XrdXrootdPrepare.cc.

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

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 334 of file XrdXrootdPrepare.cc.

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

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

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