The Gaudi Framework  master (37c0b60a)
FileMgrTest.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "FileMgrTest.h"
12 #include <GaudiKernel/IFileMgr.h>
14 #include <GaudiKernel/MsgStream.h>
15 #include <TFile.h>
16 #include <TSSLSocket.h>
17 
18 #include <fstream>
19 #include <stdio.h>
20 #ifndef __APPLE__
21 # include <ext/stdio_filebuf.h> // __gnu_cxx::stdio_filebuf
22 #endif // not __APPLE__
23 
24 // Static Factory declaration
25 
27 
28 
30 FileMgrTest::FileMgrTest( const std::string& name, ISvcLocator* pSvcLocator ) : Algorithm( name, pSvcLocator ) {}
31 
32 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
33 
35 
37 
38  m_f1 = "/etc/redhat-release";
39  m_f2 = "t2.txt";
40  m_f3 = "t3.txt";
41  m_f4 = "t4.txt";
42 
43  m_fr1 = "tuple1.rt";
44  m_fr2 = "http://annwm.lbl.gov/~leggett/tuple2.rt";
45  m_fr3 = "https://t-dpm.grid.sinica.edu.tw/dpm/grid.sinica.edu.tw/home/atlas/atlasppsscratchdisk/mc10_7TeV/AOD/"
46  "e574_s1110_s1100_r1655_r1700/"
47  "mc10_7TeV.105805.filtered_minbias6.merge.AOD.e574_s1110_s1100_r1655_r1700_tid261524_00/"
48  "AOD.261524._032551.pool.root.1";
49 
50  if ( p_fileMgr.retrieve().isFailure() ) {
51  error() << "unable to get the FileMgr" << endmsg;
53  } else {
54  debug() << "got the FileMgr" << endmsg;
55  }
56 
57  Io::bfcn_action_t boa =
58  std::bind( &FileMgrTest::PosixOpenAction, this, std::placeholders::_1, std::placeholders::_2 );
59  if ( p_fileMgr->regAction( boa, Io::OPEN, Io::POSIX, "FileMgrTest::POSIXOpenAction" ).isFailure() ) {
60  error() << "unable to register POSIX file open action with FileMgr" << endmsg;
61  }
62 
63  if ( p_fileMgr->regAction( boa, Io::OPEN_ERR, Io::POSIX, "FileMgrTest::PosixOpenAction" ).isFailure() ) {
64  error() << "unable to register POSIX file open ERR action with FileMgr" << endmsg;
65  }
66 
67  Io::bfcn_action_t bob = std::bind( &FileMgrTest::allCloseAction, this, std::placeholders::_1, std::placeholders::_2 );
68  if ( p_fileMgr->regAction( bob, Io::CLOSE ).isFailure() ) {
69  // "FileMgrTest::allCloseAction").isFailure()) {
70  error() << "unable to register all Close action with FileMgr" << endmsg;
71  }
72 
73  int r;
74 
75  r = p_fileMgr->open( Io::POSIX, name(), m_f1, Io::READ, fd_1, "ASCII" );
76  if ( r != 0 ) {
77  error() << "unable to open " << m_f1 << " for reading" << endmsg;
78  } else {
79  // fp_1 = fdopen(fd_1, "r");
80  fp_1 = (FILE*)p_fileMgr->fptr( fd_1 );
81  info() << "opened " << m_f1 << " for reading with FD: " << fd_1 << " FILE* " << fp_1 << endmsg;
82  }
83 
84  r = p_fileMgr->open( Io::POSIX, name(), m_f2, Io::WRITE | Io::CREATE, fd_2, "ASCII" );
85  if ( r != 0 ) {
86  error() << "unable to open " << m_f2 << " for writing" << endmsg;
87  } else {
88  // fp_2 = fdopen(fd_2,"w");
89  fp_2 = (FILE*)p_fileMgr->fptr( fd_2 );
90  info() << "opened " << m_f2 << " for writing with FD: " << fd_2 << " FILE* " << fp_2 << endmsg;
91  }
92 
93  // make sure we have something in m_f3 to append to;
94  std::ofstream ofs{ m_f3 };
95  ofs << "initial line" << std::endl;
96  ofs.close();
97 
98  r = p_fileMgr->open( Io::POSIX, name(), m_f3, Io::WRITE | Io::APPEND, fd_3, "ASCII" );
99  if ( r != 0 ) {
100  error() << "unable to open " << m_f3 << " for write|append" << endmsg;
101  } else {
102  // fp_3 = fdopen(fd_3,"a");
103  fp_3 = (FILE*)p_fileMgr->fptr( fd_3 );
104  info() << "opened " << m_f3 << " for reading with FD: " << fd_3 << " FILE* " << fp_3 << endmsg;
105  }
106 
107  Io::Fd fd;
108 
109  error() << "the following error is expected" << endmsg;
110  r = p_fileMgr->open( Io::POSIX, name(), m_f2, Io::WRITE | Io::CREATE | Io::EXCL, fd, "ASCII" );
111  if ( r != 0 ) {
112  info() << "unable to open " << m_f2 << " for WRITE|CREATE|EXCL - expected as file already exists" << endmsg;
113  } else {
114  error() << "opened " << m_f2 << " for reading with FD: " << fd << " This should not occur!" << endmsg;
115  }
116 
117  r = p_fileMgr->open( Io::POSIX, name(), m_f1, Io::READ, fd_4, "ASCII" );
118  if ( r != 0 ) {
119  error() << "unable to open " << m_f1 << " again for reading" << endmsg;
120  } else {
121  fp_4 = (FILE*)p_fileMgr->fptr( fd_4 );
122  info() << "opened " << m_f1 << " again for reading with FD: " << fd_4 << " FILE* " << fp_4 << endmsg;
123  }
124 
125  void* vp( 0 );
126  r = p_fileMgr->open( Io::ROOT, name(), m_fr1, Io::READ, vp, "HIST" );
127  if ( r != 0 ) {
128  error() << "unable to open " << m_fr1 << " again for reading" << endmsg;
129  } else {
130  fp_r1 = (TFile*)vp;
131  info() << "opened " << m_fr1 << " for reading with ptr: " << fp_r1 << endmsg;
132  }
133 
134  r = p_fileMgr->open( Io::ROOT, name(), m_fr2, Io::READ, vp, "HIST" );
135  if ( r != 0 ) {
136  error() << "unable to open " << m_fr2 << " for reading" << endmsg;
137  } else {
138  fp_r2 = (TFile*)vp;
139  info() << "opened " << m_fr2 << " for reading with ptr: " << fp_r2 << " size: " << fp_r2->GetSize() << endmsg;
140  }
141 
142  // std::string cafile, capath,ucert,ukey;
143  // cafile = "/tmp/x509up_u6919";
144  // capath = "/afs/cern.ch/project/gd/LCG-share2/certificates";
145  // ucert = cafile;
146  // ukey = cafile;
147 
148  // TSSLSocket::SetUpSSL(cafile.c_str(), capath.c_str(), ucert.c_str(), ukey.c_str());
149  r = p_fileMgr->open( Io::ROOT, name(), m_fr3, Io::READ, vp, "HIST" );
150  if ( r != 0 ) {
151  error() << "unable to open " << m_fr3 << " for reading" << endmsg;
152  } else {
153  fp_r3 = (TFile*)vp;
154  info() << "opened " << m_fr3 << " for reading with ptr: " << fp_r3 << " size: " << fp_r3->GetSize() << endmsg;
155  }
156 
157  for ( int j = 0; j < 2; ++j ) {
158  r = p_fileMgr->open( Io::POSIX, name(), "t6.txt", Io::WRITE | Io::CREATE, fd, "ASCII" );
159  if ( r != 0 ) {
160  error() << "unable to open t6.txt for writing" << endmsg;
161  } else {
162  info() << "opened t6.txt for writing, fd: " << fd << " will now close" << endmsg;
163  r = p_fileMgr->close( fd, name() );
164  if ( r != 0 ) {
165  error() << "unable to close " << m_f1 << " with FD " << fd_1 << endmsg;
166  } else {
167  info() << "closed " << name() << endmsg;
168  }
169  }
170  }
171 
172  return st;
173 }
174 
175 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
176 
178 
179 #ifndef __APPLE__
180  info() << "writing to " << p_fileMgr->fname( (void*)fp_2 ) << endmsg;
181 
182  std::ofstream ofs;
183  __gnu_cxx::stdio_filebuf<char> fb( fp_2, std::ios::out );
184 
185  ofs.std::ios::rdbuf( &fb );
186  ofs << "Hello World!" << std::endl;
187 
188  info() << "appending to " << p_fileMgr->fname( (void*)fp_3 ) << endmsg;
189 
190  std::ofstream ofs2;
191  __gnu_cxx::stdio_filebuf<char> fb2( fp_3, std::ios::out );
192 
193  ofs2.std::ios::rdbuf( &fb2 );
194  ofs2 << "Hello World!" << std::endl;
195 #endif // not __APPLE__
196 
197  return StatusCode::SUCCESS;
198 }
199 
200 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
201 
203 
206  int i = p_fileMgr->getFiles( v );
207 
208  auto& log = info();
209  log << "listing all open files [" << i << "]" << std::endl;
210  for ( itr = v.begin(); itr != v.end(); ++itr ) { log << " " << *itr << std::endl; }
211  log << endmsg;
212 
213  i = p_fileMgr->getFiles( v, false );
214  log << "listing ALL files [" << i << "]" << std::endl;
215  for ( itr = v.begin(); itr != v.end(); ++itr ) { log << " " << *itr << std::endl; }
216  log << endmsg;
217 
220  i = p_fileMgr->getFiles( Io::POSIX, v2, false );
221  log << "listing all POSIX files ever opened [" << i << "]" << std::endl;
222  for ( it2 = v2.begin(); it2 != v2.end(); ++it2 ) { log << " " << ( *it2 )->name() << std::endl; }
223  log << endmsg;
224 
225  int r;
226 
227  r = p_fileMgr->close( fd_1, name() );
228  if ( r != 0 ) {
229  error() << "unable to close " << m_f1 << " with FD " << fd_1 << endmsg;
230  } else {
231  info() << "closed " << m_f1 << endmsg;
232  }
233 
234  r = p_fileMgr->close( fd_2, name() );
235  if ( r != 0 ) {
236  error() << "unable to close " << m_f2 << " with FD " << fd_2 << endmsg;
237  } else {
238  info() << "closed " << m_f2 << endmsg;
239  }
240 
241  r = p_fileMgr->close( fd_3, name() );
242  if ( r != 0 ) {
243  error() << "unable to close " << m_f3 << " with FD " << fd_3 << endmsg;
244  } else {
245  info() << "closed " << m_f3 << endmsg;
246  }
247 
248  r = p_fileMgr->close( fd_4, name() );
249  if ( r != 0 ) {
250  error() << "unable to close " << m_f1 << " with FD " << fd_4 << endmsg;
251  } else {
252  info() << "closed " << m_f1 << endmsg;
253  }
254 
255  r = p_fileMgr->close( fp_r1, name() );
256  if ( r != 0 ) {
257  error() << "unable to close " << m_fr1 << " with ptr " << fp_r1 << endmsg;
258  } else {
259  info() << "closed " << m_fr1 << endmsg;
260  }
261 
262  r = p_fileMgr->close( fp_r2, name() );
263  if ( r != 0 ) {
264  error() << "unable to close " << m_fr2 << " with ptr " << fp_r2 << endmsg;
265  } else {
266  info() << "closed " << m_fr2 << endmsg;
267  }
268 
269  // r = p_fileMgr->close(fp_r3,name());
270  // if (r != 0) {
271  // error() << "unable to close " << m_fr3 << " with ptr " << fp_r3
272  // << endmsg;
273  // } else {
274  // info() << "closed " << m_fr3 << endmsg;
275  // }
276 
277  return StatusCode::SUCCESS;
278 }
279 
280 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
281 
283 
284  info() << "PosixOpenAction called by " << c << " for tech " << fa->tech() << " on " << fa << endmsg;
285 
286  // if (fa.tech() != Io::POSIX) {
287 
288  // // error() << "PosixOpenAction called for incorrect tech: "
289  // // << fa.tech() << " on " << fa
290  // // << endmsg;
291 
292  // return StatusCode::SUCCESS;
293  // }
294 
295  // info() << "PosixOpenAction for " << fa
296  // << endmsg;
297 
298  return StatusCode::SUCCESS;
299 }
300 
301 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
302 
304 
305  info() << "AllCloseAction called by " << c << " for tech " << fa->tech() << " on " << fa << endmsg;
306 
307  return StatusCode::SUCCESS;
308 }
FileMgrTest::p_fileMgr
ServiceHandle< IFileMgr > p_fileMgr
Definition: FileMgrTest.h:43
IFileMgr::getFiles
virtual int getFiles(std::vector< std::string > &FILES, bool onlyOpen=true) const =0
FileMgrTest::fp_2
FILE * fp_2
Definition: FileMgrTest.h:39
Io::OPEN
@ OPEN
Definition: IFileMgr.h:278
FileMgrTest::fp_4
FILE * fp_4
Definition: FileMgrTest.h:39
Io::CLOSE
@ CLOSE
Definition: IFileMgr.h:278
FileMgrTest::finalize
StatusCode finalize() override
Definition: FileMgrTest.cpp:202
std::bind
T bind(T... args)
FileMgrTest::fd_4
Io::Fd fd_4
Definition: FileMgrTest.h:38
std::string
STL class.
FileMgrTest::fp_r1
TFile * fp_r1
Definition: FileMgrTest.h:40
Gaudi.Configuration.log
log
Definition: Configuration.py:28
Io::FileAttr::tech
IoTech tech() const
Definition: IFileMgr.h:188
FileMgrTest::fd_1
Io::Fd fd_1
Definition: FileMgrTest.h:38
FileMgrTest::m_f3
std::string m_f3
Definition: FileMgrTest.h:41
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:526
FileMgrTest
an algorithm to test the FileMgr
Definition: FileMgrTest.h:29
gaudirun.fd
fd
Definition: gaudirun.py:630
std::vector< std::string >
ISvcLocator
Definition: ISvcLocator.h:46
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
gaudirun.c
c
Definition: gaudirun.py:525
IFileMgr.h
std::function
FileMgrTest::fp_3
FILE * fp_3
Definition: FileMgrTest.h:39
Io::WRITE
@ WRITE
Definition: IFileMgr.h:38
FileMgrTest::execute
StatusCode execute() override
Definition: FileMgrTest.cpp:177
FileMgrTest::m_fr1
std::string m_fr1
Definition: FileMgrTest.h:41
StatusCode
Definition: StatusCode.h:65
IFileMgr::regAction
virtual StatusCode regAction(Io::bfcn_action_t, const Io::Action &, const std::string &d="")=0
Io::ROOT
@ ROOT
Definition: IFileMgr.h:156
ProduceConsume.j
j
Definition: ProduceConsume.py:104
std::ofstream
STL class.
FileMgrTest::fp_r2
TFile * fp_r2
Definition: FileMgrTest.h:40
FileMgrTest::initialize
StatusCode initialize() override
Definition: FileMgrTest.cpp:34
FileMgrTest::fp_1
FILE * fp_1
Definition: FileMgrTest.h:39
GaudiPartProp.tests.v2
v2
Definition: tests.py:59
Io::FileAttr
Definition: IFileMgr.h:171
FileMgrTest::allCloseAction
StatusCode allCloseAction(FILEMGR_CALLBACK_ARGS)
Definition: FileMgrTest.cpp:303
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
ServiceHandle::retrieve
StatusCode retrieve(T *&service) const override
Do the real retrieval of the Service.
Definition: ServiceHandle.h:97
Io::Fd
int Fd
Definition: IFileMgr.h:169
FileMgrTest::fd_2
Io::Fd fd_2
Definition: FileMgrTest.h:38
IFileMgr::fname
virtual const std::string & fname(const Io::Fd &) const =0
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
FileMgrTest::m_fr3
std::string m_fr3
Definition: FileMgrTest.h:41
FileMgrTest::m_f1
std::string m_f1
Definition: FileMgrTest.h:41
Io::EXCL
@ EXCL
Definition: IFileMgr.h:42
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
std::endl
T endl(T... args)
FileMgrTest::PosixOpenAction
StatusCode PosixOpenAction(FILEMGR_CALLBACK_ARGS)
Definition: FileMgrTest.cpp:282
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
Io::CREATE
@ CREATE
Definition: IFileMgr.h:41
FileMgrTest::m_f4
std::string m_f4
Definition: FileMgrTest.h:41
FileMgrTest::fd_3
Io::Fd fd_3
Definition: FileMgrTest.h:38
Io::APPEND
@ APPEND
Definition: IFileMgr.h:45
Io::OPEN_ERR
@ OPEN_ERR
Definition: IFileMgr.h:278
Properties.v
v
Definition: Properties.py:122
Io::POSIX
@ POSIX
Definition: IFileMgr.h:156
IFileMgr::close
virtual Io::close_t close(const Io::Fd, const std::string &caller)=0
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
FileMgrTest.h
IFileMgr::open
virtual Io::open_t open(const Io::IoTech &, const std::string &caller, const std::string &fname, const Io::IoFlags &, Io::Fd &, void *&, const std::string &desc, const bool shared=false)=0
FileMgrTest::m_fr2
std::string m_fr2
Definition: FileMgrTest.h:41
FileMgrTest::fp_r3
TFile * fp_r3
Definition: FileMgrTest.h:40
Io::READ
@ READ
Definition: IFileMgr.h:37
MsgStream.h
FileMgrTest::m_f2
std::string m_f2
Definition: FileMgrTest.h:41
IFileMgr::fptr
virtual void * fptr(const std::string &) const =0
PrepareBase.out
out
Definition: PrepareBase.py:20