The Gaudi Framework
master (2e57474e)
Toggle main menu visibility
Loading...
Searching...
No Matches
POSIXFileHandler.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 <
GaudiKernel/IFileMgr.h
>
12
#include <errno.h>
13
#include <fcntl.h>
14
#include <stdio.h>
15
#include <string.h>
16
#include <sys/stat.h>
17
#include <sys/types.h>
18
#include <unistd.h>
19
20
#include "
POSIXFileHandler.h
"
21
#include <
GaudiKernel/MsgStream.h
>
22
23
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25
POSIXFileHandler::POSIXFileHandler
(
IMessageSvc
* msg ) :
m_log
( msg,
"POSIXFileHandler"
) {
26
27
m_level
= msg->outputLevel(
"POSIXFileHandler"
);
28
}
29
30
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
31
32
Io::open_t
POSIXFileHandler::openPOSIXFile
(
const
std::string& n,
const
Io::IoFlags
& f,
const
std::string& desc,
33
Io::Fd
& fd,
void
*& ptr ) {
34
35
m_log
.setLevel(
m_level
);
36
37
if
(
m_log
.level() <=
MSG::DEBUG
)
38
m_log
<<
MSG::DEBUG
<<
"openPOSIXFile(\""
<< n <<
"\","
<< f <<
","
<< desc <<
")"
<<
endmsg
;
39
40
ptr =
nullptr
;
41
fd = -1;
42
43
int
mm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
44
45
fd = open( n.c_str(), (
int
)f, mm );
46
int
ierr = errno;
47
48
if
( fd == -1 ) {
49
m_log
<<
MSG::ERROR
<<
"Error opening POSIX file \""
<< n <<
"\": "
<< strerror( ierr ) <<
endmsg
;
50
return
1;
51
}
52
53
std::string m;
54
if
( f.
isRead
() ) {
55
m =
"r"
;
56
}
else
if
( f.
isWrite
() ) {
57
if
( ( f &
Io::APPEND
) ) {
58
m =
"a"
;
59
}
else
{
60
m =
"w"
;
61
}
62
}
else
if
( f.
isRdWr
() ) {
63
m =
"r+"
;
64
}
else
{
65
m_log
<<
MSG::ERROR
<<
"unknown mode "
<< f <<
" when calling fdopen on "
<< n <<
endmsg
;
66
return
1;
67
}
68
69
if
(
m_log
.level() <=
MSG::DEBUG
)
m_log
<<
MSG::DEBUG
<<
"calling fdopen with mode "
<< m <<
endmsg
;
70
71
ptr = fdopen( fd, m.c_str() );
72
ierr = errno;
73
74
if
( !ptr ) {
75
m_log
<<
MSG::ERROR
<<
"Error calling fdopen on \""
<< n <<
"\": "
<< strerror( ierr ) <<
endmsg
;
76
return
1;
77
}
78
79
m_log
<<
MSG::DEBUG
<<
"opened POSIX file, Fd: "
<< fd <<
" FILE*: "
<< ptr <<
endmsg
;
80
81
return
0;
82
}
83
84
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
85
86
Io::close_t
POSIXFileHandler::closePOSIXFile
(
Io::Fd
fd ) {
87
88
if
(
m_log
.level() <=
MSG::DEBUG
)
m_log
<<
MSG::DEBUG
<<
"closePOSIXFile(fd:"
<< fd <<
")"
<<
endmsg
;
89
90
if
( fd == -1 ) {
91
m_log
<<
MSG::ERROR
<<
"Unable to close file: FD == -1 "
<<
endmsg
;
92
return
-1;
93
}
94
95
if
( close( fd ) != 0 ) {
96
int
ierr = errno;
97
m_log
<<
MSG::ERROR
<<
"Error closing POSIX file with FD "
<< fd << strerror( ierr ) <<
endmsg
;
98
return
-1;
99
}
100
101
return
0;
102
}
103
104
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
105
106
Io::reopen_t
POSIXFileHandler::reopenPOSIXFile
(
Io::Fd
/*fd*/
,
const
Io::IoFlags
& ) {
107
108
m_log
<<
MSG::ERROR
<<
"reopen not implemented"
<<
endmsg
;
109
return
-1;
110
}
IFileMgr.h
MsgStream.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition
MsgStream.h:198
POSIXFileHandler.h
IMessageSvc
The IMessage is the interface implemented by the message service.
Definition
IMessageSvc.h:34
Io::IoFlags
Definition
IFileMgr.h:43
Io::IoFlags::isRead
bool isRead() const
Definition
IFileMgr.h:58
Io::IoFlags::isWrite
bool isWrite() const
Definition
IFileMgr.h:59
Io::IoFlags::isRdWr
bool isRdWr() const
Definition
IFileMgr.h:60
POSIXFileHandler::closePOSIXFile
Io::close_t closePOSIXFile(Io::Fd fd)
Definition
POSIXFileHandler.cpp:86
POSIXFileHandler::POSIXFileHandler
POSIXFileHandler(IMessageSvc *)
Definition
POSIXFileHandler.cpp:25
POSIXFileHandler::m_log
MsgStream m_log
Definition
POSIXFileHandler.h:38
POSIXFileHandler::openPOSIXFile
Io::open_t openPOSIXFile(const std::string &n, const Io::IoFlags &f, const std::string &desc, Io::Fd &fd, void *&ptr)
Definition
POSIXFileHandler.cpp:32
POSIXFileHandler::reopenPOSIXFile
Io::reopen_t reopenPOSIXFile(Io::Fd fd, const Io::IoFlags &)
Definition
POSIXFileHandler.cpp:106
POSIXFileHandler::m_level
int m_level
Definition
POSIXFileHandler.h:39
Io::reopen_t
int reopen_t
Definition
IFileMgr.h:232
Io::APPEND
@ APPEND
Definition
IFileMgr.h:38
Io::Fd
int Fd
Definition
IFileMgr.h:158
Io::close_t
int close_t
Definition
IFileMgr.h:231
Io::open_t
int open_t
Definition
IFileMgr.h:230
MSG::DEBUG
@ DEBUG
Definition
IMessageSvc.h:22
MSG::ERROR
@ ERROR
Definition
IMessageSvc.h:22
GaudiSvc
src
FileMgr
POSIXFileHandler.cpp
Generated on
for The Gaudi Framework by
1.17.0