FileMgr.cpp
Go to the documentation of this file.
1 #include "FileMgr.h"
2 
3 #include <fstream>
4 
5 #include "GaudiKernel/SvcFactory.h"
6 #include "GaudiKernel/ISvcLocator.h"
7 #include "GaudiKernel/IJobOptionsSvc.h"
8 
9 #include "RootFileHandler.h"
10 #include "POSIXFileHandler.h"
11 #include "boost/bind.hpp"
12 #include "boost/function.hpp"
13 
14 #define ON_DEBUG if (UNLIKELY(outputLevel() <= MSG::DEBUG))
15 #define ON_VERBOSE if (UNLIKELY(outputLevel() <= MSG::VERBOSE))
16 
17 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
19 
20 using namespace std;
21 using namespace Io;
22 
23 
24 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
26 
27 void set_bit(int& f, const unsigned int& b) {
28  f |= 1 << b;
29 }
30 
31 bool get_bit(const int& f, const unsigned int& b) {
32  return f & (1 << b);
33 }
34 
35 
36 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
37 
38 FileMgr::FileMgr(const std::string& name, ISvcLocator* svc)
39  : base_class( name, svc ),m_rfh(0), m_pfh(0),
40  m_log(msgSvc(), name )
41  {
42 
43  declareProperty("LogFile",m_logfile="");
44  declareProperty("PrintSummary",m_printSummary=false);
45  declareProperty("LoadROOTHandler", m_loadRootHandler=true);
46  declareProperty("LoadPOSIXHandler", m_loadPosixHandler=true);
47 
48  declareProperty("TSSL_UserProxy", m_ssl_proxy="X509");
49  declareProperty("TSSL_CertDir", m_ssl_cert="X509");
50 
51  m_lastErrS = "";
52 
53 }
54 
55 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
56 
58 
59 }
60 
61 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
62 
65 
66  // Super ugly hack to make sure we have the OutputLevel set first, so we
67  // can see DEBUG printouts in update handlers.
68  IJobOptionsSvc* jos(0);
69  if( serviceLocator()->service( "JobOptionsSvc", jos, true ).isSuccess() ) {
70  const std::vector<const Property*> *props = jos->getProperties( name() );
71 
72  if (props != NULL) {
73  for ( std::vector<const Property*>::const_iterator cur = props->begin();
74  cur != props->end(); cur++) {
75  if ( (*cur)->name() == "OutputLevel" ) {
76  setProperty( **cur ).ignore();
78  break;
79  }
80  }
81  }
82  }
83 
86 
87  if (status.isFailure()) {
88 
89  ON_DEBUG
90  m_log << MSG::DEBUG << "Failed to initialize the base class (Service)"
91  << endmsg;
92  return status;
93  }
94 
96  m_log << MSG::VERBOSE << "Initializing FileMgr" << endmsg;
97 
98  if (m_loadRootHandler.value()) {
99 
100  // setup file handler for ROOT
101 
102  msgSvc()->setOutputLevel( "RootFileHandler", m_outputLevel.value());
104 
105  auto rfh = m_rfh; // used in the lambdas to avoid capturing 'this'
106  Io::FileHdlr hdlr(Io::ROOT,
107  [rfh](const std::string& n, const Io::IoFlags& f,
108  const std::string& desc, Io::Fd& fd,
109  void*& ptr) -> Io::open_t {
110  return rfh->openRootFile(n, f, desc, fd, ptr);
111  },
112  [rfh](void* ptr) -> Io::close_t {
113  return rfh->closeRootFile(ptr);
114  },
115  [rfh](void* ptr, const Io::IoFlags& f) -> Io::reopen_t {
116  return rfh->reopenRootFile(ptr, f);
117  });
118 
119  if (regHandler(hdlr).isFailure()) {
120  m_log << MSG::ERROR
121  << "unable to register ROOT file handler with FileMgr"
122  << endmsg;
123  }
124  }
125 
126  if (m_loadPosixHandler.value()) {
127 
128  // setup file handler for POSIX
129 
130  msgSvc()->setOutputLevel( "POSIXFileHandler", m_outputLevel.value());
131  m_pfh = new POSIXFileHandler(msgSvc());
132 
133  auto pfh = m_pfh; // used in the lambdas to avoid capturing 'this'
134  Io::FileHdlr hdlp(Io::POSIX,
135  [pfh](const std::string& n, const Io::IoFlags& f,
136  const std::string& desc, Io::Fd& fd,
137  void*& ptr) -> Io::open_t {
138  return pfh->openPOSIXFile(n, f, desc, fd, ptr);
139  },
140  [pfh](Io::Fd fd) -> Io::close_t {
141  return pfh->closePOSIXFile(fd);
142  },
143  [pfh](Io::Fd fd, const Io::IoFlags& f) -> Io::reopen_t {
144  return pfh->reopenPOSIXFile(fd, f);
145  });
146 
147  if (regHandler(hdlp).isFailure()) {
148  m_log << MSG::ERROR
149  << "unable to register ROOT file handler with FileMgr"
150  << endmsg;
151  }
152  }
153 
154 
155 
156  return StatusCode::SUCCESS;
157 
158 }
159 
160 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
161 
164  ON_VERBOSE
165  m_log << MSG::VERBOSE << "FileMgr::finalize()" << endmsg;
166 
167 
168  if (m_printSummary || outputLevel() <= MSG::DEBUG) {
169  listHandlers();
170  listFiles();
171  listActions();
172  listSuppression();
173  }
174 
175 
176  if (m_files.size() > 0) {
178  << "At finalize, the following files remained open:"
179  << endl;
180  map<string,FileAttr*>::const_iterator itr;
181  for (itr=m_files.begin(); itr != m_files.end(); ++itr) {
182  m_log << *(itr->second) << endl;
183  }
184  m_log << endmsg;
185  }
186 
187 
188 
189  if (m_logfile.value() != "") {
190  std::ofstream ofs;
191  ofs.open(m_logfile.value().c_str());
192  if (!ofs) {
193  m_log << MSG::ERROR << "Unable to open output file \"" << m_logfile.value()
194  << "\" for writing"
195  << endmsg;
196  } else {
197  ON_DEBUG
198  m_log << MSG::DEBUG << "Saving log to \"" << m_logfile.value() << "\""
199  << endmsg;
200  fileMap::const_iterator itr;
201  for (itr=m_files.begin(); itr != m_files.end(); ++itr) {
202  ofs << itr->second->name() << " " << itr->second->tech() << " "
203  << itr->second->desc() << " " << itr->second->iflags() << endl;
204  }
205 
206  set<FileAttr> fs;
207  set<FileAttr>::const_iterator it3;
208  list<FileAttr*>::const_iterator it2;
209  for (it2=m_oldFiles.begin(); it2 != m_oldFiles.end(); ++it2) {
210  fs.insert(**it2);
211  }
212 
213  for (it3=fs.begin(); it3!=fs.end(); ++it3) {
214  ofs << (it3)->name() << " " << (it3)->tech() << " " << (it3)->desc()
215  << " " << (it3)->iflags()
216  << ( (it3->isShared()) ? " SHARED" : "" )
217  << endl;
218  }
219 
220  ofs.close();
221  }
222  }
223 
224  // cleanup FileAttrs
225  for (vector<FileAttr*>::iterator itr = m_attr.begin(); itr != m_attr.end();
226  ++itr) {
227  delete(*itr);
228  }
229  m_attr.clear();
230 
231  delete m_rfh;
232  delete m_pfh;
233 
234 
235  StatusCode status = Service::finalize();
236 
237  ON_DEBUG
238  if ( status.isSuccess() )
239  m_log << MSG::DEBUG << "Service finalised successfully" << endmsg;
240 
241  return status;
242 
243 }
244 
245 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
246 
247 void
248 FileMgr::handle(const Incident& /*inc*/) {
249 
250 }
251 
252 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
253 
256 
257  IoTech tech = fh.tech;
258 
259  if (m_handlers.find(tech) != m_handlers.end()) {
261  << "Handler for IoTech " << tech << " already registered. Ignoring."
262  << endmsg;
263  return StatusCode::SUCCESS;
264  }
265 
266  if ( ! fh.b_open_fcn ) {
267  m_log << MSG::ERROR
268  << "open handler for tech " << tech << " is NULL"
269  << endmsg;
270  return StatusCode::FAILURE;
271  }
272 
273  if ( ! fh.b_close_fcn && ! fh.b_closeP_fcn ) {
274  m_log << MSG::ERROR
275  << "no close handler for tech " << tech << " registered"
276  << endmsg;
277  return StatusCode::FAILURE;
278  }
279 
280  if ( ! fh.b_reopen_fcn && ! fh.b_reopenP_fcn) {
281  m_log << MSG::ERROR
282  << "no reopen handler for tech " << tech << " registered"
283  << endmsg;
284  return StatusCode::FAILURE;
285  }
286 
287 
288  ON_DEBUG
289  m_log << MSG::DEBUG
290  << "Successfully registered handler for tech \"" << tech << "\""
291  << endmsg;
292 
293  m_handlers[tech] = fh;
294 
295  return StatusCode::SUCCESS;
296 
297 }
298 
299 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
300 
303  FileHdlr hdlr;
304 
305  map<IoTech,FileHdlr>::iterator itr = m_handlers.find(tech);
306  if (itr == m_handlers.end()) {
307  m_log << MSG::ERROR << "Can't de-register tech " << tech
308  << " as it hasn't been registered!"
309  << endmsg;
310  return StatusCode::FAILURE;
311  } else {
312  m_handlers.erase(itr);
313  }
314 
315  return StatusCode::SUCCESS;
316 
317 }
318 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
319 
321 FileMgr::hasHandler( const IoTech& tech ) const {
322 
323  map<IoTech,FileHdlr>::const_iterator itr = m_handlers.find(tech);
324  if (itr != m_handlers.end()) {
325  return StatusCode::SUCCESS;
326  } else {
327  return StatusCode::FAILURE;
328  }
329 
330 }
331 
332 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
333 
334 open_t
335 FileMgr::open( const IoTech& tech, const std::string& caller,
336  const std::string& fname,
337  const IoFlags& flags, Fd& fd, void*& ptr,
338  const std::string& desc, bool sh) {
339 
340  return open(tech, caller, fname, desc, flags, fd, ptr, sh);
341 
342 }
343 
344 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
345 open_t
346 FileMgr::open( const IoTech& tech, const std::string& caller,
347  const std::string& fname,
348  const IoFlags& flags, Fd& fd, const std::string& desc,
349  bool sh) {
350 
351  void* dummy(0);
352  return open(tech, caller, fname, desc, flags, fd, dummy, sh);
353 
354 }
355 
356 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
357 open_t
358 FileMgr::open( const IoTech& tech, const std::string& caller,
359  const std::string& fname,
360  const IoFlags& flags, void*& ptr, const std::string& desc,
361  bool sh) {
362 
363  Fd dummy(-1);
364  return open(tech, caller, fname, desc, flags, dummy, ptr, sh);
365 
366 }
367 
368 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
369 
370 open_t
371 FileMgr::open( const IoTech& tech, const std::string& caller,
372  const std::string& fname,
373  const std::string& desc,
374  const IoFlags& flags, Fd& fd, void*& ptr, bool shared) {
375 
376  // return codes: ok == 0, warning > 0, error < 0
377  // 0: ok
378  //
379  // WARNING:
380  // 1: file already open with existing FileAttributes
381  // 2: file already open with different FileAttributes
382  // 3: file opened, but fd and ptr are both invalid
383  //
384  // ERRORS:
385  // -1: no handler for TECH
386  // -2: file already open with different tech
387  // -3: file asked to be opened in shared mode, but other open file
388  // exist that are marked unshared
389  // -4: error calling tech specific open function
390 
391 
392  ON_VERBOSE
393  m_log << MSG::VERBOSE << "open(" << tech << ","
394  << caller
395  << ",\"" << fname << "\",\""
396  << desc << "\","
397  << flags
398  << ( shared ? ",shared" : ",unshared")
399  << ")"
400  << endmsg;
401 
402  open_t r = -1;
403  FileHdlr fh;
404 
405  if (getHandler(tech,fh).isFailure()) {
406  return r;
407  }
408 
409 
410  pair<fileMap::const_iterator,fileMap::const_iterator> fitr =
411  m_files.equal_range(fname);
412  fileMap::const_iterator itr;
413 
414 
415  // make sure all files with same name have same tech
416 
417  for (itr=fitr.first; itr != fitr.second; ++itr) {
418  if (itr->second->tech() != tech) {
419 
420  m_log << MSG::ERROR << "when calling open on " << fname
421  << " with tech " << tech
422  << ", file already opened with different tech "
423  << itr->second->tech()
424  << endmsg;
425 
426  r = -1;
427 
428  return r;
429  }
430  }
431 
432 
433  // check for sharing
434 
435  if (shared) {
436 
437  bool shareable(true);
438 
439  for (itr=fitr.first; itr != fitr.second; ++itr) {
440  FileAttr* fa = itr->second;
441 
442  if (! fa->isShared()) {
443  shareable = false;
444  }
445 
446  // if ( shareable && accessMatch(fa->flags(),flags) ) {
447  if ( shareable && fa->flags().match(flags,false) ) {
448 
449  ON_DEBUG
450  m_log << MSG::DEBUG << " found shared file: "
451  << *fa << endmsg;
452 
453  fd = fa->fd();
454  ptr = fa->fptr();
455  r = 0;
456  }
457  }
458 
459  if (!shareable) {
460  // at least one of the other files was not marked shared.
461  fd = -1;
462  ptr = 0;
463  r = -1;
464  }
465  }
466 
467 
468  if (r != 0) {
469 
470  try {
471  r = fh.b_open_fcn(fname,flags,desc,fd,ptr);
472  } catch (const boost::bad_function_call& err) {
473  m_log << MSG::ERROR << "when calling open handler for " << tech
474  << " on file "
475  << fname << " caught " << err.what() << endmsg;
476  return -4;
477  } catch (...) {
478  m_log << MSG::ERROR << "when calling open handler for " << tech
479  << " on fle "
480  << fname << " caught an unknown exception." << endmsg;
481  return -4;
482  }
483 
484 
485  if (r != 0) {
487  << "open of file \"" << fname << "\", tech: \"" << tech
488  << "\", flags: \"" << flags << "\" requested by "
489  << caller
490  << " failed. return code: " << r
491  << endmsg;
492 
493  FileAttr xfa(-1,fname,desc,tech,flags,0,false);
494  execAction( &xfa, caller, Io::OPEN_ERR ).ignore();
495 
496  return r;
497  }
498  }
499 
500 
501  FileAttr *fa = new FileAttr(fd,fname,desc,tech,flags,ptr,true,shared);
502 
503  ON_DEBUG
504  m_log << MSG::DEBUG << "opened file " << *fa << endmsg;
505 
506 
507  if (fd == -1 && ptr == 0) {
508  m_log << MSG::WARNING << "when opening " << *fa << " both File Descriptor"
509  << " and File Ptr are invalid" << endmsg;
510  r = 3;
511  }
512 
513 
514  for (itr = fitr.first; itr != fitr.second; ++itr) {
515  if (fa->flags() == Io::READ || shared) {
516  // ok
517  } else if (*fa == *(itr->second) ) {
518  m_log << MSG::WARNING << "open call for file \"" << fname
519  << "\" returned a pre-existing file with identical"
520  << " FileAttributes: " << *fa << endmsg;
521  r = 1;
522  } else {
523  m_log << MSG::WARNING << "open call for file \"" << fname
524  << "\" returned a pre-existing file with different"
525  << " FileAttributes -"
526  << endl << "old: " << *(itr->second)
527  << endl << "new: " << *fa << endmsg;
528  r = 2;
529  }
530  }
531 
532  m_files.insert( pair<string,FileAttr*>(fname,fa) );
533 
534  // execute all our open callbacks
535  if (execAction( fa, caller, Io::OPEN ).isFailure()) {
537  << "at least one open callback action failed"
538  << endmsg;
539  }
540 
541 
542  return r;
543 
544 }
545 
546 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
547 
548 close_t
549 FileMgr::close( Fd fd, const std::string& caller ) {
550 
551  // return codes:
552  // < 0 : error condition
553  // 0 : actual close of one file
554  // > 0 : shared file, removed from list, no actual close, returns
555  // number of shared files still open.
556 
557  ON_VERBOSE
558  m_log << MSG::VERBOSE << "close(" << fd << ")"
559  << endmsg;
560 
561  close_t r = -1;
562 
563 
564  fileMap::iterator itr;
565  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
566  if (itr->second->fd() == fd) {
567  break;
568  }
569  }
570 
571  if (itr == m_files.end()) {
572  m_log << MSG::ERROR << "unknown file descriptor \"" << fd
573  << "\" when calling close()"
574  << endmsg;
575  return r;
576  }
577 
578 
579  IoTech tech = itr->second->tech();
580 
581  FileHdlr fh;
582 
583  if (getHandler(tech,fh).isFailure()) {
584  return r;
585  }
586 
587  if (! fh.b_close_fcn) {
588  m_log << MSG::ERROR << "no close(" << tech << ",Fd) function registered"
589  << endmsg;
590  return -1;
591  }
592 
593  FileAttr* fa = itr->second;
594 
595  // find how many times this file is open
596  pair<fileMap::const_iterator, fileMap::const_iterator> fitr =
597  m_files.equal_range(fa->name());
598 
599  int i(0);
600  for (fileMap::const_iterator it=fitr.first; it != fitr.second; ++it) {
601  if (it->second->fd() == fd) {
602  i++;
603  }
604  }
605 
606  ON_VERBOSE
607  m_log << MSG::VERBOSE << " ref count: " << i
608  << endmsg;
609 
610 
611  if (i > 1 && fa->isShared()) {
612  // file open multiple times. don't do the actual close
613  ON_DEBUG
614  m_log << MSG::DEBUG << "closing file " << fa->name() << " opened "
615  << i << " times with Fd " << fd << endmsg;
616  m_files.erase(itr);
617 
618  r = i-1;
619 
620  } else if (i == 1 || (i>1 && !fa->isShared()) ) {
621  ON_DEBUG
622  m_log << "closing " << *fa << endmsg;
623 
624 
625  try {
626  r = fh.b_close_fcn(fd);
627  } catch (const boost::bad_function_call& err) {
628  m_log << MSG::ERROR << "when calling close handler for " << tech
629  << " on file descriptor "
630  << fd << " caught " << err.what() << endmsg;
631  execAction(fa, caller, Io::CLOSE_ERR ).ignore();
632  return -1;
633  } catch (...) {
634  m_log << MSG::ERROR << "when calling close handler for " << tech
635  << " on file descriptor "
636  << fd << " caught an unknown exception." << endmsg;
637  execAction(fa, caller, Io::CLOSE_ERR ).ignore();
638  return -1;
639  }
640 
641  if (r < 0) {
643  << "close of file with FD \"" << fd
644  << "\", name: \"" << fa->name()
645  << "\", tech: \"" << tech << "\" failed"
646  << endmsg;
647 
648  execAction(fa, caller, Io::CLOSE_ERR ).ignore();
649 
650  return r;
651  }
652 
653  m_files.erase(itr);
654 
655  } else if (i <= 0) {
656  // this should never happen!
657  m_log << MSG::ERROR
658  << "ref count < 0 when closing " << fa
659  << ". This should never happen"
660  << endmsg;
661  return -1;
662 
663  }
664 
665  fa->fd(-1);
666  fa->flags(INVALID);
667  fa->isOpen(false);
668  fa->fptr(0);
669  m_oldFiles.push_back( fa );
670 
671 
672  // exec all callbacks
673  if (execAction(fa, caller, Io::CLOSE).isFailure()) {
675  << "at least one close callback action failed"
676  << endmsg;
677  }
678 
679 
680  return r;
681 
682 }
683 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
684 
685 close_t
686 FileMgr::close(void* vp, const std::string& caller) {
687 
688  // return codes:
689  // < 0 : error condition
690  // 0 : actual close of one file
691  // > 0 : shared file, removed from list, no actual close, returns
692  // number of shared files still open.
693 
694  ON_VERBOSE
695  m_log << MSG::VERBOSE << "close(" << vp << ")"
696  << endmsg;
697 
698  close_t r = -1;
699 
700  fileMap::iterator itr;
701  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
702  if (itr->second->fptr() == vp) {
703  break;
704  }
705  }
706 
707  if (itr == m_files.end()) {
708  m_log << MSG::ERROR << "unknown file ptr \"" << vp
709  << "\" when calling close()"
710  << endmsg;
711  return r;
712  }
713 
714  IoTech tech = itr->second->tech();
715 
716  FileHdlr fh;
717 
718  if (getHandler(tech,fh).isFailure()) {
719  return r;
720  }
721  if (! fh.b_closeP_fcn) {
722  m_log << MSG::ERROR << "no close(" << tech << ",void*) function registered"
723  << endmsg;
724  return -1;
725  }
726 
727  FileAttr *fa = itr->second;
728 
729  // find how many times this file is open
730  pair<fileMap::const_iterator, fileMap::const_iterator> fitr =
731  m_files.equal_range(fa->name());
732 
733  int i(0);
734  for (fileMap::const_iterator it=fitr.first; it != fitr.second; ++it) {
735  if (it->second->fptr() == vp) {
736  i++;
737  }
738  }
739 
740  ON_VERBOSE
741  m_log << MSG::VERBOSE << " ref count: " << i
742  << endmsg;
743 
744  if (i > 1 && fa->isShared()) {
745  // file open multiple times in shared access. don't do the actual close
746  ON_DEBUG
747  m_log << MSG::DEBUG << "closing file " << fa->name() << " opened "
748  << i << " times with fptr " << vp << endmsg;
749  m_files.erase(itr);
750 
751  r = i-1;
752 
753  } else if (i == 1 || (i>1 && !fa->isShared()) ) {
754  ON_DEBUG
755  m_log << MSG::DEBUG << "closing: " << *fa << endmsg;
756 
757  try {
758  r = fh.b_closeP_fcn(vp);
759  } catch (const boost::bad_function_call& err) {
760  m_log << MSG::ERROR << "when calling close handler for " << tech
761  << " on file " << fa->name()
762  << " caught " << err.what() << endmsg;
763  execAction(fa, caller, CLOSE_ERR ).ignore();
764  return -1;
765  } catch (...) {
766  m_log << MSG::ERROR << "when calling close handler for " << tech
767  << " on file " << fa->name()
768  << " caught an unknown exception." << endmsg;
769  execAction(fa, caller, CLOSE_ERR ).ignore();
770  return -1;
771  }
772 
773  if (r < 0) {
775  << "close of file with ptr \"" << vp
776  << "\", name: \"" << fa->name()
777  << "\", tech: \"" << tech << "\" failed"
778  << endmsg;
779 
780  return r;
781  }
782 
783  m_files.erase(itr);
784 
785  } else {
786  // this should never happen!
787  m_log << MSG::ERROR
788  << "ref count: " << i << " < 0 when closing " << fa
789  << ". This should never happen"
790  << endmsg;
791  return -1;
792 
793  }
794 
795  fa->fd(-1);
796  fa->flags(INVALID);
797  fa->isOpen(false);
798  fa->fptr(0);
799  m_oldFiles.push_back( fa );
800 
801 
802  // exec all callbacks
803  if (execAction(fa, caller, CLOSE).isFailure()) {
805  << "at least one close callback action failed"
806  << endmsg;
807  }
808 
809  return r;
810 
811 }
812 
813 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
814 
815 reopen_t
816 FileMgr::reopen(Fd fd, const IoFlags& flags, const std::string& caller) {
817 
818  ON_VERBOSE
819  m_log << MSG::VERBOSE << "reopen(" << fd << "," << flags
820  << "," << caller << ")"
821  << endmsg;
822 
823  reopen_t r = -1;
824 
825 
826  fileMap::iterator itr;
827  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
828  if (itr->second->fd() == fd) {
829  break;
830  }
831  }
832 
833  if (itr == m_files.end()) {
834  m_log << MSG::ERROR << "unregistered FD \"" << fd
835  << "\" when calling reopen()"
836  << endmsg;
837  return r;
838  }
839 
840  FileAttr *fa = itr->second;
841  IoTech tech = fa->tech();
842 
843  FileHdlr fh;
844 
845  if (getHandler(tech,fh).isFailure()) {
846  return r;
847  }
848 
849  fa->flags( flags );
850 
851  if ( ! fh.b_reopen_fcn ) {
852  m_log << MSG::ERROR << "no reopen(" << tech << ",Fd) function registered"
853  << endmsg;
854  return -1;
855  }
856 
857 // FIXME: what does it mean to call reopen on a shared file?
858 
859 
860  try {
861  r = fh.b_reopen_fcn(fd,flags);
862  } catch (const boost::bad_function_call& err) {
863  m_log << MSG::ERROR << "when calling reopen handler for " << tech
864  << " on file descriptor " << fd << " with flags "
865  << flags
866  << " caught " << err.what() << endmsg;
867  return -1;
868  } catch (...) {
869  m_log << MSG::ERROR << "when calling reopen handler for " << tech
870  << " on file descriptor " << fd << " with flags "
871  << flags
872  << " caught an unknown exception." << endmsg;
873  return -1;
874  }
875 
876  if (r < 0) {
878  << "reopen of file with FD \"" << fd
879  << "\", name: \"" << fa->name()
880  << "\", tech: \"" << tech
881  << "\", flags: \"" << flags << "\" failed"
882  << endmsg;
883 
884  execAction(fa, caller, Io::REOPEN_ERR ).ignore();
885 
886  return r;
887 
888  }
889 
890  fa->isOpen(true);
891  fa->flags(flags);
892 
893  // exec all callbacks
894  if (execAction(fa, caller, Io::REOPEN).isFailure()) {
896  << "at least one reopen callback action failed"
897  << endmsg;
898  }
899 
900  return r;
901 
902 }
903 
904 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
905 
906 reopen_t
907 FileMgr::reopen(void* vp, const IoFlags& flags, const std::string& caller) {
908  ON_VERBOSE
909  m_log << MSG::VERBOSE << "reopen(" << vp << "," << flags
910  << "," << caller << ")"
911  << endmsg;
912 
913  reopen_t r = -1;
914 
915  fileMap::iterator itr;
916  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
917  if (itr->second->fptr() == vp) {
918  break;
919  }
920  }
921 
922  if (itr == m_files.end()) {
923  m_log << MSG::ERROR
924  << "unregistered file ptr \"" << vp
925  << "\" when calling reopen()"
926  << endmsg;
927  return r;
928  }
929 
930  FileAttr *fa = itr->second;
931  FileHdlr fh;
932  IoTech tech = fa->tech();
933 
934  if (getHandler(tech,fh).isFailure()) {
935  return r;
936  }
937 
938  if ( ! fh.b_reopenP_fcn ) {
939  m_log << MSG::ERROR << "no reopen(" << tech << ",void*) function registered"
940  << endmsg;
941  return -1;
942  }
943 
944  try {
945  r = fh.b_reopenP_fcn(vp,flags);
946  } catch (const boost::bad_function_call& err) {
947  m_log << MSG::ERROR << "when calling reopen handler for " << tech
948  << " on file " << fa->name() << " with flags "
949  << flags
950  << " caught " << err.what() << endmsg;
951  return -1;
952  } catch (...) {
953  m_log << MSG::ERROR << "when calling reopen handler for " << tech
954  << " on file " << fa->name() << " with flags "
955  << flags
956  << " caught an unknown exception." << endmsg;
957  return -1;
958  }
959 
960  if (r < 0) {
962  << "reopen of file with ptr \"" << vp
963  << "\", name: \"" << fa->name()
964  << "\", tech: \"" << tech
965  << "\", flags: \"" << flags << "\" failed"
966  << endmsg;
967 
968  execAction(fa, caller, Io::REOPEN_ERR ).ignore();
969 
970  return r;
971 
972  }
973 
974  fa->isOpen(true);
975  fa->flags(flags);
976 
977  // exec all callbacks
978  if (execAction(fa, caller, Io::REOPEN).isFailure()) {
980  << "at least one reopen callback action failed"
981  << endmsg;
982  }
983 
984  return r;
985 
986 }
987 
988 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
989 
990 int
991 FileMgr::getFileAttr(const std::string& fname, vector<const FileAttr*>& fa) const {
992 
993  fa.clear();
994  pair<fileMap::const_iterator,fileMap::const_iterator> fitr =
995  m_files.equal_range(fname);
996 
997  fileMap::const_iterator itr;
998  for (itr=fitr.first; itr != fitr.second; ++itr) {
999  fa.push_back( (itr->second) );
1000  }
1001 
1002  fileList::const_iterator it2;
1003  for (it2=m_oldFiles.begin(); it2!=m_oldFiles.end(); ++it2) {
1004  if ( (*it2)->name() == fname ) {
1005  fa.push_back( *it2 );
1006  }
1007  }
1008 
1009  return fa.size();
1010 
1011 
1012 }
1013 
1014 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1015 
1016 StatusCode
1017 FileMgr::getFileAttr(const Fd fd, const FileAttr*& fa) const {
1018 
1019  fileMap::const_iterator itr;
1020  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
1021  if (itr->second->fd() == fd) {
1022  fa = (itr->second);
1023  return StatusCode::SUCCESS;
1024  }
1025  }
1026 
1027  fileList::const_iterator it2;
1028  for (it2=m_oldFiles.begin(); it2!=m_oldFiles.end(); ++it2) {
1029  if ( (*it2)->fd() == fd ) {
1030  fa = *it2;
1031  return StatusCode::SUCCESS;
1032  }
1033  }
1034 
1035  return StatusCode::FAILURE;
1036 
1037 }
1038 
1039 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1040 
1041 StatusCode
1042 FileMgr::getFileAttr(void* vp, const FileAttr*& fa) const {
1043 
1044  fileMap::const_iterator itr;
1045  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
1046  if (itr->second->fptr() == vp) {
1047  fa = (itr->second);
1048  return StatusCode::SUCCESS;
1049  }
1050  }
1051 
1052  fileList::const_iterator it2;
1053  for (it2=m_oldFiles.begin(); it2!=m_oldFiles.end(); ++it2) {
1054  if ( (*it2)->fptr() == vp ) {
1055  fa = *it2;
1056  return StatusCode::SUCCESS;
1057  }
1058  }
1059 
1060  return StatusCode::FAILURE;
1061 
1062 }
1063 
1064 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1065 
1066 int
1067 FileMgr::getFiles(vector<string>& files, bool op) const {
1068 
1069  files.clear();
1070 
1071  map<string,FileAttr*>::const_iterator itr;
1072  for (itr=m_files.begin(); itr != m_files.end(); ++itr) {
1073  if (find( files.begin(), files.end(), itr->first) == files.end()) {
1074  files.push_back(itr->first);
1075  }
1076  }
1077 
1078  if (!op) {
1079  std::list<FileAttr*>::const_iterator it2;
1080  for (it2=m_oldFiles.begin(); it2!=m_oldFiles.end(); ++it2) {
1081  if (find(files.begin(), files.end(), (*it2)->name()) == files.end()) {
1082  files.push_back( (*it2)->name());
1083  }
1084  }
1085  }
1086 
1087  return files.size();
1088 
1089 }
1090 
1091 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1092 
1093 int
1094 FileMgr::getFiles(vector<const Io::FileAttr*>& files, bool op) const {
1095 
1096  files.clear();
1097 
1098  map<string,FileAttr*>::const_iterator itr;
1099  for (itr=m_files.begin(); itr != m_files.end(); ++itr) {
1100  files.push_back( (itr->second) );
1101  }
1102 
1103  if (!op) {
1104  std::list<FileAttr*>::const_iterator it2;
1105  for (it2=m_oldFiles.begin(); it2!=m_oldFiles.end(); ++it2) {
1106  files.push_back( *it2 );
1107  }
1108  }
1109 
1110  return files.size();
1111 
1112 }
1113 
1114 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1115 
1116 int
1117 FileMgr::getFiles(const IoTech& tech, vector<string>& files, bool op) const {
1118 
1119  if (tech == UNKNOWN) {
1120  return getFiles(files,op);
1121  }
1122 
1123  files.clear();
1124 
1125  map<string,FileAttr*>::const_iterator itr;
1126  for (itr=m_files.begin(); itr != m_files.end(); ++itr) {
1127  if (itr->second->tech() == tech &&
1128  find(files.begin(),files.end(),itr->first) == files.end()) {
1129  files.push_back(itr->first);
1130  }
1131  }
1132 
1133  if (!op) {
1134  std::list<FileAttr*>::const_iterator it2;
1135  for (it2=m_oldFiles.begin(); it2!=m_oldFiles.end(); ++it2) {
1136  if ( (*it2)->tech() == tech &&
1137  find(files.begin(), files.end(), (*it2)->name()) == files.end()) {
1138  files.push_back( (*it2)->name());
1139  }
1140  }
1141  }
1142 
1143  return files.size();
1144 
1145 }
1146 
1147 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1148 
1149 int
1150 FileMgr::getFiles(const IoTech& tech, vector<const Io::FileAttr*>& files,
1151  bool op) const {
1152 
1153  if (tech == UNKNOWN) {
1154  return getFiles(files,op);
1155  }
1156 
1157  files.clear();
1158 
1159  map<string,FileAttr*>::const_iterator itr;
1160  for (itr=m_files.begin(); itr != m_files.end(); ++itr) {
1161  if (itr->second->tech() == tech) {
1162  files.push_back( (itr->second) );
1163  }
1164  }
1165 
1166  if (!op) {
1167  std::list<FileAttr*>::const_iterator it2;
1168  for (it2=m_oldFiles.begin(); it2!=m_oldFiles.end(); ++it2) {
1169  if ( (*it2)->tech() == tech) {
1170  files.push_back( *it2 );
1171  }
1172  }
1173  }
1174 
1175  return files.size();
1176 
1177 }
1178 
1179 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1180 
1181 int
1182 FileMgr::getFiles(const IoTech& tech, const IoFlags& flags,
1183  vector<string>& files, bool op) const {
1184 
1185  files.clear();
1186 
1187  map<string,FileAttr*>::const_iterator itr;
1188  for (itr=m_files.begin(); itr != m_files.end(); ++itr) {
1189  if (itr->second->tech() == tech || tech == UNKNOWN) {
1190  if ( itr->second->flags() == flags ) {
1191  if (find( files.begin(), files.end(), itr->first ) == files.end()) {
1192  files.push_back(itr->first);
1193  }
1194  }
1195  }
1196  }
1197 
1198  if (!op) {
1199  std::list<FileAttr*>::const_iterator it2;
1200  for (it2=m_oldFiles.begin(); it2!=m_oldFiles.end(); ++it2) {
1201  if ( (*it2)->tech() == tech || tech == UNKNOWN) {
1202  if ( (*it2)->flags() == flags ) {
1203  if (find(files.begin(), files.end(), (*it2)->name()) == files.end()){
1204  files.push_back( (*it2)->name());
1205  }
1206  }
1207  }
1208  }
1209  }
1210 
1211  return files.size();
1212 
1213 }
1214 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1215 
1216 int
1217 FileMgr::getFiles(const IoTech& tech, const IoFlags& flags,
1218  vector<const Io::FileAttr*>& files, bool op) const {
1219 
1220  files.clear();
1221 
1222  map<string,FileAttr*>::const_iterator itr;
1223  for (itr=m_files.begin(); itr != m_files.end(); ++itr) {
1224  if (itr->second->tech() == tech || tech == UNKNOWN) {
1225  if ( itr->second->flags() == flags ) {
1226  files.push_back( (itr->second) );
1227  }
1228  }
1229  }
1230 
1231  if (!op) {
1232  std::list<FileAttr*>::const_iterator it2;
1233  for (it2=m_oldFiles.begin(); it2!=m_oldFiles.end(); ++it2) {
1234  if ( (*it2)->tech() == tech || tech == UNKNOWN) {
1235  if ( (*it2)->flags() == flags ) {
1236  files.push_back( *it2);
1237  }
1238  }
1239  }
1240  }
1241 
1242  return files.size();
1243 
1244 }
1245 
1246 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1247 
1248 // get all descriptors known
1249 // return number found
1250 
1251 int
1252 FileMgr::getFd(vector<Fd>& fd) const {
1253 
1254  map<Fd,FileAttr*>::const_iterator itr;
1255  for (itr=m_descriptors.begin(); itr != m_descriptors.end(); ++itr) {
1256  fd.push_back(itr->first);
1257  }
1258 
1259  return m_descriptors.size();
1260 
1261 }
1262 
1263 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1264 
1265 // get all descriptors given tech
1266 // return number found
1267 
1268 int
1269 FileMgr::getFd(const IoTech& tech, vector<Fd>& fd) const {
1270 
1271  if (tech == UNKNOWN) {
1272  return getFd( fd );
1273  }
1274 
1275  fd.clear();
1276  map<Fd,FileAttr*>::const_iterator itr;
1277  for (itr=m_descriptors.begin(); itr != m_descriptors.end(); ++itr) {
1278  if (itr->second->tech() == tech) {
1279  fd.push_back(itr->first);
1280  }
1281  }
1282 
1283  return fd.size();
1284 
1285 }
1286 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1287 
1288 // get all descriptors of given tech and flags
1289 // return number found
1290 
1291 int
1292 FileMgr::getFd(const IoTech& tech, const IoFlags& flags, vector<Fd>& fd) const {
1293 
1294  fd.clear();
1295  map<Fd,FileAttr*>::const_iterator itr;
1296  for (itr=m_descriptors.begin(); itr != m_descriptors.end(); ++itr) {
1297  if (itr->second->tech() == tech || tech == UNKNOWN) {
1298  if ( itr->second->flags() == flags ) {
1299  fd.push_back(itr->first);
1300  }
1301  }
1302  }
1303 
1304  return fd.size();
1305 
1306 }
1307 
1308 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1309 
1310 const std::string&
1311 FileMgr::fname(const Io::Fd& fd) const {
1312 
1313  fileMap::const_iterator itr;
1314  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
1315  if (itr->second->fd() == fd) {
1316  return itr->second->name();
1317  }
1318  }
1319 
1320  static const std::string s_empty = "";
1321  return s_empty;
1322 
1323 }
1324 
1325 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1326 
1327 const std::string&
1328 FileMgr::fname(void* vp) const {
1329 
1330  fileMap::const_iterator itr;
1331  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
1332  if (itr->second->fptr() == vp) {
1333  return itr->second->name();
1334  }
1335  }
1336 
1337  static const std::string s_empty = "";
1338  return s_empty;
1339 
1340 }
1341 
1342 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1343 
1344 Io::Fd
1345 FileMgr::fd(const std::string& fname) const {
1346 
1347  pair<fileMap::const_iterator,fileMap::const_iterator> fitr =
1348  m_files.equal_range(fname);
1349 
1350  fileMap::const_iterator itr;
1351  for (itr=fitr.first; itr != fitr.second; ++itr) {
1352  if (itr->second->fd() != -1) {
1353  return itr->second->fd();
1354  }
1355  }
1356 
1357  return -1;
1358 
1359 }
1360 
1361 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1362 
1363 Io::Fd
1364 FileMgr::fd(void* fptr) const {
1365 
1366  fileMap::const_iterator itr;
1367  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
1368  if (itr->second->fptr() == fptr) {
1369  return itr->second->fd();
1370  }
1371  }
1372 
1373  return -1;
1374 
1375 }
1376 
1377 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1378 
1379 void*
1380 FileMgr::fptr(const std::string& fname) const {
1381  pair<fileMap::const_iterator,fileMap::const_iterator> fitr =
1382  m_files.equal_range(fname);
1383 
1384  fileMap::const_iterator itr;
1385  for (itr=fitr.first; itr != fitr.second; ++itr) {
1386  if (itr->second->fptr() != 0) {
1387  return itr->second->fptr();
1388  }
1389  }
1390 
1391  return 0;
1392 
1393 }
1394 
1395 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1396 
1397 void*
1398 FileMgr::fptr(const Io::Fd& fd) const {
1399 
1400  fileMap::const_iterator itr;
1401  for (itr = m_files.begin(); itr != m_files.end(); ++itr) {
1402  if (itr->second->fd() == fd) {
1403  return itr->second->fptr();
1404  }
1405  }
1406 
1407  return 0;
1408 
1409 }
1410 
1411 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1412 
1413 void
1415 
1416  m_log << MSG::INFO << "listing registered files ["
1417  << (m_files.size() + m_oldFiles.size() )
1418  << "]:" << endl;
1419 
1420  fileMap::const_iterator itr;
1421  for (itr=m_files.begin(); itr != m_files.end(); ++itr) {
1422  m_log << *(itr->second) << endl;
1423  }
1424 
1425  for (list<FileAttr*>::const_iterator it2=m_oldFiles.begin();
1426  it2 != m_oldFiles.end(); ++it2) {
1427  m_log << **it2 << endl;
1428  }
1429 
1430  m_log << endmsg;
1431 
1432 }
1433 
1434 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1435 
1436 int
1437 FileMgr::getLastError(std::string& err) const {
1438 
1439  err = m_lastErrS;
1440 
1441  return m_lastErr;
1442 
1443 }
1444 
1445 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1446 
1447 StatusCode
1448 FileMgr::getHandler(const Io::IoTech& tech, Io::FileHdlr& hdlr) const {
1449 
1450  std::map<IoTech,FileHdlr>::const_iterator itr = m_handlers.find(tech);
1451  if (itr != m_handlers.end()) {
1452  hdlr = itr->second;
1453  return StatusCode::SUCCESS;
1454  } else {
1455  m_log << MSG::ERROR
1456  << "no handler for tech " << tech << " registered"
1457  << endmsg;
1458  return StatusCode::FAILURE;
1459  }
1460 
1461 }
1462 
1463 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1464 
1465 StatusCode
1466 FileMgr::getHandler(const std::string& fname, Io::FileHdlr& hdlr) const {
1467 
1468  pair<fileMap::const_iterator,fileMap::const_iterator> fitr =
1469  m_files.equal_range(fname);
1470 
1471  if (fitr.first == fitr.second) {
1472  m_log << MSG::ERROR
1473  << "no file \"" << fname << "\" registered. Cannot determine tech"
1474  << endmsg;
1475  return StatusCode::FAILURE;
1476  }
1477 
1478  fileMap::const_iterator itr = fitr.first;
1479  IoTech tech = itr->second->tech();
1480 
1481  itr++;
1482  while( itr != fitr.second ) {
1483  if ( itr->second->tech() != tech ) {
1484  m_log << MSG::ERROR
1485  << "multiple technologies registered for file \"" << fname
1486  << "\". Cannot determine handler" << endmsg;
1487  return StatusCode::FAILURE;
1488  }
1489  itr++;
1490  }
1491 
1492  return getHandler(tech,hdlr);
1493 
1494 }
1495 
1496 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1497 
1498 void
1500 
1501  m_log << MSG::INFO
1502  << "Listing registered handlers:" << endl;
1503 
1504  map<IoTech,FileHdlr>::const_iterator itr;
1505  for (itr=m_handlers.begin(); itr != m_handlers.end(); ++itr) {
1506 
1507  m_log << " " << itr->first << endl;
1508  }
1509  m_log << endmsg;
1510 
1511 }
1512 
1513 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1514 
1515 StatusCode
1516 FileMgr::regAction(bfcn_action_t bf, const Io::Action& a, const std::string& d) {
1517 
1518  ON_DEBUG
1519  m_log << MSG::DEBUG << "registering " << a << " action "
1520  << System::typeinfoName(bf.target_type()) << endmsg;
1521 
1522  if (d == "") {
1523  m_actions[Io::UNKNOWN][a].push_back
1524  (make_pair(bf,System::typeinfoName(bf.target_type())));
1525  } else {
1526  m_actions[Io::UNKNOWN][a].push_back(make_pair(bf,d));
1527  }
1528 
1529 
1530  return StatusCode::SUCCESS;
1531 
1532 }
1533 
1534 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1535 
1536 StatusCode
1538  const std::string& d) {
1539 
1540  ON_DEBUG
1541  m_log << MSG::DEBUG << "registering " << a << " action "
1542  << System::typeinfoName(bf.target_type())
1543  << " for tech " << t << endmsg;
1544 
1545  if (d == "") {
1546  m_actions[t][a].push_back
1547  (make_pair(bf, System::typeinfoName(bf.target_type())));
1548  } else {
1549  m_actions[t][a].push_back(make_pair(bf,d));
1550  }
1551 
1552  return StatusCode::SUCCESS;
1553 
1554 }
1555 
1556 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1557 
1558 void
1560 
1561  m_log << MSG::INFO << "listing registered actions" << endl;
1562 
1563  actionMap::const_iterator itr;
1564 
1565  map<Io::IoTech, actionMap>::const_iterator iit;
1566  for (iit = m_actions.begin(); iit != m_actions.end(); ++iit) {
1567  Io::IoTech t = iit->first;
1568  const actionMap& m = iit->second;
1569 
1570  if (m.size() != 0) {
1571  m_log << " --- Tech: ";
1572  if (t == Io::UNKNOWN) {
1573  m_log << "ALL ---" << endl;
1574  } else {
1575  m_log << t << " ---" << endl;
1576  }
1577 
1578  actionMap::const_iterator iia;
1579  for (iia = m.begin() ; iia != m.end(); ++iia) {
1580  if (iia->second.size() != 0) {
1581  // for (list<bfcn_action_t>::const_iterator it2 = iia->second.begin();
1582  // it2 != iia->second.end(); ++it2) {
1583  for (list<bfcn_desc_t>::const_iterator it2 = iia->second.begin();
1584  it2 != iia->second.end(); ++it2) {
1585 
1586  m_log << " " << iia->first << " "
1587  << it2->second
1588  << endl;
1589  }
1590  }
1591  }
1592 
1593  }
1594  }
1595 
1596 
1597  m_log << endmsg;
1598 
1599 }
1600 
1601 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1602 
1603 StatusCode
1604 FileMgr::execAction( Io::FileAttr* fa, const std::string& caller,
1605  const Io::Action& a) const {
1606 
1607  Io::IoTech tech = fa->tech();
1608 
1609  StatusCode s1,s2;
1610 
1611  std::map<IoTech, actionMap>::const_iterator itr;
1612  itr = m_actions.find(Io::UNKNOWN);
1613 
1614  if (itr != m_actions.end() && itr->second.size() != 0) {
1615  const actionMap &m = itr->second;
1616  s1 = execActs(fa, caller, a, m);
1617  }
1618 
1619  itr = m_actions.find(tech);
1620  if (itr != m_actions.end() && itr->second.size() != 0) {
1621  const actionMap &m = itr->second;
1622  s2 = execActs(fa, caller, a, m);
1623  }
1624 
1625  if (s1.isFailure() || s2.isFailure()) {
1626  return StatusCode::FAILURE;
1627  } else {
1628  return StatusCode::SUCCESS;
1629  }
1630 
1631 }
1632 
1633 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1634 
1635 StatusCode
1636 FileMgr::execActs(Io::FileAttr* fa, const std::string& caller,
1637  const Io::Action& a, const actionMap& m) const {
1638 
1639  actionMap::const_iterator mitr = m.find(a);
1640 
1641  if (mitr == m.end() || mitr->second.size() == 0) {
1642  return StatusCode::SUCCESS;
1643  }
1644 
1645  ON_DEBUG
1646  m_log << MSG::DEBUG
1647  << "executing " << mitr->second.size() << " " << a
1648  << " actions on "
1649  << *fa << " from "
1650  << caller
1651  << endmsg;
1652 
1653 
1654  bool fail(false);
1655 
1656  supMap::const_iterator it2 = m_supMap.find(fa->name());
1657  if (it2 != m_supMap.end()) {
1658  if (get_bit(it2->second,a) || get_bit(it2->second,Io::INVALID_ACTION)) {
1659  ON_DEBUG
1660  m_log << MSG::DEBUG << " --> suppressing callback action for "
1661  << a
1662  << endmsg;
1663  return StatusCode::SUCCESS;
1664  }
1665  }
1666 
1667  for (list<bfcn_desc_t>::const_iterator itr = mitr->second.begin();
1668  itr != mitr->second.end(); ++itr) {
1669 
1670  ON_DEBUG
1671  m_log << MSG::DEBUG << "executing "
1672  << itr->second << endmsg;
1673 
1674  if ( (((itr->first))(fa,caller)).isFailure() ) {
1675  m_log << MSG::WARNING << "execution of "
1676  << itr->second << " on " << *fa
1677  << " failed during " << a << " action"
1678  << endmsg;
1679  fail = true;
1680  }
1681 
1682  }
1683 
1684  if (fail) {
1685  return StatusCode::FAILURE;
1686  } else {
1687  return StatusCode::SUCCESS;
1688  }
1689 
1690 }
1691 
1692 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1693 
1694 bool
1696  bool /*strict*/) const {
1697 
1698  ON_VERBOSE
1699  m_log << MSG::VERBOSE << "accessMatch old: " << fold
1700  << " new: " << fnew
1701  << endmsg;
1702 
1703 
1704  if ( ((fold == Io::READ) && (fnew == Io::READ)) ||
1705  ((fold & Io::WRITE) != 0 && (fnew & Io::WRITE) != 0) ||
1706  ((fold & Io::RDWR) != 0 && (fnew & Io::RDWR) != 0) ) {
1707  return true;
1708  } else {
1709  return false;
1710  }
1711 
1712 }
1713 
1714 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1715 
1716 void
1717 FileMgr::suppressAction(const std::string& f) {
1718 
1720 
1721 }
1722 
1723 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1724 
1725 void
1726 FileMgr::suppressAction(const std::string& f, const Io::Action& a) {
1727 
1728  supMap::iterator it2 = m_supMap.find(f);
1729  if (it2 == m_supMap.end()) {
1730  int b(0);
1731  set_bit(b,a);
1732  m_supMap[f] = b;
1733  } else {
1734  set_bit(it2->second, a);
1735  }
1736 
1737 }
1738 
1739 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1740 
1741 void
1743 
1744  if (m_supMap.size() == 0) return;
1745 
1746  m_log << MSG::INFO << "listing suppressed file actions" << endl;
1747 
1748  supMap::const_iterator it2;
1749  for (it2=m_supMap.begin(); it2 != m_supMap.end(); ++it2) {
1750  m_log << " " << it2->first;
1751  if (get_bit(it2->second, Io::INVALID_ACTION)) {
1752  m_log << " ALL" << endl;
1753  } else {
1754  for (int i=0; i<Io::INVALID_ACTION; ++i) {
1755  if (get_bit(it2->second,i)) { m_log << " " << (Io::Action)i; }
1756  }
1757  m_log << endl;
1758  }
1759  }
1760 
1761  m_log << endmsg;
1762 
1763 }
IoTech
Definition: IFileMgr.h:170
StringProperty m_logfile
Definition: FileMgr.h:135
IntegerProperty m_outputLevel
Service output level.
Definition: Service.h:244
virtual const std::vector< const Property * > * getProperties(const std::string &client) const =0
Get the properties associated to a given client.
virtual void listFiles() const
Definition: FileMgr.cpp:1414
Definition: IFileMgr.h:19
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
virtual Io::reopen_t reopen(const Fd, const IoFlags &, const std::string &caller)
virtual Io::open_t open(const Io::IoTech &, const std::string &caller, const std::string &fname, const Io::IoFlags &, Io::Fd &fd, void *&ptr, const std::string &desc="", const bool shared=false)
Definition: FileMgr.cpp:335
std::function< StatusCode(FILEMGR_CALLBACK_ARGS) > bfcn_action_t
Definition: IFileMgr.h:340
virtual StatusCode setProperty(const Property &p)
Set the property by property.
Definition: Service.cpp:340
virtual StatusCode regAction(Io::bfcn_action_t, const Io::Action &, const std::string &desc="")
Definition: FileMgr.cpp:1516
int reopen_t
Definition: IFileMgr.h:280
int Fd
Definition: IFileMgr.h:197
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
bfcn_reopen_t b_reopen_fcn
Definition: IFileMgr.h:295
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
MsgStream m_log
Definition: FileMgr.h:176
supMap m_supMap
Definition: FileMgr.h:169
virtual int getFileAttr(const std::string &, std::vector< const FileAttr * > &) const
Definition: FileMgr.cpp:991
void * fptr() const
Definition: IFileMgr.h:215
fileMap m_files
Definition: FileMgr.h:157
int outputLevel() const
get the Service's output level
Definition: Service.h:251
int open_t
Definition: IFileMgr.h:278
fileList m_oldFiles
Definition: FileMgr.h:164
std::map< Io::Action, std::list< bfcn_desc_t > > actionMap
Definition: FileMgr.h:145
STL namespace.
bool isOpen() const
Definition: IFileMgr.h:216
StatusCode execAction(Io::FileAttr *, const std::string &, const Io::Action &) const
Definition: FileMgr.cpp:1604
virtual int getFiles(std::vector< std::string > &, bool onlyOpen=true) const
Definition: FileMgr.cpp:1067
virtual ~FileMgr()
Definition: FileMgr.cpp:57
void set_bit(int &f, const unsigned int &b)
Definition: FileMgr.cpp:27
virtual StatusCode getHandler(const IoTech &, FileHdlr &) const
std::map< Fd, FileAttr * > m_descriptors
Definition: FileMgr.h:159
Fd fd() const
Definition: IFileMgr.h:209
virtual StatusCode regHandler(FileHdlr)
Definition: FileMgr.cpp:255
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: FileMgr.cpp:163
virtual void handle(const Incident &)
Inform that a new incident has occurred.
Definition: FileMgr.cpp:248
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: FileMgr.cpp:64
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
bool accessMatch(const Io::IoFlags &, const Io::IoFlags &, bool strict=false) const
Definition: FileMgr.cpp:1695
virtual void suppressAction(const std::string &)
Definition: FileMgr.cpp:1717
virtual void listActions() const
Definition: FileMgr.cpp:1559
Action
Definition: IFileMgr.h:316
virtual const std::string & fname(const Io::Fd &) const
Definition: FileMgr.cpp:1311
const std::string & name() const
Definition: IFileMgr.h:210
std::map< IoTech, FileHdlr > m_handlers
Definition: FileMgr.h:158
virtual Io::close_t close(const Fd, const std::string &caller)
std::vector< FileAttr * > m_attr
Definition: FileMgr.h:161
Main interface for the JobOptions service.
virtual StatusCode deregHandler(const IoTech &)
Definition: FileMgr.cpp:302
bool isShared() const
Definition: IFileMgr.h:217
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
constexpr double m
Definition: SystemOfUnits.h:93
bfcn_close_t b_close_fcn
Definition: IFileMgr.h:293
StatusCode execActs(Io::FileAttr *, const std::string &, const Io::Action &, const actionMap &m) const
Definition: FileMgr.cpp:1636
bfcn_closeP_t b_closeP_fcn
Definition: IFileMgr.h:294
BooleanProperty m_printSummary
Definition: FileMgr.h:136
bfcn_reopenP_t b_reopenP_fcn
Definition: IFileMgr.h:296
string s1
Definition: ana.py:142
const TYPE & value() const
explicit conversion
Definition: Property.h:355
std::string m_lastErrS
Definition: FileMgr.h:171
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:329
bool get_bit(const int &f, const unsigned int &b)
Definition: FileMgr.cpp:31
FileMgr(const std::string &name, ISvcLocator *svc)
Definition: FileMgr.cpp:38
RootFileHandler * m_rfh
Definition: FileMgr.h:174
BooleanProperty m_loadRootHandler
Definition: FileMgr.h:136
POSIXFileHandler * m_pfh
Definition: FileMgr.h:175
bfcn_open_t b_open_fcn
Definition: IFileMgr.h:292
virtual StatusCode initialize()
Initialization (from CONFIGURED to INITIALIZED).
Definition: Service.cpp:72
BooleanProperty m_loadPosixHandler
Definition: FileMgr.h:136
#define DECLARE_SERVICE_FACTORY(x)
Definition: Service.h:291
virtual void listSuppression() const
Definition: FileMgr.cpp:1742
IoTech tech() const
Definition: IFileMgr.h:212
bool match(const IoFlags &fa, bool strict=true) const
Definition: IFileMgr.h:59
StringProperty m_ssl_cert
Definition: FileMgr.h:137
Base class for all Incidents (computing events).
Definition: Incident.h:16
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:106
virtual Io::Fd fd(const std::string &) const
Definition: FileMgr.cpp:1345
Templated class to add the standard messaging functionalities.
StatusCode service(const std::string &name, const T *&psvc, bool createIf=true) const
Access a service by name, creating it if it doesn't already exist.
Definition: Service.h:143
virtual StatusCode hasHandler(const IoTech &) const
Definition: FileMgr.cpp:321
virtual void * fptr(const std::string &) const
Definition: FileMgr.cpp:1380
int m_lastErr
Definition: FileMgr.h:172
#define ON_DEBUG
Definition: FileMgr.cpp:14
void ignore() const
Definition: StatusCode.h:107
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:212
StringProperty m_ssl_proxy
Definition: FileMgr.h:137
list i
Definition: ana.py:128
IoFlags flags() const
Definition: IFileMgr.h:213
#define ON_VERBOSE
Definition: FileMgr.cpp:15
IoTech tech
Definition: IFileMgr.h:290
virtual void listHandlers() const
Definition: FileMgr.cpp:1499
string s2
Definition: ana.py:143
virtual StatusCode finalize()
Finalize (from INITIALIZED to CONFIGURED).
Definition: Service.cpp:197
int close_t
Definition: IFileMgr.h:279
virtual void setOutputLevel(int new_level)=0
Set new global output level threshold.
virtual int getFd(std::vector< Fd > &) const
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:334
std::map< IoTech, actionMap > m_actions
Definition: FileMgr.h:166
virtual int getLastError(std::string &) const
Definition: FileMgr.cpp:1437