XRootD
Loading...
Searching...
No Matches
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"
41#include "XrdApps/XrdCpFile.hh"
42#include "XrdCks/XrdCksCalc.hh"
45#include "XrdSys/XrdSysE2T.hh"
46#include "XrdSys/XrdSysError.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{
78static XrdSysError eDest(&Logger, "");
79};
80
81XrdSysError *XrdCpConfig::Log = &XrdCpConfiguration::eDest;
82
83const char *XrdCpConfig::opLetters = ":C:d:D:EfFhHI:NpPrRsS:t:T:vVX:y:z:ZA";
84
85struct 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
129{
130 if ((PName = rindex(pgm, '/'))) PName++;
131 else PName = pgm;
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
193void 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//
227do{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;
276 break;
278 break;
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;
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;
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//
377 if (dstFile->Protocol != XrdCpFile::isFile
378 && dstFile->Protocol != XrdCpFile::isStdIO
379 && dstFile->Protocol != XrdCpFile::isXroot
380 && dstFile->Protocol != XrdCpFile::isXroots
381 && dstFile->Protocol != XrdCpFile::isPelican
382 && dstFile->Protocol != XrdCpFile::isS3
383 && dstFile->Protocol != XrdCpFile::isHttp
384 && dstFile->Protocol != XrdCpFile::isHttps)
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)
390 | (dstFile->Protocol == XrdCpFile::isStdIO);
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 if (!numFiles) UMSG("Source not specified.");
415 if (Opts & opt1Src && numFiles > 1)
416 FMSG("Only a single source is allowed.", 2);
417 srcFile = pBase.Next;
418
419// Check if we have an appropriate destination
420//
421 if (dstFile->Protocol == XrdCpFile::isFile && (numFiles > 1
422 || (OpSpec & DoRecurse && srcFile->Protocol != XrdCpFile::isFile)))
423 FMSG("Destination is neither remote nor a directory.", 2);
424
425// Do the dumb check
426//
427 if (isLcl && Opts & optNoLclCp)
428 FMSG("All files are local; use 'cp' instead!", 1);
429
430// Check for checksum spec conflicts
431//
432 if (OpSpec & DoCksum)
433 {if (CksData.Length && numFiles > 1)
434 FMSG("Checksum with fixed value requires a single input file.", 2);
435 if (CksData.Length && OpSpec & DoRecurse)
436 FMSG("Checksum with fixed value conflicts with '--recursive'.", 2);
437 }
438
439// Now extend all local sources if recursive is in effect
440//
441 if (OpSpec & DoRecurse && !(Opts & optNoXtnd))
442 {pPrev = &pBase; pBase.Next = srcFile;
443 while((pFile = pPrev->Next))
444 {if (pFile->Protocol != XrdCpFile::isDir) pPrev = pFile;
445 else {Path = pFile->Path;
446 pPrev->Next = pFile->Next;
447 if (Verbose) EMSG("Indexing files in " <<Path);
448 numFiles--;
449 if ((rc = pFile->Extend(&pLast, numFiles, totBytes)))
450 FMSG(XrdSysE2T(rc) <<" indexing " <<Path, 2);
451 if (pFile->Next)
452 {pLast->Next = pPrev->Next;
453 pPrev->Next = pFile->Next;
454 }
455 delete pFile;
456 }
457 }
458 if (!(srcFile = pBase.Next))
459 FMSG("No regular files found to copy!", 2);
460 if (Verbose) EMSG("Copying " <<Human(totBytes, Buff, sizeof(Buff))
461 <<" from " <<numFiles
462 <<(numFiles != 1 ? " files." : " file."));
463 }
464}
465
466/******************************************************************************/
467/* P r i v a t e M e t h o d s */
468/******************************************************************************/
469/******************************************************************************/
470/* Private: a 2 i */
471/******************************************************************************/
472
473int XrdCpConfig::a2i(const char *item, int *val, int minv, int maxv)
474{
475 char *eP;
476
477// Convert the numeric argument
478//
479 errno = 0;
480 *val = strtol(item, &eP, 10);
481 if (errno || *eP) ZMSG("'" <<OpName() <<"' argument is not a number.");
482
483// Impose min/max limits
484//
485 if (*val < minv)
486 ZMSG("'" <<OpName() <<"' argument must be >= " <<minv <<'.');
487 if (maxv >= 0 && *val > maxv)
488 ZMSG("'" <<OpName() <<"' argument must be <= " <<maxv <<'.');
489 return 1;
490}
491/******************************************************************************/
492/* Private: a 2 l */
493/******************************************************************************/
494
495int XrdCpConfig::a2l(const char *item, long long *val,
496 long long minv, long long maxv)
497{
498 char *eP;
499
500// Convert the numeric argument
501//
502 errno = 0;
503 *val = strtoll(item, &eP, 10);
504 if (errno || *eP) ZMSG("'" <<OpName() <<"' argument is not a number.");
505
506// Impose min/max limits
507//
508 if (*val < minv)
509 ZMSG("'" <<OpName() <<"' argument must be >= " <<minv <<'.');
510 if (maxv >= 0 && *val > maxv)
511 ZMSG("'" <<OpName() <<"' argument must be <= " <<maxv <<'.');
512 return 1;
513}
514
515/******************************************************************************/
516/* Private: a 2 t */
517/******************************************************************************/
518
519int XrdCpConfig::a2t(const char *item, int *val, int minv, int maxv)
520{ int qmult;
521 char *eP, *fP = (char *)item + strlen(item) - 1;
522
523// Get scaling
524//
525 if (*fP == 's' || *fP == 'S') qmult = 1;
526 else if (*fP == 'm' || *fP == 'M') qmult = 60;
527 else if (*fP == 'h' || *fP == 'H') qmult = 60*60;
528 else if (*fP == 'd' || *fP == 'D') qmult = 60*60*24;
529 else {qmult = 1; fP++;}
530
531// Convert the value
532//
533 errno = 0;
534 *val = strtoll(item, &eP, 10) * qmult;
535 if (errno || eP != fP)
536 ZMSG("'" <<OpName() <<"' argument is not a valid time.");
537
538// Impose min/max limits
539//
540 if (*val < minv)
541 ZMSG("'" <<OpName() <<"' argument must be >= " <<minv <<'.');
542 if (maxv >= 0 && *val > maxv)
543 ZMSG("'" <<OpName() <<"' argument must be <= " <<maxv <<'.');
544 return 1;
545}
546
547/******************************************************************************/
548/* Private: a 2 x */
549/******************************************************************************/
550
551int XrdCpConfig::a2x(const char *Val, char *Buff, int Vlen)
552{
553 int n, i = 0, Odd = 0;
554 if (Vlen & 0x01) return 0;
555 while(Vlen--)
556 { if (*Val >= '0' && *Val <= '9') n = *Val-48;
557 else if (*Val >= 'a' && *Val <= 'f') n = *Val-87;
558 else if (*Val >= 'A' && *Val <= 'F') n = *Val-55;
559 else return 0;
560 if (Odd) Buff[i++] |= n;
561 else Buff[i ] = n << 4;
562 Val++; Odd = ~Odd;
563 }
564 return 1;
565}
566
567/******************************************************************************/
568/* Private: a 2 z */
569/******************************************************************************/
570
571int XrdCpConfig::a2z(const char *item, long long *val,
572 long long minv, long long maxv)
573{ long long qmult;
574 char *eP, *fP = (char *)item + strlen(item) - 1;
575
576// Get scaling
577//
578 if (*fP == 'k' || *fP == 'K') qmult = 1024LL;
579 else if (*fP == 'm' || *fP == 'M') qmult = 1024LL*1024LL;
580 else if (*fP == 'g' || *fP == 'G') qmult = 1024LL*1024LL*1024LL;
581 else if (*fP == 't' || *fP == 'T') qmult = 1024LL*1024LL*1024LL*1024LL;
582 else {qmult = 1; fP++;}
583
584// Convert the value
585//
586 errno = 0;
587 *val = strtoll(item, &eP, 10) * qmult;
588 if (errno || eP != fP)
589 ZMSG("'" <<OpName() <<"' argument is not a valid time.");
590
591// Impose min/max limits
592//
593 if (*val < minv)
594 ZMSG("'" <<OpName() <<"' argument must be >= " <<minv <<'.');
595 if (maxv >= 0 && *val > maxv)
596 ZMSG("'" <<OpName() <<"' argument must be <= " <<maxv <<'.');
597 return 1;
598}
599
600/******************************************************************************/
601/* Private: d e f C k s */
602/******************************************************************************/
603
604int XrdCpConfig::defCks(const char *opval)
605{
606 if( CksVal )
607 {
608 std::string cksum( opval );
609 size_t pos = cksum.find( ':' );
610 std::string mode = cksum.substr( pos + 1 );
611 if( mode != "source" )
612 FMSG("Additional checksum must be of mode 'source'.", 13);
613 AddCksVal.push_back( cksum.substr( 0, pos ) );
614 return 1;
615 }
616
617 static XrdVERSIONINFODEF(myVer, xrdcp, XrdVNUMBER, XrdVERSION);
618 const char *Colon = index(opval, ':');
619 char csName[XrdCksData::NameSize];
620 int n;
621
622// Initialize the checksum manager if we have not done so already
623//
624 if (!CksMan)
625 {CksMan = new XrdCksManager(Log, 0, myVer, true);
626 if (!(CksMan->Init("")))
627 {delete CksMan; CksMan = 0;
628 FMSG("Unable to initialize checksum processing.", 13);
629 }
630 }
631
632// Copy out the checksum name
633//
634 n = (Colon ? Colon - opval : strlen(opval));
635 if (n >= XrdCksData::NameSize)
636 UMSG("Invalid checksum type, '" <<opval <<"'.");
637 strncpy(csName, opval, n); csName[n] = 0;
638 toLower( csName );
639
640// Get a checksum object for this checksum
641//
642 if( strcmp( csName, "auto" ) )
643 {
644 if (CksObj) {delete CksObj; CksObj = 0;}
645 if (!CksData.Set(csName) || !(CksObj = CksMan->Object(CksData.Name)))
646 UMSG("Invalid checksum type, '" <<csName <<"'.");
647 CksObj->Type(CksLen);
648 }
649
650// Reset checksum information
651//
652 CksData.Length = 0;
653 OpSpec &= ~(DoCkprt | DoCksrc | DoCksum);
654
655// Check for any additional arguments
656//
657 if (Colon)
658 {Colon++;
659 if (!(*Colon)) UMSG(CksData.Name <<" argument missing after ':'.");
660 if (!strcmp(Colon, "print")) OpSpec |= (DoCkprt | DoCksum);
661 else if (!strcmp(Colon, "source")) OpSpec |= (DoCkprt | DoCksrc);
662 else {n = strlen(Colon);
663 if (n != CksLen*2 || !CksData.Set(Colon, n))
664 UMSG("Invalid " <<CksData.Name <<" value '" <<Colon <<"'.");
665 OpSpec |= DoCksum;
666 }
667 } else OpSpec |= DoCksum;
668
669// All done
670//
671 CksVal = opval;
672 return 1;
673}
674
675/******************************************************************************/
676/* Private: d e f O p q */
677/******************************************************************************/
678
679int XrdCpConfig::defOpq(const char *theOp)
680{
681 const char *oVal = theOp+3;
682
683// Make sure opaque information was specified
684//
685 if (!(*oVal)) UMSG("'" <<theOp <<"' opaque data not specified.");
686
687// Set proper opaque data
688//
689 if (*(theOp+2) == 'S') srcOpq = oVal;
690 else dstOpq = oVal;
691
692// All done
693//
694 return 1;
695}
696
697/******************************************************************************/
698/* Private: d e f O p t */
699/******************************************************************************/
700
701int XrdCpConfig::defOpt(const char *theOp, const char *theArg)
702{
703 defVar *dP;
704 int opval, isInt = (*(theOp+2) == 'I');
705 const char *vName = theOp+3;
706 char *eP;
707
708// Make sure define variable name specified
709//
710 if (!(*vName)) UMSG("'" <<theOp <<"' variable not specified.");
711
712// Make sure we have a value
713//
714 if (!theArg) UMSG("'" <<theOp <<"' argument not specified.");
715
716// For integer arguments convert the value
717//
718 if (isInt)
719 {errno = 0;
720 opval = strtol(theArg, &eP, 10);
721 if (errno || *eP) UMSG("'" <<theOp <<"' argument is not a number.");
722 dP = new defVar(vName, opval);
723 if (!intDend) intDefs = intDend = dP;
724 else {intDend->Next = dP; intDend = dP;}
725 } else {
726 dP = new defVar(vName, theArg);
727 if (!strDend) strDefs = strDend = dP;
728 else {strDend->Next = dP; strDend = dP;}
729 }
730
731// Convert the argument
732//
733 return 2;
734}
735
736/******************************************************************************/
737/* Private: d e f P x y */
738/******************************************************************************/
739
740void XrdCpConfig::defPxy(const char *opval)
741{
742 const char *Colon = index(opval, ':');
743 char *eP;
744 int n;
745
746// Make sure the host was specified
747//
748 if (Colon == opval) UMSG("Proxy host not specified.");
749
750// Make sure the port was specified
751//
752 if (!Colon || !(*(Colon+1))) UMSG("Proxy port not specified.");
753
754// Make sure the port is a valid number that is not too big
755//
756 errno = 0;
757 pPort = strtol(Colon+1, &eP, 10);
758 if (errno || *eP || pPort < 1 || pPort > 65535)
759 UMSG("Invalid proxy port, '" <<opval <<"'.");
760
761// Copy out the proxy host
762//
763 if (pHost) free(pHost);
764 n = Colon - opval + 1;
765 pHost = (char *)malloc(n);
766 strncpy(pHost, opval, n-1);
767 pHost[n-1] = 0;
768}
769
770
771/******************************************************************************/
772/* H u m a n */
773/******************************************************************************/
774
775const char *XrdCpConfig::Human(long long inval, char *Buff, int Blen)
776{
777 static const char *sfx[] = {" bytes", "KB", "MB", "GB", "TB", "PB"};
778 unsigned int i;
779
780 for (i = 0; i < sizeof(sfx)/sizeof(sfx[0]) - 1 && inval >= 1024; i++)
781 inval = inval/1024;
782
783 snprintf(Buff, Blen, "%lld%s", inval, sfx[i]);
784 return Buff;
785}
786
787/******************************************************************************/
788/* Private: L e g a c y */
789/******************************************************************************/
790
791int XrdCpConfig::Legacy(int oIndex)
792{
793 extern int optind;
794 char *oArg;
795 int rc;
796
797// if (!Argv[oIndex]) return 0;
798
799 while(oIndex < Argc && (*Argv[oIndex] != '-' || *(Argv[oIndex]+1) == '\0'))
800 parmVal[parmCnt++] = Argv[oIndex++];
801 if (oIndex >= Argc) return 0;
802
803 if (oIndex+1 >= Argc || *Argv[oIndex+1] == '-') oArg = 0;
804 else oArg = Argv[oIndex+1];
805 if (!(rc = Legacy(Argv[oIndex], oArg))) return 0;
806 optind = oIndex + rc;
807
808 return 1;
809}
810
811/******************************************************************************/
812
813int XrdCpConfig::Legacy(const char *theOp, const char *theArg)
814{
815 if (!strcmp(theOp, "-adler")) return defCks("adler32:source");
816
817 if (!strncmp(theOp, "-DI", 3) || !strncmp(theOp, "-DS", 3))
818 return defOpt(theOp, theArg);
819
820 if (!strcmp(theOp, "-extreme") || !strcmp(theOp, "-x"))
821 {if (nSrcs <= 1) {nSrcs = dfltSrcs; OpSpec |= DoSources;}
822 return 1;
823 }
824
825 if (!strcmp(theOp, "-np")) {OpSpec |= DoNoPbar; return 1;}
826
827 if (!strcmp(theOp, "-md5")) return defCks("md5:source");
828
829 if (!strncmp(theOp,"-OD",3) || !strncmp(theOp,"-OS",3)) return defOpq(theOp);
830
831 if (!strcmp(theOp, "-version")) {std::cerr <<XrdVERSION <<std::endl; exit(0);}
832
833 if (!strcmp(theOp, "-force"))
834 FMSG("-force is no longer supported; use --retry instead!",22);
835
836 return 0;
837}
838
839/******************************************************************************/
840/* Private: L i c e n s e */
841/******************************************************************************/
842
843void XrdCpConfig::License()
844{
845 const char *theLicense =
846#include "../../LICENSE"
847;
848
849 std::cerr <<theLicense;
850 exit(0);
851}
852
853/******************************************************************************/
854/* Private: O p N a m e */
855/******************************************************************************/
856
857const char *XrdCpConfig::OpName()
858{
859 extern int optind, optopt;
860 static char oName[4] = {'-', 0, 0, 0};
861
862 if (!optopt || optopt == '-' || *(Argv[optind-1]+1) == '-')
863 return Argv[optind-1];
864 oName[1] = optopt;
865 return oName;
866}
867
868/******************************************************************************/
869/* p r o c F i l e */
870/******************************************************************************/
871
872void XrdCpConfig::ProcFile(const char *fname)
873{
874 int rc;
875
876// Chain in this file in the input list
877//
878 pLast->Next = pFile = new XrdCpFile(fname, rc);
879 if (rc) FMSG("Invalid url, '" <<fname <<"'.", 22);
880
881// For local files, make sure it exists and get its size
882//
883 if (pFile->Protocol == XrdCpFile::isFile && (rc = pFile->Resolve()))
884 FMSG(XrdSysE2T(rc) <<" processing " <<pFile->Path, 2);
885
886// Process file based on type (local or remote)
887//
888 if (pFile->Protocol == XrdCpFile::isFile) totBytes += pFile->fSize;
889 else if (pFile->Protocol == XrdCpFile::isDir)
890 {if (!(OpSpec & DoRecurse))
891 FMSG(pFile->Path <<" is a directory.", 2);
892 }
893 else if (pFile->Protocol == XrdCpFile::isStdIO)
894 {if (Opts & optNoStdIn)
895 FMSG("Using stdin as a source is disallowed.", 22);
896 if (numFiles)
897 FMSG("Multiple sources disallowed with stdin.", 22);
898 }
899 else if (!((pFile->Protocol == XrdCpFile::isXroot) ||
900 (pFile->Protocol == XrdCpFile::isXroots) ||
901 (pFile->Protocol == XrdCpFile::isHttp) ||
902 (pFile->Protocol == XrdCpFile::isHttps) ||
903 (pFile->Protocol == XrdCpFile::isPelican) ||
904 (pFile->Protocol == XrdCpFile::isS3)))
905 {FMSG(pFile->ProtName <<" file protocol is not supported.", 22)}
906 else if (OpSpec & DoRecurse && !(Opts & optRmtRec))
907 {FMSG("Recursive copy from a remote host is not supported.",22)}
908 else isLcl = 0;
909
910// Update last pointer and we are done if this is stdin
911//
912 numFiles++;
913 pLast = pFile;
914}
915
916/******************************************************************************/
917/* U s a g e */
918/******************************************************************************/
919
920void XrdCpConfig::Usage(int rc)
921{
922 static const char *Syntax = "\n"
923 "Usage: xrdcp [<options>] <src> [<src> [. . .]] <dest>\n";
924
925 static const char *Syntax1= "\n"
926 "Usage: xrdcp [<options>] <src> <dest>\n";
927
928 static const char *Options= "\n"
929 "Options: [--cksum <args>] [--coerce] [--continue]\n"
930 " [--debug <lvl>] [--dynamic-src] [--force] [--help]\n"
931 " [--infiles <fn>] [--license] [--nopbar] [--notlsok]\n"
932 " [--parallel <n>] [--posc] [--proxy <host>:<port>]\n"
933 " [--recursive] [--retry <n>] [--retry-policy <force|continue>]\n"
934 " [--rm-bad-cksum] [--server] [--silent] [--sources <n>]\n"
935 " [--streams <n>] [--tlsmetalink] [--tlsnodata]\n"
936 " [--tpc [delegate] {first|only}] [--verbose] [--version]\n"
937 " [--xattr] [--xrate <rate>] [--xrate-threshold <rate>]\n"
938 " [--zip <file>] [--zip-append] [--zip-mtln-cksum]\n";
939
940 static const char *Syntax2= "\n"
941 "<src>: [[x]root[s]://<host>[:<port>]/]<path> | -";
942
943 static const char *Syntay2= "\n"
944 "<src>: [[x]root[s]://<host>[:<port>]/]<path>";
945
946 static const char *Syntax3= "\n"
947 "<dest>: [[x]root[s]://<host>[:<port>]/]<path> | -";
948
949 static const char *Detail = "\n"
950 "Note: using a dash (-) for <src> uses stdin and for <dest> stdout\n\n"
951 "-C | --cksum <args> verifies the checksum at the destination as provided\n"
952 " by the source server or locally computed. The args are\n"
953 " <ckstype>[:{<value>|print|source}]\n"
954 " where <ckstype> is one of adler32, crc32, crc32c, md5,\n"
955 " zcrc32 or auto. If 'auto' is chosen, xrdcp will try to\n"
956 " automatically infer the right checksum type based on the\n"
957 " source/destination configuration, source file type\n"
958 " (e.g. metalink, ZIP), and available checksum plug-ins.\n"
959 " If the hex value of the checksum is given, it is used.\n"
960 " Otherwise, the server's checksum is used for remote files\n"
961 " and computed for local files. Specifying print merely\n"
962 " prints the checksum but does not verify it.\n"
963 "-F | --coerce coerces the copy by ignoring file locking semantics\n"
964 " --continue continue copying a file from the point where the previous\n"
965 " copy was interrupted\n"
966 "-d | --debug <lvl> sets the debug level: 0 off, 1 low, 2 medium, 3 high\n"
967 "-Z | --dynamic-src file size may change during the copy\n"
968 "-f | --force replaces any existing output file\n"
969 "-h | --help prints this information\n"
970 "-I | --infiles <fname> specifies the file that contains a list of input files\n"
971 "-H | --license prints license terms and conditions\n"
972 "-N | --nopbar does not print the progress bar\n"
973 " --notlsok if server is too old to support TLS encryption fallback\n"
974 " to unencrypted communication\n"
975 " --parallel <n> number of files to copy at the same time\n"
976 "-P | --posc enables persist on successful close semantics\n"
977 "-D | --proxy <host>:<port> uses the specified SOCKS4 proxy connection\n"
978 "-r | --recursive recursively copies all source files\n"
979 "-t | --retry <n> maximum number of times to retry failed copy-jobs\n"
980 " --retry-policy <policy> retry policy: force or continue\n"
981 " --rm-bad-cksum remove the target file if checksum verification failed\n"
982 " (enables also POSC semantics)\n"
983 " --server runs in a server environment with added operations\n"
984 "-s | --silent produces no output other than error messages\n"
985 "-y | --sources <n> uses up to the number of sources specified in parallel\n"
986 "-S | --streams <n> copies using the specified number of TCP connections\n"
987 " --tlsmetalink convert [x]root to [x]roots protocol in metalinks\n"
988 "-E | --tlsnodata in case of [x]roots protocol, encrypt only the control\n"
989 " stream and leave the data streams unencrypted\n"
990 "-T | --tpc <args> uses third party copy mode between the src and dest.\n"
991 " Both the src and dest must allow tpc mode. Argument\n"
992 " 'first' tries tpc and if it fails, does a normal copy;\n"
993 " while 'only' fails the copy unless tpc succeeds.\n"
994 "-v | --verbose produces more information about the copy\n"
995 "-V | --version prints the version number\n"
996 " --xattr preserve extended attributes\n"
997 "-X | --xrate <rate> limits the transfer to the specified rate. You can\n"
998 " suffix the value with 'k', 'm', or 'g'\n"
999 " --xrate-threshold <rate> If the transfer rate drops below given threshold force\n"
1000 " the client to use different source or if no more sources\n"
1001 " are available fail the transfer. You can suffix the value\n"
1002 " with 'k', 'm', or 'g'\n"
1003 "-z | --zip <file> treat the source as a ZIP archive containing given file\n"
1004 " --zip-append append file to existing zip archive\n"
1005 " --zip-mtln-cksum use the checksum available in a metalink file even if\n"
1006 " a file is being extracted from a ZIP archive\n"
1007 "\n"
1008 "Legacy options: [-adler] [-DI<var> <val>] [-DS<var> <val>] [-np]\n"
1009 " [-md5] [-OD<cgi>] [-OS<cgi>] [-version] [-x]";
1010
1011 std::cerr <<(Opts & opt1Src ? Syntax1 : Syntax) <<Options;
1012 std::cerr <<(Opts & optNoStdIn ? Syntay2 : Syntax2) <<Syntax3 <<std::endl;
1013 if (!rc) std::cerr <<Detail <<std::endl;
1014 exit(rc);
1015}
int inList(const char *var, const char **Vec)
#define OPT_TYPE
#define ZMSG(x)
#define FMSG(x, y)
#define EMSG(x)
#define UMSG(x)
static XrdSysError eDest(0,"crypto_")
static XrdVERSIONINFODEF(compiledVer, XrdHttpProtocolTest, XrdVNUMBER, XrdVERSION)
int optopt
int optind
#define open
Definition XrdPosix.hh:76
XrdOucString Path
struct myOpts opts
const char * XrdSysE2T(int errcode)
Definition XrdSysE2T.cc:104
static const int NameSize
Definition XrdCksData.hh:41
static const uint64_t OpVerbose
static const uint64_t OpXAttr
static const uint64_t DoProxy
defVar * intDefs
void Config(int argc, char **argv, int Opts=0)
static const uint64_t OpRecurse
XrdCksCalc * CksObj
static const uint64_t OpContinue
static const uint64_t DoRetry
static const uint64_t DoStreams
std::vector< std::string > AddCksVal
static const uint64_t OpParallel
static const uint64_t OpZipAppend
const char * dstOpq
static const uint64_t DoZipMtlnCksum
XrdCksData CksData
static const uint64_t OpSilent
static const uint64_t OpTlsMLF
static const uint64_t OpNoTlsOK
XrdCks * CksMan
XrdCpFile * srcFile
const char * Pgm
XrdCpFile * dstFile
static const uint64_t OpRetryPolicy
static const uint64_t OpDebug
char * zipFile
XrdCpConfig(const char *pgname)
static const uint64_t OpRetry
static const uint64_t DoNoPbar
static const uint64_t OpXrateThreshold
static const uint64_t DoCoerce
static const uint64_t OpServer
static const uint64_t DoForce
static const uint64_t OpVersion
static const uint64_t OpTpc
static const uint64_t DoParallel
static const uint64_t DoRmOnBadCksum
static const uint64_t DoNoTlsOK
static const uint64_t OpPath
long long OpSpec
static const uint64_t OpRecursv
static const uint64_t OpProxy
static const uint64_t DoTpc
static const uint64_t DoDebug
static const uint64_t OpSources
static const uint64_t DoCksum
defVar * strDefs
static const uint64_t OpPosc
static const uint64_t DoXrate
static const uint64_t DoCksrc
static const uint64_t OpZipMtlnCksum
static const int opt1Src
static const uint64_t OpXrate
static const uint64_t DoTpcDlgt
static const uint64_t DoZip
static const uint64_t DoVerbose
static const uint64_t DoContinue
static const uint64_t OpCksum
const char * CksVal
static const uint64_t DoRecurse
static const uint64_t OpTlsNoData
static const int optNoLclCp
static const uint64_t DoXrateThreshold
const char * srcOpq
static const uint64_t DoZipAppend
static const uint64_t OpIfile
static const uint64_t DoDynaSrc
long long xRate
static const int optNoStdIn
static const uint64_t OpNoPbar
static const uint64_t DoSources
static const uint64_t DoSilent
static const uint64_t OpForce
static const uint64_t OpRmOnBadCksum
static const uint64_t OpDynaSrc
static const uint64_t DoXAttr
static const int optNoXtnd
static const uint64_t DoTlsMLF
static const uint64_t OpZip
static const uint64_t OpLicense
static const uint64_t DoIfile
static const uint64_t OpHelp
static XrdSysError * Log
static const int optRmtRec
static const uint64_t DoRetryPolicy
std::string RetryPolicy
static const uint64_t DoPath
static const uint64_t DoPosc
static const uint64_t OpStreams
long long xRateThreshold
static const uint64_t OpCoerce
long long totBytes
static const uint64_t DoTpcOnly
static const uint64_t DoTlsNoData
static const uint64_t DoServer
static const uint64_t DoCkprt
static void SetMsgPfx(const char *pfx)
Definition XrdCpFile.hh:57
XrdCpFile * Next
Definition XrdCpFile.hh:44
static XrdSysLogger Logger