test
CMS 3D CMS Logo

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