-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdCpy.cc
1657 lines (1358 loc) · 51.1 KB
/
XrdCpy.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************/
/* */
/* X r d C p y . c c */
/* */
/* (c) 2012 by the Board of Trustees of the Leland Stanford, Jr., University */
/* All Rights Reserved */
/* Author: Fabrizio Furano (INFN Padova, 2004) */
/* Modified by Andrew Hanushevsky (2012) under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/* */
/* This file is part of the XRootD software suite. */
/* */
/* XRootD is free software: you can redistribute it and/or modify it under */
/* the terms of the GNU Lesser General Public License as published by the */
/* Free Software Foundation, either version 3 of the License, or (at your */
/* option) any later version. */
/* */
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
/* License for more details. */
/* */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
/* */
/* The copyright holder's institutional names and contributor's names may not */
/* be used to endorse or promote products derived from this software without */
/* specific prior written permission of the institution or contributor. */
/* Author: Fabrizio Furano (INFN Padova, 2004) */
/* Modified by Andrew Hanushevsky (2012) under contract */
/* DE-AC02-76-SFO0515 with the Department of Energy */
/******************************************************************************/
//////////////////////////////////////////////////////////////////////////
// //
// A cp-like command line tool for xrootd environments //
// //
//////////////////////////////////////////////////////////////////////////
#include "XrdClient/XrdClientUrlInfo.hh"
#include "XrdClient/XrdClientReadCache.hh"
#include "XrdSys/XrdSysPthread.hh"
#include "XrdClient/XrdClient.hh"
#include "XrdClient/XrdCpMthrQueue.hh"
#include "XrdClient/XrdClientConn.hh"
#include "XrdClient/XrdClientDebug.hh"
#include "XrdClient/XrdCpWorkLst.hh"
#include "XrdClient/XrdClientEnv.hh"
#include "XrdSys/XrdSysPlatform.hh"
#include "XrdClient/XrdClientAbsMonIntf.hh"
#include "XrdClient/XrdcpXtremeRead.hh"
#include "XrdCks/XrdCks.hh"
#include "XrdCks/XrdCksCalc.hh"
#include "XrdCks/XrdCksData.hh"
#include "XrdApps/XrdCpConfig.hh"
#include "XrdApps/XrdCpFile.hh"
#include "XrdOuc/XrdOucTokenizer.hh"
#include "XrdOuc/XrdOucTPC.hh"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sstream>
#ifndef WIN32
#include <sys/time.h>
#include <unistd.h>
#include <dlfcn.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <sstream>
#ifdef HAVE_LIBZ
#include <zlib.h>
#endif
/******************************************************************************/
/* G l o b a l C o n f i g u r a t i o n */
/******************************************************************************/
namespace XrdCopy
{
XrdCpConfig Config("xrdcp");
XrdCksData srcCksum, dstCksum;
XrdCksCalc *csObj;
XrdClient *tpcSrc;
char tpcKey[32];
long long tpcFileSize;
pthread_t tpcTID;
int tpcPB;
int isSrv;
int isTPC;
int getCks;
int lenCks;
int prtCks;
int setCks;
int verCks;
int xeqCks;
static const int rwMode = kXR_ur | kXR_uw | kXR_gw | kXR_gr | kXR_or;
}
using namespace XrdCopy;
#define EMSG(x) {if (isSrv) cout <<Config.Pgm <<": " <<x <<endl;\
else cerr <<Config.Pgm <<": " <<x <<endl;}
extern "C" {
/////////////////////////////////////////////////////////////////////
// function + macro to allow formatted print via cout,cerr
/////////////////////////////////////////////////////////////////////
void cout_print(const char *format, ...)
{
char cout_buff[4096];
va_list args;
va_start(args, format);
vsprintf(cout_buff, format, args);
va_end(args);
cout << cout_buff;
}
void cerr_print(const char *format, ...)
{
char cerr_buff[4096];
va_list args;
va_start(args, format);
vsprintf(cerr_buff, format, args);
va_end(args);
cerr <<cerr_buff;
}
#define COUT(s) do { \
cout_print s; \
} while (0)
#define CERR(s) do { \
cerr_print s; \
} while (0)
}
//////////////////////////////////////////////////////////////////////
struct XrdCpInfo {
XrdClient *XrdCli;
int localfile;
long long len, bread, bwritten;
XrdCpMthrQueue queue;
XrdClientAbsMonIntf *mon;
XrdCpInfo() : XrdCli(0),localfile(0),len(0),bread(0),bwritten(0),mon(0) {}
} cpnfo;
#define XRDCP_BLOCKSIZE (8*1024*1024)
#define XRDCP_XRDRASIZE (30*XRDCP_BLOCKSIZE)
#define XRDCP_VERSION "(C) 2004-2011 by the XRootD collaboration. Version: "XrdVSTRING
///////////////////////////////////////////////////////////////////////
// Coming from parameters on the cmd line
bool summary=false; // print summary
bool progbar=true; // print progbar
bool Verbose=true; // be verbose
XrdOucString monlibname = "libXrdCpMonitorClient.so"; // Default name for the ext monitoring lib
// Default open flags for opening a file (xrd)
kXR_unt16 xrd_wr_flags=kXR_async | kXR_mkpath | kXR_open_updt | kXR_new;
// Flags for open() to force overwriting or not. Default is not.
#define LOC_WR_FLAGS_FORCE ( O_CREAT | O_WRONLY | O_TRUNC | O_BINARY );
#define LOC_WR_FLAGS ( O_CREAT | O_WRONLY | O_EXCL | O_BINARY );
int loc_wr_flags = LOC_WR_FLAGS;
bool recurse = false;
bool doXtremeCp = false;
XrdOucString XtremeCpRdr;
///////////////////////
// To compute throughput etc
struct timeval abs_start_time;
struct timeval abs_stop_time;
struct timezone tz;
/******************************************************************************/
/* p r i n t _ s u m m a r y */
/******************************************************************************/
void print_summary(const char* src, const char* dst, unsigned long long bytesread)
{
gettimeofday (&abs_stop_time, &tz);
float abs_time=((float)((abs_stop_time.tv_sec- abs_start_time.tv_sec)*1000 +
(abs_stop_time.tv_usec-abs_start_time.tv_usec)/1000));
XrdOucString xsrc(src);
XrdOucString xdst(dst);
xsrc.erase(xsrc.rfind('?'));
xdst.erase(xdst.rfind('?'));
COUT(("[xrdcp] #################################################################\n"));
COUT(("[xrdcp] # Source Name : %s\n",xsrc.c_str()));
COUT(("[xrdcp] # Destination Name : %s\n",xdst.c_str()));
COUT(("[xrdcp] # Data Copied [bytes] : %lld\n",bytesread));
COUT(("[xrdcp] # Realtime [s] : %f\n",abs_time/1000.0));
if (abs_time > 0) {
COUT(("[xrdcp] # Eff.Copy. Rate[MB/s] : %f\n",bytesread/abs_time/1000.0));
}
if (xeqCks)
{static const int Bsz = 64;
char Buff[Bsz];
dstCksum.Get(Buff,Bsz);
COUT(("[xrdcp] # %8s : %s\n", dstCksum.Name, Buff));
}
COUT(("[xrdcp] #################################################################\n"));
}
/******************************************************************************/
/* p r i n t _ p r o g b a r */
/******************************************************************************/
void print_progbar(unsigned long long bytesread, unsigned long long size) {
CERR(("[xrootd] Total %.02f MB\t|",(float)size/1024/1024));
for (int l=0; l< 20;l++) {
if (l< ( (int)(20.0*bytesread/size)))
CERR(("="));
if (l==( (int)(20.0*bytesread/size)))
CERR((">"));
if (l> ( (int)(20.0*bytesread/size)))
CERR(("."));
}
float abs_time=((float)((abs_stop_time.tv_sec - abs_start_time.tv_sec) *1000 +
(abs_stop_time.tv_usec - abs_start_time.tv_usec) / 1000));
CERR(("| %.02f %% [%.01f MB/s]\r",100.0*bytesread/size,bytesread/abs_time/1000.0));
}
/******************************************************************************/
/* p r i n t _ c h k s u m */
/******************************************************************************/
void print_chksum(const char* src, unsigned long long bytesread)
{
char Buff[64];
dstCksum.Get(Buff, sizeof(Buff));
XrdOucString xsrc(src);
xsrc.erase(xsrc.rfind('?'));
cout <<dstCksum.Name <<": " <<Buff <<' ' <<xsrc <<' ' <<bytesread <<endl;
}
/******************************************************************************/
/* d o P r o g B a r */
/******************************************************************************/
void *doProgBar(void *Parm)
{
XrdClientUrlInfo *dUrl = (XrdClientUrlInfo *)Parm;
XrdClientAdmin Adm(dUrl->GetUrl().c_str());
const char *fName = dUrl->File.c_str();
long long fSize;
long id, flags, mtime;
// Prevent cancelation as the admin client can't handle that
//
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 0);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
// Open a path to the destination
//
if (!Adm.Connect()) return 0;
// Print the progress bar until we are canceled
//
while(Adm.Stat(fName, id, fSize, flags, mtime))
{gettimeofday(&abs_stop_time,&tz);
print_progbar(fSize, tpcFileSize);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_testcancel();
sleep(3);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
}
// All done
//
return 0;
}
/******************************************************************************/
/* u n d o P r o g B a r */
/******************************************************************************/
void undoProgBar(int isOK)
{
void *thret;
if (tpcPB)
{tpcPB = 0;
pthread_cancel(tpcTID);
pthread_join(tpcTID, &thret);
if (isOK)
{gettimeofday(&abs_stop_time,&tz);
print_progbar(tpcFileSize, tpcFileSize);
}
cerr <<endl;
}
}
/******************************************************************************/
/* c p F a t a l */
/******************************************************************************/
int cpFatal(const char *Act, XrdClient *cSrc, XrdClient *cDst)
{
XrdClient *cObj;
const char *Msg;
if (tpcPB) undoProgBar(0);
if (cSrc) {cObj = cSrc; Msg = "Copy from ";}
else {cObj = cDst; Msg = "Copy to ";}
EMSG(Msg <<cObj->GetCurrentUrl().Host.c_str() <<" failed on " <<Act <<"!");
EMSG(ServerError(cObj));
return -1;
}
/******************************************************************************/
/* g e t F N a m e */
/******************************************************************************/
const char *getFName(const char *Url)
{
static char fBuff[2048];
const char *Qmark = index(Url, '?');
if (!Qmark) return Url;
int n = (Qmark - Url);
if (n >= (int)sizeof(fBuff)) n = sizeof(fBuff)-1;
strncpy(fBuff, Url, n);
fBuff[n] = 0;
return fBuff;
}
/******************************************************************************/
/* R e a d e r T h r e a d _ x r d */
/******************************************************************************/
// The body of a thread which reads from the global
// XrdClient and keeps the queue filled
//____________________________________________________________________________
void *ReaderThread_xrd(void *)
{
Info(XrdClientDebug::kHIDEBUG,
"ReaderThread_xrd",
"Reader Thread starting.");
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 0);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
void *buf;
long long offs = 0;
int nr = 1;
long long bread = 0, len = 0;
long blksize;
len = cpnfo.len;
while ((nr > 0) && (offs < len)) {
buf = malloc(XRDCP_BLOCKSIZE);
if (!buf) {
EMSG("Copy failed; out of memory.");
_exit(13);
}
blksize = xrdmin(XRDCP_BLOCKSIZE, len-offs);
if ( (nr = cpnfo.XrdCli->Read(buf, offs, blksize)) ) {
cpnfo.queue.PutBuffer(buf, offs, nr);
cpnfo.XrdCli->RemoveDataFromCache(offs, offs+nr-1, false);
bread += nr;
offs += nr;
}
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_testcancel();
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
}
cpnfo.bread = bread;
// This ends the transmission... bye bye
cpnfo.queue.PutBuffer(0, 0, 0);
return 0;
}
/******************************************************************************/
/* R e a d e r T h r e a d _ x r d _ x t r e m e */
/******************************************************************************/
// The body of a thread which reads from the global
// XrdClient and keeps the queue filled
// This is the thread for extreme reads, in this case we may have multiple of these
// threads, reading the same file from different server endpoints
//____________________________________________________________________________
struct xtreme_threadnfo {
XrdXtRdFile *xtrdhandler;
// The client used by this thread
XrdClient *cli;
// A unique integer identifying the client instance
int clientidx;
// The block from which to start prefetching/reading
int startfromblk;
// Max convenient number of outstanding blks
int maxoutstanding;
};
void *ReaderThread_xrd_xtreme(void *parm)
{
Info(XrdClientDebug::kHIDEBUG,
"ReaderThread_xrd_xtreme",
"Reader Thread starting.");
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 0);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
void *buf;
int nr = 1;
int noutstanding = 0;
// Which block to read
XrdXtRdBlkInfo *blknfo = 0;
xtreme_threadnfo *thrnfo = (xtreme_threadnfo *)parm;
// Block to prefetch
int lastprefetched = thrnfo->startfromblk;
int lastread = lastprefetched;
thrnfo->cli->Open(0, 0, true);
thrnfo->cli->SetCacheParameters(XRDCP_BLOCKSIZE*4*thrnfo->maxoutstanding*2, 0, XrdClientReadCache::kRmBlk_FIFO);
if (thrnfo->cli->IsOpen_wait())
while (nr > 0) {
// Keep always some blocks outstanding from the point of view of this reader
while (noutstanding < thrnfo->maxoutstanding) {
int lp;
lp = thrnfo->xtrdhandler->GetBlkToPrefetch(lastprefetched, thrnfo->clientidx, blknfo);
if (lp >= 0) {
//cout << "cli: " << thrnfo->clientidx << " prefetch: " << lp << " offs: " << blknfo->offs << " len: " << blknfo->len << endl;
if ( thrnfo->cli->Read_Async(blknfo->offs, blknfo->len) == kOK ) {
lastprefetched = lp;
noutstanding++;
}
else break;
}
else break;
}
int lr = thrnfo->xtrdhandler->GetBlkToRead(lastread, thrnfo->clientidx, blknfo);
if (lr >= 0) {
buf = malloc(blknfo->len);
if (!buf) {
EMSG("Copy failed; out of memory.");
_exit(13);
}
//cout << "cli: " << thrnfo->clientidx << " read: " << lr << " offs: " << blknfo->offs << " len: " << blknfo->len << endl;
// It is very important that the search for a blk to read starts from the first block upwards
nr = thrnfo->cli->Read(buf, blknfo->offs, blknfo->len);
if ( nr >= 0 ) {
lastread = lr;
noutstanding--;
// If this block was stolen by somebody else then this client has to be penalized
// If this client stole the blk to some other client, then this client has to be rewarded
int reward = thrnfo->xtrdhandler->MarkBlkAsRead(lr);
if (reward >= 0)
// Enqueue the block only if it was not already read
cpnfo.queue.PutBuffer(buf, blknfo->offs, nr);
if (reward > 0) {
thrnfo->maxoutstanding++;
thrnfo->maxoutstanding = xrdmin(20, thrnfo->maxoutstanding);
thrnfo->cli->SetCacheParameters(XRDCP_BLOCKSIZE*4*thrnfo->maxoutstanding*2, 0, XrdClientReadCache::kRmBlk_FIFO);
}
if (reward < 0) {
thrnfo->maxoutstanding--;
free(buf);
}
if (thrnfo->maxoutstanding <= 0) {
sleep(1);
thrnfo->maxoutstanding = 1;
}
}
// It is very important that the search for a blk to read starts from the first block upwards
thrnfo->cli->RemoveDataFromCache(blknfo->offs, blknfo->offs+blknfo->len-1, false);
}
else {
if (thrnfo->xtrdhandler->AllDone()) break;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
sleep(1);
}
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
pthread_testcancel();
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
}
// We get here if there are no more blocks to read or to steal from other readers
// This ends the transmission... bye bye
cpnfo.queue.PutBuffer(0, 0, 0);
return 0;
}
/******************************************************************************/
/* R e a d e r T h r e a d _ l o c */
/******************************************************************************/
// The body of a thread which reads from the global filehandle
// and keeps the queue filled
//____________________________________________________________________________
void *ReaderThread_loc(void *) {
Info(XrdClientDebug::kHIDEBUG,
"ReaderThread_loc",
"Reader Thread starting.");
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, 0);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
void *buf;
long long offs = 0;
int nr = 1;
long long bread = 0;
while (nr > 0) {
buf = malloc(XRDCP_BLOCKSIZE);
if (!buf) {
EMSG("Copy failed; out of memory.");
_exit(13);
}
//------------------------------------------------------------------------
// If this read fails it means that either the program logic is
// flawed, or there was a low level hardware failure. In either case
// continuing may cause more harm than good.
//------------------------------------------------------------------------
nr = read( cpnfo.localfile, buf, XRDCP_BLOCKSIZE );
if( nr < 0 )
{
EMSG(strerror( errno ) <<" reading local file.");
_exit(17);
}
if( nr > 0)
{
cpnfo.queue.PutBuffer(buf, offs, nr);
bread += nr;
offs += nr;
}
}
cpnfo.bread = bread;
// This ends the transmission... bye bye
cpnfo.queue.PutBuffer(0, 0, 0);
return 0;
}
/******************************************************************************/
/* C r e a t e D e s t P a t h _ l o c */
/******************************************************************************/
int CreateDestPath_loc(XrdOucString path, bool isdir) {
// We need the path name without the file
if (!isdir) {
int pos = path.rfind('/');
if (pos != STR_NPOS)
path.erase(pos);
else path = "";
}
if (path != "")
return ( MAKEDIR(
path.c_str(),
S_IRUSR | S_IWUSR | S_IXUSR |
S_IRGRP | S_IWGRP | S_IXGRP |
S_IROTH | S_IXOTH)
);
else
return 0;
}
/******************************************************************************/
/* g e t C k s u m */
/******************************************************************************/
int getCksum(XrdCksData &cksData, const char *Path)
{
const char *Lfn;
char *csResp, *tP;
// Point to absolute path
// 0123456
if (!strcmp(Path, "root://")) Lfn = Path + 7;
else Lfn = Path + 8;
if ((Lfn = index(Lfn, '/'))) Lfn++;
else Lfn = Path;
// Get the checksum from the server
//
XrdClientAdmin Adm(Path);
if (!(Adm.Connect()) || !(Adm.GetChecksum((kXR_char *)Lfn,(kXR_char **)&csResp)))
{EMSG("Unable to obtain checksum for '"<< getFName(Path) <<"'.");
EMSG(Adm.LastServerError()->errmsg);
return 0;
}
// Get checksum name and make sure it matches
//
XrdOucTokenizer csData(csResp);
csData.GetLine();
if ((tP = csData.GetToken()) && strcmp(tP, Config.CksData.Name))
{EMSG("Only " <<tP <<" checksums supported by "
<<Adm.GetCurrentUrl().Host.c_str());
free(csResp);
return 0;
}
// Get the token value
//
if (tP && (tP = csData.GetToken()) && !cksData.Set(tP, strlen(tP))) tP = 0;
// Check that all went well
//
if (!tP) EMSG("Invalid checksum returned for '" <<getFName(Path) <<"'.");
// Return result
//
free(csResp);
return (tP ? 1 : 0);
}
/******************************************************************************/
/* v a l T P C */
/******************************************************************************/
int valTPC(XrdClient *cObj, int isDest)
{
kXR_char qArg[4];
kXR_char respBuff[128];
// Ask the sever if it supports tpc
//
strcpy((char *)qArg, "tpc");
if (cObj->Query(kXR_Qconfig, qArg, respBuff, sizeof(respBuff))
&& isdigit(*respBuff) && atoi((const char *)respBuff) > 0) return 1;
// Nope, we don't support this
//
EMSG("Host " <<cObj->GetCurrentUrl().Host.c_str()
<<" does not support third party copies.");
// If we are the destination, unlink any partially created file
//
if (isDest)
{XrdClientAdmin Adm(cObj->GetCurrentUrl().GetUrl().c_str());
if (Adm.Connect()) Adm.Rm(cObj->GetCurrentUrl().File.c_str());
// cerr <<cObj->GetCurrentUrl().GetUrl().c_str() <<endl;
// cerr <<cObj->GetCurrentUrl().File.c_str() <<endl;
}
return 0;
}
/******************************************************************************/
/* g e n D e s t C g i */
/******************************************************************************/
char *genDestCgi(XrdClient *xrdsrc, const char *src)
{
union {int *intP;
int intV[2];
} iKey;
XrdClientStatInfo stat;
XrdOucString dCGI;
int myKey[3], xTTL = 0;
const char *Path, *cksVal, *cgiP;
char *qP, aszBuff[128], lfnBuff[1032], cgiBuff[2048];
// Make sure that the source supports 3rd party copy
//
if (!valTPC(xrdsrc, 0)) return 0;
// Extract out the source lfn
//
Path = src + (*src == 'x' ? 8 : 7);
if (!(Path = index(Path, '/')))
{EMSG("Unable to extract lfn from '" <<getFName(src) <<"'."); return 0;}
strncpy(lfnBuff, Path+1, sizeof(lfnBuff));
lfnBuff[sizeof(lfnBuff)-1] = 0;
if ((qP = index(lfnBuff, '?'))) *qP = 0;
// Generate a key
//
gettimeofday(&abs_start_time,&tz);
myKey[0] = abs_start_time.tv_usec;
myKey[1] = getpid() | (getppid() << 16);
iKey.intP = &myKey[0];
myKey[2] = iKey.intV[0] ^ iKey.intV[1];
sprintf(tpcKey, "%08x%08x%08x", myKey[0], myKey[1], myKey[2]);
// Check if we should add checksum information
//
cksVal = (verCks ? Config.CksVal : 0);
// Generate the cgi for the destination
//
std::ostringstream o; o << xrdsrc->GetCurrentUrl().Host.c_str() << ":";
o << xrdsrc->GetCurrentUrl().Port;
cgiP = XrdOucTPC::cgiC2Dst(tpcKey, o.str().c_str(),
lfnBuff, cksVal, cgiBuff, sizeof(cgiBuff));
if (*cgiP == '!')
{EMSG("Unable to setup destination url. " <<cgiP+1); return 0;}
// Start the url with the size hint
//
xrdsrc->Stat(&stat);
tpcFileSize = static_cast<long long>(stat.size);
sprintf(aszBuff, "?oss.asize=%lld&", tpcFileSize);
dCGI = aszBuff;
// Add all other information
//
if (Config.dstOpq) {dCGI += Config.dstOpq; dCGI += '&';}
dCGI += cgiBuff;
// cerr <<"Dest url: " <<dCGI.c_str() <<endl;
// All done
//
return strdup(dCGI.c_str());
}
/******************************************************************************/
/* d o C p _ x r d 3 x r d */
/******************************************************************************/
int doCp_xrd3xrd(XrdClient *xrddest, const char *src, const char *dst)
{
struct sdHelper
{XrdClient *Src;
sdHelper() : Src(0) {}
~sdHelper() {if (Src) delete Src;}
} Client;
XrdClientUrlInfo dUrl;
XrdOucString sUrl(tpcSrc->GetCurrentUrl().GetUrl().c_str());
XrdOucString *rCGI, dstUrl;
pthread_t myTID;
int xTTL = -1;
const char *cgiP;
char *qP;
char cgiBuff[1024];
// Append any redirection cgi information to our source spec
//
rCGI = &(tpcSrc->GetClientConn()->fRedirCGI);
if (rCGI->length() > 0)
{if (sUrl.find("?") == STR_NPOS) sUrl += '?';
else sUrl += '&';
sUrl += *rCGI;
}
//cerr <<"tpc: bfr src=" <<src <<endl;
//cerr <<"tpc: bfr dst=" <<dst <<endl;
//cerr <<"tpc: bfr scl=" <<tpcSrc->GetCurrentUrl().GetUrl().c_str() <<endl;
//cerr <<"tpc: bfr dcl=" <<xrddest->GetCurrentUrl().GetUrl().c_str() <<endl;
// Verify that the destination supports 3rd party stuff
//
if (!valTPC(xrddest, 1)) return 8;
// Generate source cgi string
//
cgiP = XrdOucTPC::cgiC2Src(tpcKey, xrddest->GetCurrentUrl().Host.c_str(),
xTTL, cgiBuff, sizeof(cgiBuff));
if (*cgiP == '!')
{EMSG("Unable to setup source url. " <<cgiP+1); return 8;}
// Add the cgi string to the source
//
if (sUrl.find("?") == STR_NPOS) sUrl += '?';
else sUrl += '&';
sUrl += cgiBuff;
//cerr <<"tpc: aft scl=" <<sUrl.c_str()<<endl;
//cerr <<"tpc: aft dcl=" <<xrddest->GetCurrentUrl().GetUrl().c_str() <<endl;
// Open the source
//
Client.Src = new XrdClient(sUrl.c_str());
if ((!Client.Src->Open(0, kXR_async) ||
(Client.Src->LastServerResp()->status != kXR_ok)))
return cpFatal("open", Client.Src, 0);
// Start the progress bar if so wanted
//
tpcPB = !Config.Want(XrdCpConfig::DoNoPbar);
if (tpcPB)
// {dUrl = xrddest->GetCurrentUrl();
//cerr <<"tpc: pbr dcl=" <<dst<<endl;
{dstUrl = dst; dUrl = dstUrl;
tpcPB = !XrdSysThread::Run(&tpcTID, doProgBar, (void *)&dUrl,
XRDSYSTHREAD_HOLD);
}
// Now do a sync operation on the destination
//
if (!xrddest->Sync()) return cpFatal("rendezvous", 0, xrddest);
// One more sync will start the copy
//
gettimeofday(&abs_start_time,&tz);
if (!xrddest->Sync()) return cpFatal("sync", 0, xrddest);
// Stop the progress bar
//
if (tpcPB) undoProgBar(1);
// Close the file
//
if(!xrddest->Close()) return cpFatal("close", 0, xrddest);
// Do checksum processing
//
if (xeqCks && prtCks)
{if (!getCksum(dstCksum, dst)) {EMSG("Unable to print checksum!")}
else print_chksum(src, tpcFileSize);
if (summary) print_summary(src, dst, tpcFileSize);
}
// All done
//
return 0;
}
/******************************************************************************/
/* d o C p _ x r d 2 x r d */
/******************************************************************************/
int doCp_xrd2xrd(XrdClient **xrddest, const char *src, const char *dst) {
// ----------- xrd to xrd affair
pthread_t myTID;
XrdClientVector<pthread_t> myTIDVec;
void *thret;
XrdClientStatInfo stat;
int retvalue = 0;
// If we need to verify checksums then we will need to get the checksum
// from the source unless a specific checksum has been specified.
//
if (verCks && getCks && !getCksum(srcCksum, src)) return -ENOTSUP;
gettimeofday(&abs_start_time,&tz);
// Open the input file (xrdc)
// If Xrdcli is non-null, the correct src file has already been opened
if (!cpnfo.XrdCli)
{cpnfo.XrdCli = new XrdClient(src);
if ( ( !cpnfo.XrdCli->Open(0, kXR_async) ||
(cpnfo.XrdCli->LastServerResp()->status != kXR_ok) ) )
{cpFatal("open", cpnfo.XrdCli, 0);
delete cpnfo.XrdCli;
cpnfo.XrdCli = 0;
return 1;
}
}
cpnfo.XrdCli->Stat(&stat);
cpnfo.len = stat.size;
XrdOucString dest = AddSizeHint( dst, stat.size );
// if xrddest if nonzero, then the file is already opened for writing
if (!*xrddest) {
*xrddest = new XrdClient(dest.c_str());
if (!PedanticOpen4Write(*xrddest, rwMode, xrd_wr_flags))
{cpFatal("open", 0, *xrddest);
delete cpnfo.XrdCli;
delete *xrddest;
*xrddest = 0;
cpnfo.XrdCli = 0;
return -1;
}
}
// If the Extreme Copy flag is set, we try to find more sources for this file
// Each source gets assigned to a different reader thread
XrdClientVector<XrdClient *> xtremeclients;
XrdXtRdFile *xrdxtrdfile = 0;
if (doXtremeCp)
XrdXtRdFile::GetListOfSources(cpnfo.XrdCli, XtremeCpRdr,
xtremeclients, Config.nSrcs);
// Start reader on xrdc
if (doXtremeCp && (xtremeclients.GetSize() > 1)) {
// Beware... with the extreme copy the normal read ahead mechanism
// makes no sense at all.
//EnvPutInt(NAME_REMUSEDCACHEBLKS, 1);
xrdxtrdfile = new XrdXtRdFile(XRDCP_BLOCKSIZE*4, cpnfo.len);
for (int iii = 0; iii < xtremeclients.GetSize(); iii++) {
xtreme_threadnfo *nfo = new(xtreme_threadnfo);
nfo->xtrdhandler = xrdxtrdfile;
nfo->cli = xtremeclients[iii];
nfo->clientidx = xrdxtrdfile->GimmeANewClientIdx();
nfo->startfromblk = iii*xrdxtrdfile->GetNBlks() / xtremeclients.GetSize();
nfo->maxoutstanding = xrdmin( 5, xrdxtrdfile->GetNBlks() / xtremeclients.GetSize() );
if (nfo->maxoutstanding < 1) nfo->maxoutstanding = 1;
XrdSysThread::Run(&myTID, ReaderThread_xrd_xtreme,
(void *)nfo, XRDSYSTHREAD_HOLD);
myTIDVec.Push_back(myTID);
}
}
else {
XrdSysThread::Run(&myTID,ReaderThread_xrd,(void *)&cpnfo,XRDSYSTHREAD_HOLD);
myTIDVec.Push_back(myTID);
}
int len = 1;
void *buf;
long long offs = 0;
long long bytesread=0;
long long size = cpnfo.len;
bool draining = false;
// Loop to write until ended or timeout err
while (1) {
if (xrdxtrdfile && xrdxtrdfile->AllDone()) draining = true;
if (draining && !cpnfo.queue.GetLength()) break;
if ( cpnfo.queue.GetBuffer(&buf, offs, len) ) {
if (len && buf) {
bytesread+=len;
if (progbar) {
gettimeofday(&abs_stop_time,&tz);
print_progbar(bytesread,size);
}
if (!(*xrddest)->Write(buf, offs, len)) {
cpFatal("write", 0, *xrddest);
retvalue = 11;
break;
}
if (cpnfo.mon)
cpnfo.mon->PutProgressInfo(bytesread, cpnfo.len, (float)bytesread / cpnfo.len * 100.0);
free(buf);
}
else
if (!xrdxtrdfile && ( ((buf == 0) && (len == 0)) || (bytesread >= size))) {