The Gaudi Framework  master (2df85225)
Loading...
Searching...
No Matches
POSIXFileHandler.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 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\***********************************************************************************/
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 "POSIXFileHandler.h"
22
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25POSIXFileHandler::POSIXFileHandler( IMessageSvc* msg ) : m_log( msg, "POSIXFileHandler" ) {
26
27 m_level = msg->outputLevel( "POSIXFileHandler" );
28}
29
30/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
31
32Io::open_t POSIXFileHandler::openPOSIXFile( const std::string& n, const Io::IoFlags& f, const std::string& desc,
33 Io::Fd& fd, void*& ptr ) {
34
35 m_log.setLevel( m_level );
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}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
The IMessage is the interface implemented by the message service.
Definition IMessageSvc.h:34
bool isRead() const
Definition IFileMgr.h:58
bool isWrite() const
Definition IFileMgr.h:59
bool isRdWr() const
Definition IFileMgr.h:60
Io::close_t closePOSIXFile(Io::Fd fd)
POSIXFileHandler(IMessageSvc *)
Io::open_t openPOSIXFile(const std::string &n, const Io::IoFlags &f, const std::string &desc, Io::Fd &fd, void *&ptr)
Io::reopen_t reopenPOSIXFile(Io::Fd fd, const Io::IoFlags &)
int reopen_t
Definition IFileMgr.h:232
@ APPEND
Definition IFileMgr.h:38
int Fd
Definition IFileMgr.h:158
int close_t
Definition IFileMgr.h:231
int open_t
Definition IFileMgr.h:230
@ DEBUG
Definition IMessageSvc.h:22
@ ERROR
Definition IMessageSvc.h:22