XRootD
XrdCpConfig.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X r d C p C o n f i g . c c */
4 /* */
5 /* (c) 2011 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* All Rights Reserved */
7 /* Produced by Andrew Hanushevsky for Stanford University under contract */
8 /* DE-AC02-76-SFO0515 with the Department of Energy */
9 /* */
10 /* This file is part of the XRootD software suite. */
11 /* */
12 /* XRootD is free software: you can redistribute it and/or modify it under */
13 /* the terms of the GNU Lesser General Public License as published by the */
14 /* Free Software Foundation, either version 3 of the License, or (at your */
15 /* option) any later version. */
16 /* */
17 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20 /* License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25 /* */
26 /* The copyright holder's institutional names and contributor's names may not */
27 /* be used to endorse or promote products derived from this software without */
28 /* specific prior written permission of the institution or contributor. */
29 /******************************************************************************/
30 
31 #include <fcntl.h>
32 #include <getopt.h>
33 #include <cstdio>
34 #include <cstdlib>
35 #include <cstring>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 
39 #include "XrdVersion.hh"
40 #include "XrdApps/XrdCpConfig.hh"
41 #include "XrdApps/XrdCpFile.hh"
42 #include "XrdCks/XrdCksCalc.hh"
43 #include "XrdCks/XrdCksManager.hh"
44 #include "XrdOuc/XrdOucStream.hh"
45 #include "XrdSys/XrdSysE2T.hh"
46 #include "XrdSys/XrdSysError.hh"
47 #include "XrdSys/XrdSysHeaders.hh"
48 #include "XrdSys/XrdSysLogger.hh"
49 
50 
51 /******************************************************************************/
52 /* D e f i n e M a c r o s */
53 /******************************************************************************/
54 
55 #define EMSG(x) std::cerr <<PName <<": " <<x <<std::endl
56 
57 #define FMSG(x,y) {EMSG(x);exit(y);}
58 
59 #define UMSG(x) {EMSG(x);Usage(22);}
60 
61 #define ZMSG(x) {EMSG(x);return 0;}
62 
63 // Bypass stupid issue with stupid solaris for missdefining 'struct opt'.
64 //
65 #ifdef __solaris__
66 #define OPT_TYPE (char *)
67 #else
68 #define OPT_TYPE
69 #endif
70 
71 /******************************************************************************/
72 /* S t a t i c M e m b e r s */
73 /******************************************************************************/
74 
76 {
78 static XrdSysError eDest(&Logger, "");
79 };
80 
82 
83 const char *XrdCpConfig::opLetters = ":C:d:D:EfFhHI:NpPrRsS:t:T:vVX:y:z:ZA";
84 
85 struct option XrdCpConfig::opVec[] = // For getopt_long()
86  {
87  {OPT_TYPE "cksum", 1, 0, XrdCpConfig::OpCksum},
88  {OPT_TYPE "coerce", 0, 0, XrdCpConfig::OpCoerce},
89  {OPT_TYPE "continue", 0, 0, XrdCpConfig::OpContinue},
90  {OPT_TYPE "debug", 1, 0, XrdCpConfig::OpDebug},
91  {OPT_TYPE "dynamic-src", 0, 0, XrdCpConfig::OpDynaSrc},
92  {OPT_TYPE "force", 0, 0, XrdCpConfig::OpForce},
93  {OPT_TYPE "help", 0, 0, XrdCpConfig::OpHelp},
94  {OPT_TYPE "infiles", 1, 0, XrdCpConfig::OpIfile},
95  {OPT_TYPE "license", 0, 0, XrdCpConfig::OpLicense},
96  {OPT_TYPE "nopbar", 0, 0, XrdCpConfig::OpNoPbar},
97  {OPT_TYPE "notlsok", 0, 0, XrdCpConfig::OpNoTlsOK},
98  {OPT_TYPE "parallel", 1, 0, XrdCpConfig::OpParallel},
99  {OPT_TYPE "path", 0, 0, XrdCpConfig::OpPath},
100  {OPT_TYPE "posc", 0, 0, XrdCpConfig::OpPosc},
101  {OPT_TYPE "proxy", 1, 0, XrdCpConfig::OpProxy},
102  {OPT_TYPE "recursive", 0, 0, XrdCpConfig::OpRecurse},
103  {OPT_TYPE "retry", 1, 0, XrdCpConfig::OpRetry},
104  {OPT_TYPE "retry-policy", 1, 0, XrdCpConfig::OpRetryPolicy},
105  {OPT_TYPE "rm-bad-cksum", 0, 0, XrdCpConfig::OpRmOnBadCksum},
106  {OPT_TYPE "server", 0, 0, XrdCpConfig::OpServer},
107  {OPT_TYPE "silent", 0, 0, XrdCpConfig::OpSilent},
108  {OPT_TYPE "sources", 1, 0, XrdCpConfig::OpSources},
109  {OPT_TYPE "streams", 1, 0, XrdCpConfig::OpStreams},
110  {OPT_TYPE "tlsmetalink", 0, 0, XrdCpConfig::OpTlsMLF},
111  {OPT_TYPE "tlsnodata", 0, 0, XrdCpConfig::OpTlsNoData},
112  {OPT_TYPE "tpc", 1, 0, XrdCpConfig::OpTpc},
113  {OPT_TYPE "verbose", 0, 0, XrdCpConfig::OpVerbose},
114  {OPT_TYPE "version", 0, 0, XrdCpConfig::OpVersion},
115  {OPT_TYPE "xattr", 0, 0, XrdCpConfig::OpXAttr},
116  {OPT_TYPE "xrate", 1, 0, XrdCpConfig::OpXrate},
117  {OPT_TYPE "xrate-threshold",1, 0, XrdCpConfig::OpXrateThreshold},
118  {OPT_TYPE "zip", 1, 0, XrdCpConfig::OpZip},
119  {OPT_TYPE "zip-append", 0, 0, XrdCpConfig::OpZipAppend},
120  {OPT_TYPE "zip-mtln-cksum", 0, 0, XrdCpConfig::OpZipMtlnCksum},
121  {0, 0, 0, 0}
122  };
123 
124 /******************************************************************************/
125 /* C o n s t r u c t o r */
126 /******************************************************************************/
127 
128 XrdCpConfig::XrdCpConfig(const char *pgm)
129 {
130  if ((PName = rindex(pgm, '/'))) PName++;
131  else PName = pgm;
132  XrdCpFile::SetMsgPfx(PName);
133  intDefs = 0;
134  intDend = 0;
135  strDefs = 0;
136  strDend = 0;
137  dstOpq = 0;
138  srcOpq = 0;
139  pHost = 0;
140  pPort = 0;
141  xRate = 0;
142  xRateThreshold = 0;
143  Parallel = 1;
144  OpSpec = 0;
145  Dlvl = 0;
146  nSrcs = 1;
147  nStrm = 0;
148  Retry =-1;
149  RetryPolicy = "force";
150  Verbose = 0;
151  numFiles = 0;
152  totBytes = 0;
153  CksLen = 0;
154  CksMan = 0;
155  CksObj = 0;
156  CksVal = 0;
157  srcFile = 0;
158  dstFile = 0;
159  inFile = 0;
160  parmVal = 0;
161  parmCnt = 0;
162  zipFile = 0;
163 }
164 
165 /******************************************************************************/
166 /* D e s t r u c t o r */
167 /******************************************************************************/
168 
170 {
171  XrdCpFile *pNow;
172  defVar *dP;
173 
174  if (inFile) free(inFile);
175  if (pHost) free(pHost);
176  if (parmVal) free(parmVal);
177  if (CksObj) delete CksObj;
178  if (CksMan) delete CksMan;
179  if (zipFile) free(zipFile);
180  if (dstFile) delete dstFile;
181 
182  while((pNow = pFile)) {pFile = pFile->Next; delete pNow;}
183 
184  while((dP = intDefs)) {intDefs = dP->Next; delete dP;}
185  while((dP = strDefs)) {strDefs = dP->Next; delete dP;}
186 
187 }
188 
189 /******************************************************************************/
190 /* C o n f i g */
191 /******************************************************************************/
192 
193 void XrdCpConfig::Config(int aCnt, char **aVec, int opts)
194 {
195  extern char *optarg;
196  extern int optind, opterr;
197  static int pgmSet = 0;
198  char Buff[128], *Path, opC;
199  XrdCpFile pBase;
200  int i, rc;
201 
202 // Allocate a parameter vector
203 //
204  if (parmVal) free(parmVal);
205  parmVal = (char **)malloc(aCnt*sizeof(char *));
206 
207 // Preset handling options
208 //
209  Argv = aVec;
210  Argc = aCnt;
211  Opts = opts;
212  opterr = 0;
213  optind = 1;
214  opC = 0;
215 
216 // Set name of executable for error messages
217 //
218  if (!pgmSet)
219  {char *Slash = rindex(aVec[0], '/');
220  pgmSet = 1;
221  Pgm = (Slash ? Slash+1 : aVec[0]);
222  Log->SetPrefix(Pgm);
223  }
224 
225 // Process legacy options first before atempting normal options
226 //
227 do{while(optind < Argc && Legacy(optind)) {}
228  if ((opC = getopt_long(Argc, Argv, opLetters, opVec, &i)) != (char)-1)
229  switch(opC)
230  {case OpCksum: defCks(optarg);
231  break;
232  case OpCoerce: OpSpec |= DoCoerce;
233  break;
234  case OpDebug: OpSpec |= DoDebug;
235  if (!a2i(optarg, &Dlvl, 0, 3)) Usage(22);
236  break;
237  case OpDynaSrc: OpSpec |= DoDynaSrc;
238  break;
239  case OpForce: OpSpec |= DoForce;
240  break;
241  case OpZip: OpSpec |= DoZip;
242  if (zipFile) free(zipFile);
243  zipFile = strdup(optarg);
244  break;
245  case OpHelp: Usage(0);
246  break;
247  case OpIfile: if (inFile) free(inFile);
248  inFile = strdup(optarg);
249  OpSpec |= DoIfile;
250  break;
251  case OpLicense: License();
252  break;
253  case OpNoPbar: OpSpec |= DoNoPbar;
254  break;
255  case OpNoTlsOK: OpSpec |= DoNoTlsOK;
256  break;
257  case OpPath: OpSpec |= DoPath;
258  break;
259  case OpPosc: OpSpec |= DoPosc;
260  break;
261  case OpProxy: OpSpec |= DoProxy;
262  defPxy(optarg);
263  break;
264  case OpRecurse: OpSpec |= DoRecurse;
265  break;
266  case OpRecursv: OpSpec |= DoRecurse;
267  break;
268  case OpRetry: OpSpec |= DoRetry;
269  if (!a2i(optarg, &Retry, 0, -1)) Usage(22);
270  break;
272  RetryPolicy = optarg;
273  if( RetryPolicy != "force" && RetryPolicy != "continue" ) Usage(22);
274  break;
275  case OpZipAppend: OpSpec |= DoZipAppend;
276  break;
278  break;
279  case OpSilent: OpSpec |= DoSilent|DoNoPbar;
280  break;
281  case OpSources: OpSpec |= DoSources;
282  if (!a2i(optarg, &nSrcs, 1, 32)) Usage(22);
283  break;
284  case OpStreams: OpSpec |= DoStreams;
285  if (!a2i(optarg, &nStrm, 1, 15)) Usage(22);
286  break;
287  case OpTlsNoData: OpSpec |= DoTlsNoData;
288  break;
289  case OpTlsMLF: OpSpec |= DoTlsMLF;
290  break;
291  case OpTpc: OpSpec |= DoTpc;
292  if (!strcmp("delegate", optarg))
293  {OpSpec|= DoTpcDlgt;
294  if (optind >= Argc)
295  {UMSG("Missing tpc qualifier after "
296  "'delegate'");
297  }
298  optarg = Argv[optind++];
299  }
300  if (!strcmp("only", optarg)) OpSpec|= DoTpcOnly;
301  else if (strcmp("first", optarg))
302  {optind--;
303  UMSG("Invalid option, '" <<OpName()
304  <<' ' <<optarg <<"' ");
305  }
306  break;
307  case OpVerbose: OpSpec |= DoVerbose;
308  Verbose = 1;
309  break;
310  case OpVersion: std::cerr <<XrdVERSION <<std::endl; exit(0);
311  break;
312  case OpXrate: OpSpec |= DoXrate;
313  if (!a2z(optarg, &xRate, 10*1024LL, -1)) Usage(22);
314  break;
316  if (!a2z(optarg, &xRateThreshold, 10*1024LL, -1)) Usage(22);
317  break;
318  case OpParallel: OpSpec |= DoParallel;
319  if (!a2i(optarg, &Parallel, 1, 128)) Usage(22);
320  break;
321  case OpXAttr : OpSpec |= DoXAttr;
322  break;
324  break;
326  break;
327  case OpContinue : OpSpec |= DoContinue;
328  break;
329  case ':': UMSG("'" <<OpName() <<"' argument missing.");
330  break;
331  case '?': if (!Legacy(optind-1))
332  UMSG("Invalid option, '" <<OpName() <<"'.");
333  break;
334  default: UMSG("Internal error processing '" <<OpName() <<"'.");
335  break;
336  }
337  } while(opC != (char)-1 && optind < Argc);
338 
339 // Make sure we have the right number of files
340 //
341  if (inFile) {if (!parmCnt ) UMSG("Destination not specified.");}
342  else { if (!parmCnt ) UMSG("No files specified.");
343  if ( parmCnt == 1 ) UMSG("Destination not specified.");
344  }
345 
346 // Check for conflicts wit third party copy
347 //
348  if (OpSpec & DoTpc && nSrcs > 1)
349  UMSG("Third party copy requires a single source.");
350 
351 // Check for conflicts with ZIP archive
352 //
353  if( OpSpec & DoZip & DoCksrc )
354  UMSG("Cannot calculate source checksum for a file in ZIP archive.");
355 
356  if( ( OpSpec & DoZip & DoCksum ) && !CksData.HasValue() )
357  UMSG("Cannot calculate source checksum for a file in ZIP archive.");
358 
359 // Turn off verbose if we are in server mode
360 //
361  if (OpSpec & DoServer)
362  {OpSpec &= ~DoVerbose;
363  Verbose = 0;
364  }
365 
366 // Turn on auto-path creation if requested via envar
367 //
368  if (getenv("XRD_MAKEPATH")) OpSpec |= DoPath;
369 
370 // Process the destination first as it is special
371 //
372  dstFile = new XrdCpFile(parmVal[--parmCnt], rc);
373  if (rc) FMSG("Invalid url, '" <<dstFile->Path <<"'.", 22);
374 
375 // Do a protocol check
376 //
385  {FMSG(dstFile->ProtName <<"file protocol is not supported.", 22)}
386 
387 // Resolve this file if it is a local file
388 //
389  isLcl = (dstFile->Protocol == XrdCpFile::isFile)
391  if (isLcl && (rc = dstFile->Resolve()))
392  {if (rc != ENOENT || (Argc - optind - 1) > 1 || OpSpec & DoRecurse)
393  FMSG(XrdSysE2T(rc) <<" processing " <<dstFile->Path, 2);
394  }
395 
396 // Now pick up all the source files from the command line
397 //
398  pLast = &pBase;
399  for (i = 0; i < parmCnt; i++) ProcFile(parmVal[i]);
400 
401 // If an input file list was specified, process it as well
402 //
403  if (inFile)
405  char *fname;
406  int inFD = open(inFile, O_RDONLY);
407  if (inFD < 0) FMSG(XrdSysE2T(errno) <<" opening infiles " <<inFile, 2);
408  inList.Attach(inFD);
409  while((fname = inList.GetLine())) if (*fname) ProcFile(fname);
410  }
411 
412 // Check if we have any sources or too many sources
413 //
414  srcFile = pBase.Next;
415 
416  if (!numFiles || !srcFile)
417  UMSG("Source not specified.");
418 
419  if (Opts & opt1Src && numFiles > 1)
420  FMSG("Only a single source is allowed.", 2);
421 
422 // Check if we have an appropriate destination
423 //
426  FMSG("Destination is neither remote nor a directory.", 2);
427 
428 // Do the dumb check
429 //
430  if (isLcl && Opts & optNoLclCp)
431  FMSG("All files are local; use 'cp' instead!", 1);
432 
433 // Check for checksum spec conflicts
434 //
435  if (OpSpec & DoCksum)
436  {if (CksData.Length && numFiles > 1)
437  FMSG("Checksum with fixed value requires a single input file.", 2);
438  if (CksData.Length && OpSpec & DoRecurse)
439  FMSG("Checksum with fixed value conflicts with '--recursive'.", 2);
440  }
441 
442 // Now extend all local sources if recursive is in effect
443 //
444  if (OpSpec & DoRecurse && !(Opts & optNoXtnd))
445  {pPrev = &pBase; pBase.Next = srcFile;
446  while((pFile = pPrev->Next))
447  {if (pFile->Protocol != XrdCpFile::isDir) pPrev = pFile;
448  else {Path = pFile->Path;
449  pPrev->Next = pFile->Next;
450  if (Verbose) EMSG("Indexing files in " <<Path);
451  numFiles--;
452  if ((rc = pFile->Extend(&pLast, numFiles, totBytes)))
453  FMSG(XrdSysE2T(rc) <<" indexing " <<Path, 2);
454  if (pFile->Next)
455  {pLast->Next = pPrev->Next;
456  pPrev->Next = pFile->Next;
457  }
458  delete pFile;
459  }
460  }
461  if (!(srcFile = pBase.Next))
462  FMSG("No regular files found to copy!", 2);
463  if (Verbose) EMSG("Copying " <<Human(totBytes, Buff, sizeof(Buff))
464  <<" from " <<numFiles
465  <<(numFiles != 1 ? " files." : " file."));
466  }
467 }
468 
469 /******************************************************************************/
470 /* P r i v a t e M e t h o d s */
471 /******************************************************************************/
472 /******************************************************************************/
473 /* Private: a 2 i */
474 /******************************************************************************/
475 
476 int XrdCpConfig::a2i(const char *item, int *val, int minv, int maxv)
477 {
478  char *eP;
479 
480 // Convert the numeric argument
481 //
482  errno = 0;
483  *val = strtol(item, &eP, 10);
484  if (errno || *eP) ZMSG("'" <<OpName() <<"' argument is not a number.");
485 
486 // Impose min/max limits
487 //
488  if (*val < minv)
489  ZMSG("'" <<OpName() <<"' argument must be >= " <<minv <<'.');
490  if (maxv >= 0 && *val > maxv)
491  ZMSG("'" <<OpName() <<"' argument must be <= " <<maxv <<'.');
492  return 1;
493 }
494 /******************************************************************************/
495 /* Private: a 2 l */
496 /******************************************************************************/
497 
498 int XrdCpConfig::a2l(const char *item, long long *val,
499  long long minv, long long maxv)
500 {
501  char *eP;
502 
503 // Convert the numeric argument
504 //
505  errno = 0;
506  *val = strtoll(item, &eP, 10);
507  if (errno || *eP) ZMSG("'" <<OpName() <<"' argument is not a number.");
508 
509 // Impose min/max limits
510 //
511  if (*val < minv)
512  ZMSG("'" <<OpName() <<"' argument must be >= " <<minv <<'.');
513  if (maxv >= 0 && *val > maxv)
514  ZMSG("'" <<OpName() <<"' argument must be <= " <<maxv <<'.');
515  return 1;
516 }
517 
518 /******************************************************************************/
519 /* Private: a 2 t */
520 /******************************************************************************/
521 
522 int XrdCpConfig::a2t(const char *item, int *val, int minv, int maxv)
523 { int qmult;
524  char *eP, *fP = (char *)item + strlen(item) - 1;
525 
526 // Get scaling
527 //
528  if (*fP == 's' || *fP == 'S') qmult = 1;
529  else if (*fP == 'm' || *fP == 'M') qmult = 60;
530  else if (*fP == 'h' || *fP == 'H') qmult = 60*60;
531  else if (*fP == 'd' || *fP == 'D') qmult = 60*60*24;
532  else {qmult = 1; fP++;}
533 
534 // Convert the value
535 //
536  errno = 0;
537  *val = strtoll(item, &eP, 10) * qmult;
538  if (errno || eP != fP)
539  ZMSG("'" <<OpName() <<"' argument is not a valid time.");
540 
541 // Impose min/max limits
542 //
543  if (*val < minv)
544  ZMSG("'" <<OpName() <<"' argument must be >= " <<minv <<'.');
545  if (maxv >= 0 && *val > maxv)
546  ZMSG("'" <<OpName() <<"' argument must be <= " <<maxv <<'.');
547  return 1;
548 }
549 
550 /******************************************************************************/
551 /* Private: a 2 x */
552 /******************************************************************************/
553 
554 int XrdCpConfig::a2x(const char *Val, char *Buff, int Vlen)
555 {
556  int n, i = 0, Odd = 0;
557  if (Vlen & 0x01) return 0;
558  while(Vlen--)
559  { if (*Val >= '0' && *Val <= '9') n = *Val-48;
560  else if (*Val >= 'a' && *Val <= 'f') n = *Val-87;
561  else if (*Val >= 'A' && *Val <= 'F') n = *Val-55;
562  else return 0;
563  if (Odd) Buff[i++] |= n;
564  else Buff[i ] = n << 4;
565  Val++; Odd = ~Odd;
566  }
567  return 1;
568 }
569 
570 /******************************************************************************/
571 /* Private: a 2 z */
572 /******************************************************************************/
573 
574 int XrdCpConfig::a2z(const char *item, long long *val,
575  long long minv, long long maxv)
576 { long long qmult;
577  char *eP, *fP = (char *)item + strlen(item) - 1;
578 
579 // Get scaling
580 //
581  if (*fP == 'k' || *fP == 'K') qmult = 1024LL;
582  else if (*fP == 'm' || *fP == 'M') qmult = 1024LL*1024LL;
583  else if (*fP == 'g' || *fP == 'G') qmult = 1024LL*1024LL*1024LL;
584  else if (*fP == 't' || *fP == 'T') qmult = 1024LL*1024LL*1024LL*1024LL;
585  else {qmult = 1; fP++;}
586 
587 // Convert the value
588 //
589  errno = 0;
590  *val = strtoll(item, &eP, 10) * qmult;
591  if (errno || eP != fP)
592  ZMSG("'" <<OpName() <<"' argument is not a valid time.");
593 
594 // Impose min/max limits
595 //
596  if (*val < minv)
597  ZMSG("'" <<OpName() <<"' argument must be >= " <<minv <<'.');
598  if (maxv >= 0 && *val > maxv)
599  ZMSG("'" <<OpName() <<"' argument must be <= " <<maxv <<'.');
600  return 1;
601 }
602 
603 /******************************************************************************/
604 /* Private: d e f C k s */
605 /******************************************************************************/
606 
607 int XrdCpConfig::defCks(const char *opval)
608 {
609  if( CksVal )
610  {
611  std::string cksum( opval );
612  size_t pos = cksum.find( ':' );
613  std::string mode = cksum.substr( pos + 1 );
614  if( mode != "source" )
615  FMSG("Additional checksum must be of mode 'source'.", 13);
616  AddCksVal.push_back( cksum.substr( 0, pos ) );
617  return 1;
618  }
619 
620  static XrdVERSIONINFODEF(myVer, xrdcp, XrdVNUMBER, XrdVERSION);
621  const char *Colon = index(opval, ':');
622  char csName[XrdCksData::NameSize];
623  int n;
624 
625 // Initialize the checksum manager if we have not done so already
626 //
627  if (!CksMan)
628  {CksMan = new XrdCksManager(Log, 0, myVer, true);
629  if (!(CksMan->Init("")))
630  {delete CksMan; CksMan = 0;
631  FMSG("Unable to initialize checksum processing.", 13);
632  }
633  }
634 
635 // Copy out the checksum name
636 //
637  n = (Colon ? Colon - opval : strlen(opval));
638  if (n >= XrdCksData::NameSize)
639  UMSG("Invalid checksum type, '" <<opval <<"'.");
640  strncpy(csName, opval, n); csName[n] = 0;
641  toLower( csName );
642 
643 // Get a checksum object for this checksum
644 //
645  if( strcmp( csName, "auto" ) )
646  {
647  if (CksObj) {delete CksObj; CksObj = 0;}
648  if (!CksData.Set(csName) || !(CksObj = CksMan->Object(CksData.Name)))
649  UMSG("Invalid checksum type, '" <<csName <<"'.");
650  CksObj->Type(CksLen);
651  }
652 
653 // Reset checksum information
654 //
655  CksData.Length = 0;
656  OpSpec &= ~(DoCkprt | DoCksrc | DoCksum);
657 
658 // Check for any additional arguments
659 //
660  if (Colon)
661  {Colon++;
662  if (!(*Colon)) UMSG(CksData.Name <<" argument missing after ':'.");
663  if (!strcmp(Colon, "print")) OpSpec |= (DoCkprt | DoCksum);
664  else if (!strcmp(Colon, "source")) OpSpec |= (DoCkprt | DoCksrc);
665  else {n = strlen(Colon);
666  if (n != CksLen*2 || !CksData.Set(Colon, n))
667  UMSG("Invalid " <<CksData.Name <<" value '" <<Colon <<"'.");
668  OpSpec |= DoCksum;
669  }
670  } else OpSpec |= DoCksum;
671 
672 // All done
673 //
674  CksVal = opval;
675  return 1;
676 }
677 
678 /******************************************************************************/
679 /* Private: d e f O p q */
680 /******************************************************************************/
681 
682 int XrdCpConfig::defOpq(const char *theOp)
683 {
684  const char *oVal = theOp+3;
685 
686 // Make sure opaque information was specified
687 //
688  if (!(*oVal)) UMSG("'" <<theOp <<"' opaque data not specified.");
689 
690 // Set proper opaque data
691 //
692  if (*(theOp+2) == 'S') srcOpq = oVal;
693  else dstOpq = oVal;
694 
695 // All done
696 //
697  return 1;
698 }
699 
700 /******************************************************************************/
701 /* Private: d e f O p t */
702 /******************************************************************************/
703 
704 int XrdCpConfig::defOpt(const char *theOp, const char *theArg)
705 {
706  defVar *dP;
707  int opval, isInt = (*(theOp+2) == 'I');
708  const char *vName = theOp+3;
709  char *eP;
710 
711 // Make sure define variable name specified
712 //
713  if (!(*vName)) UMSG("'" <<theOp <<"' variable not specified.");
714 
715 // Make sure we have a value
716 //
717  if (!theArg) UMSG("'" <<theOp <<"' argument not specified.");
718 
719 // For integer arguments convert the value
720 //
721  if (isInt)
722  {errno = 0;
723  opval = strtol(theArg, &eP, 10);
724  if (errno || *eP) UMSG("'" <<theOp <<"' argument is not a number.");
725  dP = new defVar(vName, opval);
726  if (!intDend) intDefs = intDend = dP;
727  else {intDend->Next = dP; intDend = dP;}
728  } else {
729  dP = new defVar(vName, theArg);
730  if (!strDend) strDefs = strDend = dP;
731  else {strDend->Next = dP; strDend = dP;}
732  }
733 
734 // Convert the argument
735 //
736  return 2;
737 }
738 
739 /******************************************************************************/
740 /* Private: d e f P x y */
741 /******************************************************************************/
742 
743 void XrdCpConfig::defPxy(const char *opval)
744 {
745  const char *Colon = index(opval, ':');
746  char *eP;
747  int n;
748 
749 // Make sure the host was specified
750 //
751  if (Colon == opval) UMSG("Proxy host not specified.");
752 
753 // Make sure the port was specified
754 //
755  if (!Colon || !(*(Colon+1))) UMSG("Proxy port not specified.");
756 
757 // Make sure the port is a valid number that is not too big
758 //
759  errno = 0;
760  pPort = strtol(Colon+1, &eP, 10);
761  if (errno || *eP || pPort < 1 || pPort > 65535)
762  UMSG("Invalid proxy port, '" <<opval <<"'.");
763 
764 // Copy out the proxy host
765 //
766  if (pHost) free(pHost);
767  n = Colon - opval + 1;
768  pHost = (char *)malloc(n);
769  strncpy(pHost, opval, n-1);
770  pHost[n-1] = 0;
771 }
772 
773 
774 /******************************************************************************/
775 /* H u m a n */
776 /******************************************************************************/
777 
778 const char *XrdCpConfig::Human(long long inval, char *Buff, int Blen)
779 {
780  static const char *sfx[] = {" bytes", "KB", "MB", "GB", "TB", "PB"};
781  unsigned int i;
782 
783  for (i = 0; i < sizeof(sfx)/sizeof(sfx[0]) - 1 && inval >= 1024; i++)
784  inval = inval/1024;
785 
786  snprintf(Buff, Blen, "%lld%s", inval, sfx[i]);
787  return Buff;
788 }
789 
790 /******************************************************************************/
791 /* Private: L e g a c y */
792 /******************************************************************************/
793 
794 int XrdCpConfig::Legacy(int oIndex)
795 {
796  extern int optind;
797  char *oArg;
798  int rc;
799 
800 // if (!Argv[oIndex]) return 0;
801 
802  while(oIndex < Argc && (*Argv[oIndex] != '-' || *(Argv[oIndex]+1) == '\0'))
803  parmVal[parmCnt++] = Argv[oIndex++];
804  if (oIndex >= Argc) return 0;
805 
806  if (oIndex+1 >= Argc || *Argv[oIndex+1] == '-') oArg = 0;
807  else oArg = Argv[oIndex+1];
808  if (!(rc = Legacy(Argv[oIndex], oArg))) return 0;
809  optind = oIndex + rc;
810 
811  return 1;
812 }
813 
814 /******************************************************************************/
815 
816 int XrdCpConfig::Legacy(const char *theOp, const char *theArg)
817 {
818  if (!strcmp(theOp, "-adler")) return defCks("adler32:source");
819 
820  if (!strncmp(theOp, "-DI", 3) || !strncmp(theOp, "-DS", 3))
821  return defOpt(theOp, theArg);
822 
823  if (!strcmp(theOp, "-extreme") || !strcmp(theOp, "-x"))
824  {if (nSrcs <= 1) {nSrcs = dfltSrcs; OpSpec |= DoSources;}
825  return 1;
826  }
827 
828  if (!strcmp(theOp, "-np")) {OpSpec |= DoNoPbar; return 1;}
829 
830  if (!strcmp(theOp, "-md5")) return defCks("md5:source");
831 
832  if (!strncmp(theOp,"-OD",3) || !strncmp(theOp,"-OS",3)) return defOpq(theOp);
833 
834  if (!strcmp(theOp, "-version")) {std::cerr <<XrdVERSION <<std::endl; exit(0);}
835 
836  if (!strcmp(theOp, "-force"))
837  FMSG("-force is no longer supported; use --retry instead!",22);
838 
839  return 0;
840 }
841 
842 /******************************************************************************/
843 /* Private: L i c e n s e */
844 /******************************************************************************/
845 
846 void XrdCpConfig::License()
847 {
848  const char *theLicense =
849 #include "../../LICENSE"
850 ;
851 
852  std::cerr <<theLicense;
853  exit(0);
854 }
855 
856 /******************************************************************************/
857 /* Private: O p N a m e */
858 /******************************************************************************/
859 
860 const char *XrdCpConfig::OpName()
861 {
862  extern int optind, optopt;
863  static char oName[4] = {'-', 0, 0, 0};
864 
865  if (!optopt || optopt == '-' || *(Argv[optind-1]+1) == '-')
866  return Argv[optind-1];
867  oName[1] = optopt;
868  return oName;
869 }
870 
871 /******************************************************************************/
872 /* p r o c F i l e */
873 /******************************************************************************/
874 
875 void XrdCpConfig::ProcFile(const char *fname)
876 {
877  int rc;
878 
879 // Chain in this file in the input list
880 //
881  pLast->Next = pFile = new XrdCpFile(fname, rc);
882  if (rc) FMSG("Invalid url, '" <<fname <<"'.", 22);
883 
884 // For local files, make sure it exists and get its size
885 //
886  if (pFile->Protocol == XrdCpFile::isFile && (rc = pFile->Resolve()))
887  FMSG(XrdSysE2T(rc) <<" processing " <<pFile->Path, 2);
888 
889 // Process file based on type (local or remote)
890 //
891  if (pFile->Protocol == XrdCpFile::isFile) totBytes += pFile->fSize;
892  else if (pFile->Protocol == XrdCpFile::isDir)
893  {if (!(OpSpec & DoRecurse))
894  FMSG(pFile->Path <<" is a directory.", 2);
895  }
896  else if (pFile->Protocol == XrdCpFile::isStdIO)
897  {if (Opts & optNoStdIn)
898  FMSG("Using stdin as a source is disallowed.", 22);
899  if (numFiles)
900  FMSG("Multiple sources disallowed with stdin.", 22);
901  }
902  else if (!((pFile->Protocol == XrdCpFile::isXroot) ||
903  (pFile->Protocol == XrdCpFile::isXroots) ||
904  (pFile->Protocol == XrdCpFile::isHttp) ||
905  (pFile->Protocol == XrdCpFile::isHttps) ||
906  (pFile->Protocol == XrdCpFile::isPelican) ||
907  (pFile->Protocol == XrdCpFile::isS3)))
908  {FMSG(pFile->ProtName <<" file protocol is not supported.", 22)}
909  else if (OpSpec & DoRecurse && !(Opts & optRmtRec))
910  {FMSG("Recursive copy from a remote host is not supported.",22)}
911  else isLcl = 0;
912 
913 // Update last pointer and we are done if this is stdin
914 //
915  numFiles++;
916  pLast = pFile;
917 }
918 
919 /******************************************************************************/
920 /* U s a g e */
921 /******************************************************************************/
922 
923 void XrdCpConfig::Usage(int rc)
924 {
925  static const char *Syntax = "\n"
926  "Usage: xrdcp [<options>] <src> [<src> [. . .]] <dest>\n";
927 
928  static const char *Syntax1= "\n"
929  "Usage: xrdcp [<options>] <src> <dest>\n";
930 
931  static const char *Options= "\n"
932  "Options: [--cksum <args>] [--coerce] [--continue]\n"
933  " [--debug <lvl>] [--dynamic-src] [--force] [--help]\n"
934  " [--infiles <fn>] [--license] [--nopbar] [--notlsok]\n"
935  " [--parallel <n>] [--posc] [--proxy <host>:<port>]\n"
936  " [--recursive] [--retry <n>] [--retry-policy <force|continue>]\n"
937  " [--rm-bad-cksum] [--server] [--silent] [--sources <n>]\n"
938  " [--streams <n>] [--tlsmetalink] [--tlsnodata]\n"
939  " [--tpc [delegate] {first|only}] [--verbose] [--version]\n"
940  " [--xattr] [--xrate <rate>] [--xrate-threshold <rate>]\n"
941  " [--zip <file>] [--zip-append] [--zip-mtln-cksum]\n";
942 
943  static const char *Syntax2= "\n"
944  "<src>: [[x]root[s]://<host>[:<port>]/]<path> | -";
945 
946  static const char *Syntay2= "\n"
947  "<src>: [[x]root[s]://<host>[:<port>]/]<path>";
948 
949  static const char *Syntax3= "\n"
950  "<dest>: [[x]root[s]://<host>[:<port>]/]<path> | -";
951 
952  static const char *Detail = "\n"
953  "Note: using a dash (-) for <src> uses stdin and for <dest> stdout\n\n"
954  "-C | --cksum <args> verifies the checksum at the destination as provided\n"
955  " by the source server or locally computed. The args are\n"
956  " <ckstype>[:{<value>|print|source}]\n"
957  " where <ckstype> is one of adler32, crc32, crc32c, md5,\n"
958  " zcrc32 or auto. If 'auto' is chosen, xrdcp will try to\n"
959  " automatically infer the right checksum type based on the\n"
960  " source/destination configuration, source file type\n"
961  " (e.g. metalink, ZIP), and available checksum plug-ins.\n"
962  " If the hex value of the checksum is given, it is used.\n"
963  " Otherwise, the server's checksum is used for remote files\n"
964  " and computed for local files. Specifying print merely\n"
965  " prints the checksum but does not verify it.\n"
966  "-F | --coerce coerces the copy by ignoring file locking semantics\n"
967  " --continue continue copying a file from the point where the previous\n"
968  " copy was interrupted\n"
969  "-d | --debug <lvl> sets the debug level: 0 off, 1 low, 2 medium, 3 high\n"
970  "-Z | --dynamic-src file size may change during the copy\n"
971  "-f | --force replaces any existing output file\n"
972  "-h | --help prints this information\n"
973  "-I | --infiles <fname> specifies the file that contains a list of input files\n"
974  "-H | --license prints license terms and conditions\n"
975  "-N | --nopbar does not print the progress bar\n"
976  " --notlsok if server is too old to support TLS encryption fallback\n"
977  " to unencrypted communication\n"
978  " --parallel <n> number of files to copy at the same time\n"
979  "-P | --posc enables persist on successful close semantics\n"
980  "-D | --proxy <host>:<port> uses the specified SOCKS4 proxy connection\n"
981  "-r | --recursive recursively copies all source files\n"
982  "-t | --retry <n> maximum number of times to retry failed copy-jobs\n"
983  " --retry-policy <policy> retry policy: force or continue\n"
984  " --rm-bad-cksum remove the target file if checksum verification failed\n"
985  " (enables also POSC semantics)\n"
986  " --server runs in a server environment with added operations\n"
987  "-s | --silent produces no output other than error messages\n"
988  "-y | --sources <n> uses up to the number of sources specified in parallel\n"
989  "-S | --streams <n> copies using the specified number of TCP connections\n"
990  " --tlsmetalink convert [x]root to [x]roots protocol in metalinks\n"
991  "-E | --tlsnodata in case of [x]roots protocol, encrypt only the control\n"
992  " stream and leave the data streams unencrypted\n"
993  "-T | --tpc <args> uses third party copy mode between the src and dest.\n"
994  " Both the src and dest must allow tpc mode. Argument\n"
995  " 'first' tries tpc and if it fails, does a normal copy;\n"
996  " while 'only' fails the copy unless tpc succeeds.\n"
997  "-v | --verbose produces more information about the copy\n"
998  "-V | --version prints the version number\n"
999  " --xattr preserve extended attributes\n"
1000  "-X | --xrate <rate> limits the transfer to the specified rate. You can\n"
1001  " suffix the value with 'k', 'm', or 'g'\n"
1002  " --xrate-threshold <rate> If the transfer rate drops below given threshold force\n"
1003  " the client to use different source or if no more sources\n"
1004  " are available fail the transfer. You can suffix the value\n"
1005  " with 'k', 'm', or 'g'\n"
1006  "-z | --zip <file> treat the source as a ZIP archive containing given file\n"
1007  " --zip-append append file to existing zip archive\n"
1008  " --zip-mtln-cksum use the checksum available in a metalink file even if\n"
1009  " a file is being extracted from a ZIP archive\n"
1010  "\n"
1011  "Legacy options: [-adler] [-DI<var> <val>] [-DS<var> <val>] [-np]\n"
1012  " [-md5] [-OD<cgi>] [-OS<cgi>] [-version] [-x]";
1013 
1014  std::cerr <<(Opts & opt1Src ? Syntax1 : Syntax) <<Options;
1015  std::cerr <<(Opts & optNoStdIn ? Syntay2 : Syntax2) <<Syntax3 <<std::endl;
1016  if (!rc) std::cerr <<Detail <<std::endl;
1017  exit(rc);
1018 }
int inList(const char *var, const char **Vec)
#define OPT_TYPE
Definition: XrdCpConfig.cc:68
#define ZMSG(x)
Definition: XrdCpConfig.cc:61
#define FMSG(x, y)
Definition: XrdCpConfig.cc:57
#define EMSG(x)
Definition: XrdCpConfig.cc:55
#define UMSG(x)
Definition: XrdCpConfig.cc:59
XrdSysError eDest(0, "HttpMon")
int optopt
int optind
#define open
Definition: XrdPosix.hh:78
XrdOucString Path
struct myOpts opts
const char * XrdSysE2T(int errcode)
Definition: XrdSysE2T.cc:104
virtual const char * Type(int &csSize)=0
int Set(const char *csName)
Definition: XrdCksData.hh:81
char Length
Definition: XrdCksData.hh:52
bool HasValue()
Definition: XrdCksData.hh:125
static const int NameSize
Definition: XrdCksData.hh:41
char Name[NameSize]
Definition: XrdCksData.hh:44
virtual int Init(const char *ConfigFN, const char *DfltCalc=0)=0
virtual XrdCksCalc * Object(const char *name)
Definition: XrdCks.hh:214
static void SetMsgPfx(const char *pfx)
Definition: XrdCpFile.hh:57
PType Protocol
Definition: XrdCpFile.hh:49
int Resolve()
Definition: XrdCpFile.cc:158
int Extend(XrdCpFile **pLast, int &nFile, long long &nBytes)
Definition: XrdCpFile.cc:131
long long fSize
Definition: XrdCpFile.hh:51
char * Path
Definition: XrdCpFile.hh:45
char ProtName[8]
Definition: XrdCpFile.hh:50
XrdCpFile * Next
Definition: XrdCpFile.hh:44
const char * SetPrefix(const char *prefix)
Definition: XrdSysError.hh:194
XrdVERSIONINFODEF(myVersion, cmsclient, XrdVNUMBER, XrdVERSION)
static XrdSysLogger Logger
Definition: XrdCpConfig.cc:77
static const uint64_t OpVerbose
Definition: XrdCpConfig.hh:154
static const uint64_t OpXAttr
Definition: XrdCpConfig.hh:185
static const uint64_t DoProxy
Definition: XrdCpConfig.hh:128
defVar * intDefs
Definition: XrdCpConfig.hh:63
void Config(int argc, char **argv, int Opts=0)
Definition: XrdCpConfig.cc:193
static const uint64_t OpRecurse
Definition: XrdCpConfig.hh:130
XrdCksCalc * CksObj
Definition: XrdCpConfig.hh:87
static const uint64_t OpContinue
Definition: XrdCpConfig.hh:194
static const uint64_t DoRetry
Definition: XrdCpConfig.hh:135
static const uint64_t DoStreams
Definition: XrdCpConfig.hh:147
std::vector< std::string > AddCksVal
Definition: XrdCpConfig.hh:97
static const uint64_t OpParallel
Definition: XrdCpConfig.hh:162
static const uint64_t OpZipAppend
Definition: XrdCpConfig.hh:203
const char * dstOpq
Definition: XrdCpConfig.hh:65
static const uint64_t DoZipMtlnCksum
Definition: XrdCpConfig.hh:189
XrdCksData CksData
Definition: XrdCpConfig.hh:85
static const uint64_t OpSilent
Definition: XrdCpConfig.hh:140
static const uint64_t OpTlsMLF
Definition: XrdCpConfig.hh:179
static const uint64_t OpNoTlsOK
Definition: XrdCpConfig.hh:176
XrdCks * CksMan
Definition: XrdCpConfig.hh:86
XrdCpFile * srcFile
Definition: XrdCpConfig.hh:90
const char * Pgm
Definition: XrdCpConfig.hh:67
XrdCpFile * dstFile
Definition: XrdCpConfig.hh:91
static const uint64_t OpRetryPolicy
Definition: XrdCpConfig.hh:200
static const uint64_t OpDebug
Definition: XrdCpConfig.hh:107
char * zipFile
Definition: XrdCpConfig.hh:93
XrdCpConfig(const char *pgname)
Definition: XrdCpConfig.cc:128
static const uint64_t OpRetry
Definition: XrdCpConfig.hh:134
static const uint64_t DoNoPbar
Definition: XrdCpConfig.hh:122
static const uint64_t OpXrateThreshold
Definition: XrdCpConfig.hh:197
static const uint64_t DoCoerce
Definition: XrdCpConfig.hh:105
static const uint64_t OpServer
Definition: XrdCpConfig.hh:137
static const uint64_t DoForce
Definition: XrdCpConfig.hh:111
static const uint64_t OpVersion
Definition: XrdCpConfig.hh:157
static const uint64_t OpTpc
Definition: XrdCpConfig.hh:149
static const uint64_t DoParallel
Definition: XrdCpConfig.hh:163
static const uint64_t DoRmOnBadCksum
Definition: XrdCpConfig.hh:192
static const uint64_t DoNoTlsOK
Definition: XrdCpConfig.hh:177
static const uint64_t OpPath
Definition: XrdCpConfig.hh:182
long long OpSpec
Definition: XrdCpConfig.hh:73
static const uint64_t OpRecursv
Definition: XrdCpConfig.hh:131
static const uint64_t OpProxy
Definition: XrdCpConfig.hh:127
static const uint64_t DoTpc
Definition: XrdCpConfig.hh:150
static const uint64_t DoDebug
Definition: XrdCpConfig.hh:108
char * pHost
Definition: XrdCpConfig.hh:71
static const uint64_t OpSources
Definition: XrdCpConfig.hh:143
static const uint64_t DoCksum
Definition: XrdCpConfig.hh:101
defVar * strDefs
Definition: XrdCpConfig.hh:64
static const uint64_t OpPosc
Definition: XrdCpConfig.hh:124
static const uint64_t DoXrate
Definition: XrdCpConfig.hh:160
static const uint64_t DoCksrc
Definition: XrdCpConfig.hh:100
static const uint64_t OpZipMtlnCksum
Definition: XrdCpConfig.hh:188
static const int opt1Src
Definition: XrdCpConfig.hh:210
static const uint64_t OpXrate
Definition: XrdCpConfig.hh:159
static const uint64_t DoTpcDlgt
Definition: XrdCpConfig.hh:152
static const uint64_t DoZip
Definition: XrdCpConfig.hh:171
static const uint64_t DoVerbose
Definition: XrdCpConfig.hh:155
static const uint64_t DoContinue
Definition: XrdCpConfig.hh:195
static const uint64_t OpCksum
Definition: XrdCpConfig.hh:99
const char * CksVal
Definition: XrdCpConfig.hh:88
static const uint64_t DoRecurse
Definition: XrdCpConfig.hh:132
static const uint64_t OpTlsNoData
Definition: XrdCpConfig.hh:173
static const int optNoLclCp
Definition: XrdCpConfig.hh:214
static const uint64_t DoXrateThreshold
Definition: XrdCpConfig.hh:198
const char * srcOpq
Definition: XrdCpConfig.hh:66
static const uint64_t DoZipAppend
Definition: XrdCpConfig.hh:204
static const uint64_t OpIfile
Definition: XrdCpConfig.hh:116
static const uint64_t DoDynaSrc
Definition: XrdCpConfig.hh:166
long long xRate
Definition: XrdCpConfig.hh:68
static const int optNoStdIn
Definition: XrdCpConfig.hh:213
static const uint64_t OpNoPbar
Definition: XrdCpConfig.hh:121
static const uint64_t DoSources
Definition: XrdCpConfig.hh:144
static const uint64_t DoSilent
Definition: XrdCpConfig.hh:141
static const uint64_t OpForce
Definition: XrdCpConfig.hh:110
static const uint64_t OpRmOnBadCksum
Definition: XrdCpConfig.hh:191
static const uint64_t OpDynaSrc
Definition: XrdCpConfig.hh:165
static const uint64_t DoXAttr
Definition: XrdCpConfig.hh:186
static const int optNoXtnd
Definition: XrdCpConfig.hh:211
static const uint64_t DoTlsMLF
Definition: XrdCpConfig.hh:180
static const uint64_t OpZip
Definition: XrdCpConfig.hh:170
static const uint64_t OpLicense
Definition: XrdCpConfig.hh:119
static const uint64_t DoIfile
Definition: XrdCpConfig.hh:117
static const uint64_t OpHelp
Definition: XrdCpConfig.hh:113
static XrdSysError * Log
Definition: XrdCpConfig.hh:95
static const int optRmtRec
Definition: XrdCpConfig.hh:212
static const uint64_t DoRetryPolicy
Definition: XrdCpConfig.hh:201
std::string RetryPolicy
Definition: XrdCpConfig.hh:78
static const uint64_t DoPath
Definition: XrdCpConfig.hh:183
static const uint64_t DoPosc
Definition: XrdCpConfig.hh:125
static const uint64_t OpStreams
Definition: XrdCpConfig.hh:146
long long xRateThreshold
Definition: XrdCpConfig.hh:69
static const uint64_t OpCoerce
Definition: XrdCpConfig.hh:104
long long totBytes
Definition: XrdCpConfig.hh:83
static const uint64_t DoTpcOnly
Definition: XrdCpConfig.hh:151
static const uint64_t DoTlsNoData
Definition: XrdCpConfig.hh:174
static const uint64_t DoServer
Definition: XrdCpConfig.hh:138
static const uint64_t DoCkprt
Definition: XrdCpConfig.hh:102