![]() |
|
|
Bdb packages | Design docs | Source docs | Guidelines | Recent releases |
|
Main Page Modules Namespace List Class Hierarchy Alphabetical List Compound List File List Compound Members File Members /BdbDistTools/BdbDistFile.cc
Go to the documentation of this file.00001 //-------------------------------------------------------------------------- 00002 // File and Version Information: 00003 // $Id: BdbDistFile.cc,v 1.11 2002/01/26 04:28:54 objysrv Exp $ 00004 // 00005 // Description: 00006 // BdbDistFile is a parser for the name of the databases. 00007 // 00008 // Environment: 00009 // Software developed for the BaBar Detector at the SLAC B-Factory. 00010 // 00011 // Author List: 00012 // Jean-Noel Albert Original Author 00013 // 00014 // Copyright Information: 00015 // Copyright (C) 1999 00016 // 00017 //------------------------------------------------------------------------ 00018 00019 //----------------------- 00020 // This Class's Header -- 00021 //----------------------- 00022 #include "BdbDistTools/BdbDistFile.hh" 00023 00024 //------------- 00025 // C Headers -- 00026 //------------- 00027 extern "C" { 00028 #include <stdio.h> 00029 #include <strings.h> 00030 #include <string.h> 00031 #include <unistd.h> 00032 #include <stdlib.h> 00033 #include <sys/types.h> 00034 #include <sys/stat.h> 00035 #include <assert.h> 00036 } 00037 00038 //------------------------------- 00039 // Collaborating Class Headers -- 00040 //------------------------------- 00041 #include "BdbAccess/BdbFSMgr.hh" 00042 #include "BdbAccess/BdbCatalogFSHandler.hh" 00043 #include "BdbAccess/BdbFileConfigSpec.hh" 00044 #include "BdbAccess/BdbFileConfig.hh" 00045 #include "BdbAccess/BdbFSMgr.hh" 00046 #include "BdbEventStore/BdbEventStore.hh" 00047 // #include "BdbDistTools/BdbDistributionErrors.hh" 00048 #include "BdbAccess/BdbTFileSystem.hh" 00049 00050 //---------------- 00051 // Constructors -- 00052 //---------------- 00053 BdbDistFile::BdbDistFile () 00054 : _daemon(0), _fsmgr(0) 00055 { 00056 _useDaemon = d_True; 00057 BdbDistFile::reset(); 00058 } 00059 00060 BdbDistFile::BdbDistFile (const char* filepath, const char* host, int port) 00061 : _daemon(0), _fsmgr(0) 00062 { 00063 _useDaemon = d_True; 00064 parse(filepath, host, port); 00065 } 00066 00067 //-------------- 00068 // Destructor -- 00069 //-------------- 00070 BdbDistFile::~BdbDistFile () 00071 { 00072 BdbDistFile::reset(); 00073 } 00074 00075 void BdbDistFile::reset() 00076 { 00077 _filepath = ""; 00078 _host = ""; 00079 _port = 0; 00080 00081 _directory = ""; 00082 _filename = ""; 00083 _suffix = ""; 00084 _fsRoot = ""; 00085 _fsSubDir = ""; 00086 _checkedPath= false; 00087 00088 _fsLoaded = d_False; 00089 delete _daemon; 00090 _daemon = 0; 00091 delete _fsmgr; 00092 _fsmgr= 0; 00093 00094 BdbDbNameParser::reset(); 00095 } 00096 00097 ////////////////////////////////////////////////////////////////////////////////////// 00098 00099 //---------- 00100 // Parser -- 00101 //---------- 00102 BdbStatus BdbDistFile::parse (const char* filepath, const char* host, int port) 00103 { 00104 BdbDistFile::reset(); 00105 00106 // Save the received parameters 00107 _filepath = filepath; 00108 _host = host; 00109 _port = port; 00110 00111 // Work on a writeable copy of the string 00112 char* locstr = new char[strlen(filepath) + 1]; 00113 char* topstr = locstr; // Save the top of the string (-> delete[]) 00114 strcpy(locstr, filepath); 00115 00116 // Extract the name of the host (if any) 00117 // Note: if a host name is given using "host", 00118 // the parsed name is not saved 00119 char* sep = rindex(locstr, ':'); 00120 if (0 != sep) { 00121 *sep++ = '\0'; 00122 char* eoh = index(locstr, ':'); // Can have 2 ':'... 00123 if (0 != eoh) 00124 *eoh = 0; 00125 if (0 == host || '\0' == *host) 00126 _host = locstr; 00127 locstr = sep; 00128 } 00129 00130 // Extract the name of the directory (if any) 00131 sep = rindex(locstr, '/'); 00132 if (0 != sep) { 00133 *sep++ = '\0'; 00134 _directory = locstr; 00135 locstr = sep; 00136 } 00137 00138 // Save the name of the file without the directory part 00139 _filename = locstr; 00140 00141 // Search the suffix and save it 00142 if (0 != (sep = index(locstr, '.'))) { 00143 _suffix = sep; 00144 *sep = '\0'; 00145 } 00146 00147 // The rest of "locstr" is the system name of the database 00148 BdbStatus stt = BdbDbNameParser::parse(locstr); 00149 delete[] topstr; 00150 return stt; 00151 } 00152 00153 ////////////////////////////////////////////////////////////////////////////////////// 00154 00155 //------------- 00156 // Accessors -- 00157 //------------- 00158 00159 BdbTString BdbDistFile::fullPath() 00160 { 00161 if ("" != _host) 00162 return _host + ":" + _filepath; 00163 00164 return _filepath; 00165 } 00166 00167 BdbTString BdbDistFile::filePath() 00168 { 00169 return _filepath; 00170 } 00171 00172 BdbTString BdbDistFile::directory() 00173 { 00174 return _directory; 00175 } 00176 00177 BdbTString BdbDistFile::fileName() 00178 { 00179 return _filename; 00180 } 00181 00182 BdbTString BdbDistFile::extension() 00183 { 00184 return _suffix; 00185 } 00186 00187 ////////////////////////////////////////////////////////////////////////////////////// 00188 00189 void BdbDistFile::loadFSInfo() 00190 { 00191 _fsLoaded = d_True; 00192 00193 d_Boolean initialized = d_False; 00194 _fsSubDir = ""; 00195 _checkedPath= false; 00196 00197 // Try to load the File Configuration 00198 BdbFileConfigSpec fsspec; 00199 BdbRef(BdbFileConfig) fc; 00200 00201 d_Boolean haveFSConfig = 00202 (BdbcSuccess == fsspec.initFileConfigObject(fc)); 00203 00204 // Try to found a FS in the database 00205 if (haveFSConfig) { 00206 BdbTFileSystem fs; 00207 if (BdbcSuccess == fc->lookupFS(fs, _filepath, _host)) { 00208 if ("" == _host) 00209 _host = fs.hostName(); 00210 if (_port <= 0) 00211 _port = fs.portNr(); 00212 00213 _fsRoot = fs.dirName(); 00214 initialized = d_True; 00215 } 00216 } 00217 00218 // Try to see if the file is in the catalog 00219 if (! initialized) { 00220 BdbCatalogFSHandler catFS; 00221 if (strncmp(_filepath, catFS.baseDir(), strlen(catFS.baseDir())) == 0 && 00222 ("" == _host || BdbTFileSystem::hostEqual(_host, catFS.host()))) 00223 { 00224 if ("" == _host) 00225 _host = catFS.host(); 00226 if (_port <= 0) 00227 _port = catFS.portNr(); 00228 00229 _fsRoot = catFS.baseDir(); 00230 initialized = d_True; 00231 } 00232 } 00233 00234 // Else ... 00235 if (! initialized) { 00236 if ("" == _host) 00237 _host = BdbTFileSystem::localHostName(); 00238 _fsRoot = ""; 00239 } 00240 00241 // If a port number is defined but no daemon is present, 00242 // try to create one (the user calls the constructor with a port number) 00243 // if the file is not locally visible 00244 if (0 != _port && "" != _host && 0 == _daemon) { 00245 // if (access(_filepath, F_OK) != 0) { 00246 if (!BdbTFileSystem::hostEqual(_host, BdbTFileSystem::localHostName())) { 00247 if (_port < 0) 00248 _port = 3333; 00249 _daemon = new BdbPud(_host, _port); 00250 } 00251 } 00252 } 00253 00254 00255 // Hopefully the root will have been loaded from the file config 00256 // or catalogue. If these failed, checkPathInfo guesses. 00257 // We only try to guess if we need this info to prevent spurious warning 00258 // messages. 00259 00260 void BdbDistFile::checkPathInfo() 00261 { 00262 if (_checkedPath) return; 00263 _checkedPath= true; 00264 00265 // Extract the subdirectory path from the file path 00266 if ("" != _fsRoot) { 00267 int len = strlen(_fsRoot); 00268 if (strncmp(_filepath, _fsRoot, len) == 0) { 00269 const char* subdir = &(_directory.data())[len]; 00270 if (*subdir == '/') 00271 subdir++; 00272 _fsSubDir = subdir; 00273 } else { 00274 cerr << "BdbDistFile::loadFSInfo warning: " << _filepath 00275 << " is not in " << _fsRoot << endl; 00276 } 00277 } 00278 00279 // Panic: there is no valid FS Info - try to imagine what can be the root... 00280 else { 00281 // Try to found the domain name in the directory path 00282 BdbTString domaindir("/", domainName(), "/"); 00283 00284 // Copy the directory to a local writeable buffer 00285 char* dir = new char[strlen(directory()) + 1]; 00286 strcpy(dir, directory()); 00287 00288 // Iterate to find the domain directory 00289 char* subdir = dir; 00290 char* tmp; 00291 while ((tmp = strstr(subdir, domaindir)) != 0) { 00292 subdir = tmp+1; 00293 } 00294 00295 if (dir != subdir) { 00296 *(subdir - 1) = '\0'; 00297 _fsSubDir = subdir; 00298 _fsRoot = dir; 00299 cerr << "BdbDistFile::loadFSInfo warning: had to guess root " 00300 << dir << endl; 00301 } else { // Really a problem... 00302 cerr << "BdbDistFile::loadFSInfo error: unable to guess root directory in " 00303 << dir << endl; 00304 } 00305 delete[] dir; 00306 } 00307 } 00308 00309 BdbFSMgr* BdbDistFile::getFSMgr() 00310 { 00311 if (!_fsmgr) { 00312 _fsmgr= new BdbFSMgr; 00313 _fsmgr->rebuildFSInfo(domainName(), authLevelName(), userGroupName(), 00314 componentName()); 00315 } 00316 return _fsmgr; 00317 } 00318 00319 ////////////////////////////////////////////////////////////////////////////////////// 00320 00321 void BdbDistFile::useDaemon(d_Boolean mode) 00322 { 00323 _useDaemon = mode; 00324 } 00325 00326 BdbStatus BdbDistFile::setDaemon (const char* host, int port) 00327 { 00328 if ((0 == host || '\0' == *host) && "" == _host && 00329 0 == port && 0 == _port) 00330 return BdbcError; // Invalid host & port 00331 00332 if (0 != host && '\0' != *host) 00333 _host = host; 00334 if (0 != port) 00335 _port = port; 00336 00337 BdbPud* daemon = new BdbPud(_host, _port); 00338 if (0 == daemon) 00339 return BdbcError; 00340 00341 if (0 != _daemon) 00342 delete _daemon; 00343 00344 _daemon = daemon; 00345 return BdbcSuccess; 00346 } 00347 00348 ////////////////////////////////////////////////////////////////////////////////////// 00349 00350 d_Boolean BdbDistFile::hasDaemon() 00351 { 00352 return (0 != _daemon); 00353 } 00354 00355 BdbTString BdbDistFile::host() 00356 { 00357 if ("" == _host && !_fsLoaded) 00358 loadFSInfo(); 00359 return _host; 00360 } 00361 00362 int BdbDistFile::pudPort () 00363 { 00364 if (0 == _port && !_fsLoaded) 00365 loadFSInfo(); 00366 return _port; 00367 } 00368 00369 BdbTString BdbDistFile::fsRoot() 00370 { 00371 if ("" == _fsRoot && !_fsLoaded) 00372 loadFSInfo(); 00373 checkPathInfo(); 00374 00375 return _fsRoot; 00376 } 00377 00378 BdbTString BdbDistFile::fsSubDir() 00379 { 00380 if ("" == _fsSubDir && !_fsLoaded) 00381 loadFSInfo(); 00382 checkPathInfo(); 00383 return _fsSubDir; 00384 } 00385 00386 ////////////////////////////////////////////////////////////////////////////////////// 00387 00388 d_Boolean BdbDistFile::isFileExist() 00389 { 00390 if (!_fsLoaded) 00391 loadFSInfo(); 00392 00393 if (_useDaemon && 0 != _daemon) 00394 return _daemon->checkExist(_filepath); 00395 00396 return getFSMgr()->checkExist(_filepath); 00397 } 00398 00399 d_Boolean BdbDistFile::isHostEqual(BdbTString host) 00400 { 00401 if (!_fsLoaded) 00402 loadFSInfo(); 00403 00404 return BdbTFileSystem::hostEqual(_host, host); 00405 } 00406 00407 BdbStatus BdbDistFile::fileSize(off_t& size) 00408 { 00409 #if defined(BABAR_LARGEFILE) && defined(_FILE_OFFSET_BITS) 00410 /* Check if the includes are called in the correct order */ 00411 assert((sizeof(off_t)*8) == _FILE_OFFSET_BITS); 00412 #endif 00413 if (!_fsLoaded) 00414 loadFSInfo(); 00415 00416 if (_useDaemon && 0 != _daemon) 00417 return _daemon->getFileSize(_filepath, size); 00418 00419 return getFSMgr()->getFileSize(_filepath, size); 00420 } 00421 00422 BdbStatus BdbDistFile::modificationDate(time_t& mtime) 00423 { 00424 if (!_fsLoaded) 00425 loadFSInfo(); 00426 00427 if (_useDaemon && 0 != _daemon) 00428 return _daemon->getModificationDate(_filepath, mtime); 00429 00430 return getFSMgr()->getModificationDate(_filepath, mtime); 00431 } 00432 00433 BdbStatus BdbDistFile::chmodRead() 00434 { 00435 if (!_fsLoaded) 00436 loadFSInfo(); 00437 00438 if (_useDaemon && 0 != _daemon) 00439 return _daemon->chmodRead(_filepath); 00440 00441 return getFSMgr()->chmodRead(_filepath); 00442 } 00443 00444 BdbStatus BdbDistFile::chmodWrite() 00445 { 00446 if (!_fsLoaded) 00447 loadFSInfo(); 00448 00449 if (_useDaemon && 0 != _daemon) 00450 return _daemon->chmodWrite(_filepath); 00451 00452 return getFSMgr()->chmodWrite(_filepath); 00453 } 00454 00455 BdbStatus BdbDistFile::mkDir() 00456 { 00457 if (!_fsLoaded) 00458 loadFSInfo(); 00459 00460 if (_useDaemon && 0 != _daemon) 00461 return _daemon->mkDir(_directory); 00462 00463 return getFSMgr()->mkDir(_directory); 00464 } 00465 00466 BdbStatus BdbDistFile::rename (BdbDistFile& to) 00467 { 00468 if (!_fsLoaded) 00469 loadFSInfo(); 00470 00471 if (!BdbTFileSystem::hostEqual(_host, to._host)) 00472 return BdbcError; 00473 // return BdbSignal(BdbcUserError, BdbDistributionNoHostOp, 0, 00474 // "BdbDistFile::rename"); 00475 00476 if (_useDaemon && 0 != _daemon) 00477 return _daemon->moveFile(_filepath, to._filepath); 00478 00479 return getFSMgr()->moveFile(_filepath, to._filepath); 00480 } 00481 00482 BdbStatus BdbDistFile::copy (BdbDistFile& to) 00483 { 00484 if (!_fsLoaded) 00485 loadFSInfo(); 00486 00487 if (BdbTFileSystem::hostEqual(_host, to._host)) 00488 return BdbcError; 00489 // return BdbSignal(BdbcUserError, BdbDistributionHostNotSup, 0, 00490 // "BdbDistFile::copy"); 00491 00492 if (_useDaemon && 0 != _daemon) 00493 return _daemon->copyFile(_filepath, to._filepath); 00494 00495 return getFSMgr()->copyFile(_filepath, to._filepath); 00496 } 00497 00498 BdbStatus BdbDistFile::remove () 00499 { 00500 if (!_fsLoaded) 00501 loadFSInfo(); 00502 00503 if (_useDaemon && 0 != _daemon) 00504 return _daemon->rmFile(_filepath); 00505 00506 return getFSMgr()->rmFile(_filepath); 00507 } 00508 00509 ////////////////////////////////////////////////////////////////////////////////////// 00510 00511 BdbStatus BdbDistFile::tdf(char* buffer, int count) 00512 { 00513 // Check if the FS Info are valid 00514 if ("" == fsRoot().data() || "" == fsSubDir().data()) { 00515 cerr << "Invalid FS Info - can't found the FS Root or SubDir" << endl; 00516 abort(); 00517 } 00518 00519 #if defined(BABAR_LARGEFILE) && defined(_FILE_OFFSET_BITS) 00520 /* Check if the includes are called in the correct order */ 00521 assert((sizeof(off_t)*8) == _FILE_OFFSET_BITS); 00522 #endif 00523 char internal[1024]; 00524 BdbTString format; 00525 00526 format = "FILE %s NUMBER %d FILE %s DIR %s HOST %s SIZE %f"; 00527 00528 if (sizeof(off_t) > sizeof(int)) { 00529 // DEC/OSF - stat.st_size is a 64-bits long int (no %lld defined) 00530 if (sizeof(off_t) == sizeof(long)) 00531 format += " FULLSIZE %ld"; 00532 00533 // Sun/Solaris with Large File option - stat.st_size is a 64-bits long int 00534 // %d & %ld are for 32-bits int & long 00535 // %lld is for 64-bits long long int 00536 else 00537 format += " FULLSIZE %lld"; 00538 } 00539 else { 00540 // 32-bits UNIX system 00541 format += " FULLSIZE %d"; 00542 } 00543 00544 format += " TIME %d ROOT %s SUBDIR %s"; 00545 00546 off_t size = 0; 00547 fileSize(size); 00548 00549 time_t mtime = 0; 00550 modificationDate(mtime); 00551 00552 int blen = sprintf(internal, format.data(), 00553 name().data(), number(), 00554 fileName().data(), directory().data(), host().data(), 00555 size/1024./1024., size, mtime, 00556 fsRoot().data(), fsSubDir().data()); 00557 if (blen >= count) 00558 return BdbcError; // Can save the TDF 00559 00560 strcpy(buffer, internal); 00561 return BdbcSuccess; 00562 } 00563
BaBar Public Site | SLAC | News | Links | Who's Who | Contact Us
Page Owner: Jacek Becla
Last Update: October 04, 2002