Bdb packages | Design docs | Source docs | Guidelines | Recent releases

Search | Site Map .

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/BdbAccess/BdbFSMgr.cc

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // File:
00003 //      BdbFSMgr.cc
00004 //
00005 // Description:
00006 //      Class BdbFSMgr implementation. The class provides an API to all
00007 //      the file system operations
00008 //
00009 // Environment:
00010 //      Software developed for the BaBar Detector at the SLAC B-Factory
00011 //
00012 // Author List:
00013 //      Jacek Becla                Original Author
00014 //
00015 // Copyright Information:
00016 //      Copyright (C) 1997      Stanford Linear Accelerator Center
00017 //
00018 //------------------------------------------------------------------------------
00019 
00020 //-----------------------
00021 // This Class's Header --
00022 //-----------------------
00023 #ifndef BDBFSMGR_HH
00024 #include "BdbAccess/BdbFSMgr.hh"
00025 #endif
00026 
00027 
00028 //-------------------------------
00029 // Collaborating Class Headers --
00030 //-------------------------------
00031 #include "BdbApplication/BdbDebug.hh"
00032 #include "BdbAccess/BdbClusterConfigMgr.hh"
00033 #include "BdbAccess/BdbFileConfigSpec.hh"
00034 #include "BdbAccess/BdbDomainSpec.hh"
00035 #include "BdbAccess/BdbCatalogFSHandler.hh"
00036 #include "BdbPud/BdbPud.hh"
00037 #include "BdbAccess/BdbAccessErrors.hh"
00038 #include "BdbAccess/BdbFileSystem.hh"
00039 #include "BdbAccess/BdbClustHintSetupMgr.hh"
00040 
00041 
00042 //-------------
00043 // C Headers --
00044 //-------------
00045 extern "C" {
00046 #include <unistd.h>       /* symlink, geteuid, access */
00047 #include <stdlib.h>       /* system                  */
00048 #include <sys/types.h>    /* before stat and statvfs */
00049 #include <sys/stat.h>     /* umask, mkdir            */
00050 #include <grp.h>          /* getgrnam                */
00051 #include <sys/statvfs.h>  /* statvfs                 */
00052 #include <assert.h>
00053 int statvfs(const char *, struct statvfs *) ; /* This line is required by DEC C++ but does no harm for other platforms */
00054 }
00055 
00056 //---------------
00057 // C++ Headers --
00058 //---------------
00059 #include <fstream.h>
00060 
00061 
00062 d_Boolean BdbFSMgr::_neverUseDaemon  = d_True ;   // by default daemons are disabled. If rebuildFSInfo() 
00063                                                          // and finish with success daemons are enabled
00064 
00065 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00066 //
00067 //   C O N S T R U C T O R    /    D E S T R U C T O R
00068 //
00069 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00070 
00071 
00072 BdbFSMgr::BdbFSMgr()
00073 {
00074     _fsGroup       = 0;
00075     _currentFSNr   = 0;
00076 
00077 }
00078 
00079 
00080 
00081 BdbFSMgr::~BdbFSMgr()
00082 {}
00083 
00084 
00085 
00086 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00087 //
00088 //     F I L E   S Y S T E M   O P E R A T I O N S
00089 //
00090 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00091 
00092 
00093 BdbStatus BdbFSMgr::copyFile(const char* source, const char* target, d_Boolean anyDaemon)
00094 {
00095     if ( daemonDisabled() ) {
00096         ooVString cmd = "cp " ;
00097         cmd += source ; cmd += " " ; cmd += target ;
00098         // COUT1 << "No daemon, cmd: " << cmd.head() << endl ;
00099 
00100         if ( system(cmd.head()) != 0 )
00101             return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 
00102                    0, "BdbFSMgr::copyFile()-noDaemon", cmd.head()) ;
00103 
00104         return BdbcSuccess ;
00105     }
00106         
00107         return selectDaemon(anyDaemon)->copyFile(source, target) ;
00108 }
00109 
00110 
00111 BdbStatus BdbFSMgr::moveFile(const char* source, const char* target, d_Boolean anyDaemon)
00112 {
00113     if ( daemonDisabled() ) {
00114         ooVString cmd = "mv " ;
00115         cmd += source ; cmd += " " ; cmd += target ;
00116         // COUT1 << "No daemon, cmd: " << cmd.head() << endl ;
00117 
00118         if ( system(cmd.head()) != 0 )
00119             return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 
00120                              0, "BdbFSMgr::moveFile()-noDaemon", cmd.head()) ;
00121 
00122         return BdbcSuccess ;
00123     }
00124         
00125         return selectDaemon(anyDaemon)->moveFile(source, target) ;
00126 }
00127 
00128 
00129 BdbStatus BdbFSMgr::checkAvailSpace(const char* path, d_ULong minSpaceNeeded, 
00130                                     d_Boolean* isAvailable, d_Boolean anyDaemon)
00131 {
00132     *isAvailable = d_False ;
00133         
00134     if ( daemonDisabled() )
00135         {  struct statvfs buf ;
00136         if ( statvfs(path, &buf) != 0 )
00137             return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, 
00138                              "BdbFSMgr::checkAvailSpace", "in statvfs") ;
00139         // COUT1 << "No daemon, disk: " << path << " has available: " 
00140         //       << ((buf.f_bsize / 1024) * buf.f_bfree) 
00141         //               << " KB, min: " << minSpaceNeeded << " KB" << endl ;
00142 
00143         if ( ((buf.f_bsize / 1024) * buf.f_bfree) >= minSpaceNeeded ) {
00144             *isAvailable = d_True;
00145         }
00146 
00147         return BdbcSuccess ;
00148    }
00149 
00150         
00151         return selectDaemon(anyDaemon)->checkAvailSpace(path, minSpaceNeeded, isAvailable) ;
00152 }
00153 
00154 
00155 BdbStatus BdbFSMgr::chmodWrite(const char* fileName, d_Boolean anyDaemon)
00156 {
00157    if ( daemonDisabled() )
00158       return changeFileMode(0664, fileName) ;
00159 
00160         return selectDaemon(anyDaemon)->chmodWrite(fileName) ;
00161 }
00162 
00163 
00164 BdbStatus BdbFSMgr::chmodRead(const char* fileName, d_Boolean anyDaemon)
00165 {
00166    if ( daemonDisabled() )
00167                 return changeFileMode(0444, fileName) ;
00168         
00169         return selectDaemon(anyDaemon)->chmodRead(fileName) ;
00170 }
00171 
00172 
00173 BdbStatus BdbFSMgr::checkExist(const char* fileName, d_Boolean anyDaemon)
00174 {
00175    if ( daemonDisabled() )
00176    {  if ( access(fileName, F_OK) == 0 )
00177          return BdbcSuccess ;
00178       return BdbcError ;
00179    }
00180 
00181    return selectDaemon(anyDaemon)->checkExist(fileName) ;
00182 }
00183 
00184 
00185 BdbStatus BdbFSMgr::rmFile(const char* fileName, d_Boolean anyDaemon)
00186 {
00187         if ( daemonDisabled() )
00188    {  if ( remove(fileName) == 0 )
00189                    return BdbcSuccess ;
00190            return BdbcError ;
00191    }
00192 
00193         return selectDaemon(anyDaemon)->rmFile(fileName) ;
00194 }
00195 
00196 
00197 BdbStatus 
00198 BdbFSMgr::createFile(const char* fileName, d_Boolean anyDaemon)
00199 {
00200     if ( daemonDisabled() ) {
00201         ofstream f(fileName);
00202         f.close();
00203         return BdbcSuccess;
00204     }
00205 
00206     return selectDaemon(anyDaemon)->createFile(fileName) ;
00207 }
00208 
00209 
00210 BdbStatus BdbFSMgr::mkDir(const char* dirName, d_Boolean anyDaemon)
00211 {
00212     if ( checkExist(dirName, anyDaemon) ) {
00213         // COUT1 << "directory: " << dirName << " already existed" << endl ;
00214         return BdbcSuccess ;
00215         }
00216 
00217     char oneLevelUpDir[256], *tmp;
00218 
00219     strcpy( oneLevelUpDir, dirName );
00220     tmp = strrchr( oneLevelUpDir, '/' );
00221 
00222     if( tmp == oneLevelUpDir ) // doesn't want to create root directory
00223         return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, "BdbFSMgr::mkDir", dirName) ;                    
00224 
00225     *tmp = '\0'; // cut one level
00226 
00227     BdbStatus result = mkDir( oneLevelUpDir, anyDaemon );
00228     if( result != BdbcSuccess )
00229         return result;
00230 
00231         if ( daemonDisabled() ) {
00232         if ( checkExist(dirName) ) {
00233             // COUT1 << "directory: " << dirName << " already existed" << endl ;
00234             return BdbcSuccess ;
00235         }
00236 
00237         if ( mkdir(dirName, 0x3FD) != 0 )
00238             return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, "BdbFSMgr::mkDir", dirName) ;                        
00239 
00240         if ( chmod(dirName, 0775) != 0 )
00241             return BdbSignal(BdbcWarning, BdbAccessErrCommandFailed, 0, "BdbFSMgr::mkDir", "chmod", dirName) ;
00242                 
00243         struct group* grStr = getgrnam(BdbDomain::groupName()) ;  // get the number of the group (bfactory at SLAC)
00244 
00245         if ( grStr != 0 ) {
00246             int owner = -1 ; // indicates that the owner is not changed
00247             chown(dirName, (uid_t)owner, grStr->gr_gid) ;
00248         }
00249         // do not check the returned status because on AFS it can fail.
00250         // At SLAC only authusers are allowed to run chgrp.
00251         // On AFS acl metters, not the group write permissions.
00252 
00253         // COUT1 << "new directory: " << dirName << endl ;
00254         return BdbcSuccess ;
00255     }
00256 
00257     return selectDaemon(anyDaemon)->mkDir(dirName) ;
00258 }
00259 
00260 
00261 BdbStatus BdbFSMgr::rmDir(const char* dirName, d_Boolean anyDaemon)
00262 {
00263         if ( daemonDisabled() )
00264         {       if ( rmdir(dirName) == 0 )
00265                         return BdbcSuccess ;
00266                 return BdbcError ;
00267         }
00268 
00269         return selectDaemon(anyDaemon)->rmDir(dirName) ;
00270 }
00271 
00272 
00273 BdbStatus BdbFSMgr::changeFileMode(int mode, const char* fileName) const
00274 {
00275    struct stat buf ;
00276         if ( stat(fileName, &buf) == 0)          // check if owner of a file == owner of a current process
00277            if ( buf.st_uid != geteuid() )        // if yes, chmod without daemon
00278                    return BdbSignal(BdbcUserError, BdbAccessErrNotAnOwner, 0, "BdbFSMgr::changeFileMode", fileName) ;                      
00279 
00280         if ( chmod(fileName, mode) != 0 )
00281                 return BdbSignal(BdbcWarning, BdbAccessErrCommandFailed, 0, "BdbFSMgr::changeFileMode", "chmod", fileName) ;
00282         
00283         // COUT2 << "standard chmod: " << mode << fileName << " suceeded" << endl ;
00284 
00285         return BdbcSuccess ;
00286 }
00287 
00288 
00289 BdbStatus BdbFSMgr::getFileSize(const char* fileName, 
00290                                 off_t& theSize, 
00291                                 d_Boolean anyDaemon)
00292 {
00293  #if defined(BABAR_LARGEFILE) && defined(_FILE_OFFSET_BITS)
00294     /* Check if the includes are called in the correct order */
00295     assert((sizeof(off_t)*8) == _FILE_OFFSET_BITS);
00296 #endif
00297     const char* fn = "BdbFSMgr::getFileSize";
00298     
00299     theSize = 0 ;
00300     
00301     if ( daemonDisabled() ) {
00302         struct stat statbuf;
00303         if (stat(fileName, &statbuf) == 0) {
00304             theSize = statbuf.st_size;
00305             return BdbcSuccess;
00306         }
00307         return BdbSignal(BdbcWarning, BdbAccessErrCommandFailed, 
00308                          0, fn, "getFileSize", fileName);
00309     }
00310     
00311     return selectDaemon(anyDaemon)->getFileSize(fileName, theSize);
00312 }
00313 
00314 BdbStatus BdbFSMgr::getModificationDate(const char* fileName, 
00315                                         time_t& stime, 
00316                                         d_Boolean anyDaemon)
00317 {
00318     const char* fn = "BdbFSMgr::getModificationDate";
00319     
00320     stime = 0 ;
00321     
00322     if ( daemonDisabled() ) {
00323         struct stat statbuf;
00324         if (stat(fileName, &statbuf) == 0) {
00325             stime = statbuf.st_mtime;
00326             return BdbcSuccess;
00327         }
00328         return BdbSignal(BdbcWarning, BdbAccessErrCommandFailed, 
00329                          0, fn, "getModificationDate", fileName);
00330     }
00331     
00332     return selectDaemon(anyDaemon)->getModificationDate(fileName, stime);
00333 }
00334 
00335 
00336 
00337 BdbStatus
00338 BdbFSMgr::purgeMigrateFile(const char* path, const char* params)
00339 {
00340     if ( daemonDisabled() ) {
00341         COUT1 << "Daemon disabled, puring/migration not supported "
00342               << "for local file systems" << endl;
00343         return BdbcError;
00344     }
00345 
00346     return selectDaemon(d_True)->purgeMigrateFile(path, params);
00347 }
00348 
00349 
00350 
00351 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00352 //
00353 //       M A N A G I N G   M U L T I P L E   F S
00354 //
00355 //       The functions in this block should be never 
00356 //       called if fs-info is not loaded
00357 //
00358 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00359 
00360 
00361 // If this function has been called it means fs-related info has been 
00362 // loaded to the configuration database. 
00363 // If this function has never been called, daemons are not used and 
00364 // all the data is placed on the catalog FS
00365 BdbStatus 
00366 BdbFSMgr::rebuildFSInfo(const char* domainName, 
00367                         const char* authLevelName,
00368                         const char* authName, 
00369                         const char* compName)
00370 {
00371     const char* fn = "BdbFSMgr::rebuildFSInfo";
00372 
00373         // ----- initialize catalog daemon -----
00374         if ( BdbCatalogFSHandler::instance()->update() != BdbcSuccess ) {
00375         return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, fn, 
00376                          "in updatecatalogDaemon->update");
00377     }
00378    
00379    if ( compName == 0 ) {
00380        return BdbSignal(BdbcUserError, BdbAccessErrNameNotValid, 0, fn);
00381    }
00382 
00383    BdbClustHintSetupMgr* chsMgr = BdbClustHintSetupMgr::instance();
00384    if ( BdbcSuccess != chsMgr->initCHSetupObj() ) {
00385        return BdbcError;
00386    }
00387    if ( ! chsMgr->fcLoaded() ) {
00388        return BdbcError;
00389    }
00390 
00391    chsMgr->setStrings(BdbClusterConfigMgr::getClusterName(), 
00392                       domainName, authLevelName, authName, compName);
00393 
00394    _neverUseDaemon = d_False;
00395 
00396     // ----- initialize default dameon -----
00397     if ( ! firstAvailableDisk() ) {
00398         _neverUseDaemon = d_True;
00399         return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, fn, 
00400                          "in firstAvailableDisk");
00401     }
00402 
00403     return BdbcSuccess;
00404 }
00405 
00406 
00407 BdbStatus 
00408 BdbFSMgr::firstAvailableDisk(d_ULong currentGlobalFSNr)
00409 {
00410     const char* fn = "BdbFSMgr::firstAvailableDisk" ;
00411 
00412     char* groupName = 0;
00413     vector<BdbTFileSystem*>* vector = 0;
00414     
00415     if ( BdbcSuccess != BdbClustHintSetupMgr::instance()->retrieveFileSystems(groupName, vector) ) {
00416         _fsGroup = 0;
00417         return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 
00418                          0, fn, "retrieveFileSystems");
00419     }
00420     
00421     d_ULong size = vector->size();
00422 
00423     d_Boolean FSFound = d_False;
00424     d_Boolean newLoop = d_False;
00425     
00426     do {
00427         for ( _currentFSNr = 0; _currentFSNr<size ; _currentFSNr++ ) {
00428             BdbTFileSystem tSys = *((*vector)[_currentFSNr]);
00429 
00430             // initialize any daemon
00431             if ( _anyFSHandler.update(&tSys) != BdbcSuccess ) {
00432                 for ( int i=0 ; i<size ; i++) {
00433                     delete (*vector)[i];
00434                 }
00435                 delete vector;
00436                 _fsGroup = 0;
00437                             return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 
00438                                  0, fn, "in default->update") ;
00439             }
00440 
00441                     if ( isAvailableSpace() ) {
00442                 FSFound = d_True ;
00443                 COUT1 << "Switching to " << tSys.hostName() 
00444                       << "::" << tSys.dirName() << endl ;
00445                 for ( int i=0 ; i<size ; i++) {
00446                     delete (*vector)[i];
00447                 }
00448                 delete vector;
00449                 _fsGroup = groupName;
00450                 delete [] groupName;
00451                             return BdbcSuccess ; // the only successful exit 
00452             }
00453         }
00454         if ( ! newLoop ) {
00455             _currentFSNr = 0 ;
00456             newLoop = d_True ;
00457         } else {
00458             _fsGroup = 0;
00459             return BdbSignal(BdbcFatalError, BdbAccessErrNoMoreSpace, 0, fn) ;
00460         }
00461     } while ( ! FSFound ) ;
00462 
00463     for ( int i=0 ; i<size ; i++) {
00464         delete (*vector)[i];
00465     }
00466     delete vector;
00467     _fsGroup = 0;
00468 
00469     return BdbcError;  // should never reach this point
00470 }
00471 
00472 
00473 
00474 
00475 
00476 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00477 //
00478 //        M A N A G I N G   F R E E   S P A C E
00479 //
00480 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00481 
00482 BdbStatus BdbFSMgr::verifyDiskSpace(d_Boolean* diskChanged)
00483 {
00484         if ( ! isAvailableSpace() )
00485         {       _currentFSNr ++ ;
00486       if ( ! firstAvailableDisk() )
00487                         return BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, "BdbFSMgr::verifyDiskSpace", "in firstAvailableDisk") ;  
00488                 *diskChanged = d_True ;
00489         }
00490         return BdbcSuccess ;
00491 }
00492 
00493 
00494 
00495 d_Boolean BdbFSMgr::isAvailableSpace()
00496 {
00497    if ( daemonDisabled() )
00498         {       struct statvfs buf ;
00499                 if ( statvfs(baseDir(), &buf) != 0 )
00500                 {       BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, "BdbFSMgr::isAvailableSpace() in statvfs", baseDir()) ;
00501                         return d_False ;
00502                 }
00503       
00504                 if ( getenv("BDBDEBUG2") != 0 )
00505                         COUT2 << "disk: " << baseDir() << " has available: " << ((buf.f_bsize / 1024) * buf.f_bfree) 
00506                   << " KB, min: " << minDiskSpace() << " KB" << endl ;
00507 
00508                 return ((buf.f_bsize / 1024) * buf.f_bfree) >= minDiskSpace() ;
00509         }
00510 
00511 
00512         d_Boolean isAvailable = d_False ;
00513 
00514    if ( ! selectDaemon(d_True)->checkAvailSpace(baseDir(), minDiskSpace(), &isAvailable) )
00515         {       BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, "BdbFSMgr::isAvailableSpace", "in daemon.checkAvailSpace") ;
00516                 return d_False ;
00517         }
00518 
00519         return isAvailable ;
00520 }
00521 
00522 
00523 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00524 //
00525 //           S E L E C T    M E T H O D S
00526 //
00527 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00528 
00529 
00530 BdbPud* 
00531 BdbFSMgr::selectDaemon(d_Boolean anyDaemon)
00532 {
00533         if ( anyDaemon && ! daemonDisabled() ) {
00534         if ( ! _anyFSHandler.isInitialized() ) {
00535             if ( ! firstAvailableDisk() ) { // this will make all the initialization
00536                 BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 
00537                           0, "BdbFSMgr::selectDaemon", "in firstAvailableDisk");
00538                                 return 0;
00539             }
00540         }
00541         return _anyFSHandler.daemon();
00542     }
00543     return BdbCatalogFSHandler::instance()->daemon();
00544 }
00545 
00546 
00547 
00548 const char* 
00549 BdbFSMgr::baseDir(d_Boolean anyFS)
00550 {
00551     if ( ! anyFS || daemonDisabled() ) {
00552         return BdbCatalogFSHandler::instance()->baseDir();
00553     }
00554     
00555 
00556         if ( ! _anyFSHandler.isInitialized() ) {
00557         if ( ! firstAvailableDisk() ) { // this will make all the initialization
00558                         BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 
00559                       0, "BdbFSMgr::baseDir", "in firstAvailableDisk");
00560                         return 0;
00561                 }
00562     }
00563         
00564    return _anyFSHandler.baseDir();
00565 }
00566 
00567 
00568 const char* 
00569 BdbFSMgr::host(d_Boolean anyFS)
00570 {
00571     if ( ! anyFS || daemonDisabled() ) {
00572         return BdbCatalogFSHandler::instance()->host() ;
00573     }
00574     
00575 
00576         if ( ! _anyFSHandler.isInitialized() )
00577         if ( ! firstAvailableDisk() ) // this will make all the initialization
00578                 {       BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, "BdbFSMgr::host", "in firstAvailableDisk") ;
00579                         return 0 ;
00580                 }
00581         
00582    return _anyFSHandler.host() ;
00583 }
00584 
00585 
00586 d_ULong  
00587 BdbFSMgr::minDiskSpace(d_Boolean anyFS)
00588 {
00589     if ( ! anyFS || daemonDisabled() ) {
00590         return BdbCatalogFSHandler::instance()->minDiskSpace() ;
00591     }
00592     
00593         if ( ! _anyFSHandler.isInitialized() )
00594         if ( ! firstAvailableDisk() ) // this will make all the initialization
00595                 {       BdbSignal(BdbcUserError, BdbAccessErrFunctionFailed, 0, "BdbFSMgr::minDiskSpace", "in firstAvailableDisk") ;
00596                         return 0 ;
00597                 }
00598         
00599    return _anyFSHandler.minDiskSpace() ;
00600 }
00601 
00602 
00603 
00604 
00605 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00606 //
00607 //      I N D E P E N D E N T      M E T H O D S
00608 //
00609 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00610 
00611 
00612 
00613 // the methods below are independed, they create and 
00614 // configure the BdbPud object on the fly
00615 
00616 /**
00617  **    BdbFSMgr::getFDFilePath
00618  **    The first argument: bootFilePath must have the 
00619  **    following format: hostName::fullPath
00620  **
00621  **    The second argument is an address of the string, which
00622  **    will be set to the federated database path (taken from the 
00623  **    boot file. The function uses the pud daemon (it requires
00624  **    it to be running on the host specified in the bootFilePath.
00625  **    The function talks to port number 3333 (hardcoded!!!)
00626  **/
00627 
00628 BdbStatus
00629 BdbFSMgr::getFDFilePath(const char* bootFilePath, 
00630                         char*& theFDPath) const
00631 {
00632     const char* fn = "BdbFSMgr::getFDFilePath";
00633     
00634     // find "::"        
00635     int pos = 0;
00636     while ( *(bootFilePath+pos) != '\0' && *(bootFilePath+pos) != ':') {
00637         pos++;
00638     }
00639 
00640     if ( *(bootFilePath+pos) == '\0' || *(bootFilePath+pos+1) != ':') {
00641         return BdbSignal(BdbcUserError, BdbAccessErrInvBootFile, 0, fn);
00642     }
00643 
00644     // set the hostName and thePath
00645     char* hostName = new char [pos+1] ;
00646     strncpy(hostName, bootFilePath, pos);
00647     hostName[pos] = '\0';
00648 
00649     char* thePath = new char [strlen(bootFilePath) - pos + 1];
00650     strcpy(thePath, bootFilePath+pos+2);
00651     
00652     // configure the daemon
00653     BdbPud theDaemon(hostName, getPortNr(hostName));
00654     
00655     // do the real job
00656     BdbStatus retV = theDaemon.getFDFilePath(thePath, theFDPath);
00657     
00658     delete [] hostName;
00659     delete [] thePath;
00660 
00661     return retV;
00662 }
00663 
00664 
00665 
00666 /**
00667  **  BdbFSMgr::getPortNr
00668  **
00669  **  The function determines the pud demon's port number on the 
00670  **  "host". The default value for all hosts is 3333. It can be 
00671  **  overwritten using the environment variable "PUD_PORT_NR_HOSTNAME"
00672  **  where HOSTNAME is the name of the host.
00673  **
00674  **  Example: PUD_PORT_NR_objyserv2.slac.stanford.edu 3334
00675  **  Note, that PUD_PORT_NR_objyserv2.slac.stanford.edu and 
00676  **  PUD_PORT_NR_objyserv2 are not the same.
00677  **/  
00678  
00679 int
00680 BdbFSMgr::getPortNr(const char* host) const
00681 {
00682     // check if the default value of the port number 
00683     // has not been overwritten
00684     
00685         // build the name of the environment variable
00686     char* envVarName = new char [strlen(host)+16];
00687     strcpy(envVarName, "PUD_PORT_NR_");
00688     strcat(envVarName, host);
00689 
00690     char* portStr = getenv(envVarName);
00691     
00692     delete [] envVarName;
00693     
00694     if ( portStr != (char*) 0 ) {
00695         int portNr = atoi(portStr);
00696         if ( portNr > 0 ) {
00697             COUT2n << "BdbFSMgr::getPortNr: portNr = " << portNr << endl ;
00698             return portNr;
00699         }
00700     }
00701     COUT2n << "BdbFSMgr::getPortNr: portNr = 3333" << endl ;
00702  
00703     return 3333;  // default value
00704 }
00705     
00706 

 


BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us

Page Owner: Jacek Becla
Last Update: October 04, 2002