| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
16 POSIXFileHandler::POSIXFileHandler( IMessageSvc* msg ):
17 m_log(msg,"POSIXFileHandler") {
18
19 m_level = msg->outputLevel("POSIXFileHandler");
20
21 }
22
23
24 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
25
26 Io::open_t
27 POSIXFileHandler::openPOSIXFile(const std::string& n, const Io::IoFlags& f,
28 const std::string& desc, Io::Fd& fd, void*& ptr) {
29
30 m_log.setLevel(m_level);
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
95 Io::close_t
96 POSIXFileHandler::closePOSIXFile(Io::Fd fd) {
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
124 Io::reopen_t
125 POSIXFileHandler::reopenPOSIXFile(Io::Fd /*fd*/, const Io::IoFlags&) {
126
127 m_log << MSG::ERROR << "reopen not implemented" << endmsg;
128
129 return -1;
130
131 }