All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
POSIXFileHandler.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/IFileMgr.h"
2 #include <errno.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 
10 #include "POSIXFileHandler.h"
11 #include "GaudiKernel/MsgStream.h"
12 
13 
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
17  m_log(msg,"POSIXFileHandler") {
18 
19  m_level = msg->outputLevel("POSIXFileHandler");
20 
21 }
22 
23 
24 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
25 
28  const std::string& desc, Io::Fd& fd, void*& ptr) {
29 
31 
32  if (m_log.level() <= MSG::DEBUG)
33  m_log << MSG::DEBUG << "openPOSIXFile(\"" << n << "\","
34  << f << "," << desc << ")"
35  << endmsg;
36 
37  ptr = nullptr;
38  fd = -1;
39 
40  int mm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
41 
42  fd = open(n.c_str(), (int) f, mm);
43  int ierr = errno;
44 
45  if (fd == -1) {
46  m_log << MSG::ERROR << "Error opening POSIX file \"" << n << "\": "
47  << strerror(ierr)
48  << endmsg;
49  return 1;
50  }
51 
52  std::string m;
53  if (f.isRead()) {
54  m = "r";
55  } else if ( f.isWrite() ) {
56  if ( (f & Io::APPEND) ) {
57  m = "a";
58  } else {
59  m = "w";
60  }
61  } else if ( f.isRdWr() ) {
62  m = "r+";
63  } else {
64  m_log << MSG::ERROR << "unknown mode " << f
65  << " when calling fdopen on " << n
66  << endmsg;
67  return 1;
68  }
69 
70 
71  if (m_log.level() <= MSG::DEBUG)
72  m_log << MSG::DEBUG << "calling fdopen with mode " << m << endmsg;
73 
74  ptr = fdopen(fd, m.c_str());
75  ierr = errno;
76 
77  if (!ptr) {
78  m_log << MSG::ERROR << "Error calling fdopen on \"" << n << "\": "
79  << strerror(ierr)
80  << endmsg;
81  return 1;
82  }
83 
84  m_log << MSG::DEBUG << "opened POSIX file, Fd: " << fd
85  << " FILE*: " << ptr
86  << endmsg;
87 
88  return 0;
89 
90 }
91 
92 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
93 
96 
97  if (m_log.level() <= MSG::DEBUG)
98  m_log << MSG::DEBUG << "closePOSIXFile(fd:" << fd << ")"
99  << endmsg;
100 
101 
102  if (fd == -1) {
103  m_log << MSG::ERROR << "Unable to close file: FD == -1 "
104  << endmsg;
105  return -1;
106  }
107 
108  if (close(fd) != 0) {
109  int ierr = errno;
110  m_log << MSG::ERROR << "Error closing POSIX file with FD " << fd
111  << strerror(ierr)
112  << endmsg;
113  return -1;
114  }
115 
116  return 0;
117 
118 }
119 
120 
121 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
122 
125 
126  m_log << MSG::ERROR << "reopen not implemented" << endmsg;
127  return -1;
128 
129 }
Io::close_t closePOSIXFile(Io::Fd fd)
Io::reopen_t reopenPOSIXFile(Io::Fd fd, const Io::IoFlags &)
MSG::Level level()
Retrieve output level.
Definition: MsgStream.h:112
int reopen_t
Definition: IFileMgr.h:254
int Fd
Definition: IFileMgr.h:172
POSIXFileHandler(IMessageSvc *)
int open_t
Definition: IFileMgr.h:252
constexpr double mm
Definition: SystemOfUnits.h:85
STL class.
constexpr double m
Definition: SystemOfUnits.h:93
bool isWrite() const
Definition: IFileMgr.h:55
The IMessage is the interface implemented by the message service.
Definition: IMessageSvc.h:57
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:106
bool isRead() const
Definition: IFileMgr.h:54
bool isRdWr() const
Definition: IFileMgr.h:56
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
int close_t
Definition: IFileMgr.h:253
virtual int outputLevel() const =0
Retrieve the current output level threshold.