The Gaudi Framework  v32r2 (46d42edc)
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  m_level = msg->outputLevel( "POSIXFileHandler" );
18 }
19 
20 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
21 
23  Io::Fd& fd, void*& ptr ) {
24 
26 
27  if ( m_log.level() <= MSG::DEBUG )
28  m_log << MSG::DEBUG << "openPOSIXFile(\"" << n << "\"," << f << "," << desc << ")" << endmsg;
29 
30  ptr = nullptr;
31  fd = -1;
32 
33  int mm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
34 
35  fd = open( n.c_str(), (int)f, mm );
36  int ierr = errno;
37 
38  if ( fd == -1 ) {
39  m_log << MSG::ERROR << "Error opening POSIX file \"" << n << "\": " << strerror( ierr ) << endmsg;
40  return 1;
41  }
42 
43  std::string m;
44  if ( f.isRead() ) {
45  m = "r";
46  } else if ( f.isWrite() ) {
47  if ( ( f & Io::APPEND ) ) {
48  m = "a";
49  } else {
50  m = "w";
51  }
52  } else if ( f.isRdWr() ) {
53  m = "r+";
54  } else {
55  m_log << MSG::ERROR << "unknown mode " << f << " when calling fdopen on " << n << endmsg;
56  return 1;
57  }
58 
59  if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "calling fdopen with mode " << m << endmsg;
60 
61  ptr = fdopen( fd, m.c_str() );
62  ierr = errno;
63 
64  if ( !ptr ) {
65  m_log << MSG::ERROR << "Error calling fdopen on \"" << n << "\": " << strerror( ierr ) << endmsg;
66  return 1;
67  }
68 
69  m_log << MSG::DEBUG << "opened POSIX file, Fd: " << fd << " FILE*: " << ptr << endmsg;
70 
71  return 0;
72 }
73 
74 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
75 
77 
78  if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "closePOSIXFile(fd:" << fd << ")" << endmsg;
79 
80  if ( fd == -1 ) {
81  m_log << MSG::ERROR << "Unable to close file: FD == -1 " << endmsg;
82  return -1;
83  }
84 
85  if ( close( fd ) != 0 ) {
86  int ierr = errno;
87  m_log << MSG::ERROR << "Error closing POSIX file with FD " << fd << strerror( ierr ) << endmsg;
88  return -1;
89  }
90 
91  return 0;
92 }
93 
94 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
95 
97 
98  m_log << MSG::ERROR << "reopen not implemented" << endmsg;
99  return -1;
100 }
Io::close_t closePOSIXFile(Io::Fd fd)
Io::reopen_t reopenPOSIXFile(Io::Fd fd, const Io::IoFlags &)
int reopen_t
Definition: IFileMgr.h:235
int Fd
Definition: IFileMgr.h:159
POSIXFileHandler(IMessageSvc *)
int open_t
Definition: IFileMgr.h:233
constexpr double mm
Definition: SystemOfUnits.h:84
bool isWrite() const
Definition: IFileMgr.h:56
STL class.
bool isRdWr() const
Definition: IFileMgr.h:57
constexpr double m
Definition: SystemOfUnits.h:92
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:37
Io::open_t openPOSIXFile(const std::string &n, const Io::IoFlags &f, const std::string &desc, Io::Fd &fd, void *&ptr)
bool isRead() const
Definition: IFileMgr.h:55
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:98
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:103
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
int close_t
Definition: IFileMgr.h:234