All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
27 POSIXFileHandler::openPOSIXFile(const std::string& n, const Io::IoFlags& f,
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 = 0;
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) != 0) {
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  FILE* fp = fdopen(fd, m.c_str());
75  ierr = errno;
76  ptr = (void*) fp;
77 
78  if (ptr == 0) {
79  m_log << MSG::ERROR << "Error calling fdopen on \"" << n << "\": "
80  << strerror(ierr)
81  << endmsg;
82  return 1;
83  }
84 
85  m_log << MSG::DEBUG << "opened POSIX file, Fd: " << fd
86  << " FILE*: " << ptr
87  << endmsg;
88 
89  return 0;
90 
91 }
92 
93 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
94 
97 
98  if (m_log.level() <= MSG::DEBUG)
99  m_log << MSG::DEBUG << "closePOSIXFile(fd:" << fd << ")"
100  << endmsg;
101 
102 
103  if (fd == -1) {
104  m_log << MSG::ERROR << "Unable to close file: FD == -1 "
105  << endmsg;
106  return -1;
107  }
108 
109  if (close(fd) != 0) {
110  int ierr = errno;
111  m_log << MSG::ERROR << "Error closing POSIX file with FD " << fd
112  << strerror(ierr)
113  << endmsg;
114  return -1;
115  }
116 
117  return 0;
118 
119 }
120 
121 
122 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
123 
126 
127  m_log << MSG::ERROR << "reopen not implemented" << endmsg;
128 
129  return -1;
130 
131 }
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:280
int Fd
Definition: IFileMgr.h:197
POSIXFileHandler(IMessageSvc *)
int open_t
Definition: IFileMgr.h:278
virtual int outputLevel() const =0
Retrieve the current output level threshold.
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)
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:279