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 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
ret
prodAgent to be discontinued
virtual void close(void)
Definition: IOChannel.cc:84
IOSize writev(const IOBuffer *from, IOSize buffers) override
IOSize readv(IOBuffer *into, IOSize buffers) override
IOSize write(const void *from, IOSize n) override
virtual void setBlocking(bool value)
Definition: value.py:1
int read(void)
Definition: IOInput.cc:52
IOSize size(void) const
Definition: IOBuffer.h:34
int IOFD
Definition: IOTypes.h:22
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
virtual bool isBlocking(void) const
bool sysclose(IOFD fd, int *error=nullptr)
#define O_NONBLOCK
Definition: SysFile.h:21
size_t IOSize
Definition: IOTypes.h:14
virtual IOFD fd(void) const
Definition: IOChannel.cc:69