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