-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdFrmTransfer.cc
836 lines (728 loc) · 29 KB
/
XrdFrmTransfer.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
/******************************************************************************/
/* */
/* X r d F r m T r a n s f e r . c c */
/* */
/* (c) 2010 by the Board of Trustees of the Leland Stanford, Jr., University */
/* All Rights Reserved */
/* Produced by Andrew Hanushevsky for Stanford University 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. */
/******************************************************************************/
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <utime.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "XrdFrc/XrdFrcCID.hh"
#include "XrdFrc/XrdFrcRequest.hh"
#include "XrdFrc/XrdFrcTrace.hh"
#include "XrdFrc/XrdFrcXAttr.hh"
#include "XrdFrm/XrdFrmCns.hh"
#include "XrdFrm/XrdFrmConfig.hh"
#include "XrdFrm/XrdFrmMonitor.hh"
#include "XrdFrm/XrdFrmTransfer.hh"
#include "XrdFrm/XrdFrmXfrJob.hh"
#include "XrdFrm/XrdFrmXfrQueue.hh"
#include "XrdNet/XrdNetCmsNotify.hh"
#include "XrdOss/XrdOss.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOuc/XrdOucMsubs.hh"
#include "XrdOuc/XrdOucProg.hh"
#include "XrdOuc/XrdOucSxeq.hh"
#include "XrdOuc/XrdOucUtils.hh"
#include "XrdOuc/XrdOucXAttr.hh"
#include "XrdSys/XrdSysError.hh"
#include "XrdSys/XrdSysFD.hh"
#include "XrdSys/XrdSysPlatform.hh"
using namespace XrdFrc;
using namespace XrdFrm;
/******************************************************************************/
/* L o c a l C l a s s e s */
/******************************************************************************/
struct XrdFrmTranArg
{
XrdOucEnv *theEnv;
XrdOucProg *theCmd;
XrdOucMsubs *theVec;
char *theSrc;
char *theDst;
char *theINS;
char theMDP[8];
XrdFrmTranArg(XrdOucEnv *Env)
: theEnv(Env), theCmd(0), theVec(0), theSrc(0),
theDst(0), theINS(0)
{theMDP[0] = '0'; theMDP[1] = 0;}
~XrdFrmTranArg() {}
};
struct XrdFrmTranChk
{ struct stat *Stat;
int lkfd;
int lkfx;
XrdFrmTranChk(struct stat *sP) : Stat(sP), lkfd(-1), lkfx(0) {}
~XrdFrmTranChk() {if (lkfd >= 0) close(lkfd);}
};
/******************************************************************************/
/* S t a t i c s */
/******************************************************************************/
XrdSysMutex XrdFrmTransfer::pMutex;
XrdOucHash<char> XrdFrmTransfer::pTab;
/******************************************************************************/
/* C o n s t r u c t o r */
/******************************************************************************/
XrdFrmTransfer::XrdFrmTransfer()
{
int i;
// Construct program objects
//
for (i = 0; i < 4; i++)
xfrCmd[i] = (Config.xfrCmd[i].theVec ? new XrdOucProg(&Say) : 0);
}
/******************************************************************************/
/* Public: c h e c k F F */
/******************************************************************************/
const char *XrdFrmTransfer::checkFF(const char *Path)
{
EPNAME("checkFF");
struct stat buf;
// Check for a fail file
//
if (!stat(Path, &buf))
{if (buf.st_ctime+Config.FailHold >= time(0))
return "request previously failed";
if (Config.Test) {DEBUG("would have removed '" <<Path <<"'");}
else {unlink(Path);
DEBUG("removed '" <<Path <<"'");
}
}
// Return all is well
//
return 0;
}
/******************************************************************************/
/* F e t c h */
/******************************************************************************/
const char *XrdFrmTransfer::Fetch()
{
EPNAME("Fetch");
static const mode_t fMode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
static const int crOpts = (O_CREAT|O_TRUNC)<<8|XRDOSS_mkpath;
XrdOucEnv myEnv(xfrP->reqData.Opaque?xfrP->reqData.LFN+xfrP->reqData.Opaque:0);
XrdFrmTranArg cmdArg(&myEnv);
struct stat pfnStat;
time_t xfrET;
const char *eTxt, *retMsg = 0;
char lfnpath[MAXPATHLEN+1024+512+8], *Lfn, Rfn[MAXPATHLEN+256], *theSrc;
char pdBuff[1024];
int iXfr, pdSZ, lfnEnd, rc, isURL = 0, doRM = 0;
long long fSize = 0;
// The remote source is either the url-lfn or a translated lfn
//
if ((isURL = xfrP->reqData.LFO)) theSrc = xfrP->reqData.LFN;
else {if (!Config.RemotePath(xfrP->reqData.LFN, Rfn, sizeof(Rfn)))
return "lfn2rfn failed";
theSrc = Rfn;
isURL = (*Rfn != '/');
}
// Check if we can actually handle this transfer
//
if (isURL)
{if (xfrCmd[2]) iXfr = 2;
else return "url copies not configured";
} else {
if (xfrCmd[0]) iXfr = 0;
else return "non-url copies not configured";
}
// Check for a fail file
//
if ((eTxt = ffCheck())) return eTxt;
// Check if the file exists
//
Lfn = (xfrP->reqData.LFN)+xfrP->reqData.LFO;
if (!Config.Stat(Lfn, xfrP->PFN, &pfnStat))
{DEBUG(xfrP->PFN <<" exists; not fetched.");
return 0;
}
// Construct the file name to which to we originally transfer the data. This is
// the lfn if we do not pre-allocate files and "lfn.anew" otherwise.
//
lfnEnd = strlen(Lfn);
strlcpy(lfnpath, Lfn, sizeof(lfnpath)-8);
if (Config.xfrCmd[iXfr].Opts & Config.cmdAlloc)
{strcpy(&lfnpath[lfnEnd], ".anew");
strcpy(&xfrP->PFN[xfrP->pfnEnd], ".anew");
}
// Setup the command
//
cmdArg.theCmd = xfrCmd[iXfr];
cmdArg.theVec = Config.xfrCmd[iXfr].theVec;
cmdArg.theSrc = theSrc;
cmdArg.theDst = xfrP->PFN;
cmdArg.theINS = xfrP->reqData.iName;
if (!SetupCmd(&cmdArg)) return "incoming transfer setup failed";
// If the copycmd needs a placeholder in the filesystem for this transfer, we
// must create one. We first remove any existing "anew" file because we will
// over-write it. The create process will create a lock file if need be.
// However, we can ignore it as we are the only ones actually using it.
//
if (Config.xfrCmd[iXfr].Opts & Config.cmdAlloc)
{Config.ossFS->Unlink(lfnpath);
rc = Config.ossFS->Create(xfrP->reqData.User,lfnpath,fMode,myEnv,crOpts);
if (rc)
{Say.Emsg("Fetch", rc, "create placeholder for", lfnpath);
return "create failed";
}
doRM = 1;
} else doRM = Config.xfrCmd[iXfr].Opts & Config.cmdRME;
// Setup program monitoring data
//
pdSZ = (Config.xfrCmd[iXfr].Opts & Config.cmdXPD ? sizeof(pdBuff) : 0);
// Now run the command to get the file and make sure the file is there
// If it is, make sure that if a lock file exists its date/time is greater than
// the file we just fetched; then rename it to be the correct name.
//
xfrET = time(0);
if (!(rc = cmdArg.theCmd->Run(pdBuff, pdSZ)))
{if ((rc = Config.Stat(lfnpath, xfrP->PFN, &pfnStat)))
{Say.Emsg("Fetch", lfnpath, "fetched but not resident!"); fSize = 0;}
else {fSize = pfnStat.st_size;
if (Config.xfrCmd[iXfr].Opts & Config.cmdAlloc)
FetchDone(lfnpath, pfnStat, rc);
}
}
// Clean up if we failed otherwise tell the cmsd that we have a new file. Upon
// failure we issue a a remove as we don't want the temp file to exist.
//
xfrP->PFN[xfrP->pfnEnd] = '\0';
if (rc)
{if (doRM) Config.ossFS->Unlink(lfnpath);
ffMake(rc == -2);
if (rc == -2) {xfrP->RetCode = 2; retMsg = "file not found";}
else retMsg = "fetch failed";
} else if (Config.cmsPath) Config.cmsPath->Have(Lfn);
// We completed, see if we need to do statistics
//
if ((Config.xfrCmd[iXfr].Opts & Config.cmdStats) || XrdFrmMonitor::monSTAGE
|| (Trace.What & TRACE_Debug))
{time_t eNow = time(0);
int inqT, xfrT;
inqT = static_cast<int>(xfrET - time_t(xfrP->reqData.addTOD));
if ((xfrT = static_cast<int>(eNow - xfrET)) <= 0) xfrT = 1;
if (((Config.xfrCmd[iXfr].Opts & Config.cmdStats)
|| (Trace.What & TRACE_Debug)) && !retMsg)
{char sbuff[80];
sprintf(sbuff, "Got: %lld qt: %d xt: %d up: ", fSize, inqT, xfrT);
lfnpath[lfnEnd] = '\0';
Say.Say(0, sbuff, xfrP->reqData.User, " ", lfnpath);
}
if (XrdFrmMonitor::monSTAGE)
{if (rc < 0) rc = -rc;
snprintf(lfnpath+lfnEnd, sizeof(lfnpath)-lfnEnd-1,
"\n&tod=%lld&sz=%lld&qt=%d&tm=%d&op=%c&rc=%d%s%s",
static_cast<long long>(eNow), fSize, inqT, xfrT,
xfrP->Act, rc, (pdSZ ? "&pd=" : ""), (pdSZ ? pdBuff : ""));
XrdFrmMonitor::Map(XROOTD_MON_MAPSTAG,xfrP->reqData.User,lfnpath);
}
}
// All done
//
return retMsg;
}
/******************************************************************************/
/* F e t c h D o n e */
/******************************************************************************/
const char *XrdFrmTransfer::FetchDone(char *lfnpath, struct stat &Stat, int &rc)
{
// If we are running in new mode, update file attributes
//
rc = 0;
if (Config.runNew && Config.NeedsCTA(lfnpath))
{XrdOucXAttr<XrdFrcXAttrCpy> cpyInfo;
cpyInfo.Attr.cpyTime = static_cast<long long>(Stat.st_mtime);
if ((rc = cpyInfo.Set(xfrP->PFN)))
Say.Emsg("Fetch", rc, "set copy time xattr on", xfrP->PFN);
}
// Check for a lock file and if we have one, reset it's time or delete it
//
if (Config.runOld && Config.NeedsCTA(lfnpath))
{struct stat lkfStat;
strcpy(&xfrP->PFN[xfrP->pfnEnd+5], ".lock");
if (!stat(xfrP->PFN, &lkfStat))
{if (Config.runNew && !rc) unlink(xfrP->PFN);
else {struct utimbuf tbuff;
tbuff.actime = tbuff.modtime = Stat.st_mtime;
if ((rc = utime(xfrP->PFN, &tbuff)))
Say.Emsg("Fetch", rc, "set utime on", xfrP->PFN);
}
}
}
// Now rename the lfn to be what it needs to be in the end
//
if (!rc && (rc=Config.ossFS->Rename(lfnpath,xfrP->reqData.LFN)))
Say.Emsg("Fetch", rc, "rename", lfnpath);
else XrdFrmCns::Add(xfrP->reqData.User, xfrP->reqData.LFN,
Stat.st_size, Stat.st_mode);
// Done
//
return (rc ? "Failed" : 0);
}
/******************************************************************************/
/* Private: f f C h e c k */
/******************************************************************************/
const char *XrdFrmTransfer::ffCheck()
{
const char *eTxt;
// Generate proper fail file path and check if it exists
//
if (Config.xfrFdir)
{char ffPath[MAXPATHLEN+8];
if (Config.xfrFdln+xfrP->pfnEnd+5 >= int(sizeof(ffPath))) return 0;
strcpy(ffPath, Config.xfrFdir);
strcpy(ffPath+Config.xfrFdln, xfrP->PFN);
strcpy(ffPath+Config.xfrFdln+xfrP->pfnEnd, ".fail");
eTxt = checkFF(ffPath);
} else {
strcpy(&xfrP->PFN[xfrP->pfnEnd], ".fail");
eTxt = checkFF(xfrP->PFN);
xfrP->PFN[xfrP->pfnEnd] = '\0';
}
// Determine result
//
if (eTxt) xfrP->RetCode = 1;
return eTxt;
}
/******************************************************************************/
/* Private: f f M a k e */
/******************************************************************************/
void XrdFrmTransfer::ffMake(int nofile){
static const mode_t fMode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
static const mode_t dMode = S_IXUSR|S_IWGRP|S_IXGRP|S_IXOTH | fMode;
char ffPath[MAXPATHLEN+8], *ffP;
int myFD;
// Generate fail file path
//
if (Config.xfrFdir)
{if (Config.xfrFdln+xfrP->pfnEnd+5 >= int(sizeof(ffPath))) return;
strcpy(ffPath, Config.xfrFdir);
strcpy(ffPath+Config.xfrFdln, xfrP->PFN);
strcpy(ffPath+Config.xfrFdln+xfrP->pfnEnd, ".fail");
XrdOucUtils::makePath(ffPath, dMode);
ffP = ffPath;
} else {
strcpy(&xfrP->PFN[xfrP->pfnEnd], ".fail");
ffP = xfrP->PFN;
}
// Create a fail file and if failure is due to "file not found" set the mtime
// to 2 so that the oss layer picks up the same error in the future.
//
myFD = open(ffP, O_CREAT, fMode);
if (myFD >= 0)
{close(myFD);
if (nofile)
{struct utimbuf tbuff;
tbuff.actime = time(0); tbuff.modtime = 2;
utime(ffP, &tbuff);
}
}
if (!Config.xfrFdir) xfrP->PFN[xfrP->pfnEnd] = '\0';
}
/******************************************************************************/
/* I n i t */
/******************************************************************************/
void *InitXfer(void *parg)
{ XrdFrmTransfer *xP = new XrdFrmTransfer;
xP->Start();
return (void *)0;
}
int XrdFrmTransfer::Init()
{
pthread_t tid;
int retc, n;
// Initialize the cluster identification object first
//
CID.Init(Config.QPath);
// Initialize the transfer queue first
//
if (!XrdFrmXfrQueue::Init()) return 0;
// Start the required number of transfer threads
//
n = Config.xfrMax;
while(n--)
{if ((retc = XrdSysThread::Run(&tid, InitXfer, (void *)0,
XRDSYSTHREAD_BIND, "transfer")))
{Say.Emsg("main", retc, "create xfr thread"); return 0;}
}
// All done
//
return 1;
}
/******************************************************************************/
/* Private: S e t u p C m d */
/******************************************************************************/
int XrdFrmTransfer::SetupCmd(XrdFrmTranArg *argP)
{
char *pdata[XrdOucMsubs::maxElem + 2], *cP;
int pdlen[XrdOucMsubs::maxElem + 2], i, k, n;
XrdOucMsubsInfo
Info(xfrP->reqData.User, argP->theEnv, Config.the_N2N,
xfrP->reqData.LFN+xfrP->reqData.LFO,
argP->theSrc, xfrP->reqData.Prty,
xfrP->reqData.Options & XrdFrcRequest::makeRW?O_RDWR:O_RDONLY,
argP->theMDP, xfrP->reqData.ID, xfrP->PFN, argP->theDst);
// We must establish the host, cluster and instance name if we have one
//
if (argP->theEnv)
{argP->theEnv->Put(SEC_HOST, Config.myName);
if (argP->theINS)
{CID.Get(argP->theINS, CMS_CID, argP->theEnv);
argP->theEnv->Put(XRD_INS, argP->theINS);
}
}
// Substitute in the parameters
//
k = argP->theVec->Subs(Info, pdata, pdlen);
// Catenate all of the arguments
//
*cmdBuff = '\0'; n = sizeof(cmdBuff) - 4; cP = cmdBuff;
for (i = 0; i < k; i++)
{n -= pdlen[i];
if (n < 0)
{Say.Emsg("Setup",E2BIG,"build command line for", xfrP->reqData.LFN);
return 0;
}
strcpy(cP, pdata[i]); cP += pdlen[i];
}
// Now setup the command
//
return (argP->theCmd->Setup(cmdBuff, &Say) == 0);
}
/******************************************************************************/
/* Public: S t a r t */
/******************************************************************************/
void XrdFrmTransfer::Start()
{
EPNAME("Transfer"); // Wrong but looks better
const char *Msg;
// Prime I/O queue selection
// Endless loop looking for transfer jobs
//
while(1)
{xfrP = XrdFrmXfrQueue::Get();
DEBUG(xfrP->Type <<" starting " <<xfrP->reqData.LFN
<<" for " <<xfrP->reqData.User);
Msg = (xfrP->qNum & XrdFrcRequest::outQ ? Throw() : Fetch());
if (Msg && !(xfrP->RetCode)) xfrP->RetCode = 1;
xfrP->PFN[xfrP->pfnEnd] = 0;
if (xfrP->RetCode || Config.Verbose)
{char buff1[80], buff2[80];
sprintf(buff1, "%s for %s", xfrP->RetCode ? "failed" : "complete",
xfrP->reqData.User);
if (xfrP->RetCode == 0) *buff2 = 0;
else sprintf(buff2, "; %s", (Msg ? Msg : "reason unknown"));
Say.Say(0, xfrP->Type, buff1, xfrP->reqData.LFN,buff2);
} else {
DEBUG(xfrP->Type
<<(xfrP->RetCode ? " failed " : " complete ")
<< xfrP->reqData.LFN <<" rc=" <<xfrP->RetCode
<<' ' <<(Msg ? Msg : ""));
}
XrdFrmXfrQueue::Done(xfrP, Msg);
}
}
/******************************************************************************/
/* Private: T r a c k D C */
/******************************************************************************/
int XrdFrmTransfer::TrackDC(char *Lfn, char *Mdp, char *Rfn)
{
char *FName, *Slash, *Slush = 0, *begRfn = Rfn;
int n = -1;
// If this is a url, then don't back space into the url part
//
if (*Rfn != '/'
&& (Slash = index(Rfn, '/')) && *(Slash+1) == '/'
&& (Slash = index(Slash+2, '/')) && *(Slash+1) == '/') begRfn = Slash+1;
// Discard the filename component
//
if (!(FName = rindex(begRfn, '/')) || FName == begRfn) return 0;
*FName = 0; Slash = Slush = FName;
// Try to find the created directory path
//
pMutex.Lock();
while(Slash != begRfn && !pTab.Find(Rfn))
{do {Slash--;} while(Slash != begRfn && *Slash != '/');
if (Slush) *Slush = '/';
*Slash = 0; Slush = Slash;
n++;
}
pMutex.UnLock();
// Compute offset of uncreated part
//
*Slash = '/';
if (Slash == begRfn) n = 0;
else n = (n >= 0 ? Slash - begRfn : FName - begRfn);
sprintf(Mdp, "%d", n);
// All done
//
return n;
}
/******************************************************************************/
int XrdFrmTransfer::TrackDC(char *Rfn)
{
char *Slash;
// Trim off the trailing end
//
if (!(Slash = rindex(Rfn, '/')) || Slash == Rfn) return 0;
*Slash = 0;
// The path has been added, do insert it into the table of created paths
//
pMutex.Lock();
pTab.Add(Rfn, 0, 0, Hash_data_is_key);
pMutex.UnLock();
*Slash = '/';
return 0;
}
/******************************************************************************/
/* T h r o w */
/******************************************************************************/
const char *XrdFrmTransfer::Throw()
{
XrdOucEnv myEnv(xfrP->reqData.Opaque?xfrP->reqData.LFN+xfrP->reqData.Opaque:0);
XrdFrmTranArg cmdArg(&myEnv);
struct stat begStat, endStat;
XrdFrmTranChk Chk(&begStat);
time_t xfrET;
const char *eTxt, *retMsg = 0;
char Rfn[MAXPATHLEN+256], *lfnpath = xfrP->reqData.LFN, *theDest;
char pdBuff[1024];
int isMigr = xfrP->reqData.Options & XrdFrcRequest::Migrate;
int iXfr, isURL, pdSZ, rc, mDP = -1;
// The remote source is either the url-lfn or a translated lfn
//
if ((isURL = xfrP->reqData.LFO)) theDest = xfrP->reqData.LFN;
else {if (!Config.RemotePath(xfrP->reqData.LFN, Rfn, sizeof(Rfn)))
return "lfn2rfn failed";
theDest = Rfn;
isURL = (*Rfn != '/');
}
// Check if we can actually handle this transfer
//
if (isURL)
{if (xfrCmd[3]) iXfr = 3;
else return "url copies not configured";
} else {
if (xfrCmd[1]) iXfr = 1;
else return "non-url copies not configured";
}
// Check if the file exists (we only copy resident files)
//
if (Config.Stat(lfnpath+xfrP->reqData.LFO, xfrP->PFN, &begStat))
return (xfrP->reqFQ ? "file not found" : 0);
// Check for a fail file
//
if ((eTxt = ffCheck())) return eTxt;
// If this is an mss migration request, then recheck if the file can and
// need to be migrated based on the lock file. This also obtains a directory
// lock and lock file lock, as needed. If the file need not be migrated but
// should be purge, we will get a null string error.
//
if (isMigr && (eTxt = ThrowOK(&Chk)))
{if (*eTxt) return eTxt;
if (!(xfrP->reqData.Options & XrdFrcRequest::Purge)) return "logic error";
Throwaway();
return 0;
}
// Setup the command, including directory tracking, as needed
//
cmdArg.theCmd = xfrCmd[iXfr];
cmdArg.theVec = Config.xfrCmd[iXfr].theVec;
cmdArg.theDst = theDest;
cmdArg.theSrc = xfrP->PFN;
cmdArg.theINS = xfrP->reqData.iName;
if (Config.xfrCmd[iXfr].Opts & Config.cmdMDP)
mDP = TrackDC(lfnpath+xfrP->reqData.LFO, cmdArg.theMDP, Rfn);
if (!SetupCmd(&cmdArg)) return "outgoing transfer setup failed";
// Setup program monitoring data
//
pdSZ = (Config.xfrCmd[iXfr].Opts & Config.cmdXPD ? sizeof(pdBuff) : 0);
// Now run the command to put the file. If the command fails and this is a
// migration request, cretae a fail file if one does not exist.
//
xfrET = time(0);
if ((rc = cmdArg.theCmd->Run(pdBuff, pdSZ)))
{if (isMigr) ffMake(rc == -2);
retMsg = "copy failed";
}
// Track directory creations if we need to track them
//
if (!rc && mDP >= 0) TrackDC(Rfn);
// Obtain state of the file after the copy and make sure the file was not
// modified during the copy. This is an error for queued requests but
// internally generated requests will simply be retried.
//
if (!rc)
{if ((rc = Config.Stat(lfnpath+xfrP->reqData.LFO, xfrP->PFN, &endStat)))
{Say.Emsg("Throw", lfnpath, "transfered but not found!");
retMsg = "unable to verify copy";
} else {
if (begStat.st_mtime != endStat.st_mtime
|| begStat.st_size != endStat.st_size)
{Say.Emsg("Throw", lfnpath, "modified during transfer!");
retMsg = "file modified during copy"; rc = 1;
}
}
}
// Purge the file if so wanted. Otherwise, if this is a migration request,
// make sure that if a lock file exists its date/time is equal to the file
// we just copied to prevent the file from being copied again (we have a lock).
//
if (!rc)
{if (xfrP->reqData.Options & XrdFrcRequest::Purge) Throwaway();
else if (isMigr) ThrowDone(&Chk, endStat.st_mtime);
}
// Do statistics if so wanted
//
if ((Config.xfrCmd[iXfr].Opts & Config.cmdStats) || XrdFrmMonitor::monMIGR
|| (Trace.What & TRACE_Debug))
{time_t eNow = time(0);
int inqT, xfrT;
long long Fsize = begStat.st_size;
inqT = static_cast<int>(xfrET - time_t(xfrP->reqData.addTOD));
if ((xfrT = static_cast<int>(eNow - xfrET)) <= 0) xfrT = 1;
if (((Config.xfrCmd[iXfr].Opts & Config.cmdStats)
|| (Trace.What & TRACE_Debug)) && !rc)
{char sbuff[80];
sprintf(sbuff, "Put: %lld qt: %d xt: %d up: ",Fsize,inqT,xfrT);
Say.Say(0, sbuff, xfrP->reqData.User, " ", xfrP->reqData.LFN);
}
if (XrdFrmMonitor::monMIGR)
{char monBuff[MAXPATHLEN+1024+512+8];
if (rc < 0) rc = -rc;
snprintf(monBuff, sizeof(monBuff),
"%s\n&tod=%lld&sz=%lld&qt=%d&tm=%d&op=%c&rc=%d%s%s",
xfrP->reqData.LFN, static_cast<long long>(eNow), Fsize,
inqT, xfrT, xfrP->Act, rc,
(pdSZ ? "&pd=" : ""), (pdSZ ? pdBuff : ""));
XrdFrmMonitor::Map(XROOTD_MON_MAPMIGR,xfrP->reqData.User,monBuff);
}
}
// All done
//
return retMsg;
}
/******************************************************************************/
/* Private: T h r o w a w a y */
/******************************************************************************/
void XrdFrmTransfer::Throwaway()
{
EPNAME("Throwaway");
// Purge the file. We do this via the pfn but also indicate we want all
// migration support suffixes removed it they exist. Notify the cmsd & cnsd.
//
if (Config.Test) {DEBUG("Would have removed '" <<xfrP->PFN <<"'");}
else {Config.ossFS->Unlink(xfrP->PFN, XRDOSS_isPFN|XRDOSS_isMIG);
DEBUG("removed '" <<xfrP->PFN <<"'");
if (Config.cmsPath) Config.cmsPath->Gone(xfrP->PFN);
XrdFrmCns::Rm(xfrP->PFN);
}
}
/******************************************************************************/
/* Private: T h r o w D o n e */
/******************************************************************************/
void XrdFrmTransfer::ThrowDone(XrdFrmTranChk *cP, time_t endTime)
{
// Update file attributes if we are running in new mode, otherwise do
//
if (Config.runNew)
{XrdOucXAttr<XrdFrcXAttrCpy> cpyInfo;
cpyInfo.Attr.cpyTime = static_cast<long long>(endTime);
if (cpyInfo.Set(xfrP->PFN, cP->lkfd))
Say.Emsg("Throw", "Unable to set copy time xattr for", xfrP->PFN);
else if (cP->lkfx)
{strcpy(&xfrP->PFN[xfrP->pfnEnd], ".lock");
unlink(xfrP->PFN);
xfrP->PFN[xfrP->pfnEnd] = '\0';
}
} else {
struct stat Stat;
strcpy(&xfrP->PFN[xfrP->pfnEnd], ".lock");
if (!stat(xfrP->PFN, &Stat))
{struct utimbuf tbuff;
tbuff.actime = tbuff.modtime = endTime;
if (utime(xfrP->PFN, &tbuff))
Say.Emsg("Throw", errno, "set utime for", xfrP->PFN);
}
xfrP->PFN[xfrP->pfnEnd] = '\0';
}
}
/******************************************************************************/
/* Private: T h r o w O K */
/******************************************************************************/
const char *XrdFrmTransfer::ThrowOK(XrdFrmTranChk *cP)
{
class fdClose
{public:
int Num;
fdClose() : Num(-1) {}
~fdClose() {if (Num >= 0) close(Num);}
} fnFD;
XrdOucXAttr<XrdFrcXAttrCpy> cpyInfo;
struct stat lokStat;
int statRC;
// Check if the file is in use by checking if we got an exclusive lock
//
if ((fnFD.Num = XrdSysFD_Open(xfrP->PFN, O_RDWR)) < 0)
return "unable to open file";
if (XrdOucSxeq::Serialize(fnFD.Num,XrdOucSxeq::noWait)) return "file in use";
// Get the info on the lock file (enabled if old mode is in effect
//
if (Config.runOld)
{strcpy(&xfrP->PFN[xfrP->pfnEnd], ".lock");
statRC = stat(xfrP->PFN, &lokStat);
xfrP->PFN[xfrP->pfnEnd] = '\0';
} else statRC = 1;
if (statRC && !Config.runNew) return "missing lock file";
// If running in new mode then we must get the extened attribute for this file
// unless we got the lock file time which takes precendence.
//
if (Config.runNew)
{if (!statRC)
cpyInfo.Attr.cpyTime = static_cast<long long>(lokStat.st_mtime);
else if (cpyInfo.Get(xfrP->PFN, fnFD.Num) <= 0)
return "unable to get copy time xattr";
}
// Verify the information
//
if (cpyInfo.Attr.cpyTime >= static_cast<long long>(cP->Stat->st_mtime))
{if (xfrP->reqData.Options & XrdFrcRequest::Purge) return "";
return "already migrated";
}
// Keep the lock on the base file until we are through. No one is allowed to
// modify this file until we have migrate it.
//
cP->lkfd = fnFD.Num;
cP->lkfx = statRC == 0;
fnFD.Num = -1;
return 0;
}