CMS 3D CMS Logo

UnixIOChannel.cc
Go to the documentation of this file.
5 #include <algorithm>
6 #include <vector>
7 #include <cassert>
8 
9 IOSize IOChannel::read(void *into, IOSize n) {
10  ssize_t s;
11  do
12  s = ::read(fd(), into, n);
13  while (s == -1 && errno == EINTR);
14 
15  if (s == -1)
16  throwStorageError(edm::errors::FileReadError, "Calling IOChannel::read()", "read()", errno);
17 
18  return s;
19 }
20 
22  assert(!buffers || into);
23 
24  // readv may not support zero buffers.
25  if (!buffers)
26  return 0;
27 
28  ssize_t n = 0;
29 
30  // Convert the buffers to system format.
31  std::vector<iovec> bufs(buffers);
32  for (IOSize i = 0; i < buffers; ++i) {
33  bufs[i].iov_len = into[i].size();
34  bufs[i].iov_base = (caddr_t)into[i].data();
35  }
36 
37  // Read as long as signals cancel the read before doing anything.
38  do
39  n = ::readv(fd(), &bufs[0], buffers);
40  while (n == -1 && errno == EINTR);
41 
42  // If it was serious error, throw it.
43  if (n == -1)
44  throwStorageError(edm::errors::FileReadError, "Calling IOChannel::readv", "readv()", errno);
45 
46  // Return the number of bytes actually read.
47  return n;
48 }
49 
53 IOSize IOChannel::write(const void *from, IOSize n) {
54  ssize_t s;
55  do
56  s = ::write(fd(), from, n);
57  while (s == -1 && errno == EINTR);
58 
59  if (s == -1 && errno != EWOULDBLOCK)
60  throwStorageError(edm::errors::FileWriteError, "Calling IOChannel::write()", "write()", errno);
61 
62  return s >= 0 ? s : 0;
63 }
64 
65 IOSize IOChannel::writev(const IOBuffer *from, IOSize buffers) {
66  assert(!buffers || from);
67 
68  // writev may not support zero buffers.
69  if (!buffers)
70  return 0;
71 
72  ssize_t n = 0;
73 
74  // Convert the buffers to system format.
75  std::vector<iovec> bufs(buffers);
76  for (IOSize i = 0; i < buffers; ++i) {
77  bufs[i].iov_len = from[i].size();
78  bufs[i].iov_base = (caddr_t)from[i].data();
79  }
80 
81  // Read as long as signals cancel the read before doing anything.
82  do
83  n = ::writev(fd(), &bufs[0], buffers);
84  while (n == -1 && errno == EINTR);
85 
86  // If it was serious error, throw it.
87  if (n == -1)
88  throwStorageError(edm::errors::FileWriteError, "Calling IOChannel::writev()", "writev()", errno);
89 
90  // Return the number of bytes actually written.
91  return n;
92 }
93 
98 #ifdef O_NONBLOCK
99  int mode;
100  int off = value ? ~0 : ~(O_NDELAY | O_NONBLOCK);
101  int on = value ? O_NONBLOCK : 0;
102 
103  if ((mode = fcntl(fd(), F_GETFL, 0)) == -1 || fcntl(fd(), F_SETFL, (mode & off) | on) == -1)
104  throwStorageError("FileSetBlockingError", "Calling IOChannel::setBlocking()", "fcntl()", errno);
105 #elif defined FIONBIO
106  int mode = value;
107  if (ioctl(fd(), FIONBIO, &value) == -1)
108  throwStorageError("FileSetBlockingError", "Calling IOChannel::setBlocking()", "ioctl()", errno);
109 #endif
110 }
111 
112 bool IOChannel::isBlocking(void) const {
113 #ifdef O_NONBLOCK
114  int mode;
115  if ((mode = fcntl(fd(), F_GETFL, 0)) == -1)
116  throwStorageError("FileIsBlockingError", "Calling IOChannel::isBlocking()", "fcntl()", errno);
117 
118  return mode & (O_NDELAY | O_NONBLOCK);
119 #else // ! O_NONBLOCK
120  return true;
121 #endif // O_NONBLOCK
122 }
123 
127 bool IOChannel::sysclose(IOFD fd, int *error /* = 0 */) {
128  int ret = ::close(fd);
129  if (error)
130  *error = errno;
131  return ret != -1;
132 }
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:543
IOChannel::readv
IOSize readv(IOBuffer *into, IOSize buffers) override
Definition: UnixIOChannel.cc:21
IOBuffer::size
IOSize size(void) const
Definition: IOBuffer.h:34
mps_fire.i
i
Definition: mps_fire.py:428
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
IOChannel::close
virtual void close(void)
Definition: IOChannel.cc:84
ALCARECOPromptCalibProdSiPixelAli0T_cff.mode
mode
Definition: ALCARECOPromptCalibProdSiPixelAli0T_cff.py:96
edm::errors::FileWriteError
Definition: EDMException.h:66
cms::cuda::assert
assert(be >=bs)
IOChannel::isBlocking
virtual bool isBlocking(void) const
Definition: UnixIOChannel.cc:112
relativeConstraints.error
error
Definition: relativeConstraints.py:53
EDMException.h
ztee.fd
fd
Definition: ztee.py:136
alignCSCRings.s
s
Definition: alignCSCRings.py:92
IOChannel::writev
IOSize writev(const IOBuffer *from, IOSize buffers) override
Definition: UnixIOChannel.cc:65
IOChannel::setBlocking
virtual void setBlocking(bool value)
Definition: UnixIOChannel.cc:97
SysIOChannel.h
IOBuffer
Definition: IOBuffer.h:7
value
Definition: value.py:1
throwStorageError
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
IOFD
int IOFD
Definition: IOTypes.h:22
IOChannel::sysclose
bool sysclose(IOFD fd, int *error=nullptr)
Definition: UnixIOChannel.cc:127
relativeConstraints.value
value
Definition: relativeConstraints.py:53
IOChannel::write
IOSize write(const void *from, IOSize n) override
Definition: UnixIOChannel.cc:53
IOInput::read
int read(void)
Definition: IOInput.cc:52
O_NONBLOCK
#define O_NONBLOCK
Definition: SysFile.h:21
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
IOChannel::fd
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
Throw.h
IOSize
size_t IOSize
Definition: IOTypes.h:14
edm::errors::FileReadError
Definition: EDMException.h:50
IOChannel.h