CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
DCacheFile Class Reference

#include <DCacheFile.h>

Inheritance diagram for DCacheFile:
Storage IOInput IOOutput

Public Member Functions

virtual void abort (void)
 
void close (void) override
 
virtual void create (const char *name, bool exclusive=false, int perms=0666)
 
virtual void create (const std::string &name, bool exclusive=false, int perms=0666)
 
 DCacheFile (void)
 
 DCacheFile (IOFD fd)
 
 DCacheFile (const char *name, int flags=IOFlags::OpenRead, int perms=0666)
 
 DCacheFile (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666)
 
virtual void open (const char *name, int flags=IOFlags::OpenRead, int perms=0666)
 
virtual void open (const std::string &name, int flags=IOFlags::OpenRead, int perms=0666)
 
IOOffset position (IOOffset offset, Relative whence=SET) override
 
IOSize read (void *into, IOSize n) override
 
IOSize readv (IOBuffer *into, IOSize buffers) override
 
IOSize readv (IOPosBuffer *into, IOSize buffers) override
 
void resize (IOOffset size) override
 
IOSize write (const void *from, IOSize n) override
 
 ~DCacheFile (void) override
 
- Public Member Functions inherited from Storage
virtual bool eof (void) const
 
virtual void flush (void)
 
virtual IOOffset position (void) const
 
virtual bool prefetch (const IOPosBuffer *what, IOSize n)
 
virtual IOSize read (void *into, IOSize n, IOOffset pos)
 
IOSize read (IOBuffer into, IOOffset pos)
 
virtual void rewind (void)
 
virtual IOOffset size (void) const
 
 Storage (void)
 
virtual IOSize write (const void *from, IOSize n, IOOffset pos)
 
IOSize write (IOBuffer from, IOOffset pos)
 
virtual IOSize writev (const IOPosBuffer *from, IOSize buffers)
 
 ~Storage (void) override
 
- Public Member Functions inherited from IOInput
int read (void)
 
IOSize read (IOBuffer into)
 
IOSize xread (IOBuffer into)
 
IOSize xread (void *into, IOSize n)
 
IOSize xreadv (IOBuffer *into, IOSize buffers)
 
virtual ~IOInput (void)
 Destruct the stream. A no-op. More...
 
- Public Member Functions inherited from IOOutput
IOSize write (unsigned char byte)
 
IOSize write (IOBuffer from)
 
virtual IOSize writev (const IOBuffer *from, IOSize buffers)
 
IOSize xwrite (const void *from, IOSize n)
 
IOSize xwrite (IOBuffer from)
 
IOSize xwritev (const IOBuffer *from, IOSize buffers)
 
virtual ~IOOutput (void)
 Destruct the stream. A no-op. More...
 

Private Attributes

bool m_close
 
IOFD m_fd
 
std::string m_name
 

Additional Inherited Members

- Public Types inherited from Storage
enum  Relative { SET, CURRENT, END }
 

Detailed Description

Definition at line 8 of file DCacheFile.h.

Constructor & Destructor Documentation

DCacheFile::DCacheFile ( void  )

Definition at line 11 of file DCacheFile.cc.

13  m_close (false)
14 {}
IOFD m_fd
Definition: DCacheFile.h:46
bool m_close
Definition: DCacheFile.h:47
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
DCacheFile::DCacheFile ( IOFD  fd)

Definition at line 16 of file DCacheFile.cc.

17  : m_fd (fd),
18  m_close (true)
19 {}
IOFD m_fd
Definition: DCacheFile.h:46
bool m_close
Definition: DCacheFile.h:47
DCacheFile::DCacheFile ( const char *  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)

Definition at line 21 of file DCacheFile.cc.

References open().

25  m_close (false)
26 { open (name, flags, perms); }
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
IOFD m_fd
Definition: DCacheFile.h:46
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:73
bool m_close
Definition: DCacheFile.h:47
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
DCacheFile::DCacheFile ( const std::string &  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)

Definition at line 28 of file DCacheFile.cc.

References flags, and open().

32  m_close (false)
33 { open (name.c_str (), flags, perms); }
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
IOFD m_fd
Definition: DCacheFile.h:46
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:73
bool m_close
Definition: DCacheFile.h:47
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
DCacheFile::~DCacheFile ( void  )
override

Definition at line 35 of file DCacheFile.cc.

References m_close, and m_name.

36 {
37  if (m_close)
38  edm::LogError("DCacheFileError")
39  << "Destructor called on dCache file '" << m_name
40  << "' but the file is still open";
41 }
bool m_close
Definition: DCacheFile.h:47
std::string m_name
Definition: DCacheFile.h:48

Member Function Documentation

void DCacheFile::abort ( void  )
virtual

Definition at line 180 of file DCacheFile.cc.

References EDM_IOFD_INVALID, m_close, and m_fd.

181 {
182  if (m_fd != EDM_IOFD_INVALID)
183  dc_close (m_fd);
184 
185  m_close = false;
187 }
IOFD m_fd
Definition: DCacheFile.h:46
bool m_close
Definition: DCacheFile.h:47
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
void DCacheFile::close ( void  )
overridevirtual

Reimplemented from Storage.

Definition at line 154 of file DCacheFile.cc.

References EDM_IOFD_INVALID, m_close, m_fd, and m_name.

Referenced by lumiQTWidget.ApplicationWindow::fileQuit(), esMonitoring.AsyncLineReaderMixin::handle_close(), esMonitoring.FDJsonServer::handle_close(), Vispa.Gui.BoxContentDialog.BoxContentDialog::keyPressEvent(), Vispa.Gui.FindDialog.FindDialog::keyPressEvent(), and open().

155 {
156  if (m_fd == EDM_IOFD_INVALID)
157  {
158  edm::LogError("DCacheFileError")
159  << "DCacheFile::close(name='" << m_name
160  << "') called but the file is not open";
161  m_close = false;
162  return;
163  }
164 
165  dc_errno = 0;
166  if (dc_close (m_fd) == -1)
167  edm::LogWarning("DCacheFileWarning")
168  << "dc_close(name='" << m_name
169  << "') failed with error '" << dc_strerror (dc_errno)
170  << "' (dc_errno=" << dc_errno << ")";
171 
172  m_close = false;
174 
175  // Caused hang. Will be added back after problem is fixed.
176  // edm::LogInfo("DCacheFileInfo") << "Closed " << m_name;
177 }
IOFD m_fd
Definition: DCacheFile.h:46
bool m_close
Definition: DCacheFile.h:47
std::string m_name
Definition: DCacheFile.h:48
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
void DCacheFile::create ( const char *  name,
bool  exclusive = false,
int  perms = 0666 
)
virtual

Definition at line 45 of file DCacheFile.cc.

References open(), IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.

48 {
49  open (name,
51  | (exclusive ? IOFlags::OpenExclusive : 0)),
52  perms);
53 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:73
void DCacheFile::create ( const std::string &  name,
bool  exclusive = false,
int  perms = 0666 
)
virtual

Definition at line 56 of file DCacheFile.cc.

References open(), IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.

59 {
60  open (name.c_str (),
62  | (exclusive ? IOFlags::OpenExclusive : 0)),
63  perms);
64 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:73
void DCacheFile::open ( const char *  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)
virtual

Definition at line 73 of file DCacheFile.cc.

References cms::Exception::addContext(), close(), TauDecayModes::dec, EDM_IOFD_INVALID, edm::errors::FileOpenError, m_close, m_fd, m_name, dataset::name, O_NONBLOCK, IOFlags::OpenAppend, IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenNonBlock, IOFlags::OpenRead, IOFlags::OpenTruncate, IOFlags::OpenUnbuffered, and IOFlags::OpenWrite.

Referenced by create(), DCacheFile(), and open().

76 {
77  // Actual open
78  if ((name == nullptr) || (*name == 0)) {
80  ex << "Cannot open a file without a name";
81  ex.addContext("Calling DCacheFile::open()");
82  throw ex;
83  }
84 
85  if ((flags & (IOFlags::OpenRead | IOFlags::OpenWrite)) == 0) {
87  ex << "Must open file '" << name << "' at least for read or write";
88  ex.addContext("Calling DCacheFile::open()");
89  throw ex;
90  }
91 
92  // If I am already open, close old file first
93  if (m_fd != EDM_IOFD_INVALID && m_close)
94  close ();
95 
96  // Translate our flags to system flags
97  int openflags = 0;
98 
100  openflags |= O_RDWR;
101  else if (flags & IOFlags::OpenRead)
102  openflags |= O_RDONLY;
103  else if (flags & IOFlags::OpenWrite)
104  openflags |= O_WRONLY;
105 
107  openflags |= O_NONBLOCK;
108 
110  openflags |= O_APPEND;
111 
113  openflags |= O_CREAT;
114 
116  openflags |= O_EXCL;
117 
119  openflags |= O_TRUNC;
120 
121  IOFD newfd = EDM_IOFD_INVALID;
122  dc_errno = 0;
123  if ((newfd = dc_open (name, openflags, perms)) == -1) {
125  ex << "dc_open(name='" << name
126  << "', flags=0x" << std::hex << openflags
127  << ", permissions=0" << std::oct << perms << std::dec
128  << ") => error '" << dc_strerror(dc_errno)
129  << "' (dc_errno=" << dc_errno << ")";
130  ex.addContext("Calling DCacheFile::open()");
131  throw ex;
132  }
133 
134  m_name = name;
135  m_fd = newfd;
136 
137  // Turn off read-ahead, or adjust read-ahead size depending on
138  // whether buffering has been requested. This is a very tricky
139  // balance here. Without read-ahead data processing appears to
140  // become exceedingly slow, and with default (1MB) read-ahead
141  // it appears to saturate disk servers and network. Try tread
142  // reasonable middle ground here.
144  dc_noBuffering(m_fd);
145  else
146  dc_setBufferSize(m_fd, 64000);
147 
148  m_close = true;
149 
150  edm::LogInfo("DCacheFileInfo") << "Opened " << m_name;
151 }
void close(void) override
Definition: DCacheFile.cc:154
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
IOFD m_fd
Definition: DCacheFile.h:46
bool m_close
Definition: DCacheFile.h:47
int IOFD
Definition: IOTypes.h:22
std::string m_name
Definition: DCacheFile.h:48
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
#define O_NONBLOCK
Definition: SysFile.h:21
void DCacheFile::open ( const std::string &  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)
virtual

Definition at line 67 of file DCacheFile.cc.

References flags, and open().

70 { open (name.c_str (), flags, perms); }
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DCacheFile.cc:73
IOOffset DCacheFile::position ( IOOffset  offset,
Relative  whence = SET 
)
overridevirtual

Implements Storage.

Definition at line 340 of file DCacheFile.cc.

References cms::Exception::addContext(), Storage::CURRENT, EDM_IOFD_INVALID, Storage::END, m_fd, m_name, mps_fire::result, and Storage::SET.

341 {
342  if (m_fd == EDM_IOFD_INVALID) {
343  cms::Exception ex("FilePositionError");
344  ex << "DCacheFile::position() called on a closed file";
345  throw ex;
346  }
347  if (whence != CURRENT && whence != SET && whence != END) {
348  cms::Exception ex("FilePositionError");
349  ex << "DCacheFile::position() called with incorrect 'whence' parameter";
350  throw ex;
351  }
353  int mywhence = (whence == SET ? SEEK_SET
354  : whence == CURRENT ? SEEK_CUR
355  : SEEK_END);
356 
357  dc_errno = 0;
358  if ((result = dc_lseek64 (m_fd, offset, mywhence)) == -1) {
359  cms::Exception ex("FilePositionError");
360  ex << "dc_lseek64(name='" << m_name << "', offset=" << offset
361  << ", whence=" << mywhence << ") failed with error '"
362  << dc_strerror (dc_errno) << "' (dc_errno=" << dc_errno << ")";
363  ex.addContext("Calling DCacheFile::position()");
364  throw ex;
365  }
366  // FIXME: dCache returns incorrect value on SEEK_END.
367  // Remove this hack when dcap has been fixed.
368  if (whence == SEEK_END && (result = dc_lseek64 (m_fd, result, SEEK_SET)) == -1) {
369  cms::Exception ex("FilePositionError");
370  ex << "dc_lseek64(name='" << m_name << "', offset=" << offset
371  << ", whence=" << SEEK_SET << ") failed with error '"
372  << dc_strerror (dc_errno) << "' (dc_errno=" << dc_errno << ")";
373  ex.addContext("Calling DCacheFile::position()");
374  throw ex;
375  }
376  return result;
377 }
IOFD m_fd
Definition: DCacheFile.h:46
int64_t IOOffset
Definition: IOTypes.h:19
std::string m_name
Definition: DCacheFile.h:48
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
IOSize DCacheFile::read ( void *  into,
IOSize  n 
)
overridevirtual

Read into into at most n number of bytes.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

Returns
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons.
Exceptions
Incase of error, a #IOError exception is thrown. This includes the situation where the input stream is in non-blocking mode and no input is currently available (FIXME: make this simpler; clarify which exception).

Implements IOInput.

Definition at line 206 of file DCacheFile.cc.

References cms::Exception::addContext(), BUGLINE, edm::errors::FileReadError, m_fd, m_name, and alignCSCRings::s.

207 {
208  IOSize done = 0;
209  while (done < n)
210  {
211  dc_errno = 0;
212  ssize_t s = dc_read (m_fd, (char *) into + done, n - done);
213  if (s == -1) {
215  ex << "dc_read(name='" << m_name << "', n=" << (n-done)
216  << ") failed with error '" << dc_strerror(dc_errno)
217  << "' (dc_errno=" << dc_errno << ")";
218  ex.addContext("Calling DCacheFile::read()");
219  throw ex;
220  }
221  else if (s == 0)
222  // end of file
223  break;
224  else if (s < ssize_t (n-done))
225  edm::LogInfo("DCacheFileWarning")
226  << "dc_read(name='" << m_name << "', n=" << (n-done)
227  << ") returned a short read of " << s << " bytes; "
228  << "please report a bug in dCache referencing the "
229  << "comment on line " << BUGLINE << " of " << __FILE__;
230  done += s;
231  }
232 
233  return done;
234 }
IOFD m_fd
Definition: DCacheFile.h:46
static const int BUGLINE
Definition: DCacheFile.cc:190
std::string m_name
Definition: DCacheFile.h:48
size_t IOSize
Definition: IOTypes.h:14
IOSize DCacheFile::readv ( IOBuffer into,
IOSize  buffers 
)
overridevirtual

Read from the input stream into multiple scattered buffers. There are buffers to fill in an array starting at into; the memory those buffers occupy does not need to be contiguous. The buffers are filled in the order given, eac buffer is filled fully before the subsequent buffers.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

The base class implementation uses read(void *, IOSize) method, but derived classes may implement a more efficient alternative.

Returns
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons. Note that the return value indicates the number of bytes read, not the number of buffers; it is the sum total of bytes filled into all the buffers.
Exceptions
Incase of error, a #IOError exception is thrown. However if some data has already been read, the error is swallowed and the method returns the data read so far. It is assumed that persistent errors will occur anyway on the next read and sporadic errors like stream becoming unvailable can be ignored. Use xread() if a different policy is desirable.

Reimplemented from IOInput.

Definition at line 265 of file DCacheFile.cc.

References cms::Exception::addContext(), data, edm::errors::FileReadError, mps_fire::i, m_fd, m_name, gen::n, and IOBuffer::size().

266 {
267  assert (! buffers || into);
268 
269  // readv may not support zero buffers.
270  if (! buffers)
271  return 0;
272 
273  // Convert the buffers to system format.
274  std::vector<iovec> bufs (buffers);
275  for (IOSize i = 0; i < buffers; ++i)
276  {
277  bufs [i].iov_len = into [i].size ();
278  bufs [i].iov_base = (caddr_t) into [i].data ();
279  }
280 
281  // Read as long as signals cancel the read before doing anything.
282  dc_errno = 0;
283  ssize_t n = dc_readv (m_fd, &bufs [0], buffers);
284 
285  // If it was serious error, throw it.
286  if (n == -1) {
288  ex << "dc_readv(name='" << m_name << "', iov[" << buffers
289  << "]) failed with error '" << dc_strerror(dc_errno)
290  << "' (dc_errno=" << dc_errno << ")";
291  ex.addContext("Calling DCacheFile::readv()");
292  throw ex;
293  }
294 
295  // Return the number of bytes actually read.
296  return n;
297 }
IOFD m_fd
Definition: DCacheFile.h:46
IOSize size(void) const
Definition: IOBuffer.h:50
std::string m_name
Definition: DCacheFile.h:48
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
size_t IOSize
Definition: IOTypes.h:14
IOSize DCacheFile::readv ( IOPosBuffer into,
IOSize  buffers 
)
overridevirtual

Reimplemented from Storage.

Definition at line 300 of file DCacheFile.cc.

References cms::Exception::addContext(), data, edm::errors::FileReadError, mps_fire::i, m_fd, m_name, gen::n, IOPosBuffer::offset(), IOPosBuffer::size(), and pileupDistInMC::total.

301 {
302  assert (! buffers || into);
303 
304  // readv may not support zero buffers.
305  if (! buffers)
306  return 0;
307 
308  // Convert the buffers to system format.
309  std::vector<iovec2> bufs (buffers);
310  IOSize total = 0;
311  for (IOSize i = 0; i < buffers; ++i)
312  {
313  bufs [i].offset = into [i].offset ();
314  bufs [i].len = into [i].size ();
315  bufs [i].buf = (char *) into [i].data ();
316  total += into [i].size ();
317  }
318 
319  // Read as long as signals cancel the read before doing anything.
320  dc_errno = 0;
321  ssize_t n = dc_readv2 (m_fd, &bufs [0], buffers);
322 
323  // If it was serious error, throw it.
324  if (n == -1) {
326  ex << "dc_readv2(name='" << m_name << "', iov2[" << buffers
327  << "]) failed with error '" << dc_strerror(dc_errno)
328  << "' (dc_errno=" << dc_errno << ")";
329  ex.addContext("Calling DCacheFile::readv()");
330  throw ex;
331  }
332  // dc_readv2 returns 0 on success.
333  return (n == 0) ? total : 0;
334 }
IOFD m_fd
Definition: DCacheFile.h:46
IOOffset offset(void) const
Definition: IOPosBuffer.h:54
IOSize size(void) const
Definition: IOPosBuffer.h:64
std::string m_name
Definition: DCacheFile.h:48
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
size_t IOSize
Definition: IOTypes.h:14
void DCacheFile::resize ( IOOffset  size)
overridevirtual

Implements Storage.

Definition at line 380 of file DCacheFile.cc.

References m_name.

Referenced by Vispa.Gui.TextDialog.TextDialog::__init__(), Vispa.Plugins.ConfigEditor.ToolDialog.ToolDialog::__init__(), Vispa.Main.MainWindow.MainWindow::_loadIni(), and Vispa.Gui.PortConnection.PointToPointConnection::updateConnection().

381 {
382  cms::Exception ex("FileResizeError");
383  ex << "DCacheFile::resize(name='" << m_name << "') not implemented";
384  throw ex;
385 }
std::string m_name
Definition: DCacheFile.h:48
IOSize DCacheFile::write ( const void *  from,
IOSize  n 
)
overridevirtual

Write n bytes of data starting at address from.

Returns
The number of bytes written. Normally this will be n, but can be less, even zero, for example if the stream is non-blocking mode and cannot accept input at this time.
Exceptions
Incase of error, an exception is thrown. However if the stream is in non-blocking mode and cannot accept output, it will not throw an exception – the return value will be less than requested.

Implements IOOutput.

Definition at line 237 of file DCacheFile.cc.

References cms::Exception::addContext(), BUGLINE, edm::errors::FileWriteError, m_fd, m_name, and alignCSCRings::s.

238 {
239  IOSize done = 0;
240  while (done < n)
241  {
242  dc_errno = 0;
243  ssize_t s = dc_write (m_fd, (const char *) from + done, n - done);
244  if (s == -1) {
246  ex << "dc_write(name='" << m_name << "', n=" << (n-done)
247  << ") failed with error '" << dc_strerror(dc_errno)
248  << "' (dc_errno=" << dc_errno << ")";
249  ex.addContext("Calling DCacheFile::write()");
250  throw ex;
251  }
252  else if (s < ssize_t (n-done))
253  edm::LogInfo("DCacheFileWarning")
254  << "dc_write(name='" << m_name << "', n=" << (n-done)
255  << ") returned a short write of " << s << " bytes; "
256  << "please report a bug in dCache referencing the "
257  << "comment on line " << BUGLINE << " of " << __FILE__;
258  done += s;
259  }
260 
261  return done;
262 }
IOFD m_fd
Definition: DCacheFile.h:46
static const int BUGLINE
Definition: DCacheFile.cc:190
std::string m_name
Definition: DCacheFile.h:48
size_t IOSize
Definition: IOTypes.h:14

Member Data Documentation

bool DCacheFile::m_close
private

Definition at line 47 of file DCacheFile.h.

Referenced by abort(), close(), open(), and ~DCacheFile().

IOFD DCacheFile::m_fd
private

Definition at line 46 of file DCacheFile.h.

Referenced by abort(), close(), open(), position(), read(), readv(), and write().

std::string DCacheFile::m_name
private

Definition at line 48 of file DCacheFile.h.

Referenced by close(), open(), position(), read(), readv(), resize(), write(), and ~DCacheFile().