CMS 3D CMS Logo

UnixFile.cc
Go to the documentation of this file.
5 #include <cassert>
6 
7 using namespace IOFlags;
8 
10  IOFD copyfd;
11  if ((copyfd = ::dup(fd)) == EDM_IOFD_INVALID)
12  throwStorageError("FileDuplicateError", "Calling File::sysduplicate()", "dup()", errno);
13 
14  return copyfd;
15 }
16 
17 void File::sysopen(const char *name, int flags, int perms, IOFD &newfd, unsigned int & /*newflags*/) {
18  // Translate our flags to system flags.
19  int openflags = 0;
20 
21  if ((flags & OpenRead) && (flags & OpenWrite))
22  openflags |= O_RDWR;
23  else if (flags & OpenRead)
24  openflags |= O_RDONLY;
25  else if (flags & OpenWrite)
26  openflags |= O_WRONLY;
27 
28  if (flags & OpenNonBlock)
29  openflags |= O_NONBLOCK;
30 
31  if (flags & OpenAppend)
32  openflags |= O_APPEND;
33 
34 #ifdef O_SYNC
35  if (flags & OpenUnbuffered)
36  openflags |= O_SYNC;
37 #else
38  if (flags & OpenUnbuffered)
39  newflags |= OpenUnbuffered;
40 #endif
41 
42  if (flags & OpenCreate)
43  openflags |= O_CREAT;
44 
45  if (flags & OpenExclusive)
46  openflags |= O_EXCL;
47 
48  if (flags & OpenTruncate)
49  openflags |= O_TRUNC;
50 
51  if (flags & OpenNotCTTY)
52  openflags |= O_NOCTTY;
53 
54  if ((newfd = ::open(name, openflags, perms)) == -1)
55  throwStorageError(edm::errors::FileOpenError, "Calling File::sysopen()", "open()", errno);
56 }
57 
59  assert(pos >= 0);
60 
61  ssize_t s;
62  do
63  s = ::pread(fd(), into, n, pos);
64  while (s == -1 && errno == EINTR);
65 
66  if (s == -1)
67  throwStorageError(edm::errors::FileReadError, "Calling File::read()", "pread()", errno);
68 
69  return s;
70 }
71 
72 IOSize File::write(const void *from, IOSize n, IOOffset pos) {
73  assert(pos >= 0);
74 
75  ssize_t s;
76  do
77  s = ::pwrite(fd(), from, n, pos);
78  while (s == -1 && errno == EINTR);
79 
80  if (s == -1)
81  throwStorageError(edm::errors::FileWriteError, "Calling File::write()", "pwrite()", errno);
82 
83  if (m_flags & OpenUnbuffered)
84  // FIXME: Exception handling?
85  flush();
86 
87  return s;
88 }
89 
90 IOOffset File::size(void) const {
91  IOFD fd = this->fd();
92  assert(fd != EDM_IOFD_INVALID);
93 
94  struct stat info;
95  if (fstat(fd, &info) == -1)
96  throwStorageError("FileSizeError", "Calling File::size()", "fstat()", errno);
97 
98  return info.st_size;
99 }
100 
102  IOFD fd = this->fd();
103  assert(fd != EDM_IOFD_INVALID);
104  assert(whence == CURRENT || whence == SET || whence == END);
105 
107  int mywhence = (whence == SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END);
108  if ((result = ::lseek(fd, offset, mywhence)) == -1)
109  throwStorageError("FilePositionError", "Calling File::position()", "lseek()", errno);
110 
111  return result;
112 }
113 
115  IOFD fd = this->fd();
116  assert(fd != EDM_IOFD_INVALID);
117 
118  if (ftruncate(fd, size) == -1)
119  throwStorageError("FileResizeError", "Calling File::resize()", "ftruncate()", errno);
120 }
121 
122 void File::flush(void) {
123  IOFD fd = this->fd();
124  assert(fd != EDM_IOFD_INVALID);
125 
126 #if _POSIX_SYNCHRONIZED_IO > 0
127  if (fdatasync(fd) == -1)
128  throwStorageError("FileFlushError", "Calling File::flush()", "fdatasync()", errno);
129 #elif _POSIX_FSYNC > 0
130  if (fsync(fd) == -1)
131  throwStorageError("FileFlushError", "Calling File::flush()", "fsync()", errno);
132 #endif
133 }
134 
135 bool File::sysclose(IOFD fd, int *error /* = 0 */) {
136  int ret = ::close(fd);
137  if (error)
138  *error = errno;
139  return ret != -1;
140 }
size
Write out results.
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
static void sysopen(const char *name, int flags, int perms, IOFD &newfd, unsigned &newflags)
Definition: UnixFile.cc:17
Definition: IOFlags.h:4
ret
prodAgent to be discontinued
IOSize write(const void *from, IOSize n) override
Definition: File.cc:209
IOOffset size(void) const override
Definition: UnixFile.cc:90
Relative
Definition: Storage.h:22
static bool sysclose(IOFD fd, int *error=nullptr)
Definition: UnixFile.cc:135
virtual IOOffset position(void) const
Definition: Storage.cc:72
static IOFD sysduplicate(IOFD fd)
Definition: UnixFile.cc:9
int read(void)
Definition: IOInput.cc:52
void flush(void) override
Definition: UnixFile.cc:122
void resize(IOOffset size) override
Definition: UnixFile.cc:114
int64_t IOOffset
Definition: IOTypes.h:19
int IOFD
Definition: IOTypes.h:22
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
#define O_NONBLOCK
Definition: SysFile.h:21
size_t IOSize
Definition: IOTypes.h:14
fd
Definition: ztee.py:136