CMS 3D CMS Logo

File.cc
Go to the documentation of this file.
4 #include <cassert>
5 
6 using namespace IOFlags;
7 
8 //<<<<<< PRIVATE DEFINES >>>>>>
9 //<<<<<< PRIVATE CONSTANTS >>>>>>
10 //<<<<<< PRIVATE TYPES >>>>>>
11 //<<<<<< PUBLIC VARIABLE DEFINITIONS >>>>>>
12 //<<<<<< PRIVATE VARIABLE DEFINITIONS >>>>>>
13 //<<<<<< CLASS STRUCTURE INITIALIZATION >>>>>>
14 //<<<<<< PUBLIC FUNCTION DEFINITIONS >>>>>>
15 //<<<<<< PRIVATE FUNCTION DEFINITIONS >>>>>>
16 //<<<<<< MEMBER FUNCTION DEFINITIONS >>>>>>
17 
53 
57 File::File(void) {
59  m_flags = 0;
60 }
61 
65 File::File(IOFD fd, bool autoclose /* = true */) {
66  this->fd(fd);
67  m_flags = autoclose ? InternalAutoClose : 0;
68 }
69 
71 File::File(IOFD fd, unsigned flags) {
72  this->fd(fd);
73  m_flags = flags;
74 }
75 
77 File::File(const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/) { open(name, flags, perms); }
78 
80 File::File(const std::string &name, int flags /*= OpenRead*/, int perms /*= 0666*/) {
81  open(name.c_str(), flags, perms);
82 }
83 
87 File::~File(void) {
88  if (m_flags & InternalAutoClose)
89  abort();
90 }
91 
93 
98 void File::setAutoClose(bool autoclose) {
99  m_flags &= ~InternalAutoClose;
100  if (autoclose)
101  m_flags |= InternalAutoClose;
102 }
103 
105 
110 File *File::duplicate(bool copy) const {
111  File *dup = new File(fd(), copy ? m_flags : 0);
112  return copy ? this->duplicate(dup) : dup;
113 }
114 
118  IOFD fd = this->fd();
119  assert(fd != EDM_IOFD_INVALID);
120  assert(child);
121  child->fd(sysduplicate(fd));
122  child->m_flags = m_flags;
123  return child;
124 }
125 
127 
131 void File::create(const char *name, bool exclusive /*=false*/, int perms /*=0666*/) {
132  open(name, (OpenCreate | OpenWrite | OpenTruncate | (exclusive ? OpenExclusive : 0)), perms);
133 }
134 
139 void File::create(const std::string &name, bool exclusive /*=false*/, int perms /*=0666*/) {
140  open(name.c_str(), (OpenCreate | OpenWrite | OpenTruncate | (exclusive ? OpenExclusive : 0)), perms);
141 }
142 
148 void File::open(const std::string &name, int flags /*= OpenRead*/, int perms /*= 0666*/) {
149  open(name.c_str(), flags, perms);
150 }
151 
156 void File::open(const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/) {
157  // is zero and always implied. OTOH, existence check should be
158  // done with Filename::exists() -- see comments there about what
159  // can happen on a WIN32 remote share even if the file doesn't
160  // exist. For now make sure that read or write was asked for.
161 
162  assert(name && *name);
163  assert(flags & (OpenRead | OpenWrite));
164 
165  // If I am already open, close the old file first.
166  if (fd() != EDM_IOFD_INVALID && (m_flags & InternalAutoClose))
167  close();
168 
169  IOFD newfd = EDM_IOFD_INVALID;
170  unsigned newflags = InternalAutoClose;
171 
172  sysopen(name, flags, perms, newfd, newflags);
173 
174  fd(newfd);
175  m_flags = newflags;
176 }
177 
179  this->fd(fd);
180  m_flags = 0;
181 }
182 
184 
185 bool File::prefetch(const IOPosBuffer *what, IOSize n) {
186  IOFD fd = this->fd();
187  for (IOSize i = 0; i < n; ++i) {
188 #if F_RDADVISE
189  radvisory info;
190  info.ra_offset = what[i].offset();
191  info.ra_count = what[i].size();
192  fcntl(fd, F_RDADVISE, &info);
193 #elif _POSIX_ADVISORY_INFO > 0
194  posix_fadvise(fd, what[i].offset(), what[i].size(), POSIX_FADV_WILLNEED);
195 #else
196 #error advisory read ahead not available on this platform
197 #endif
198  }
199  return true;
200 }
201 
203 IOSize File::read(void *into, IOSize n) { return IOChannel::read(into, n); }
204 
206 IOSize File::readv(IOBuffer *into, IOSize length) { return IOChannel::readv(into, length); }
207 
209 IOSize File::write(const void *from, IOSize n) {
210  // FIXME: This may create a race condition or cause trouble on
211  // remote files. Should be currently needed only on WIN32.
212  if (m_flags & OpenAppend)
213  position(0, END);
214 
215  IOSize s = IOChannel::write(from, n);
216 
217  if (m_flags & OpenUnbuffered)
218  // FIXME: Exception handling?
219  flush();
220 
221  return s;
222 }
223 
225 IOSize File::writev(const IOBuffer *from, IOSize length) {
226  // FIXME: This may create a race condition or cause trouble on
227  // remote files. Should be currently needed only on WIN32.
228  if (m_flags & OpenAppend)
229  position(0, END);
230 
231  IOSize s = IOChannel::writev(from, length);
232 
233  if (m_flags & OpenUnbuffered)
234  // FIXME: Exception handling?
235  flush();
236 
237  return s;
238 }
239 
241 void File::close(void) {
242  IOFD fd = this->fd();
243  assert(fd != EDM_IOFD_INVALID);
244 
245  int error;
246  if (!sysclose(fd, &error))
247  throwStorageError("FileCloseError", "Calling File::close()", "sysclose", error);
248 
249  m_flags &= ~InternalAutoClose;
250  this->fd(EDM_IOFD_INVALID);
251 }
252 
254 void File::abort(void) {
255  IOFD fd = this->fd();
256  if (fd != EDM_IOFD_INVALID) {
257  sysclose(fd);
258  m_flags &= ~InternalAutoClose;
259  this->fd(EDM_IOFD_INVALID);
260  }
261 }
size
Write out results.
virtual void setAutoClose(bool closeit)
Definition: File.cc:98
IOSize writev(const IOBuffer *from, IOSize length) override
Definition: File.cc:225
static const TGPicture * info(bool iBackgroundIsBlack)
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
unsigned m_flags
Definition: File.h:64
Definition: IOFlags.h:4
virtual void create(const char *name, bool exclusive=false, int perms=0666)
Definition: File.cc:131
virtual void attach(IOFD fd)
Definition: File.cc:178
IOSize writev(const IOBuffer *from, IOSize buffers) override
IOSize write(const void *from, IOSize n) override
Definition: File.cc:209
IOSize readv(IOBuffer *into, IOSize buffers) override
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: File.cc:156
virtual void abort(void)
Definition: File.cc:254
IOSize write(const void *from, IOSize n) override
void close(void) override
Definition: File.cc:241
IOSize readv(IOBuffer *into, IOSize length) override
Definition: File.cc:206
File(void)
Definition: File.cc:57
int read(void)
Definition: IOInput.cc:52
IOOffset offset(void) const
Definition: IOPosBuffer.h:39
IOSize size(void) const
Definition: IOPosBuffer.h:45
~File(void) override
Definition: File.cc:87
int IOFD
Definition: IOTypes.h:22
static int position[264][3]
Definition: ReadPGInfo.cc:289
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
File * duplicate(bool copy) const
Definition: File.cc:110
size_t IOSize
Definition: IOTypes.h:14
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
Definition: File.h:11
fd
Definition: ztee.py:136
bool prefetch(const IOPosBuffer *what, IOSize n) override
Definition: File.cc:185