xrootd
|
00001 #ifndef __XRDCnsLogRec_H_ 00002 #define __XRDCnsLogRec_H_ 00003 /******************************************************************************/ 00004 /* */ 00005 /* X r d C n s L o g R e c . h h */ 00006 /* */ 00007 /* (c) 2009 by the Board of Trustees of the Leland Stanford, Jr., University */ 00008 /* All Rights Reserved */ 00009 /* Produced by Andrew Hanushevsky for Stanford University under contract */ 00010 /* DE-AC02-76-SFO0515 with the Department of Energy */ 00011 /******************************************************************************/ 00012 00013 // $Id$ 00014 00015 #include <stddef.h> 00016 #include <stdio.h> 00017 #include <string.h> 00018 #include <time.h> 00019 #include <sys/param.h> 00020 #include <sys/types.h> 00021 00022 #include "XrdSys/XrdSysPlatform.hh" 00023 #include "XrdSys/XrdSysPthread.hh" 00024 00025 class XrdCnsLogRec 00026 { 00027 public: 00028 00029 static const int maxClients = 4; 00030 00031 struct Ctl 00032 {short dataLen; // Length of data in Arg (eof when 0) 00033 short lfn1Len; // strlen(lfn1) 00034 short lfn2Len; // strlen(lfn2) 00035 short Mode; // File mode (create and mkdir) 00036 char Done[maxClients]; // 1->Record processed 00037 int Rsvd; // Reserved field 00038 long long Size; // Valid when Closew 00039 }; 00040 00041 struct Arg 00042 {char Type; // Event code 00043 char Mode[3]; // Mode (create, inv, mkdir) 00044 char SorT[12]; // Size (closew, inv) | TOD (eol) 00045 char Mount; // Mount Index 00046 char Space; // Space Index 00047 char Rsvd[6]; // Reserved (blank filled) 00048 char lfn[MAXPATHLEN*2+3]; // lfn1 [lfn2] \n 00049 }; 00050 00051 struct LogRec 00052 {struct Ctl Hdr; 00053 struct Arg Data; 00054 }; 00055 00056 static const int OffDone = offsetof(LogRec, Hdr.Done); 00057 static const int FixDLen = offsetof(Arg, lfn); 00058 static const int MinSize = sizeof(Ctl); // Header 00059 static const int MaxSize = sizeof(Arg); // Data 00060 static const long tBase = 1248126834L; 00061 00062 static const char lrClosew = 't'; 00063 static const char lrCreate = 'c'; 00064 static const char lrEOL = 'E'; // Internal 00065 static const char lrInvD = 'I'; // Internal dir inventory 00066 static const char lrInvF = 'i'; // Internal file inventory 00067 static const char lrMkdir = 'd'; 00068 static const char lrMount = 'M'; // Internal Mount inventory 00069 static const char lrMv = 'm'; 00070 static const char lrRm = 'r'; 00071 static const char lrRmdir = 'D'; 00072 static const char lrSpace = 'S'; // Internal Space inventory 00073 static const char lrTOD = 'T'; // Internal TOD 00074 00075 static const char *IArg; 00076 static const char *iArg; 00077 00078 static XrdCnsLogRec *Alloc(); 00079 00080 inline const char *Data() {return (const char *)&Rec.Data;} 00081 00082 inline int DLen() {return Rec.Hdr.dataLen;} 00083 00084 inline int Done(int iPos) {return Rec.Hdr.Done[iPos];} 00085 00086 static XrdCnsLogRec *Get(char &lrType); 00087 00088 inline const char *Lfn1() {return Rec.Data.lfn;} 00089 00090 inline const char *Lfn1(int &Len) {Len=Rec.Hdr.lfn1Len; return Rec.Data.lfn;} 00091 00092 inline const char *Lfn2() {return Rec.Data.lfn+Rec.Hdr.lfn1Len+1;} 00093 00094 inline mode_t Mode() {return static_cast<mode_t>(Rec.Hdr.Mode);} 00095 00096 void Queue(); 00097 00098 void Recycle(); 00099 00100 inline char *Record() {return (char *)&Rec;} 00101 00102 // setLfn1() must be called prior to calling setLfn2() or setData() 00103 // 00104 int setData(const char *dP1, const char *dP2=0); 00105 00106 void setDone(int iPos, char Val=1) {Rec.Hdr.Done[iPos] = Val;} 00107 00108 int setLen() {if (Rec.Hdr.lfn1Len) 00109 {Rec.Hdr.dataLen = FixDLen+Rec.Hdr.lfn1Len+1; 00110 if (Rec.Hdr.lfn2Len) 00111 Rec.Hdr.dataLen += Rec.Hdr.lfn2Len+1; 00112 } else Rec.Hdr.dataLen = 0; 00113 return static_cast<int>(Rec.Hdr.dataLen); 00114 } 00115 00116 inline int setLfn1(const char *lfn) 00117 {int n; 00118 n = strlcpy(Rec.Data.lfn, lfn, MAXPATHLEN+1); 00119 if (n > MAXPATHLEN) return 0; 00120 Rec.Hdr.lfn1Len = n; 00121 return n; 00122 } 00123 00124 inline int setLfn2(const char *lfn) 00125 {int n; 00126 setSize(static_cast<long long>(Rec.Hdr.lfn1Len)); 00127 n = strlcpy(Rec.Data.lfn + Rec.Hdr.lfn1Len + 1, 00128 lfn, MAXPATHLEN+1); 00129 if (n > MAXPATHLEN) return 0; 00130 Rec.Hdr.lfn2Len = n; 00131 return n; 00132 } 00133 00134 inline void setMode(mode_t Mode) {char Save = *Rec.Data.SorT; 00135 Rec.Hdr.Mode = Mode; 00136 sprintf(Rec.Data.Mode, "%03o", 00137 511 & static_cast<int>(Mode)); 00138 *Rec.Data.SorT = Save; 00139 } 00140 00141 inline void setMount(char mCode) {Rec.Data.Mount = mCode;} 00142 00143 inline void setSize(long long Size) {char Save = Rec.Data.Mount; 00144 Rec.Hdr.Size = Size; 00145 sprintf(Rec.Data.SorT, "%12lld", 00146 (Size > 0 ? Size & 0x7fffffffffLL 00147 : Size)); 00148 Rec.Data.Mount = Save; 00149 } 00150 00151 inline void setSpace(char sCode) {Rec.Data.Space = sCode;} 00152 00153 inline void setTime(long TOD=time(0)){char Save = Rec.Data.Mount; 00154 sprintf(Rec.Data.SorT, "%12ld", 00155 TOD-tBase); 00156 Rec.Data.Mount = Save; 00157 } 00158 00159 inline void setType(const char evt) {Rec.Data.Type = evt;} 00160 00161 int setType(const char *evt); 00162 00163 inline long long Size() {return Rec.Hdr.Size;} 00164 00165 inline char Space(){return Rec.Data.Space;} 00166 00167 inline int L1sz() {return Rec.Hdr.lfn1Len;} 00168 00169 inline int L2sz() {return Rec.Hdr.lfn2Len;} 00170 00171 char Type() {return Rec.Data.Type;} 00172 00173 XrdCnsLogRec(const char rType=0) : Next(0) 00174 {memset(&Rec, 0, sizeof(Rec.Hdr)); 00175 memset(&Rec.Data,' ', FixDLen); 00176 Rec.Data.Type = rType; 00177 if (rType == lrEOL || rType == lrTOD) 00178 {setTime(); 00179 Rec.Hdr.dataLen=FixDLen; 00180 if (rType == lrTOD) 00181 {Rec.Data.lfn[0] = ' '; 00182 Rec.Data.lfn[1] = '\0'; 00183 Rec.Hdr.lfn1Len = 1; 00184 } 00185 } 00186 } 00187 ~XrdCnsLogRec() {} 00188 00189 private: 00190 00191 static XrdSysSemaphore qSem; 00192 static XrdSysMutex qMutex; 00193 static XrdSysMutex fMutex; 00194 static XrdCnsLogRec *freeRec; 00195 static XrdCnsLogRec *frstRec; 00196 static XrdCnsLogRec *lastRec; 00197 static int Running; 00198 00199 XrdCnsLogRec *Next; 00200 LogRec Rec; 00201 }; 00202 #endif