CMS 3D CMS Logo

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