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