DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
OksSystem::Descriptor Class Reference

File descriptor / Socket wrapper. More...

#include <Descriptor.hpp>

Public Member Functions

 Descriptor (const File *file, int flags, mode_t perm)
 
 ~Descriptor ()
 
 operator int () const throw ()
 
void close ()
 close the descriptor
 
void close_safe () throw ()
 close the descriptor no exception
 
int read (void *buffer, size_t number) const
 
int write (const void *buffer, size_t number) const
 
int fd () const throw ()
 file descritptor
 
void closeOnExec ()
 It flags the file descriptor to be closed after any call to the exec okssystem function.
 

Static Public Member Functions

static int flags (bool read_mode, bool write_mode)
 

Protected Member Functions

void open (const File *file, int flags, mode_t perm)
 internal open method
 

Private Attributes

int m_fd
 
std::string m_name
 internal file descriptor
 

Detailed Description

File descriptor / Socket wrapper.

This class represents a low level file descriptor. The descriptor is opened when the object is created. It can be close explicitely, or implicetly when the object is destroyed

Author
Matthias Wiesmann
Version
1.0

Definition at line 29 of file Descriptor.hpp.

Constructor & Destructor Documentation

◆ Descriptor()

OksSystem::Descriptor::Descriptor ( const File * file,
int flags,
mode_t perm )

Definition at line 35 of file Descriptor.cpp.

35 {
36 ERS_ASSERT( file )
37 open(file,i_flags,perm);
38} // Descriptor
#define ERS_ASSERT(expression)
void open(const File *file, int flags, mode_t perm)
internal open method

◆ ~Descriptor()

OksSystem::Descriptor::~Descriptor ( )

Definition at line 40 of file Descriptor.cpp.

40 {
41 if (m_fd>=0) {
42 close_safe();
43 }
44} // ~Descriptor
void close_safe()
close the descriptor no exception

Member Function Documentation

◆ close()

void OksSystem::Descriptor::close ( )

close the descriptor

Closes the descriptor

Parameters
fileoptional pointer to the file that we close (used for pretty printing potential exceptions).
Exceptions
OksSystem::CloseFailif there is problem in the close okssystem call

Definition at line 73 of file Descriptor.cpp.

73 {
74 const int status = ::close(m_fd);
75 if (status<0) {
76 throw OksSystem::CloseFileIssue( ERS_HERE, errno, m_name.c_str() );
77 } //
78 m_fd = -1 ;
79} // close
#define ERS_HERE
void close()
close the descriptor
std::string m_name
internal file descriptor

◆ close_safe()

void OksSystem::Descriptor::close_safe ( )
throw ( )

close the descriptor no exception

Closes a descriptor safely, i.e without throwing exceptions If there is a proble, the information is sent to the warning stream

Definition at line 86 of file Descriptor.cpp.

86 {
87 const int status = ::close(m_fd);
88 if (status<0) {
89 ers::warning( OksSystem::CloseFileIssue( ERS_HERE, errno, m_name.c_str() ) );
90 } // if
91} // close_safe
void warning(const Issue &issue)
Definition ers.hpp:115

◆ closeOnExec()

void OksSystem::Descriptor::closeOnExec ( )

It flags the file descriptor to be closed after any call to the exec okssystem function.

Definition at line 114 of file Descriptor.cpp.

114 {
115
116 bool success = false;
117 int storeErrno = 0;
118
119 int oldFlags = ::fcntl(m_fd, F_GETFD);
120 if(oldFlags != -1) {
121 int newFlags = ::fcntl(m_fd, F_SETFD, oldFlags|FD_CLOEXEC);
122 if(newFlags == -1) {
123 storeErrno = errno;
124 } else {
125 success = true;
126 }
127 } else {
128 storeErrno = errno;
129 }
130
131 if(!success) {
132 std::string eMsg = "File descriptor for file " + m_name +
133 " will not be closed after exec. Reason: " + std::string(::strerror(storeErrno));
134 throw OksSystem::OksSystemCallIssue(ERS_HERE, storeErrno, "fcntl", eMsg.c_str());
135 }
136
137}

◆ fd()

int OksSystem::Descriptor::fd ( ) const
throw ( )

file descritptor

Definition at line 106 of file Descriptor.cpp.

106 {
107 return m_fd;
108}

◆ flags()

int OksSystem::Descriptor::flags ( bool read_mode,
bool write_mode )
static

Definition at line 21 of file Descriptor.cpp.

21 {
22 if (read_mode && write_mode) {
23 return O_RDWR | O_CREAT;
24 }
25 if (read_mode) {
26 return O_RDONLY;
27 }
28 if (write_mode) {
29 return O_WRONLY | O_CREAT;
30 }
31 return 0;
32} // flags

◆ open()

void OksSystem::Descriptor::open ( const File * file,
int i_flags,
mode_t perm )
protected

internal open method

Opens the descriptor

Parameters
filepointer to the file to open
flagsopen flags
permthe permissions

Definition at line 57 of file Descriptor.cpp.

57 {
58 ERS_ASSERT( file )
59 bool alreadyExists = file->exists();
60 m_fd = ::open(*file,i_flags,perm);
61 m_name = file->full_name();
62 if (m_fd<0) throw OksSystem::OpenFileIssue( ERS_HERE, errno, m_name.c_str() );
63 if((alreadyExists == false) && ((i_flags & O_CREAT) != 0)) {
64 ::fchmod(m_fd, perm);
65 }
66} // open

◆ operator int()

OksSystem::Descriptor::operator int ( ) const
throw ( )

Definition at line 46 of file Descriptor.cpp.

46 {
47 return m_fd;
48} // operator int()

◆ read()

int OksSystem::Descriptor::read ( void * buffer,
size_t number ) const

Definition at line 93 of file Descriptor.cpp.

93 {
94 ssize_t status = ::read(m_fd,buffer,number);
95 if (status<0) throw OksSystem::ReadIssue( ERS_HERE, errno, m_name.c_str() );
96 return status;
97} // read
int read(void *buffer, size_t number) const

◆ write()

int OksSystem::Descriptor::write ( const void * buffer,
size_t number ) const

Definition at line 100 of file Descriptor.cpp.

100 {
101 ssize_t status = ::write(m_fd,buffer,number);
102 if (status<0) throw OksSystem::WriteIssue( ERS_HERE, errno, m_name.c_str() );
103 return status;
104} // write
int write(const void *buffer, size_t number) const

Member Data Documentation

◆ m_fd

int OksSystem::Descriptor::m_fd
private

Definition at line 56 of file Descriptor.hpp.

◆ m_name

std::string OksSystem::Descriptor::m_name
private

internal file descriptor

Definition at line 57 of file Descriptor.hpp.


The documentation for this class was generated from the following files: