/
XrdPosixXrootd.cc
1694 lines (1422 loc) · 52.1 KB
/
XrdPosixXrootd.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 P o s i x X r o o t d . 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 <cerrno>
#include <fcntl.h>
#include <iostream>
#include <cstdio>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/resource.h>
#include <sys/uio.h>
#include "XrdVersion.hh"
#include "Xrd/XrdScheduler.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClFileSystem.hh"
#include "XrdCl/XrdClFile.hh"
#include "XrdCl/XrdClURL.hh"
#include "XrdCl/XrdClXRootDResponses.hh"
#include "XrdSys/XrdSysHeaders.hh"
#include "XrdSys/XrdSysPlatform.hh"
#include "XrdOuc/XrdOucCache.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOuc/XrdOucName2Name.hh"
#include "XrdOuc/XrdOucPsx.hh"
#include "XrdPosix/XrdPosixAdmin.hh"
#include "XrdPosix/XrdPosixCallBack.hh"
#include "XrdPosix/XrdPosixConfig.hh"
#include "XrdPosix/XrdPosixDir.hh"
#include "XrdPosix/XrdPosixFile.hh"
#include "XrdPosix/XrdPosixFileRH.hh"
#include "XrdPosix/XrdPosixInfo.hh"
#include "XrdPosix/XrdPosixMap.hh"
#include "XrdPosix/XrdPosixPrepIO.hh"
#include "XrdPosix/XrdPosixStats.hh"
#include "XrdPosix/XrdPosixTrace.hh"
#include "XrdPosix/XrdPosixXrootd.hh"
#include "XrdPosix/XrdPosixXrootdPath.hh"
#include "XrdSys/XrdSysTrace.hh"
/******************************************************************************/
/* S t a t i c M e m b e r s */
/******************************************************************************/
class XrdSysError;
namespace XrdPosixGlobals
{
XrdScheduler *schedP = 0;
XrdOucCache *theCache = 0;
XrdOucName2Name *theN2N = 0;
XrdSysLogger *theLogger = 0;
XrdSysError *eDest = 0;
XrdPosixStats Stats;
XrdSysTrace Trace("Posix", 0,
(getenv("XRDPOSIX_DEBUG") ? TRACE_Debug : 0));
int ddInterval= 30;
int ddMaxTries= 180/30;
XrdCl::DirListFlags::Flags dlFlag = XrdCl::DirListFlags::None;
bool oidsOK = false;
bool p2lSRC = false;
bool p2lSGI = false;
bool autoPGRD = false;
bool usingEC = false;
};
int XrdPosixXrootd::baseFD = 0;
int XrdPosixXrootd::initDone = 0;
XrdVERSIONINFO(XrdPosix,XrdPosix);
/******************************************************************************/
/* L o c a l C l a s s e s */
/******************************************************************************/
/******************************************************************************/
/* L f n P a t h */
/******************************************************************************/
namespace
{
class LfnPath
{
public:
const char *path;
LfnPath(const char *who, const char *pURL, bool ponly=true)
{path = XrdPosixXrootPath::P2L(who, pURL, relURL, ponly);}
~LfnPath() {if (relURL) free(relURL);}
private:
char *relURL;
};
}
/******************************************************************************/
/* L o c a l F u n c t i o n s */
/******************************************************************************/
namespace
{
/******************************************************************************/
/* O p e n D e f e r */
/******************************************************************************/
int OpenDefer(XrdPosixFile *fp,
XrdPosixCallBack *cbP,
XrdCl::OpenFlags::Flags XOflags,
XrdCl::Access::Mode XOmode,
bool isStream)
{
// Assign a file descriptor to this file
//
if (!(fp->AssignFD(isStream)))
{delete fp;
errno = EMFILE;
return -1;
}
// Allocate a prepare I/O object to defer this open
//
fp->PrepIO = new XrdPosixPrepIO(fp, XOflags, XOmode);
// Finalize this file object. A null argument indicates it is deferred.
//
fp->Finalize(0);
// For sync opens we just need to return the file descriptor
//
if (!cbP) return fp->FDNum();
// For async opens do the callback here and return an inprogress
//
cbP->Complete(fp->FDNum());
errno = EINPROGRESS;
return -1;
}
};
/******************************************************************************/
/* C o n s t r u c t o r */
/******************************************************************************/
XrdPosixXrootd::XrdPosixXrootd(int fdnum, int dirnum, int thrnum)
{
static XrdSysMutex myMutex;
char *cfn;
// Test if XRDCL_EC is set. That env var. is set at XrdCl::PlugInManager::LoadFactory
// in XrdClPlugInManager.cc, which is called (by XrdOssGetSS while loading
// libXrdPss.so) before this function.
// Note: some standalone programs will call this constructor directly.
XrdPosixGlobals::usingEC = getenv("XRDCL_EC")? true : false;
// Only static fields are initialized here. We need to do this only once!
//
myMutex.Lock();
if (initDone) {myMutex.UnLock(); return;}
initDone = 1;
myMutex.UnLock();
// Initialize environment as a client or a server (it differs somewhat).
// Note that we create a permanent Env since some plugins rely on it. We
// leave the logger handling to OucPsx as we do not want to enable messages
// because this is likely a client application that doesn't understand noise.
//
if (!XrdPosixGlobals::theLogger && (cfn=getenv("XRDPOSIX_CONFIG")) && *cfn)
{bool hush;
if (*cfn == '+') {hush = false; cfn++;}
else hush = (getenv("XRDPOSIX_DEBUG") == 0);
if (*cfn)
{XrdOucEnv *psxEnv = new XrdOucEnv;
psxEnv->Put("psx.Client", "1");
XrdOucPsx psxConfig(&XrdVERSIONINFOVAR(XrdPosix), cfn, 0, psxEnv);
if (!psxConfig.ClientConfig("posix.", hush)
|| !XrdPosixConfig::SetConfig(psxConfig))
{std::cerr <<"Posix: Unable to instantiate specified "
"configuration; program exiting!" <<std::endl;
exit(16);
}
}
}
// Initialize file tracking
//
baseFD = XrdPosixObject::Init(fdnum);
}
/******************************************************************************/
/* D e s t r u c t o r */
/******************************************************************************/
XrdPosixXrootd::~XrdPosixXrootd()
{
// Shutdown processing
//
XrdPosixObject::Shutdown();
initDone = 0;
}
/******************************************************************************/
/* A c c e s s */
/******************************************************************************/
int XrdPosixXrootd::Access(const char *path, int amode)
{
XrdPosixAdmin admin(path);
mode_t stMode;
bool aOK = true;
// Issue the stat and verify that all went well
//
if (!admin.Stat(&stMode)) return -1;
// Translate the mode bits
//
if (amode & R_OK && !(stMode & S_IRUSR)) aOK = 0;
if (amode & W_OK && !(stMode & S_IWUSR)) aOK = 0;
if (amode & X_OK && !(stMode & S_IXUSR)) aOK = 0;
// All done
//
if (aOK) return 0;
errno = EACCES;
return -1;
}
/******************************************************************************/
/* C l o s e */
/******************************************************************************/
int XrdPosixXrootd::Close(int fildes)
{
EPNAME("Close");
XrdCl::XRootDStatus Status;
XrdPosixFile *fP;
bool ret;
// Map the file number to the file object. In the prcess we relese the file
// number so no one can reference this file again.
//
if (!(fP = XrdPosixObject::ReleaseFile(fildes)))
{errno = EBADF; return -1;}
// Detach the file from a possible cache. We need to up the reference count
// to synchrnoize with any possible callback as we may need to place this
// object in he delayed destroy queue if it is stil being used. Note that
// the caller will get a zero return code should we delay the close.
//
fP->Ref();
if (fP->XCio->Detach((XrdOucCacheIOCD&)*fP) && fP->Refs() < 2)
{if ((ret = fP->Close(Status))) {delete fP; fP = 0;}
else if (DEBUGON)
{std::string eTxt = Status.ToString();
DEBUG(eTxt <<" closing " <<fP->Origin());
}
} else {
ret = true;
}
// If we still have a handle then we need to do a delayed delete on this
// object because either the close failed or there is still active I/O
//
if (fP) XrdPosixFile::DelayedDestroy(fP);
// Return final result
//
return (ret ? 0 : XrdPosixMap::Result(Status));
}
/******************************************************************************/
/* C l o s e d i r */
/******************************************************************************/
int XrdPosixXrootd::Closedir(DIR *dirp)
{
XrdPosixDir *dP;
int fildes = XrdPosixDir::dirNo(dirp);
// Get the directory object
//
if (!(dP = XrdPosixObject::ReleaseDir(fildes)))
{errno = EBADF; return -1;}
// Deallocate the directory
//
delete dP;
return 0;
}
/******************************************************************************/
/* e n d P o i n t */
/******************************************************************************/
int XrdPosixXrootd::endPoint(int FD, char *Buff, int Blen)
{
XrdPosixFile *fp;
int uPort;
// Find the file object
//
if (!(fp = XrdPosixObject::File(FD))) return 0;
// Make sure url is valid
//
if (!(fp->clFile.IsOpen()))
{fp->UnLock(); return -ENOTCONN;}
// Make sure we can fit result in the buffer
//
std::string dataServer;
fp->clFile.GetProperty( "DataServer", dataServer );
XrdCl::URL dataServerUrl = dataServer;
if (dataServer.size() >= (uint32_t)Blen)
{fp->UnLock(); return -ENAMETOOLONG;}
// Copy the data server location
//
strcpy(Buff, dataServer.c_str());
// Get the port and return it
//
uPort = dataServerUrl.GetPort();
fp->UnLock();
return uPort;
}
/******************************************************************************/
/* F s t a t */
/******************************************************************************/
int XrdPosixXrootd::Fstat(int fildes, struct stat *buf)
{
XrdPosixFile *fp;
int rc;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) return -1;
// First initialize the stat buffer
//
XrdPosixConfig::initStat(buf);
// Check if we can get the stat information from the cache.
//
rc = fp->XCio->Fstat(*buf);
if (rc <= 0)
{fp->UnLock();
if (!rc) return 0;
errno = -rc;
return -1;
}
// At this point we can call the file's Fstat() and if the file is not open
// it will be opened.
//
rc = fp->Fstat(*buf);
fp->UnLock();
if (rc < 0) {errno = -rc; rc = -1;}
return rc;
}
/******************************************************************************/
/* F s y n c */
/******************************************************************************/
int XrdPosixXrootd::Fsync(int fildes)
{
XrdPosixFile *fp;
int rc;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) return -1;
// Do the sync
//
if ((rc = fp->XCio->Sync()) < 0) return Fault(fp, -rc);
fp->UnLock();
return 0;
}
/******************************************************************************/
void XrdPosixXrootd::Fsync(int fildes, XrdPosixCallBackIO *cbp)
{
XrdPosixFile *fp;
// Find the file object and do the sync
//
if ((fp = XrdPosixObject::File(fildes)))
{cbp->theFile = fp;
fp->Ref(); fp->UnLock();
fp->XCio->Sync(*cbp);
} else cbp->Complete(-1);
}
/******************************************************************************/
/* F t r u n c a t e */
/******************************************************************************/
int XrdPosixXrootd::Ftruncate(int fildes, off_t offset)
{
XrdPosixFile *fp;
int rc;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) return -1;
// Do the trunc
//
if ((rc = fp->XCio->Trunc(offset)) < 0) return Fault(fp, -rc);
fp->UnLock();
return 0;
}
/******************************************************************************/
/* G e t x a t t r */
/******************************************************************************/
#ifndef ENOATTR
#define ENOATTR ENOTSUP
#endif
long long XrdPosixXrootd::Getxattr (const char *path, const char *name,
void *value, unsigned long long size)
{
XrdPosixAdmin admin(path);
XrdCl::QueryCode::Code reqCode;
int vsize = static_cast<int>(size);
// Check if user just wants the maximum length needed
//
if (size == 0) return 1024;
// Check if we support the query
//
if (name)
{ if (!strcmp(name,"xroot.cksum")) reqCode=XrdCl::QueryCode::Checksum;
else if (!strcmp(name,"xroot.space")) reqCode=XrdCl::QueryCode::Space;
else if (!strcmp(name,"xroot.xattr")) reqCode=XrdCl::QueryCode::XAttr;
else {errno = ENOATTR; return -1;}
}else {errno = EINVAL; return -1;}
// Stat the file first to allow vectoring of the request to the right server
//
if (!admin.Stat()) return -1;
// Return the result
//
return admin.Query(reqCode, value, vsize);
}
/******************************************************************************/
/* L s e e k */
/******************************************************************************/
off_t XrdPosixXrootd::Lseek(int fildes, off_t offset, int whence)
{
XrdPosixFile *fp;
long long curroffset;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) return -1;
// Set the new offset. Note that SEEK_END requires that the file be opened.
// An open may occur by calling the FSize() method via the cache pointer.
//
if (whence == SEEK_SET) curroffset = fp->setOffset(offset);
else if (whence == SEEK_CUR) curroffset = fp->addOffset(offset);
else if (whence == SEEK_END)
{curroffset = fp->XCio->FSize();
if (curroffset < 0) return Fault(fp,static_cast<int>(-curroffset));
curroffset = fp->setOffset(curroffset+offset);
}
else return Fault(fp, EINVAL);
// All done
//
fp->UnLock();
return curroffset;
}
/******************************************************************************/
/* M k d i r */
/******************************************************************************/
int XrdPosixXrootd::Mkdir(const char *path, mode_t mode)
{
XrdPosixAdmin admin(path);
XrdCl::MkDirFlags::Flags flags;
// Preferentially make the whole path unless told otherwise
//
flags = (mode & S_ISUID ? XrdCl::MkDirFlags::None
: XrdCl::MkDirFlags::MakePath);
// Make sure the admin is OK
//
if (!admin.isOK()) return -1;
// Issue the mkdir
//
return XrdPosixMap::Result(admin.Xrd.MkDir(admin.Url.GetPathWithParams(),
flags,
XrdPosixMap::Mode2Access(mode))
);
}
/******************************************************************************/
/* O p e n */
/******************************************************************************/
int XrdPosixXrootd::Open(const char *path, int oflags, mode_t mode,
XrdPosixCallBack *cbP)
{
return Open(path, oflags, mode, cbP, 0);
}
int XrdPosixXrootd::Open(const char *path, int oflags, mode_t mode,
XrdPosixCallBack *cbP, XrdPosixInfo *infoP)
{
EPNAME("Open");
XrdCl::XRootDStatus Status;
XrdPosixFile *fp;
XrdCl::Access::Mode XOmode = XrdCl::Access::None;
XrdCl::OpenFlags::Flags XOflags;
int Opts;
bool aOK, isRO = false;
// Translate R/W and R/O flags
//
if (oflags & (O_WRONLY | O_RDWR))
{Opts = XrdPosixFile::isUpdt;
XOflags = XrdCl::OpenFlags::Update;
} else {
Opts = 0;
XOflags = XrdCl::OpenFlags::Read;
isRO = true;
}
// Pass along the stream flag
//
if (oflags & isStream)
{if (XrdPosixObject::CanStream()) Opts |= XrdPosixFile::isStrm;
else {errno = EMFILE; return -1;}
}
// Translate create vs simple open. Always make dirpath on create!
//
if (oflags & O_CREAT)
{XOflags |= (oflags & O_EXCL ? XrdCl::OpenFlags::New
: XrdCl::OpenFlags::Delete);
XOflags |= XrdCl::OpenFlags::MakePath;
XOmode = XrdPosixMap::Mode2Access(mode);
}
else if (oflags & O_TRUNC && Opts & XrdPosixFile::isUpdt)
XOflags |= XrdCl::OpenFlags::Delete;
// Allocate the new file object
//
if (!(fp = new XrdPosixFile(aOK, path, cbP, Opts)))
{errno = EMFILE;
return -1;
}
// Check if all went well during allocation
//
if (!aOK) {delete fp; return -1;}
// If we have a cache, then issue a prepare as the cache may want to defer the
// open request ans we have a lot more work to do.
//
if (XrdPosixGlobals::theCache)
{int rc;
if (infoP && isRO && OpenCache(*fp, *infoP))
{delete fp;
errno = 0;
return -3;
}
rc = XrdPosixGlobals::theCache->Prepare(fp->Path(), oflags, mode);
if (rc > 0) return OpenDefer(fp, cbP, XOflags, XOmode, oflags&isStream);
if (rc < 0) {delete fp; errno = -rc; return -1;}
}
// Open the file (sync or async)
//
XrdPosixGlobals::Stats.Count((XrdPosixGlobals::Stats.X.Opens));
if (!cbP) Status = fp->clFile.Open((std::string)path, XOflags, XOmode);
else Status = fp->clFile.Open((std::string)path, XOflags, XOmode,
(XrdCl::ResponseHandler *)fp);
// If we failed, return the reason
//
if (!Status.IsOK())
{XrdPosixGlobals::Stats.Count(XrdPosixGlobals::Stats.X.OpenErrs);
XrdPosixMap::Result(Status);
int rc = errno;
if (DEBUGON && rc != ENOENT && rc != ELOOP)
{std::string eTxt = Status.ToString();
DEBUG(eTxt <<" open " <<fp->Origin());
}
delete fp;
errno = rc;
return -1;
}
// Assign a file descriptor to this file
//
if (!(fp->AssignFD(oflags & isStream)))
{delete fp;
errno = EMFILE;
return -1;
}
// Finalize the open (this gets the stat info). For async opens, the
// finalization is deferred until the callback happens.
//
if (cbP) {errno = EINPROGRESS; return -1;}
if (fp->Finalize(&Status)) return fp->FDNum();
return XrdPosixMap::Result(Status);
}
/******************************************************************************/
/* Private: O p e n C a c h e */
/******************************************************************************/
bool XrdPosixXrootd::OpenCache(XrdPosixFile &file,XrdPosixInfo &Info)
{
EPNAME("OpenCache");
int rc;
// Check if the full file is in the cache
//
rc = XrdPosixGlobals::theCache->LocalFilePath(file.Path(), Info.cachePath,
(int)sizeof(Info.cachePath),
XrdOucCache::ForAccess,
Info.ffReady);
if (rc == 0)
{Info.ffReady = true;
DEBUG("File in cache url=" <<Info.cacheURL);
return true;
}
// File is not fully in the cache
//
Info.ffReady = false;
return false;
}
/******************************************************************************/
/* O p e n d i r */
/******************************************************************************/
DIR* XrdPosixXrootd::Opendir(const char *path)
{
XrdPosixDir *dP;
DIR *dirP;
int rc;
// Get a new directory object
//
if (!(dP = new XrdPosixDir(path)))
{errno = ENOMEM; return (DIR *)0;}
// Assign a file descriptor to this file
//
if (!(dP->AssignFD()))
{delete dP;
errno = EMFILE;
return (DIR *)0;
}
// Open the directory
//
if ((dirP = dP->Open())) return dirP;
// We failed
//
rc = errno;
delete dP;
errno = rc;
return (DIR *)0;
}
/******************************************************************************/
/* P r e a d */
/******************************************************************************/
ssize_t XrdPosixXrootd::Pread(int fildes, void *buf, size_t nbyte, off_t offset)
{
XrdPosixFile *fp;
long long offs, bytes;
int iosz;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) return -1;
// Make sure the size is not too large
//
if (nbyte > (size_t)0x7fffffff) return Fault(fp,EOVERFLOW);
else iosz = static_cast<int>(nbyte);
// Issue the read
//
offs = static_cast<long long>(offset);
bytes = fp->XCio->Read((char *)buf, offs, (int)iosz);
if (bytes < 0) return Fault(fp,-bytes);
// All went well
//
fp->UnLock();
return (ssize_t)bytes;
}
/******************************************************************************/
void XrdPosixXrootd::Pread(int fildes, void *buf, size_t nbyte, off_t offset,
XrdPosixCallBackIO *cbp)
{
XrdPosixFile *fp;
long long offs;
int iosz;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) {cbp->Complete(-1); return;}
// Make sure the size is not too large
//
if (nbyte > (size_t)0x7fffffff)
{fp->UnLock();
errno = EOVERFLOW;
cbp->Complete(-1);
return;
}
// Prepare for the read
//
cbp->theFile = fp;
fp->Ref(); fp->UnLock();
iosz = static_cast<int>(nbyte);
offs = static_cast<long long>(offset);
// Issue the read
//
fp->XCio->Read(*cbp, (char *)buf, offs, (int)iosz);
}
/******************************************************************************/
/* P w r i t e */
/******************************************************************************/
ssize_t XrdPosixXrootd::Pwrite(int fildes, const void *buf, size_t nbyte, off_t offset)
{
XrdPosixFile *fp;
long long offs;
int iosz, bytes;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) return -1;
// Make sure the size is not too large
//
if (nbyte > (size_t)0x7fffffff) return Fault(fp,EOVERFLOW);
else iosz = static_cast<int>(nbyte);
// Issue the write
//
offs = static_cast<long long>(offset);
bytes = fp->XCio->Write((char *)buf, offs, (int)iosz);
if (bytes < 0) return Fault(fp,-bytes);
// All went well
//
fp->UpdtSize(offs + iosz);
fp->UnLock();
return (ssize_t)iosz;
}
/******************************************************************************/
void XrdPosixXrootd::Pwrite(int fildes, const void *buf, size_t nbyte,
off_t offset, XrdPosixCallBackIO *cbp)
{
XrdPosixFile *fp;
long long offs;
int iosz;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) {cbp->Complete(-1); return;}
// Make sure the size is not too large
//
if (nbyte > (size_t)0x7fffffff)
{fp->UnLock();
errno = EOVERFLOW;
cbp->Complete(-1);
return;
}
// Prepare for the writing
//
cbp->theFile = fp;
fp->Ref(); fp->UnLock();
iosz = static_cast<int>(nbyte);
offs = static_cast<long long>(offset);
// Issue the read
//
fp->XCio->Write(*cbp, (char *)buf, offs, (int)iosz);
}
/******************************************************************************/
/* R e a d */
/******************************************************************************/
ssize_t XrdPosixXrootd::Read(int fildes, void *buf, size_t nbyte)
{
XrdPosixFile *fp;
long long bytes;
int iosz;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) return -1;
// Make sure the size is not too large
//
if (nbyte > (size_t)0x7fffffff) return Fault(fp,EOVERFLOW);
else iosz = static_cast<int>(nbyte);
// Issue the read
//
bytes = fp->XCio->Read((char *)buf,fp->Offset(),(int)iosz);
if (bytes < 0) return Fault(fp,-bytes);
// All went well
//
fp->addOffset(bytes);
fp->UnLock();
return (ssize_t)bytes;
}
/******************************************************************************/
/* R e a d v */
/******************************************************************************/
ssize_t XrdPosixXrootd::Readv(int fildes, const struct iovec *iov, int iovcnt)
{
ssize_t bytes, totbytes = 0;
int i;
// Return the results of the read for each iov segment
//
for (i = 0; i < iovcnt; i++)
{bytes = Read(fildes,(void *)iov[i].iov_base,(size_t)iov[i].iov_len);
if (bytes > 0) totbytes += bytes;
else if (bytes < 0) return -1;
else break;
}
// All done
//
return totbytes;
}
/******************************************************************************/
/* V R e a d */
/******************************************************************************/
ssize_t XrdPosixXrootd::VRead(int fildes, const XrdOucIOVec *readV, int n)
{
XrdPosixFile *fp;
ssize_t bytes;
// Find the file object
//
if (!(fp = XrdPosixObject::File(fildes))) return -1;
// Issue the read
//
if ((bytes = fp->XCio->ReadV(readV, n)) < 0) return Fault(fp,-bytes);
// Return bytes read
//
fp->UnLock();
return bytes;
}
/******************************************************************************/
void XrdPosixXrootd::VRead(int fildes, const XrdOucIOVec *readV, int n,
XrdPosixCallBackIO *cbp)
{
XrdPosixFile *fp;
// Find the file object and issue read
//
if ((fp = XrdPosixObject::File(fildes)))
{cbp->theFile = fp;
fp->Ref(); fp->UnLock();
fp->XCio->ReadV(*cbp, readV, n);
} else cbp->Complete(-1);
}
/******************************************************************************/
/* R e a d d i r */
/******************************************************************************/
struct dirent* XrdPosixXrootd::Readdir(DIR *dirp)
{
dirent64 *dp64;
dirent *dp32; // Could be the same as dp64
if (!(dp64 = Readdir64(dirp))) return 0;
dp32 = (struct dirent *)dp64;
if ((char*)dp32->d_name != (char*)dp64->d_name)
{dp32->d_ino = dp64->d_ino;
#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__GNU__) && !(defined(__FreeBSD_kernel__) && defined(__GLIBC__))
dp32->d_off = dp64->d_off;
#endif
#ifndef __solaris__
dp32->d_type = dp64->d_type;
#endif
dp32->d_reclen = dp64->d_reclen;
strcpy(dp32->d_name, dp64->d_name);
}
return dp32;
}
struct dirent64* XrdPosixXrootd::Readdir64(DIR *dirp)
{
XrdPosixDir *dP;
dirent64 *dentP;
int rc, fildes = XrdPosixDir::dirNo(dirp);
// Find the object
//
if (!(dP = XrdPosixObject::Dir(fildes)))
{errno = EBADF; return 0;}
// Get the next directory entry
//
if (!(dentP = dP->nextEntry())) rc = dP->Status();
else rc = 0;
// Return the appropriate result
//
dP->UnLock();
if (rc) errno = rc;
return dentP;
}
/******************************************************************************/
/* R e a d d i r _ r */