Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
POSIXFileHandler.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "GaudiKernel/IFileMgr.h"
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 
20 #include "GaudiKernel/MsgStream.h"
21 #include "POSIXFileHandler.h"
22 
23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24 
25 POSIXFileHandler::POSIXFileHandler( IMessageSvc* msg ) : m_log( msg, "POSIXFileHandler" ) {
26 
27  m_level = msg->outputLevel( "POSIXFileHandler" );
28 }
29 
30 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
31 
33  Io::Fd& fd, void*& ptr ) {
34 
36 
37  if ( m_log.level() <= MSG::DEBUG )
38  m_log << MSG::DEBUG << "openPOSIXFile(\"" << n << "\"," << f << "," << desc << ")" << endmsg;
39 
40  ptr = nullptr;
41  fd = -1;
42 
43  int mm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
44 
45  fd = open( n.c_str(), (int)f, mm );
46  int ierr = errno;
47 
48  if ( fd == -1 ) {
49  m_log << MSG::ERROR << "Error opening POSIX file \"" << n << "\": " << strerror( ierr ) << endmsg;
50  return 1;
51  }
52 
53  std::string m;
54  if ( f.isRead() ) {
55  m = "r";
56  } else if ( f.isWrite() ) {
57  if ( ( f & Io::APPEND ) ) {
58  m = "a";
59  } else {
60  m = "w";
61  }
62  } else if ( f.isRdWr() ) {
63  m = "r+";
64  } else {
65  m_log << MSG::ERROR << "unknown mode " << f << " when calling fdopen on " << n << endmsg;
66  return 1;
67  }
68 
69  if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "calling fdopen with mode " << m << endmsg;
70 
71  ptr = fdopen( fd, m.c_str() );
72  ierr = errno;
73 
74  if ( !ptr ) {
75  m_log << MSG::ERROR << "Error calling fdopen on \"" << n << "\": " << strerror( ierr ) << endmsg;
76  return 1;
77  }
78 
79  m_log << MSG::DEBUG << "opened POSIX file, Fd: " << fd << " FILE*: " << ptr << endmsg;
80 
81  return 0;
82 }
83 
84 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
85 
87 
88  if ( m_log.level() <= MSG::DEBUG ) m_log << MSG::DEBUG << "closePOSIXFile(fd:" << fd << ")" << endmsg;
89 
90  if ( fd == -1 ) {
91  m_log << MSG::ERROR << "Unable to close file: FD == -1 " << endmsg;
92  return -1;
93  }
94 
95  if ( close( fd ) != 0 ) {
96  int ierr = errno;
97  m_log << MSG::ERROR << "Error closing POSIX file with FD " << fd << strerror( ierr ) << endmsg;
98  return -1;
99  }
100 
101  return 0;
102 }
103 
104 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
105 
107 
108  m_log << MSG::ERROR << "reopen not implemented" << endmsg;
109  return -1;
110 }
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
std::string
STL class.
IMessageSvc
Definition: IMessageSvc.h:47
Io::open_t
int open_t
Definition: IFileMgr.h:243
gaudirun.fd
fd
Definition: gaudirun.py:629
POSIXFileHandler::reopenPOSIXFile
Io::reopen_t reopenPOSIXFile(Io::Fd fd, const Io::IoFlags &)
Definition: POSIXFileHandler.cpp:106
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
POSIXFileHandler::m_level
int m_level
Definition: POSIXFileHandler.h:40
POSIXFileHandler.h
IFileMgr.h
POSIXFileHandler::POSIXFileHandler
POSIXFileHandler(IMessageSvc *)
Definition: POSIXFileHandler.cpp:25
Gaudi::Units::mm
constexpr double mm
Definition: SystemOfUnits.h:100
Io::reopen_t
int reopen_t
Definition: IFileMgr.h:245
POSIXFileHandler::closePOSIXFile
Io::close_t closePOSIXFile(Io::Fd fd)
Definition: POSIXFileHandler.cpp:86
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:108
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
Io::Fd
int Fd
Definition: IFileMgr.h:169
Io::close_t
int close_t
Definition: IFileMgr.h:244
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:235
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
POSIXFileHandler::m_log
MsgStream m_log
Definition: POSIXFileHandler.h:39
POSIXFileHandler::openPOSIXFile
Io::open_t openPOSIXFile(const std::string &n, const Io::IoFlags &f, const std::string &desc, Io::Fd &fd, void *&ptr)
Definition: POSIXFileHandler.cpp:32
MsgStream::setLevel
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:108
Io::IoFlags
Definition: IFileMgr.h:50
MsgStream::level
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:113
Io::APPEND
@ APPEND
Definition: IFileMgr.h:45
MsgStream.h