00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 const char *XrdClientAdmin_cCVSID = "$Id: XrdClientAdmin_c.cc 30949 2009-11-02 16:37:58Z ganis $";
00011
00012
00013
00014 #include "XrdClient/XrdClientAdmin.hh"
00015 #include "XrdClient/XrdClientDebug.hh"
00016 #include "XrdClient/XrdClientVector.hh"
00017 #include "XrdOuc/XrdOucString.hh"
00018
00019 #ifndef WIN32
00020 #include <rpc/types.h>
00021 #endif
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024
00025
00026 char *sharedbuf;
00027
00028 void SharedBufRealloc(long size) {
00029 sharedbuf = (char *)realloc(sharedbuf, size);
00030 memset(sharedbuf, 0, size);
00031 }
00032 void SharedBufFree() {
00033 if (sharedbuf) free(sharedbuf);
00034 sharedbuf = 0;
00035 }
00036
00037
00038
00039 vecString *Tokenize(const char *str, char sep)
00040 {
00041 XrdOucString s(str);
00042
00043 vecString *res = new vecString;
00044
00045 XrdOucString sl;
00046 int from = 0;
00047 while ((from = s.tokenize(sl, from, sep)) != STR_NPOS) {
00048 if (sl.length() > 0)
00049 res->Push_back(sl);
00050 }
00051 return res;
00052 }
00053
00054
00055 void BuildBoolAnswer(vecBool &vb) {
00056 SharedBufRealloc(vb.GetSize());
00057
00058 for (int i = 0; i < vb.GetSize(); i++) {
00059 sharedbuf[i] = '0';
00060 if (vb[i]) sharedbuf[i] = '1';
00061 }
00062 sharedbuf[vb.GetSize()] = '\0';
00063
00064 }
00065
00066
00067
00068
00069
00070 XrdClientAdmin *adminst = NULL;
00071
00072 extern "C" {
00073
00074 bool XrdInitialize(const char *url, const char *EnvValues) {
00075
00076
00077
00078
00079
00080
00081
00082 vecString *env = Tokenize(EnvValues, '\n');
00083
00084 for (int it = 0; it < env->GetSize(); it++) {
00085 char tok1[256], tok2[256];
00086 long v;
00087
00088 if (sscanf((*env)[it].c_str(), "%256s %ld", tok1, &v) == 2) {
00089
00090 EnvPutInt(tok1, v);
00091
00092 }
00093 else {
00094 sscanf((*env)[it].c_str(), "%256s %256s", tok1, tok2);
00095 EnvPutString(tok1, tok2);
00096
00097 }
00098 }
00099
00100 delete env;
00101
00102 DebugSetLevel(EnvGetLong(NAME_DEBUG));
00103
00104 if (!adminst)
00105 adminst = new XrdClientAdmin(url);
00106
00107 bool conn = false;
00108
00109 if (adminst) conn = adminst->Connect();
00110
00111 if (!conn) {
00112 delete adminst;
00113 adminst = NULL;
00114 }
00115
00116
00117 sharedbuf = 0;
00118 return (adminst != NULL);
00119 }
00120
00121 bool XrdTerminate() {
00122 delete adminst;
00123 adminst = NULL;
00124
00125 SharedBufFree();
00126
00127 return TRUE;
00128 }
00129
00130
00131
00132
00133
00134 char *XrdSysStatX(const char *paths_list) {
00135
00136 if (!adminst) return NULL;
00137
00138 vecString *vs = Tokenize(paths_list, '\n');
00139 SharedBufRealloc(vs->GetSize()+1);
00140
00141 adminst->SysStatX(paths_list, (kXR_char*)sharedbuf);
00142
00143
00144 for (int i = 0; i < vs->GetSize(); i++)
00145 sharedbuf[i] += '0';
00146
00147 delete vs;
00148 return(sharedbuf);
00149 }
00150
00151
00152 char *XrdExistFiles(const char *filepaths) {
00153 if (!adminst) return NULL;
00154 bool res = FALSE;
00155 vecBool vb;
00156
00157 vecString *vs = Tokenize(filepaths, '\n');
00158
00159 if ((res = adminst->ExistFiles(*vs, vb))) {
00160 BuildBoolAnswer(vb);
00161 }
00162 else SharedBufRealloc(16);
00163
00164 delete vs;
00165 return(sharedbuf);
00166
00167 }
00168
00169 char *XrdExistDirs(const char *filepaths) {
00170 if (!adminst) return NULL;
00171 bool res = FALSE;
00172 vecBool vb;
00173
00174 vecString *vs = Tokenize(filepaths, '\n');
00175
00176 if ((res = adminst->ExistDirs(*vs, vb))) {
00177 BuildBoolAnswer(vb);
00178 }
00179 else SharedBufRealloc(16);
00180
00181 delete vs;
00182 return(sharedbuf);
00183
00184 }
00185
00186 char *XrdIsFileOnline(const char *filepaths) {
00187 if (!adminst) return NULL;
00188 bool res = FALSE;
00189 vecBool vb;
00190
00191 vecString *vs = Tokenize(filepaths, '\n');
00192
00193 if ((res = adminst->IsFileOnline(*vs, vb))) {
00194 BuildBoolAnswer(vb);
00195 }
00196 else SharedBufRealloc(16);
00197
00198 delete vs;
00199 return(sharedbuf);
00200
00201 }
00202
00203
00204
00205
00206 bool XrdMv(const char *fileSrc, const char *fileDest) {
00207 if (!adminst) return adminst;
00208
00209 return(adminst->Mv(fileSrc, fileDest));
00210 }
00211
00212
00213 bool XrdMkdir(const char *dir, int user, int group, int other) {
00214 if (!adminst) return adminst;
00215
00216 return(adminst->Mkdir(dir, user, group, other));
00217 }
00218
00219
00220 bool XrdChmod(const char *file, int user, int group, int other) {
00221 if (!adminst) return adminst;
00222
00223 return(adminst->Chmod(file, user, group, other));
00224 }
00225
00226
00227 bool XrdRm(const char *file) {
00228 if (!adminst) return adminst;
00229
00230 return(adminst->Rm(file));
00231 }
00232
00233
00234 bool XrdRmdir(const char *path) {
00235 if (!adminst) return adminst;
00236
00237 return(adminst->Rmdir(path));
00238 }
00239
00240
00241 bool XrdPrepare(const char *filepaths, unsigned char opts, unsigned char prty) {
00242 if (!adminst) return adminst;
00243
00244 bool res;
00245
00246 vecString *vs = Tokenize(filepaths, '\n');
00247
00248 res = adminst->Prepare(*vs, opts, prty);
00249
00250 delete vs;
00251
00252 return(res);
00253 }
00254
00255 char *XrdDirList(const char *dir) {
00256 vecString entries;
00257 XrdOucString lst;
00258
00259 if (!adminst) return 0;
00260
00261 if (!adminst->DirList(dir, entries)) return 0;
00262
00263 joinStrings(lst, entries);
00264
00265 SharedBufRealloc(lst.length()+1);
00266 strcpy(sharedbuf, lst.c_str());
00267
00268 return sharedbuf;
00269 }
00270
00271
00272 char *XrdGetChecksum(const char *path) {
00273 if (!adminst) return 0;
00274
00275 char *chksum = 0;
00276 long chksumlen;
00277
00278
00279
00280 if ( (chksumlen = adminst->GetChecksum((kXR_char *)path, (kXR_char **)&chksum)) ) {
00281
00282
00283
00284
00285 SharedBufRealloc(chksumlen+1);
00286 strncpy(sharedbuf, chksum, chksumlen);
00287 sharedbuf[chksumlen] = 0;
00288
00289 free(chksum);
00290
00291 return sharedbuf;
00292 }
00293 else return 0;
00294
00295 }
00296
00297 char *XrdGetCurrentHost() {
00298 if (!adminst) return 0;
00299
00300 int len = adminst->GetCurrentUrl().Host.length();
00301 SharedBufRealloc(len+1);
00302 strncpy(sharedbuf, adminst->GetCurrentUrl().Host.c_str(), len);
00303 sharedbuf[len] = 0;
00304
00305 return sharedbuf;
00306 }
00307
00308 bool XrdStat(const char *fname, long *id, long long *size, long *flags, long *modtime) {
00309 if (!adminst) return false;
00310
00311 return (adminst->Stat(fname, *id, *size, *flags, *modtime));
00312
00313 }
00314
00315 }
00316