DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Descriptor.cpp
Go to the documentation of this file.
1/*
2 * Descriptor.cxx
3 * OksSystem
4 *
5 * Created by Matthias Wiesmann on 08.02.05.
6 * Copyright 2005 CERN. All rights reserved.
7 *
8 */
9
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <unistd.h>
14
15#include "okssystem/File.hpp"
18
19#include "ers/ers.hpp"
20
21int OksSystem::Descriptor::flags(bool read_mode, bool write_mode) {
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
33
34
35OksSystem::Descriptor::Descriptor(const File * file, int i_flags, mode_t perm) {
36 ERS_ASSERT( file )
37 open(file,i_flags,perm);
38} // Descriptor
39
41 if (m_fd>=0) {
42 close_safe();
43 }
44} // ~Descriptor
45
46OksSystem::Descriptor::operator int() const throw() {
47 return m_fd;
48} // operator int()
49
50
57void OksSystem::Descriptor::open(const File * file, int i_flags, mode_t perm) {
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
67
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
80
81
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
92
93int OksSystem::Descriptor::read(void* buffer, size_t number) const {
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
98
99
100int OksSystem::Descriptor::write(const void* buffer, size_t number) const {
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
105
106int OksSystem::Descriptor::fd() const throw() {
107 return m_fd;
108}
109
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}
#define ERS_ASSERT(expression)
#define ERS_HERE
Descriptor(const File *file, int flags, mode_t perm)
int read(void *buffer, size_t number) const
int fd() const
file descritptor
static int flags(bool read_mode, bool write_mode)
void open(const File *file, int flags, mode_t perm)
internal open method
void closeOnExec()
It flags the file descriptor to be closed after any call to the exec okssystem function.
void close()
close the descriptor
void close_safe()
close the descriptor no exception
int write(const void *buffer, size_t number) const
Wrapper for file operations.
Definition File.hpp:32
void warning(const Issue &issue)
Definition ers.hpp:115