File indexing completed on 2024-04-06 12:31:53
0001 #include "Utilities/StorageFactory/interface/File.h"
0002 #include "Utilities/StorageFactory/src/SysFile.h"
0003 #include "Utilities/StorageFactory/src/Throw.h"
0004 #include "FWCore/Utilities/interface/EDMException.h"
0005 #include <cassert>
0006 #include <vector>
0007
0008 using namespace edm::storage;
0009
0010 using namespace IOFlags;
0011
0012 IOFD File::sysduplicate(IOFD fd) {
0013 IOFD copyfd;
0014 if ((copyfd = ::dup(fd)) == EDM_IOFD_INVALID)
0015 throwStorageError("FileDuplicateError", "Calling File::sysduplicate()", "dup()", errno);
0016
0017 return copyfd;
0018 }
0019
0020 void File::sysopen(const char *name, int flags, int perms, IOFD &newfd, unsigned int & ) {
0021
0022 int openflags = 0;
0023
0024 if ((flags & OpenRead) && (flags & OpenWrite))
0025 openflags |= O_RDWR;
0026 else if (flags & OpenRead)
0027 openflags |= O_RDONLY;
0028 else if (flags & OpenWrite)
0029 openflags |= O_WRONLY;
0030
0031 if (flags & OpenNonBlock)
0032 openflags |= O_NONBLOCK;
0033
0034 if (flags & OpenAppend)
0035 openflags |= O_APPEND;
0036
0037 #ifdef O_SYNC
0038 if (flags & OpenUnbuffered)
0039 openflags |= O_SYNC;
0040 #else
0041 if (flags & OpenUnbuffered)
0042 newflags |= OpenUnbuffered;
0043 #endif
0044
0045 if (flags & OpenCreate)
0046 openflags |= O_CREAT;
0047
0048 if (flags & OpenExclusive)
0049 openflags |= O_EXCL;
0050
0051 if (flags & OpenTruncate)
0052 openflags |= O_TRUNC;
0053
0054 if (flags & OpenNotCTTY)
0055 openflags |= O_NOCTTY;
0056
0057 if ((newfd = ::open(name, openflags, perms)) == -1)
0058 throwStorageError(edm::errors::FileOpenError, "Calling File::sysopen()", "open()", errno);
0059 }
0060
0061 IOSize File::read(void *into, IOSize n) {
0062 ssize_t s;
0063 do
0064 s = ::read(fd(), into, n);
0065 while (s == -1 && errno == EINTR);
0066
0067 if (s == -1)
0068 throwStorageError(edm::errors::FileReadError, "Calling File::read()", "read()", errno);
0069
0070 return s;
0071 }
0072
0073 IOSize File::readv(IOBuffer *into, IOSize buffers) {
0074 assert(!buffers || into);
0075
0076
0077 if (!buffers)
0078 return 0;
0079
0080 ssize_t n = 0;
0081
0082
0083 std::vector<iovec> bufs(buffers);
0084 for (IOSize i = 0; i < buffers; ++i) {
0085 bufs[i].iov_len = into[i].size();
0086 bufs[i].iov_base = (caddr_t)into[i].data();
0087 }
0088
0089
0090 do
0091 n = ::readv(fd(), &bufs[0], buffers);
0092 while (n == -1 && errno == EINTR);
0093
0094
0095 if (n == -1)
0096 throwStorageError(edm::errors::FileReadError, "Calling File::readv", "readv()", errno);
0097
0098
0099 return n;
0100 }
0101
0102 IOSize File::read(void *into, IOSize n, IOOffset pos) {
0103 assert(pos >= 0);
0104
0105 ssize_t s;
0106 do
0107 s = ::pread(fd(), into, n, pos);
0108 while (s == -1 && errno == EINTR);
0109
0110 if (s == -1)
0111 throwStorageError(edm::errors::FileReadError, "Calling File::read()", "pread()", errno);
0112
0113 return s;
0114 }
0115
0116 IOSize File::syswrite(const void *from, IOSize n) {
0117 ssize_t s;
0118 do
0119 s = ::write(fd(), from, n);
0120 while (s == -1 && errno == EINTR);
0121
0122 if (s == -1 && errno != EWOULDBLOCK)
0123 throwStorageError(edm::errors::FileWriteError, "Calling File::syswrite()", "syswrite()", errno);
0124
0125 return s >= 0 ? s : 0;
0126 }
0127
0128 IOSize File::syswritev(const IOBuffer *from, IOSize buffers) {
0129 assert(!buffers || from);
0130
0131
0132 if (!buffers)
0133 return 0;
0134
0135 ssize_t n = 0;
0136
0137
0138 std::vector<iovec> bufs(buffers);
0139 for (IOSize i = 0; i < buffers; ++i) {
0140 bufs[i].iov_len = from[i].size();
0141 bufs[i].iov_base = (caddr_t)from[i].data();
0142 }
0143
0144
0145 do
0146 n = ::writev(fd(), &bufs[0], buffers);
0147 while (n == -1 && errno == EINTR);
0148
0149
0150 if (n == -1)
0151 throwStorageError(edm::errors::FileWriteError, "Calling Fike::syswritev()", "syswritev()", errno);
0152
0153
0154 return n;
0155 }
0156
0157 IOSize File::write(const void *from, IOSize n, IOOffset pos) {
0158 assert(pos >= 0);
0159
0160 ssize_t s;
0161 do
0162 s = ::pwrite(fd(), from, n, pos);
0163 while (s == -1 && errno == EINTR);
0164
0165 if (s == -1)
0166 throwStorageError(edm::errors::FileWriteError, "Calling File::write()", "pwrite()", errno);
0167
0168 if (m_flags & OpenUnbuffered)
0169
0170 flush();
0171
0172 return s;
0173 }
0174
0175 IOOffset File::size() const {
0176 IOFD fd = m_fd;
0177 assert(fd != EDM_IOFD_INVALID);
0178
0179 struct stat info;
0180 if (fstat(fd, &info) == -1)
0181 throwStorageError("FileSizeError", "Calling File::size()", "fstat()", errno);
0182
0183 return info.st_size;
0184 }
0185
0186 IOOffset File::position(IOOffset offset, Relative whence ) {
0187 IOFD fd = m_fd;
0188 assert(fd != EDM_IOFD_INVALID);
0189 assert(whence == CURRENT || whence == SET || whence == END);
0190
0191 IOOffset result;
0192 int mywhence = (whence == SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END);
0193 if ((result = ::lseek(fd, offset, mywhence)) == -1)
0194 throwStorageError("FilePositionError", "Calling File::position()", "lseek()", errno);
0195
0196 return result;
0197 }
0198
0199 void File::resize(IOOffset size) {
0200 IOFD fd = m_fd;
0201 assert(fd != EDM_IOFD_INVALID);
0202
0203 if (ftruncate(fd, size) == -1)
0204 throwStorageError("FileResizeError", "Calling File::resize()", "ftruncate()", errno);
0205 }
0206
0207 void File::flush() {
0208 IOFD fd = m_fd;
0209 assert(fd != EDM_IOFD_INVALID);
0210
0211 #if _POSIX_SYNCHRONIZED_IO > 0
0212 if (fdatasync(fd) == -1)
0213 throwStorageError("FileFlushError", "Calling File::flush()", "fdatasync()", errno);
0214 #elif _POSIX_FSYNC > 0
0215 if (fsync(fd) == -1)
0216 throwStorageError("FileFlushError", "Calling File::flush()", "fsync()", errno);
0217 #endif
0218 }
0219
0220 bool File::sysclose(IOFD fd, int *error ) {
0221 int ret = ::close(fd);
0222 if (error)
0223 *error = errno;
0224 return ret != -1;
0225 }