00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "XrdMon/XrdMonException.hh"
00014 #include "XrdMon/XrdMonErrors.hh"
00015 #include "XrdMon/XrdMonUtils.hh"
00016 #include "XrdSys/XrdSysHeaders.hh"
00017
00018 #include <errno.h>
00019 #include <string.h>
00020 #include <stdio.h>
00021 #include <sys/stat.h>
00022 #include <sys/time.h>
00023 #include <sys/types.h>
00024 #include <unistd.h>
00025
00026 using std::cout;
00027 using std::endl;
00028
00029
00030 string
00031 generateTimestamp()
00032 {
00033 struct timeval tv;
00034 gettimeofday(&tv, 0);
00035 const time_t sec = tv.tv_sec;
00036 struct tm *t = localtime(&sec);
00037
00038 char buf[24];
00039 sprintf(buf, "%02d%02d%02d_%02d:%02d:%02d.%03d",
00040 t->tm_year+1900,
00041 t->tm_mon+1,
00042 t->tm_mday,
00043 t->tm_hour,
00044 t->tm_min,
00045 t->tm_sec,
00046 (int)tv.tv_usec/1000);
00047 return string(buf);
00048 }
00049
00050 string
00051 timestamp2string(kXR_int32 timestamp, bool gmt)
00052 {
00053 char s[24];
00054 timestamp2string(timestamp, s, gmt);
00055 return string(s);
00056 }
00057
00058 void
00059 timestamp2string(kXR_int32 timestamp, char s[24], bool gmt)
00060 {
00061 if ( 0 == timestamp ) {
00062 strcpy(s, "0000-00-00 00:00:00");
00063 return;
00064 }
00065
00066 time_t tt = timestamp;
00067 struct tm *t = (gmt ? gmtime(&tt) : localtime(&tt));
00068
00069
00070 sprintf(s, "%4d-%02d-%02d %02d:%02d:%02d",
00071 t->tm_year+1900,
00072 t->tm_mon+1,
00073 t->tm_mday,
00074 t->tm_hour,
00075 t->tm_min,
00076 t->tm_sec);
00077 }
00078
00079
00080 pair<string, string>
00081 breakHostPort(const string& hp)
00082 {
00083 int colonPos = hp.rfind(':', hp.size());
00084 if ( colonPos == -1 ) {
00085 string se("No : in "); se += hp;
00086 throw XrdMonException(ERR_INVALIDADDR, se);
00087 }
00088 string host(hp, 0, colonPos);
00089 string port(hp, colonPos+1, hp.size()-colonPos-1);
00090 return pair<string, string>(host, port);
00091 }
00092
00093 void
00094 mkdirIfNecessary(const char* dir)
00095 {
00096 if ( 0 == access(dir, F_OK) ) {
00097 return;
00098 }
00099
00100
00101
00102 string org(dir);
00103 int size = org.size();
00104 int pos = 0;
00105 vector<string> dirs2create;
00106 if ( org[size-1] == '/' ) {
00107 org = string(org, 0, size-1);
00108 }
00109 dirs2create.push_back(org);
00110 do {
00111 pos = org.rfind('/', size-1);
00112 if ( pos == -1 ) {
00113 break;
00114 }
00115 org = string(dir, pos);
00116 if ( 0 == access(org.c_str(), F_OK) ) {
00117 break;
00118 }
00119 dirs2create.push_back(org);
00120 } while ( pos > 0 );
00121
00122 size = dirs2create.size();
00123 for ( int i=size-1 ; i>=0 ; --i ) {
00124 const char*d = dirs2create[i].c_str();
00125 if ( 0 != mkdir(d, 0x3FD) ) {
00126 char buf[256];
00127 sprintf(buf, "Failed to mkdir %s. Error: '%s'",
00128 dir, strerror (errno));
00129 throw XrdMonException(ERR_NODIR, buf);
00130 }
00131 cout << "mkdir " << d << " OK" << endl;
00132 }
00133 }
00134