The Gaudi Framework  v29r5 (37229091)
POSIXFileHandler.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/IFileMgr.h"
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 
10 #include "GaudiKernel/MsgStream.h"
11 #include "POSIXFileHandler.h"
12 
13 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
14 
15 POSIXFileHandler::POSIXFileHandler( IMessageSvc* msg ) : m_log( msg, "POSIXFileHandler" )
16 {
17 
18  m_level = msg->outputLevel( "POSIXFileHandler" );
19 }
20 
21 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 
24  Io::Fd& fd, void*& ptr )
25 {
26 
28 
29  if ( m_log.level() <= MSG::DEBUG )
30  m_log << MSG::DEBUG << "openPOSIXFile(\"" << n << "\"," << f << "," << desc << ")" << endmsg;
31 
32  ptr = nullptr;
33  fd = -1;
34 
35  int mm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
36 
37  fd = open( n.c_str(), (int)f, mm );
38  int ierr = errno;
39 
40  if ( fd == -1 ) {
41  m_log << MSG::ERROR << "Error opening POSIX file \"" << n << "\": " << strerror( ierr ) << endmsg;
42  return 1;
43  }
44 
45  std::string m;
46  if ( f.isRead() ) {
47  m = "r";
48  } else if ( f.isWrite() ) {
49  if ( ( f & Io::APPEND ) ) {
50  m = "a";
51  } else {
52  m = "w";
53  }
54  } else if ( f.isRdWr() ) {
55  m = "r+";
56  } else {
57  m_log << MSG::ERROR << "unknown mode " << f << " when calling fdopen on " << n << endmsg;
58  return 1;
59  }
60 
61  if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "calling fdopen with mode " << m << endmsg;
62 
63  ptr = fdopen( fd, m.c_str() );
64  ierr = errno;
65 
66  if ( !ptr ) {
67  m_log << MSG::ERROR << "Error calling fdopen on \"" << n << "\": " << strerror( ierr ) << endmsg;
68  return 1;
69  }
70 
71  m_log << MSG::DEBUG << "opened POSIX file, Fd: " << fd << " FILE*: " << ptr << endmsg;
72 
73  return 0;
74 }
75 
76 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
77 
79 {
80 
81  if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "closePOSIXFile(fd:" << fd << ")" << endmsg;
82 
83  if ( fd == -1 ) {
84  m_log << MSG::ERROR << "Unable to close file: FD == -1 " << endmsg;
85  return -1;
86  }
87 
88  if ( close( fd ) != 0 ) {
89  int ierr = errno;
90  m_log << MSG::ERROR << "Error closing POSIX file with FD " << fd << strerror( ierr ) << endmsg;
91  return -1;
92  }
93 
94  return 0;
95 }
96 
97 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
98 
100 {
101 
102  m_log << MSG::ERROR << "reopen not implemented" << endmsg;
103  return -1;
104 }
Io::close_t closePOSIXFile(Io::Fd fd)
Io::reopen_t reopenPOSIXFile(Io::Fd fd, const Io::IoFlags &)
int reopen_t
Definition: IFileMgr.h:251
int Fd
Definition: IFileMgr.h:171
POSIXFileHandler(IMessageSvc *)
int open_t
Definition: IFileMgr.h:249
constexpr double mm
Definition: SystemOfUnits.h:86
STL class.
constexpr double m
Definition: SystemOfUnits.h:94
bool isWrite() const
Definition: IFileMgr.h:57
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:38
Io::open_t openPOSIXFile(const std::string &n, const Io::IoFlags &f, const std::string &desc, Io::Fd &fd, void *&ptr)
T c_str(T...args)
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:102
bool isRead() const
Definition: IFileMgr.h:56
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:108
bool isRdWr() const
Definition: IFileMgr.h:58
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
int close_t
Definition: IFileMgr.h:250
virtual int outputLevel() const =0
Retrieve the current output level threshold.