CMS 3D CMS Logo

UnixFile.cc
Go to the documentation of this file.
5 #include <cassert>
6 #include <vector>
7 
8 using namespace edm::storage;
9 
10 using namespace IOFlags;
11 
13  IOFD copyfd;
14  if ((copyfd = ::dup(fd)) == EDM_IOFD_INVALID)
15  throwStorageError("FileDuplicateError", "Calling File::sysduplicate()", "dup()", errno);
16 
17  return copyfd;
18 }
19 
20 void File::sysopen(const char *name, int flags, int perms, IOFD &newfd, unsigned int & /*newflags*/) {
21  // Translate our flags to system flags.
22  int openflags = 0;
23 
24  if ((flags & OpenRead) && (flags & OpenWrite))
25  openflags |= O_RDWR;
26  else if (flags & OpenRead)
27  openflags |= O_RDONLY;
28  else if (flags & OpenWrite)
29  openflags |= O_WRONLY;
30 
31  if (flags & OpenNonBlock)
32  openflags |= O_NONBLOCK;
33 
34  if (flags & OpenAppend)
35  openflags |= O_APPEND;
36 
37 #ifdef O_SYNC
38  if (flags & OpenUnbuffered)
39  openflags |= O_SYNC;
40 #else
41  if (flags & OpenUnbuffered)
42  newflags |= OpenUnbuffered;
43 #endif
44 
45  if (flags & OpenCreate)
46  openflags |= O_CREAT;
47 
48  if (flags & OpenExclusive)
49  openflags |= O_EXCL;
50 
51  if (flags & OpenTruncate)
52  openflags |= O_TRUNC;
53 
54  if (flags & OpenNotCTTY)
55  openflags |= O_NOCTTY;
56 
57  if ((newfd = ::open(name, openflags, perms)) == -1)
58  throwStorageError(edm::errors::FileOpenError, "Calling File::sysopen()", "open()", errno);
59 }
60 
61 IOSize File::read(void *into, IOSize n) {
62  ssize_t s;
63  do
64  s = ::read(fd(), into, n);
65  while (s == -1 && errno == EINTR);
66 
67  if (s == -1)
68  throwStorageError(edm::errors::FileReadError, "Calling File::read()", "read()", errno);
69 
70  return s;
71 }
72 
73 IOSize File::readv(IOBuffer *into, IOSize buffers) {
74  assert(!buffers || into);
75 
76  // readv 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  bufs[i].iov_len = into[i].size();
86  bufs[i].iov_base = (caddr_t)into[i].data();
87  }
88 
89  // Read as long as signals cancel the read before doing anything.
90  do
91  n = ::readv(fd(), &bufs[0], buffers);
92  while (n == -1 && errno == EINTR);
93 
94  // If it was serious error, throw it.
95  if (n == -1)
96  throwStorageError(edm::errors::FileReadError, "Calling File::readv", "readv()", errno);
97 
98  // Return the number of bytes actually read.
99  return n;
100 }
101 
103  assert(pos >= 0);
104 
105  ssize_t s;
106  do
107  s = ::pread(fd(), into, n, pos);
108  while (s == -1 && errno == EINTR);
109 
110  if (s == -1)
111  throwStorageError(edm::errors::FileReadError, "Calling File::read()", "pread()", errno);
112 
113  return s;
114 }
115 
116 IOSize File::syswrite(const void *from, IOSize n) {
117  ssize_t s;
118  do
119  s = ::write(fd(), from, n);
120  while (s == -1 && errno == EINTR);
121 
122  if (s == -1 && errno != EWOULDBLOCK)
123  throwStorageError(edm::errors::FileWriteError, "Calling File::syswrite()", "syswrite()", errno);
124 
125  return s >= 0 ? s : 0;
126 }
127 
128 IOSize File::syswritev(const IOBuffer *from, IOSize buffers) {
129  assert(!buffers || from);
130 
131  // writev may not support zero buffers.
132  if (!buffers)
133  return 0;
134 
135  ssize_t n = 0;
136 
137  // Convert the buffers to system format.
138  std::vector<iovec> bufs(buffers);
139  for (IOSize i = 0; i < buffers; ++i) {
140  bufs[i].iov_len = from[i].size();
141  bufs[i].iov_base = (caddr_t)from[i].data();
142  }
143 
144  // Read as long as signals cancel the read before doing anything.
145  do
146  n = ::writev(fd(), &bufs[0], buffers);
147  while (n == -1 && errno == EINTR);
148 
149  // If it was serious error, throw it.
150  if (n == -1)
151  throwStorageError(edm::errors::FileWriteError, "Calling Fike::syswritev()", "syswritev()", errno);
152 
153  // Return the number of bytes actually written.
154  return n;
155 }
156 
157 IOSize File::write(const void *from, IOSize n, IOOffset pos) {
158  assert(pos >= 0);
159 
160  ssize_t s;
161  do
162  s = ::pwrite(fd(), from, n, pos);
163  while (s == -1 && errno == EINTR);
164 
165  if (s == -1)
166  throwStorageError(edm::errors::FileWriteError, "Calling File::write()", "pwrite()", errno);
167 
168  if (m_flags & OpenUnbuffered)
169  // FIXME: Exception handling?
170  flush();
171 
172  return s;
173 }
174 
176  IOFD fd = m_fd;
178 
179  struct stat info;
180  if (fstat(fd, &info) == -1)
181  throwStorageError("FileSizeError", "Calling File::size()", "fstat()", errno);
182 
183  return info.st_size;
184 }
185 
187  IOFD fd = m_fd;
189  assert(whence == CURRENT || whence == SET || whence == END);
190 
192  int mywhence = (whence == SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END);
193  if ((result = ::lseek(fd, offset, mywhence)) == -1)
194  throwStorageError("FilePositionError", "Calling File::position()", "lseek()", errno);
195 
196  return result;
197 }
198 
200  IOFD fd = m_fd;
202 
203  if (ftruncate(fd, size) == -1)
204  throwStorageError("FileResizeError", "Calling File::resize()", "ftruncate()", errno);
205 }
206 
207 void File::flush() {
208  IOFD fd = m_fd;
210 
211 #if _POSIX_SYNCHRONIZED_IO > 0
212  if (fdatasync(fd) == -1)
213  throwStorageError("FileFlushError", "Calling File::flush()", "fdatasync()", errno);
214 #elif _POSIX_FSYNC > 0
215  if (fsync(fd) == -1)
216  throwStorageError("FileFlushError", "Calling File::flush()", "fsync()", errno);
217 #endif
218 }
219 
220 bool File::sysclose(IOFD fd, int *error /* = 0 */) {
221  int ret = ::close(fd);
222  if (error)
223  *error = errno;
224  return ret != -1;
225 }
size
Write out results.
int64_t IOOffset
Definition: IOTypes.h:20
static const TGPicture * info(bool iBackgroundIsBlack)
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
void flush() override
Definition: UnixFile.cc:207
ret
prodAgent to be discontinued
assert(be >=bs)
IOSize syswrite(const void *from, IOSize n)
Definition: UnixFile.cc:116
IOSize syswritev(const IOBuffer *from, IOSize length)
Definition: UnixFile.cc:128
IOSize size() const
Definition: IOBuffer.h:36
static void sysopen(const char *name, int flags, int perms, IOFD &newfd, unsigned &newflags)
Definition: UnixFile.cc:20
static IOFD sysduplicate(IOFD fd)
Definition: UnixFile.cc:12
IOSize write(const void *from, IOSize n) override
Definition: File.cc:195
size_t IOSize
Definition: IOTypes.h:15
IOSize readv(IOBuffer *into, IOSize length) override
Definition: UnixFile.cc:73
IOOffset size() const override
Definition: UnixFile.cc:175
void resize(IOOffset size) override
Definition: UnixFile.cc:199
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
constexpr int EDM_IOFD_INVALID
Definition: IOTypes.h:9
virtual IOOffset position() const
Definition: Storage.cc:504
int IOFD
Definition: IOTypes.h:23
#define O_NONBLOCK
Definition: SysFile.h:23
static bool sysclose(IOFD fd, int *error=nullptr)
Definition: UnixFile.cc:220
fd
Definition: ztee.py:136