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 <fcntl.h>
16#include <sys/stat.h>
17#include <sys/types.h>
18#include <unistd.h>
19
21#include "okssystem/File.hpp"
23
24#include "ers/ers.hpp"
25
26int
27OksSystem::Descriptor::flags(bool read_mode, bool write_mode)
28{
29 if (read_mode && write_mode) {
30 return O_RDWR | O_CREAT;
31 }
32 if (read_mode) {
33 return O_RDONLY;
34 }
35 if (write_mode) {
36 return O_WRONLY | O_CREAT;
37 }
38 return 0;
39} // flags
40
41OksSystem::Descriptor::Descriptor(const File* file, int i_flags, mode_t perm)
42{
43 ERS_ASSERT(file)
44 open(file, i_flags, perm);
45} // Descriptor
46
48{
49 if (m_fd >= 0) {
50 close_safe();
51 }
52} // ~Descriptor
53
54OksSystem::Descriptor::operator int() const throw()
55{
56 return m_fd;
57} // operator int()
58
64
65void
66OksSystem::Descriptor::open(const File* file, int i_flags, mode_t perm)
67{
68 ERS_ASSERT(file)
69 bool alreadyExists = file->exists();
70 m_fd = ::open(*file, i_flags, perm);
71 m_name = file->full_name();
72 if (m_fd < 0)
73 throw OksSystem::OpenFileIssue(ERS_HERE, errno, m_name.c_str());
74 if ((alreadyExists == false) && ((i_flags & O_CREAT) != 0)) {
75 ::fchmod(m_fd, perm);
76 }
77} // open
78
83
84void
86{
87 const int status = ::close(m_fd);
88 if (status < 0) {
89 throw OksSystem::CloseFileIssue(ERS_HERE, errno, m_name.c_str());
90 } //
91 m_fd = -1;
92} // close
93
97
98void
100{
101 const int status = ::close(m_fd);
102 if (status < 0) {
103 ers::warning(OksSystem::CloseFileIssue(ERS_HERE, errno, m_name.c_str()));
104 } // if
105} // close_safe
106
107int
108OksSystem::Descriptor::read(void* buffer, size_t number) const
109{
110 ssize_t status = ::read(m_fd, buffer, number);
111 if (status < 0)
112 throw OksSystem::ReadIssue(ERS_HERE, errno, m_name.c_str());
113 return status;
114} // read
115
116int
117OksSystem::Descriptor::write(const void* buffer, size_t number) const
118{
119 ssize_t status = ::write(m_fd, buffer, number);
120 if (status < 0)
121 throw OksSystem::WriteIssue(ERS_HERE, errno, m_name.c_str());
122 return status;
123} // write
124
125int
127{
128 return m_fd;
129}
130
134
135void
137{
138
139 bool success = false;
140 int storeErrno = 0;
141
142 int oldFlags = ::fcntl(m_fd, F_GETFD);
143 if (oldFlags != -1) {
144 int newFlags = ::fcntl(m_fd, F_SETFD, oldFlags | FD_CLOEXEC);
145 if (newFlags == -1) {
146 storeErrno = errno;
147 } else {
148 success = true;
149 }
150 } else {
151 storeErrno = errno;
152 }
153
154 if (!success) {
155 std::string eMsg = "File descriptor for file " + m_name +
156 " will not be closed after exec. Reason: " + std::string(::strerror(storeErrno));
157 throw OksSystem::OksSystemCallIssue(ERS_HERE, storeErrno, "fcntl", eMsg.c_str());
158 }
159}
#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:38
void warning(const Issue &issue)
Definition ers.hpp:150