DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Descriptor.cpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3// Fork baseline commit: system-00-00-20 (2020-09-25).
4// Renamed since fork: yes (from src/Descriptor.cxx to src/Descriptor.cpp).
5
6/*
7 * Descriptor.cxx
8 * OksSystem
9 *
10 * Created by Matthias Wiesmann on 08.02.05.
11 * Copyright 2005 CERN. All rights reserved.
12 *
13 */
14
15#include <sys/types.h>
16#include <sys/stat.h>
17#include <fcntl.h>
18#include <unistd.h>
19
20#include "okssystem/File.hpp"
23
24#include "ers/ers.hpp"
25
26int OksSystem::Descriptor::flags(bool read_mode, bool write_mode) {
27 if (read_mode && write_mode) {
28 return O_RDWR | O_CREAT;
29 }
30 if (read_mode) {
31 return O_RDONLY;
32 }
33 if (write_mode) {
34 return O_WRONLY | O_CREAT;
35 }
36 return 0;
37} // flags
38
39
40OksSystem::Descriptor::Descriptor(const File * file, int i_flags, mode_t perm) {
41 ERS_ASSERT( file )
42 open(file,i_flags,perm);
43} // Descriptor
44
46 if (m_fd>=0) {
47 close_safe();
48 }
49} // ~Descriptor
50
51OksSystem::Descriptor::operator int() const throw() {
52 return m_fd;
53} // operator int()
54
55
61
62void OksSystem::Descriptor::open(const File * file, int i_flags, mode_t perm) {
63 ERS_ASSERT( file )
64 bool alreadyExists = file->exists();
65 m_fd = ::open(*file,i_flags,perm);
66 m_name = file->full_name();
67 if (m_fd<0) throw OksSystem::OpenFileIssue( ERS_HERE, errno, m_name.c_str() );
68 if((alreadyExists == false) && ((i_flags & O_CREAT) != 0)) {
69 ::fchmod(m_fd, perm);
70 }
71} // open
72
77
79 const int status = ::close(m_fd);
80 if (status<0) {
81 throw OksSystem::CloseFileIssue( ERS_HERE, errno, m_name.c_str() );
82 } //
83 m_fd = -1 ;
84} // close
85
86
90
92 const int status = ::close(m_fd);
93 if (status<0) {
94 ers::warning( OksSystem::CloseFileIssue( ERS_HERE, errno, m_name.c_str() ) );
95 } // if
96} // close_safe
97
98int OksSystem::Descriptor::read(void* buffer, size_t number) const {
99 ssize_t status = ::read(m_fd,buffer,number);
100 if (status<0) throw OksSystem::ReadIssue( ERS_HERE, errno, m_name.c_str() );
101 return status;
102} // read
103
104
105int OksSystem::Descriptor::write(const void* buffer, size_t number) const {
106 ssize_t status = ::write(m_fd,buffer,number);
107 if (status<0) throw OksSystem::WriteIssue( ERS_HERE, errno, m_name.c_str() );
108 return status;
109} // write
110
111int OksSystem::Descriptor::fd() const throw() {
112 return m_fd;
113}
114
118
120
121 bool success = false;
122 int storeErrno = 0;
123
124 int oldFlags = ::fcntl(m_fd, F_GETFD);
125 if(oldFlags != -1) {
126 int newFlags = ::fcntl(m_fd, F_SETFD, oldFlags|FD_CLOEXEC);
127 if(newFlags == -1) {
128 storeErrno = errno;
129 } else {
130 success = true;
131 }
132 } else {
133 storeErrno = errno;
134 }
135
136 if(!success) {
137 std::string eMsg = "File descriptor for file " + m_name +
138 " will not be closed after exec. Reason: " + std::string(::strerror(storeErrno));
139 throw OksSystem::OksSystemCallIssue(ERS_HERE, storeErrno, "fcntl", eMsg.c_str());
140 }
141
142}
#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
std::string m_name
internal file descriptor
Wrapper for file operations.
Definition File.hpp:37
void warning(const Issue &issue)
Definition ers.hpp:126