Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-06 10:31:20

0001 #ifndef __XRDOUCCACHE_HH__
0002 #define __XRDOUCCACHE_HH__
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                        X r d O u c C a c h e . h h                         */
0006 /*                                                                            */
0007 /* (c) 2019 by the Board of Trustees of the Leland Stanford, Jr., University  */
0008 /*                            All Rights Reserved                             */
0009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
0010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
0011 /*                                                                            */
0012 /* This file is part of the XRootD software suite.                            */
0013 /*                                                                            */
0014 /* XRootD is free software: you can redistribute it and/or modify it under    */
0015 /* the terms of the GNU Lesser General Public License as published by the     */
0016 /* Free Software Foundation, either version 3 of the License, or (at your     */
0017 /* option) any later version.                                                 */
0018 /*                                                                            */
0019 /* XRootD is distributed in the hope that it will be useful, but WITHOUT      */
0020 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or      */
0021 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public       */
0022 /* License for more details.                                                  */
0023 /*                                                                            */
0024 /* You should have received a copy of the GNU Lesser General Public License   */
0025 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file  */
0026 /* COPYING (GPL license).  If not, see <http://www.gnu.org/licenses/>.        */
0027 /*                                                                            */
0028 /* The copyright holder's institutional names and contributor's names may not */
0029 /* be used to endorse or promote products derived from this software without  */
0030 /* specific prior written permission of the institution or contributor.       */
0031 /******************************************************************************/
0032 
0033 #include <cerrno>
0034 #include <cstdint>
0035 #include <vector>
0036 
0037 #include "XrdOuc/XrdOucCacheStats.hh"
0038 #include "XrdOuc/XrdOucIOVec.hh"
0039 
0040 struct stat;
0041 class  XrdOucEnv;
0042 
0043 /******************************************************************************/
0044 /*                       X r d O u c C a c h e I O C B                        */
0045 /******************************************************************************/
0046 
0047 //-----------------------------------------------------------------------------
0048 //! The XrdOucCacheIOCB defines a callback object that must be used to handle
0049 //! asynchronous I/O operations.
0050 //-----------------------------------------------------------------------------
0051 
0052 class XrdOucCacheIOCB
0053 {
0054 public:
0055 
0056 //------------------------------------------------------------------------------
0057 //! Handle result from a previous async operation.
0058 //!
0059 //! @param result is result from a previous operation. Successful results are
0060 //!               always values >= 0 while errors are negative values and are
0061 //!               always '-errno' indicate the reason for the error.
0062 //------------------------------------------------------------------------------
0063 virtual
0064 void     Done(int result) = 0;
0065 
0066          XrdOucCacheIOCB() {}
0067 virtual ~XrdOucCacheIOCB() {}
0068 };
0069 
0070 /******************************************************************************/
0071 /*                       X r d O u c C a c h e I O C D                        */
0072 /******************************************************************************/
0073 
0074 //-----------------------------------------------------------------------------
0075 //! The XrdOucCacheIOCD defines a callback object that is used to handle
0076 //! XrdOucCacheIO::Detach() requests. It is passed to Detach() and if Detach()
0077 //! returns false then the DetachDone() method must be called when the object
0078 //! has been successfully detached from the cache.
0079 //-----------------------------------------------------------------------------
0080 
0081 class XrdOucCacheIOCD
0082 {
0083 public:
0084 
0085 //------------------------------------------------------------------------------
0086 //! Indicate that the CacheIO object has been detached.
0087 //------------------------------------------------------------------------------
0088 virtual
0089 void     DetachDone() = 0;
0090 
0091          XrdOucCacheIOCD() {}
0092 virtual ~XrdOucCacheIOCD() {}
0093 };
0094   
0095 /******************************************************************************/
0096 /*                   C l a s s   X r d O u c C a c h e I O                    */
0097 /******************************************************************************/
0098 
0099 //------------------------------------------------------------------------------
0100 //! The XrdOucCacheIO object is responsible for interacting with the original
0101 //! data source/target. It can be used with or without a front-end cache.
0102 //------------------------------------------------------------------------------
0103 
0104 class XrdOucCacheIO
0105 {
0106 public:
0107 
0108 //------------------------------------------------------------------------------
0109 //! Detach this CacheIO object from the cache.
0110 //!
0111 //! @note   This method must be called instead of using the delete operator
0112 //!         since CacheIO objects may have multiple outstanding references and
0113 //!         actual deletion may need to be deferred.
0114 //!
0115 //! @param  iocd   reference to the detach complete callback object.
0116 //!
0117 //! @return true   Deletion can occur immediately. There is no outstanding I/O.
0118 //! @return false  Deletion must be deferred until it is safe to so from the
0119 //!                cache perspective. At which point, the cache will call the
0120 //!                DetachDone() method in the passed callback object. No locks
0121 //!                may be held with respect to the CacheIO object when this is
0122 //!                done to avoid deadlocks.
0123 //------------------------------------------------------------------------------
0124 
0125 virtual bool Detach(XrdOucCacheIOCD &iocd) = 0;
0126 
0127 //------------------------------------------------------------------------------
0128 //! Obtain size of the file.
0129 //!
0130 //! @return Size of the file in bytes.
0131 //------------------------------------------------------------------------------
0132 virtual
0133 long long    FSize() = 0;
0134 
0135 //------------------------------------------------------------------------------
0136 //! Perform an fstat() operation (defaults to passthrough).
0137 //!
0138 //! @param  sbuff  reference to the stat buffer to be filled in. Only fields
0139 //!                st_size, st_blocks, st_mtime (st_atime and st_ctime may be
0140 //!                set to st_mtime), st_ino, and st_mode need to be set. All
0141 //!                other fields are preset and should not be changed.
0142 //!
0143 //! @return <0 - fstat failed, value is -errno.
0144 //!         =0 - fstat succeeded, sbuff holds stat information.
0145 //!         >0 - fstat could not be done, forward operation to next level.
0146 //------------------------------------------------------------------------------
0147 
0148 virtual int  Fstat(struct stat &sbuff) {(void)sbuff; return 1;}
0149 
0150 //-----------------------------------------------------------------------------
0151 //! Get the file's location (i.e. endpoint hostname and port)
0152 //!
0153 //! @param  refresh - when true, recomputes the location in case it changed st
0154 //!                   the location is cached from the previous successful call.
0155 //!
0156 //! @return A pointer to the file's location. It remains valid until the file
0157 //!         is closed or Location() is called with refresh set to true.
0158 //!         A null string means the file is not open or location is unknown.
0159 //-----------------------------------------------------------------------------
0160 virtual
0161 const char  *Location(bool refresh=false) {(void)refresh; return "";}
0162 
0163 //------------------------------------------------------------------------------
0164 //! Get the path associated with this object.
0165 //!
0166 //! @return Pointer to the path.
0167 //------------------------------------------------------------------------------
0168 virtual
0169 const char  *Path() = 0;
0170 
0171 //-----------------------------------------------------------------------------
0172 //! Read file pages into a buffer and return corresponding checksums.
0173 //!
0174 //! @param  buff  pointer to buffer where the bytes are to be placed.
0175 //! @param  offs  The offset where the read is to start.
0176 //! @param  rdlen The number of bytes to read.
0177 //! @param  csvec A vector whose entries which will be filled with the
0178 //!               corresponding CRC32C checksum for each page or pgae segment.
0179 //!               If a zero length vector is returned, checksums are not present.
0180 //! @param  opts  Processing options:
0181 //!               forceCS - always return checksums even when not available.
0182 //! @param  csfix When not nil, returns the number of corrected checksum errs.
0183 //!
0184 //! @return >= 0      The number of bytes placed in buffer.
0185 //! @return -errno    File could not be read, return value is the reason.
0186 //-----------------------------------------------------------------------------
0187 
0188 static const uint64_t forceCS = 0x0000000000000001ULL;
0189 
0190 virtual int  pgRead(char                  *buff,
0191                     long long              offs,
0192                     int                    rdlen,
0193                     std::vector<uint32_t> &csvec,
0194                     uint64_t               opts=0,
0195                     int                   *csfix=0);
0196 
0197 //-----------------------------------------------------------------------------
0198 //! Read file pages and checksums using asynchronous I/O (default sync).
0199 //!
0200 //! @param  iocb  reference to the callback object that receives the result. All
0201 //!               results are returned via this object's Done() method. If the
0202 //!               caller holds any locks they must be recursive locks as the
0203 //!               callback may occur on the calling thread.
0204 //! @param  buff  pointer to buffer where the bytes are to be placed.
0205 //! @param  offs  The offset where the read is to start.
0206 //! @param  rdlen The number of bytes to read.
0207 //! @param  csvec A vector which will be filled with the corresponding
0208 //!               CRC32C checksum for each page or page segment.
0209 //! @param  opts  Processing options:
0210 //!               forceCS - always return checksums even when not available.
0211 //! @param  csfix When not nil, returns the number of corrected checksum errs.
0212 //-----------------------------------------------------------------------------
0213 
0214 virtual void pgRead(XrdOucCacheIOCB       &iocb,
0215                     char                  *buff,
0216                     long long              offs,
0217                     int                    rdlen,
0218                     std::vector<uint32_t> &csvec,
0219                     uint64_t               opts=0,
0220                     int                   *csfix=0)
0221                    {iocb.Done(pgRead(buff, offs, rdlen, csvec, opts, csfix));}
0222 
0223 //-----------------------------------------------------------------------------
0224 //! Write file pages from a buffer and corresponding verified checksums.
0225 //!
0226 //! @param  buff  pointer to buffer holding the bytes to be written.
0227 //! @param  offs  The offset where the write is to start.
0228 //! @param  wrlen The number of bytes to write.
0229 //!               offs+wrlen (i.e. it establishes an end of file).
0230 //! @param  csvec A vector of that holds the corresponding verified CRC32C
0231 //!               checksum for each page or page segment.
0232 //! @param  opts  Processing options.
0233 //! @param  csfix When not nil, returns the number of corrected checksum errs.
0234 //!
0235 //! @return >= 0      The number of bytes written.
0236 //! @return -errno    File could not be written, returned value is the reason.
0237 //-----------------------------------------------------------------------------
0238 
0239 virtual int  pgWrite(char                  *buff,
0240                      long long              offs,
0241                      int                    wrlen,
0242                      std::vector<uint32_t> &csvec,
0243                      uint64_t               opts=0,
0244                      int                   *csfix=0);
0245 
0246 //-----------------------------------------------------------------------------
0247 //! Write file pages and checksums using asynchronous I/O (default sync).
0248 //!
0249 //! @param  iocb  reference to the callback object that receives the result. All
0250 //!               results are returned via this object's Done() method. If the
0251 //!               caller holds any locks they must be recursive locks as the
0252 //!               callback may occur on the calling thread.
0253 //! @param  buff  pointer to buffer holding the bytes to be written.
0254 //! @param  offs  The offset where the write is to start.
0255 //! @param  wrlen The number of bytes to write.
0256 //! @param  csvec A vector of that holds the corresponding verified CRC32C
0257 //!               checksum for each page or page segment.
0258 //! @param  opts  Processing options.
0259 //! @param  csfix When not nil, returns the number of corrected checksum errs.
0260 //-----------------------------------------------------------------------------
0261 
0262 virtual void pgWrite(XrdOucCacheIOCB       &iocb,
0263                      char                  *buff,
0264                      long long              offs,
0265                      int                    wrlen,
0266                      std::vector<uint32_t> &csvec,
0267                      uint64_t               opts=0,
0268                      int                   *csfix=0)
0269                     {iocb.Done(pgWrite(buff, offs, wrlen, csvec, opts, csfix));}
0270 
0271 //------------------------------------------------------------------------------
0272 //! Perform an asynchronous preread (may be ignored).
0273 //!
0274 //! @param  offs  the offset into the file.
0275 //! @param  rlen  the number of bytes to preread into the cache.
0276 //! @param  opts  one or more of the options defined below.
0277 //------------------------------------------------------------------------------
0278 
0279 static const int SingleUse = 0x0001; //!< Mark pages for single use
0280 
0281 virtual void Preread(long long offs, int rlen, int opts=0)
0282                     {(void)offs; (void)rlen; (void)opts;}
0283 
0284 //-----------------------------------------------------------------------------
0285 //! Set automatic preread parameters for this file (may be ignored).
0286 //!
0287 //! @param  aprP Reference to preread parameters.
0288 //-----------------------------------------------------------------------------
0289 
0290 struct aprParms
0291       {int   Trigger;   // preread if (rdln < Trigger)        (0 -> pagesize+1)
0292        int   prRecalc;  // Recalc pr efficiency every prRecalc bytes   (0->50M)
0293        int   Reserve1;
0294        short minPages;  // If rdln/pgsz < min,  preread minPages       (0->off)
0295        signed
0296        char  minPerf;   // Minimum auto preread performance required   (0->n/a)
0297        char  Reserve2;
0298        void *Reserve3;
0299 
0300              aprParms() : Trigger(0),  prRecalc(0), Reserve1(0),
0301                           minPages(0), minPerf(90), Reserve2(0), Reserve3(0) {}
0302       };
0303 
0304 virtual void Preread(aprParms &Parms) { (void)Parms; }
0305 
0306 //------------------------------------------------------------------------------
0307 //! Perform an synchronous read.
0308 //!
0309 //! @param  buff  pointer to the buffer to receive the results. The buffer must
0310 //!               remain valid until the callback is invoked.
0311 //! @param  offs  the offset into the file.
0312 //! @param  rlen  the number of bytes to read.
0313 //!
0314 //! @return       < 0 - Read failed, value is -errno.
0315 //!               >=0 - Read succeeded, value is number of bytes read.
0316 //------------------------------------------------------------------------------
0317 
0318 virtual int  Read (char *buff, long long offs, int rlen) = 0;
0319 
0320 //------------------------------------------------------------------------------
0321 //! Perform an asynchronous read (defaults to synchronous).
0322 //!
0323 //! @param  iocb  reference to the callback object that receives the result. All
0324 //!               results are returned via this object's Done() method. If the
0325 //!               caller holds any locks they must be recursive locks as the
0326 //!               callback may occur on the calling thread.
0327 //! @param  buff  pointer to the buffer to receive the results. The buffer must
0328 //!               remain valid until the callback is invoked.
0329 //! @param  offs  the offset into the file.
0330 //! @param  rlen  the number of bytes to read.
0331 //------------------------------------------------------------------------------
0332 
0333 virtual void Read (XrdOucCacheIOCB &iocb, char *buff, long long offs, int rlen)
0334                   {iocb.Done(Read(buff, offs, rlen));}
0335 
0336 //------------------------------------------------------------------------------
0337 //! Perform an synchronous vector read.
0338 //!
0339 //! @param  readV pointer to a vector of read requests.
0340 //! @param  rnum  the number of elements in the vector.
0341 //!
0342 //! @return       < 0 - ReadV failed, value is -errno.
0343 //!               >=0 - ReadV succeeded, value is number of bytes read.
0344 //------------------------------------------------------------------------------
0345 
0346 virtual int  ReadV(const XrdOucIOVec *readV, int rnum);
0347 
0348 //------------------------------------------------------------------------------
0349 //! Perform an asynchronous vector read (defaults to synchronous).
0350 //!
0351 //! @param  iocb  reference to the callback object that receives the result. All
0352 //!               results are returned via this object's Done() method. If the
0353 //!               caller holds any locks they must be recursive locks as the
0354 //!               callback may occur on the calling thread.
0355 //! @param  readV pointer to a vector of read requests.
0356 //! @param  rnum  the number of elements in the vector.
0357 //------------------------------------------------------------------------------
0358 
0359 virtual void ReadV(XrdOucCacheIOCB &iocb, const XrdOucIOVec *readV, int rnum)
0360                   {iocb.Done(ReadV(readV, rnum));}
0361 
0362 //------------------------------------------------------------------------------
0363 //! Perform an synchronous sync() operation.
0364 //!
0365 //! @return       <0 - Sync failed, value is -errno.
0366 //!               =0 - Sync succeeded.
0367 //------------------------------------------------------------------------------
0368 
0369 virtual int  Sync() = 0;
0370 
0371 //------------------------------------------------------------------------------
0372 //! Perform an asynchronous sync() operation (defaults to synchronous).
0373 //!
0374 //! @param  iocb  reference to the callback object that receives the result. All
0375 //!               results are returned via this object's Done() method. If the
0376 //!               caller holds any locks they must be recursive locks as the
0377 //!               callback may occur on the calling thread.
0378 //------------------------------------------------------------------------------
0379 
0380 virtual void Sync(XrdOucCacheIOCB &iocb) {iocb.Done(Sync());}
0381 
0382 //------------------------------------------------------------------------------
0383 //! Perform an synchronous trunc() operation.
0384 //!
0385 //! @param  offs  the size the file is have.
0386 //!
0387 //! @return       <0 - Trunc failed, value is -errno.
0388 //!               =0 - Trunc succeeded.
0389 //------------------------------------------------------------------------------
0390 
0391 virtual int  Trunc(long long offs) = 0;
0392 
0393 //------------------------------------------------------------------------------
0394 //! Perform an asynchronous trunc() operation (defaults to synchronous).
0395 //!
0396 //! @param  iocb  reference to the callback object that receives the result. All
0397 //!               results are returned via this object's Done() method. If the
0398 //!               caller holds any locks they must be recursive locks as the
0399 //!               callback may occur on the calling thread.
0400 //! @param  offs  the size the file is have.
0401 //------------------------------------------------------------------------------
0402 
0403 virtual void Trunc(XrdOucCacheIOCB &iocb, long long offs)
0404                   {iocb.Done(Trunc(offs));}
0405 
0406 //------------------------------------------------------------------------------
0407 //! Update the originally passed XrdOucCacheIO object with the object passed.
0408 //! All future uses underlying XrdOucCacheIO object must now use this object.
0409 //! Update() is called when Prepare() indicated that the file should not be
0410 //! physically opened and a file method was invoked in the XrdOucCacheIO
0411 //! passed to Attach(). When this occurs, the file is actually opened and
0412 //! Update() called to replace the original XrdOucCacheIO object with one
0413 //! that uses the newly opened file.
0414 //!
0415 //! @param iocp   reference to the new XrdOucCacheIO object.
0416 //------------------------------------------------------------------------------
0417 
0418 virtual void Update(XrdOucCacheIO &iocp) {}
0419 
0420 //------------------------------------------------------------------------------
0421 //! Perform an synchronous write.
0422 //!
0423 //! @param  buff  pointer to the buffer holding the contents. The buffer must
0424 //!               remain valid until the callback is invoked.
0425 //! @param  offs  the offset into the file.
0426 //! @param  wlen  the number of bytes to write
0427 //!
0428 //! @return       < 0 - Write failed, value is -errno.
0429 //!               >=0 - Write succeeded, value is number of bytes written.
0430 //------------------------------------------------------------------------------
0431 
0432 virtual int  Write(char *buff, long long offs, int wlen) = 0;
0433 
0434 //------------------------------------------------------------------------------
0435 //! Perform an asynchronous write (defaults to synchronous).
0436 //!
0437 //! @param  iocb  reference to the callback object that receives the result. All
0438 //!               results are returned via this object's Done() method. If the
0439 //!               caller holds any locks they must be recursive locks as the
0440 //!               callback may occur on the calling thread.
0441 //! @param  buff  pointer to the buffer holding the contents. The buffer must
0442 //!               remain valid until the callback is invoked.
0443 //! @param  offs  the offset into the file.
0444 //! @param  wlen  the number of bytes to write
0445 //------------------------------------------------------------------------------
0446 
0447 virtual void Write(XrdOucCacheIOCB &iocb, char *buff, long long offs, int wlen)
0448                   {iocb.Done(Write(buff, offs, wlen));}
0449 
0450 //------------------------------------------------------------------------------
0451 //! Perform an synchronous vector write.
0452 //!
0453 //! @param  writV pointer to a vector of write requests.
0454 //! @param  wnum  the number of elements in the vector.
0455 //!
0456 //! @return       < 0 - WriteV failed, value is -errno.
0457 //!               >=0 - WriteV succeeded, value is number of bytes written.
0458 //------------------------------------------------------------------------------
0459 
0460 virtual int  WriteV(const XrdOucIOVec *writV, int wnum);
0461 
0462 //------------------------------------------------------------------------------
0463 //! Perform an asynchronous vector write (defaults to synchronous).
0464 //!
0465 //! @param  iocb  reference to the callback object that receives the result. All
0466 //!               results are returned via this object's Done() method. If the
0467 //!               caller holds any locks they must be recursive locks as the
0468 //!               callback may occur on the calling thread.
0469 //! @param  writV pointer to a vector of read requests.
0470 //! @param  wnum  the number of elements in the vector.
0471 //------------------------------------------------------------------------------
0472 
0473 virtual void WriteV(XrdOucCacheIOCB &iocb, const XrdOucIOVec *writV, int wnum)
0474                   {iocb.Done(WriteV(writV, wnum));}
0475 
0476 //------------------------------------------------------------------------------
0477 //! Construct and Destructor
0478 //------------------------------------------------------------------------------
0479 
0480             XrdOucCacheIO() {}
0481 protected:
0482 virtual    ~XrdOucCacheIO() {}  // Always use Detach() instead of direct delete!
0483 };
0484 
0485 /******************************************************************************/
0486 /*                     C l a s s   X r d O u c C a c h e                      */
0487 /******************************************************************************/
0488   
0489 //------------------------------------------------------------------------------
0490 //! The XrdOucCache class is used to define a cache. The cache is associated
0491 //! with one or more XrdOucCacheIO objects using the Attach() method.
0492 //------------------------------------------------------------------------------
0493 
0494 class XrdOucCache
0495 {
0496 public:
0497 
0498 //------------------------------------------------------------------------------
0499 //! Obtain a new XrdOucCacheIO object that fronts an existing XrdOucCacheIO
0500 //! with this cache. Upon success a pointer to a new XrdOucCacheIO object is
0501 //! returned and must be used to read and write data with the cache interposed.
0502 //! Upon failure, the original XrdOucCacheIO object is returned with errno set.
0503 //! You can continue using the object without any cache. The new cache should
0504 //! use the methods in the passed CacheIO object to perform I/O operatios.
0505 //!
0506 //! @param  ioP     Pointer to the current CacheIO object used for I/O.
0507 //! @param  opts    Cache options as defined below. When specified, they
0508 //!                 override the default options associated with the cache,
0509 //!                 except for optRW, optNEW, and optWIN which are valid only
0510 //!                 for a r/w cache.
0511 //!
0512 //! @return Pointer to a new XrdOucCacheIO object (success) or the original
0513 //!         XrdOucCacheIO object (failure) with errno set.
0514 //------------------------------------------------------------------------------
0515 
0516 static const int optFIS = 0x0001; //!< File is structured   (e.g. root file)
0517 static const int optRW  = 0x0004; //!< File is read/write   (o/w read/only)
0518 static const int optNEW = 0x0014; //!< File is new -> optRW (o/w read or write)
0519 static const int optWIN = 0x0024; //!< File is new -> optRW use write-in cache
0520 
0521 virtual
0522 XrdOucCacheIO *Attach(XrdOucCacheIO *ioP, int opts=0) = 0;
0523 
0524 //------------------------------------------------------------------------------
0525 //! Get the path to a file that is complete in the local cache. By default, the
0526 //! file must be complete in the cache (i.e. no blocks are missing). This can
0527 //! be overridden. Thes path can be used to access the file on the local node.
0528 //!
0529 //! @param  url    - Pointer to the url of interest.
0530 //! @param  buff   - Pointer to a buffer to receive the local path to the file.
0531 //!                  If nil, no path is returned.
0532 //! @param  blen   - Length of the buffer, buff. If zero, no path is returned.
0533 //! @param  why    - One of the LFP_Reason enums describing the call:
0534 //!                  ForAccess - the path will be used to access the file. If
0535 //!                              the file is complete, the system will delay
0536 //!                              purging the file for a configurable window,
0537 //!                              should a purge be imminent. A null path is
0538 //!                              returned for any non-zero return code.
0539 //!                  ForInfo   - same as ForAccess except that purging will
0540 //!                              not be delayed if imminent. A path is always
0541 //!                              returned, if possible. Otherwise the first
0542 //!                              byte of any supplied buffer is set to 0.
0543 //!                  ForPath   - Only the path is wanted and no checks need
0544 //!                              be performed. The only possible errors are
0545 //!                              -EINVAL and -ENAMETOOLONG.
0546 //! @param  forall - When ForAccess is specified: when true makes the file
0547 //!                  world readable; otherwise, only group readable. The
0548 //!                  parameter is ignored unless "why" is ForAccess and a
0549 //!                  local file path is requested to be returned.
0550 //!
0551 //! @return 0      - the file is complete and the local path to the file is in
0552 //!                  the buffer, if it has been supllied.
0553 //!
0554 //! @return <0     - the request could not be fulfilled. The return value is
0555 //!                  -errno describing why. If a buffer was supplied and a
0556 //!                  path could be generated it is returned only if "why" is
0557 //!                  ForInfo or ForPath. Otherwise, a null path is returned.
0558 //!                  
0559 //!                  Common return codes are:
0560 //!                  -EINVAL       an argument is invalid.
0561 //!                  -EISDIR       target is a directory not a file.
0562 //!                  -ENAMETOOLONG buffer not big enough to hold path.
0563 //!                  -ENOENT       file not in cache
0564 //!                  -ENOTSUP      method not implemented
0565 //!                  -EREMOTE      file is incomplete
0566 //!
0567 //! @return >0     - Reserved for future use.
0568 //------------------------------------------------------------------------------
0569 
0570 enum LFP_Reason {ForAccess=0, ForInfo, ForPath};
0571 
0572 virtual int    LocalFilePath(const char *url, char *buff=0, int blen=0,
0573                              LFP_Reason why=ForAccess, bool forall=false)
0574                              {(void)url; (void)buff; (void)blen; (void)why;
0575                               (void)forall;
0576                              if (buff && blen > 0) *buff = 0;
0577                               return -ENOTSUP;
0578                              }
0579 
0580 //------------------------------------------------------------------------------
0581 //! Prepare the cache for a file open request. This method is called prior to
0582 //! actually opening a file. This method is meant to allow defering an open
0583 //! request or implementing the full I/O stack in the cache layer.
0584 //!
0585 //! @param  url    - Pointer to the url about to be opened.
0586 //! @param  oflags - Standard Unix open flags (see open(2)).
0587 //! @param  mode   - Standard mode flags if file is being created.
0588 //!
0589 //! @return <0 Error has occurred, return value is -errno; fail open request. 
0590 //!            The error code -EUSERS may be returned to trigger overload
0591 //!            recovery as specified by the xrootd.fsoverload directive. No
0592 //!            other method should return this error code.
0593 //!         =0 Continue with open() request.
0594 //!         >0 Defer open but treat the file as actually being open.
0595 //------------------------------------------------------------------------------
0596 
0597 virtual int    Prepare(const char *url, int oflags, mode_t mode)
0598                       {(void)url; (void)oflags; (void)mode; return 0;}
0599 
0600 //------------------------------------------------------------------------------
0601 //! Rename a file in the cache.
0602 //!
0603 //! @param  oldp - the existing path to be renamed.
0604 //! @param  newp - the new name it is to have.
0605 //!
0606 //! @return Success: 0
0607 //! @return Failure: -errno
0608 //------------------------------------------------------------------------------
0609 
0610 virtual int   Rename(const char* oldp, const char* newp)
0611                     {(void)oldp; (void)newp; return 0;}
0612 
0613 //------------------------------------------------------------------------------
0614 //! Remove a directory from the cache.
0615 //!
0616 //! @param  dirp - the existing directory path to be removed.
0617 //!
0618 //! @return Success: 0
0619 //! @return Failure: -errno
0620 //------------------------------------------------------------------------------
0621 
0622 virtual int   Rmdir(const char* dirp) {(void)dirp; return 0;}
0623 
0624 //------------------------------------------------------------------------------
0625 //! Perform a stat() operation (defaults to passthrough).
0626 //!
0627 //! @param url    pointer to the url whose stat information is wanted.
0628 //! @param sbuff  reference to the stat buffer to be filled in. Only fields
0629 //!               st_size, st_blocks, st_mtime (st_atime and st_ctime may be
0630 //!               set to st_mtime), st_ino, and st_mode need to be set. All
0631 //!               other fields are preset and should not be changed.
0632 //!
0633 //! @return <0 - Stat failed, value is -errno.
0634 //!         =0 - Stat succeeded, sbuff holds stat information.
0635 //!         >0 - Stat could not be done, forward operation to next level.
0636 //------------------------------------------------------------------------------
0637 
0638 virtual int  Stat(const char *url, struct stat &sbuff)
0639                  {(void)url; (void)sbuff; return 1;}
0640 
0641 //------------------------------------------------------------------------------
0642 //! Truncate a file in the cache to a specified size.
0643 //!
0644 //! @param  path - the path of the file to truncate.
0645 //! @param  size - the size in bytes it is to have.
0646 //!
0647 //! @return Success: 0
0648 //! @return Failure: -errno
0649 //------------------------------------------------------------------------------
0650 
0651 virtual int   Truncate(const char* path, off_t size)
0652                       {(void)path; (void)size; return 0;}
0653 
0654 //------------------------------------------------------------------------------
0655 //! Remove a file from the cache.
0656 //!
0657 //! @param  path - the path of the file to be removed.
0658 //!
0659 //! @return Success: 0
0660 //! @return Failure: -errno
0661 //------------------------------------------------------------------------------
0662 
0663 virtual int   Unlink(const char* path) {(void)path; return 0;}
0664 
0665 //------------------------------------------------------------------------------
0666 //! Perform special operation on the cache.
0667 //!
0668 //! @param cmd     - The operation to be performed.
0669 //! @param arg     - The operation argument(s).
0670 //! @param arglen  - The length of arg.
0671 //!
0672 //! @return Success: 0
0673 //! @return Failure: -errno
0674 //------------------------------------------------------------------------------
0675 
0676 enum XeqCmd {xeqNoop = 0};
0677 
0678 virtual int    Xeq(XeqCmd cmd, char *arg, int arglen)
0679                   {(void)cmd; (void)arg; (void)arglen; return -ENOTSUP;}
0680 
0681 //------------------------------------------------------------------------------
0682 //! The following holds statistics for the cache itself. It is updated as
0683 //! associated cacheIO objects are deleted and their statistics are added.
0684 //------------------------------------------------------------------------------
0685 
0686 XrdOucCacheStats Statistics;
0687 
0688 //------------------------------------------------------------------------------
0689 //! A 1-to-7 character cache type identifier (usually pfc or rmc).
0690 //------------------------------------------------------------------------------
0691 
0692 const char CacheType[8];
0693 
0694 //------------------------------------------------------------------------------
0695 //! Constructor
0696 //!
0697 //! @param ctype   - A 1-to-7 character cache type identifier.
0698 //------------------------------------------------------------------------------
0699 
0700                XrdOucCache(const char *ctype) : CacheType{}
0701 //                        : CacheType({'\0','\0','\0','\0','\0','\0','\0','\0'})
0702                           {strncpy(const_cast<char *>(CacheType), ctype,
0703                                    sizeof(CacheType));
0704                            const_cast<char *>(CacheType)[sizeof(CacheType)-1]=0;
0705                           }
0706 
0707 //------------------------------------------------------------------------------
0708 //! Destructor
0709 //------------------------------------------------------------------------------
0710 
0711 virtual       ~XrdOucCache() {}
0712 };
0713 
0714 /******************************************************************************/
0715 /*               C r e a t i n g   C a c h e   P l u g - I n s                */
0716 /******************************************************************************/
0717   
0718 //------------------------------------------------------------------------------
0719 //! Your cache plug-in must exist in a shared library and have the following
0720 //! extern C function defined whose parameters are:
0721 //!
0722 //! @param Logger  Pointer to the logger object that should be used with an
0723 //!                instance of XrdSysError to direct messages to a log file.
0724 //!                If Logger is null, you should use std::cerr to output messages.
0725 //! @param Config  Pointer to the configuration file name from where you
0726 //!                should get additional information. If Config is null, there
0727 //!                is no configuration file is present.
0728 //! @param Parms   Pointer to any parameters specified after the shared library
0729 //!                path. If Parms is null, there are no parameters.
0730 //! @param envP    Pointer to environmental information. The most relevant
0731 //!                is whether or not gStream monitoring is enabled.
0732 //!                @code {.cpp}
0733 //!                XrdXrootdGStream *gStream = (XrddXrootdGStream *)
0734 //!                                            envP->GetPtr("pfc.gStream*");
0735 //!                @endcode
0736 //! @return        A usable, fully configured, instance of an XrdOucCache
0737 //!                object upon success and a null pointer otherwise. This
0738 //!                instance is used for all operations defined by methods in
0739 //!                XrdOucCache base class.
0740 //!
0741 //! @code {.cpp}
0742 //! extern "C"
0743 //! {
0744 //! XrdOucCache *XrdOucGetCache(XrdSysLogger *Logger, // Where messages go
0745 //!                             const char   *Config, // Config file used
0746 //!                             const char   *Parms,  // Optional parm string
0747 //!                             XrdOucEnv    *envP);  // Optional environment
0748 //! }
0749 //! @endcode
0750 
0751 //------------------------------------------------------------------------------
0752 //! Declare compilation version.
0753 //!
0754 //! Additionally, you *should* declare the xrootd version you used to compile
0755 //! your plug-in. Declare it as shown below.
0756 //------------------------------------------------------------------------------
0757 
0758 /*!
0759         #include "XrdVersion.hh"
0760         XrdVERSIONINFO(XrdOucGetCache,<name>);
0761 
0762     where <name> is a 1- to 15-character unquoted name identifying your plugin.
0763 */
0764 
0765 typedef XrdOucCache *(*XrdOucCache_t)(XrdSysLogger *Logger, const char *Config,
0766                                       const char   *Parms,  XrdOucEnv  *envP);
0767 
0768 #endif