CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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)
58 {
60  m_flags = 0;
61 }
62 
66 File::File (IOFD fd, bool autoclose /* = true */)
67 {
68  this->fd (fd);
69  m_flags = autoclose ? InternalAutoClose : 0;
70 }
71 
73 File::File (IOFD fd, unsigned flags)
74 {
75  this->fd (fd);
76  m_flags = flags;
77 }
78 
80 File::File (const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/)
81 { open (name, flags, perms); }
82 
84 File::File (const std::string &name, int flags /*= OpenRead*/, int perms /*= 0666*/)
85 { open (name.c_str (), flags, perms); }
86 
91 {
92  if (m_flags & InternalAutoClose)
93  abort ();
94 }
95 
97 
102 void
103 File::setAutoClose (bool autoclose)
104 {
105  m_flags &= ~InternalAutoClose;
106  if (autoclose)
107  m_flags |= InternalAutoClose;
108 }
109 
111 
116 File *
117 File::duplicate (bool copy) const
118 {
119  File *dup = new File (fd (), copy ? m_flags : 0);
120  return copy ? this->duplicate (dup) : dup;
121 }
122 
125 File *
127 {
128  IOFD fd = this->fd ();
129  assert (fd != EDM_IOFD_INVALID);
130  assert (child);
131  child->fd (sysduplicate (fd));
132  child->m_flags = m_flags;
133  return child;
134 }
135 
137 
141 void
142 File::create (const char *name, bool exclusive /*=false*/, int perms/*=0666*/)
143 {
144  open (name,
146  | (exclusive ? OpenExclusive : 0)),
147  perms);
148 }
149 
154 void
155 File::create (const std::string &name, bool exclusive /*=false*/, int perms/*=0666*/)
156 {
157  open (name.c_str (),
159  | (exclusive ? OpenExclusive : 0)),
160  perms);
161 }
162 
168 void
169 File::open (const std::string &name, int flags /*= OpenRead*/, int perms /*= 0666*/)
170 { open (name.c_str (), flags, perms); }
171 
176 void
177 File::open (const char *name, int flags /*= OpenRead*/, int perms /*= 0666*/)
178 {
179  // is zero and always implied. OTOH, existence check should be
180  // done with Filename::exists() -- see comments there about what
181  // can happen on a WIN32 remote share even if the file doesn't
182  // exist. For now make sure that read or write was asked for.
183 
184  assert (name && *name);
185  assert (flags & (OpenRead | OpenWrite));
186 
187  // If I am already open, close the old file first.
188  if (fd () != EDM_IOFD_INVALID && (m_flags & InternalAutoClose))
189  close ();
190 
191  IOFD newfd = EDM_IOFD_INVALID;
192  unsigned newflags = InternalAutoClose;
193 
194  sysopen (name, flags, perms, newfd, newflags);
195 
196  fd (newfd);
197  m_flags = newflags;
198 }
199 
200 void
202 {
203  this->fd (fd);
204  m_flags = 0;
205 }
206 
208 
209 bool
211 {
212  IOFD fd = this->fd();
213  for (IOSize i = 0; i < n; ++i)
214  {
215 #if F_RDADVISE
216  radvisory info;
217  info.ra_offset = what[i].offset();
218  info.ra_count = what[i].size();
219  fcntl(fd, F_RDADVISE, &info);
220 #elif _POSIX_ADVISORY_INFO > 0
221  posix_fadvise(fd, what[i].offset(), what[i].size(), POSIX_FADV_WILLNEED);
222 #else
223 # error advisory read ahead not available on this platform
224 #endif
225  }
226  return true;
227 }
228 
230 IOSize
231 File::read (void *into, IOSize n)
232 { return IOChannel::read (into, n); }
233 
235 IOSize
236 File::readv (IOBuffer *into, IOSize length)
237 { return IOChannel::readv (into, length); }
238 
240 IOSize
241 File::write (const void *from, IOSize n)
242 {
243  // FIXME: This may create a race condition or cause trouble on
244  // remote files. Should be currently needed only on WIN32.
245  if (m_flags & OpenAppend)
246  position (0, END);
247 
248  IOSize s = IOChannel::write (from, n);
249 
250  if (m_flags & OpenUnbuffered)
251  // FIXME: Exception handling?
252  flush ();
253 
254  return s;
255 }
256 
258 IOSize
259 File::writev (const IOBuffer *from, IOSize length)
260 {
261  // FIXME: This may create a race condition or cause trouble on
262  // remote files. Should be currently needed only on WIN32.
263  if (m_flags & OpenAppend)
264  position (0, END);
265 
266  IOSize s = IOChannel::writev (from, length);
267 
268  if (m_flags & OpenUnbuffered)
269  // FIXME: Exception handling?
270  flush ();
271 
272  return s;
273 }
274 
276 void
278 {
279  IOFD fd = this->fd ();
280  assert (fd != EDM_IOFD_INVALID);
281 
282  int error;
283  if (! sysclose (fd, &error))
284  throwStorageError("FileCloseError", "Calling File::close()",
285  "sysclose", error);
286 
287  m_flags &= ~InternalAutoClose;
288  this->fd (EDM_IOFD_INVALID);
289 }
290 
292 void
294 {
295  IOFD fd = this->fd ();
296  if (fd != EDM_IOFD_INVALID)
297  {
298  sysclose (fd);
299  m_flags &= ~InternalAutoClose;
300  this->fd (EDM_IOFD_INVALID);
301  }
302 }
virtual IOSize readv(IOBuffer *into, IOSize length)
Definition: File.cc:236
virtual void setAutoClose(bool closeit)
Definition: File.cc:103
int i
Definition: DBlmapReader.cc:9
static const TGPicture * info(bool iBackgroundIsBlack)
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:7
unsigned m_flags
Definition: File.h:74
~File(void)
Definition: File.cc:90
assert(m_qm.get())
virtual void create(const char *name, bool exclusive=false, int perms=0666)
Definition: File.cc:142
virtual IOSize writev(const IOBuffer *from, IOSize buffers)
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
virtual void attach(IOFD fd)
Definition: File.cc:201
virtual bool prefetch(const IOPosBuffer *what, IOSize n)
Definition: File.cc:210
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: File.cc:177
virtual void abort(void)
Definition: File.cc:293
static const boost::regex duplicate("duplicateIOV[[:print:]]+?[S|s]ince[=| ]([[:alnum:]]+?);.*")
virtual IOSize writev(const IOBuffer *from, IOSize length)
Definition: File.cc:259
virtual IOSize write(const void *from, IOSize n)
tuple fd
Definition: ztee.py:136
File(void)
Definition: File.cc:57
int read(void)
Definition: IOInput.cc:54
IOOffset offset(void) const
Definition: IOPosBuffer.h:54
IOSize size(void) const
Definition: IOPosBuffer.h:64
virtual IOSize write(const void *from, IOSize n)
Definition: File.cc:241
int IOFD
Definition: IOTypes.h:22
static int position[264][3]
Definition: ReadPGInfo.cc:509
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
File * duplicate(bool copy) const
Definition: File.cc:117
size_t IOSize
Definition: IOTypes.h:14
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
Definition: File.h:11
virtual void close(void)
Definition: File.cc:277
tuple size
Write out results.
virtual IOSize readv(IOBuffer *into, IOSize buffers)