XRootD
XrdPrep.cc File Reference
#include <iostream>
#include <vector>
#include <string>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdSys/XrdSysE2T.hh"
#include "XrdSys/XrdSysPlatform.hh"
#include "XrdCl/XrdClFileSystem.hh"
+ Include dependency graph for XrdPrep.cc:

Go to the source code of this file.

Macros

#define EMSG(x)   std::cerr <<"xrdprep: "<<x<<std::endl
 

Functions

int main (int argc, char **argv)
 

Macro Definition Documentation

◆ EMSG

#define EMSG (   x)    std::cerr <<"xrdprep: "<<x<<std::endl

Definition at line 54 of file XrdPrep.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 105 of file XrdPrep.cc.

106 {
107  extern char *optarg;
108  extern int optind, opterr;
109 
110  static const int MaxPathLen = MAXPATHLEN+1;
111  static const PrepareFlags::Flags mPrep = PrepareFlags::Cancel |
112  PrepareFlags::Colocate | PrepareFlags::Stage |
113  PrepareFlags::Fresh | PrepareFlags::WriteMode;
114 
115  std::vector<std::string> fList;
116  FILE *Stream = 0;
117  const char *msgArgs[] = {"execute", "prepare"};
118  char Target[512];
119  char *inFile = 0;
120  PrepareFlags::Flags Opts = PrepareFlags::None;
121  int rc, Prty = 0, Debug = 0;
122  char c, lastOpt = 0;
123  bool isQuery = false, needHandle = false;
124 
125 // See simple help is needed
126 //
127  if (argc <= 1) Usage(0);
128 
129 // Process the options
130 //
131  opterr = 0;
132  if (argc > 1 && '-' == *argv[1])
133  while ((c = getopt(argc,argv,"d:Ef:p:sStw")) && ((unsigned char)c != 0xff))
134  { switch(c)
135  {
136  case 'd': if (!GetNum("debug level", optarg, &Debug, 0, 5)) exit(1);
137  break;
138  case 'E': lastOpt = c;
139  Opts |= PrepareFlags::Evict;
140  break;
141  case 'f': inFile = optarg;
142  break;
143  case 'p': lastOpt = c;
144  if (!GetNum("priority", optarg, &Prty, 0, 3)) exit(1);
145  break;
146  case 's': lastOpt = c; Opts |= PrepareFlags::Stage;
147  break;
148  case 'S': lastOpt = c; Opts |=(PrepareFlags::Stage|PrepareFlags::Colocate);
149  break;
150  case 't': lastOpt = c; Opts |= PrepareFlags::Fresh;
151  break;
152  case 'w': lastOpt = c; Opts |= PrepareFlags::WriteMode;
153  break;
154  default: EMSG("Invalid option '-"<<argv[optind-1]<<"'");
155  Usage(1);
156  }
157  }
158 
159 // The next argument is either a keyword or the hostname
160 //
161  while(optind < argc)
162  { if (!strcmp(argv[optind], "cancel")) Opts = PrepareFlags::Cancel;
163  else if (!strcmp(argv[optind], "query")) isQuery = true;
164  else if (!strcmp(argv[optind], "prepare")){optind++; break;}
165  else break;
166  if (lastOpt)
167  {EMSG('-'<<lastOpt<<"' option is invalid for '"<<argv[optind]<<"'");
168  Usage(1);
169  }
170  needHandle = true;
171  msgArgs[0] = argv[optind++];
172  break;
173  }
174 
175 // Make sure a host has been specified
176 //
177  if (optind >= argc || !isalnum(*argv[optind]))
178  {EMSG("target host name not specified");
179  Usage(1);
180  }
181 
182 // Grab the host name or address
183 //
184  strcpy(Target, "root://");
185  strcat(Target, argv[optind]);
186  optind++;
187 
188 // If we need a handle then make sure we have one
189 //
190  if (needHandle)
191  {if (optind >= argc || *argv[optind] == '/')
192  {EMSG(msgArgs[0]<<" prepare request handle not specified");
193  Usage(1);
194  }
195  }
196 
197 // Pre-process any command line paths at this point
198 //
199  std::string strArg;
200  int totArgLen = 0;
201  for (int i = optind; i < argc; i++)
202  {strArg = argv[i];
203  totArgLen += strArg.size() + 1;
204  fList.push_back(strArg);
205  }
206 
207 // If an infile was specified, make sure we can open it
208 //
209  if (inFile)
210  {if (!(Stream = fopen(inFile, "r")))
211  {EMSG("Unable to open "<<inFile<<"; "<<XrdSysE2T(errno));
212  exit(4);
213  }
214  char *sP, fBuff[MaxPathLen];
215  do {if (!(sP = fgets(fBuff, MaxPathLen, Stream))) break;
216  while(*sP && *sP == ' ') sP++;
217  if (*sP && *sP != '\n')
218  {strArg = sP;
219  if (strArg.size() && strArg.back() == '\n') strArg.pop_back();
220  while(strArg.size() && strArg.back() == ' ') strArg.pop_back();
221  totArgLen += strArg.size() + 1;
222  fList.push_back(strArg);
223  }
224  } while(!feof(Stream) && !ferror(Stream));
225  if ((rc = ferror(Stream)))
226  {EMSG("unable to read "<<inFile<<"; "<<XrdSysE2T(rc));
227  exit(4);
228  }
229  fclose(Stream);
230  }
231 
232 // If this is a prepare request then we need at least one path
233 //
234  if (!needHandle && fList.size() == 0)
235  {EMSG("No files specified for 'prepare'");
236  Usage(1);
237  }
238 
239 // Cleanup options if eviction is wanted
240 //
241  if (Opts & PrepareFlags::Evict) Opts &= ~mPrep;
242 
243 // Establish debugging level
244 //
245  if (Debug > 0)
246  {const char *dbg[] = {"Info","Warning","Error","Debug","Dump"};
247  if (Debug > 5) Debug = 5;
248  XrdOucEnv::Export("XRD_LOGLEVEL", dbg[Debug-1]);
249  }
250 
251 // Get an instance of the file system
252 //
253  FileSystem Admin(Target);
254 
255 // Issue the relevant operation
256 //
257  Buffer *response = 0;
258  XRootDStatus st;
259  if (!isQuery) st = Admin.Prepare(fList, Opts, uint8_t(Prty), response);
260  else {Buffer qryArgs(totArgLen);
261  char *bP = qryArgs.GetBuffer();
262  for (int i = 0; i < (int)fList.size(); i++)
263  {strcpy(bP, fList[i].c_str());
264  bP += fList[i].size();
265  *bP++ = '\n';
266  }
267  *(bP-1) = 0;
268  st = Admin.Query(QueryCode::Prepare, qryArgs, response);
269  }
270 
271 // Check if all went well
272 //
273  if (!st.IsOK())
274  {std::string estr = st.ToStr();
275  const char *einfo, *etxt = estr.c_str();
276  if (!(einfo = rindex(etxt, ']'))) einfo = etxt;
277  else {einfo++;
278  while(*einfo && *einfo == ' ') einfo++;
279  }
280  EMSG("Unable to "<<msgArgs[0]<<' '<<msgArgs[1]<<"; "
281  <<(char)tolower(*einfo)<<einfo+1);
282  exit(8);
283  }
284 
285 // Display the result
286 //
287  std::string rstr = response->ToString();
288  const char *xx = rstr.c_str();
289  if (*xx) std::cout << xx << std::endl;
290  delete response;
291 
292 // All done
293 //
294  exit(0);
295 }
void Usage(const char *msg)
Definition: XrdAccTest.cc:105
int optind
int fclose(FILE *stream)
#define fopen(a, b)
Definition: XrdPosix.hh:49
#define EMSG(x)
Definition: XrdPrep.cc:54
const char * XrdSysE2T(int errcode)
Definition: XrdSysE2T.cc:104
Binary blob representation.
Definition: XrdClBuffer.hh:34
std::string ToString() const
Convert the buffer to a string.
Definition: XrdClBuffer.hh:215
Send file/filesystem queries to an XRootD cluster.
std::string ToStr() const
Convert to string.
static int Export(const char *Var, const char *Val)
Definition: XrdOucEnv.cc:188
PrepareImpl< false > Prepare
XrdCmsAdmin Admin
int Opts
Definition: XrdMpxStats.cc:58
bool IsOK() const
We're fine.
Definition: XrdClStatus.hh:124

References XrdCms::Admin, XrdCl::PrepareFlags::Cancel, XrdCl::PrepareFlags::Colocate, Macaroons::Debug, EMSG, XrdCl::PrepareFlags::Evict, XrdOucEnv::Export(), fclose(), fopen, XrdCl::PrepareFlags::Fresh, XrdCl::Buffer::GetBuffer(), XrdCl::Status::IsOK(), XrdCl::PrepareFlags::None, optind, XrdMpx::Opts, XrdCl::QueryCode::Prepare, XrdCl::PrepareFlags::Stage, XrdCl::XRootDStatus::ToStr(), XrdCl::Buffer::ToString(), Usage(), XrdCl::PrepareFlags::WriteMode, and XrdSysE2T().

+ Here is the call graph for this function: