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