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