CMS 3D CMS Logo

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

#include <DavixFile.h>

Inheritance diagram for DavixFile:
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)
 
 DavixFile (void)
 
 DavixFile (const char *name, int flags=IOFlags::OpenRead, int perms=0666)
 
 DavixFile (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
 
 ~DavixFile (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...
 

Static Public Member Functions

static void configureDavixLogLevel ()
 

Private Attributes

std::unique_ptr< Davix::DavPosix > m_davixPosix
 
Davix_fd * 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 DavixFile.h.

Constructor & Destructor Documentation

DavixFile::DavixFile ( void  )

Definition at line 18 of file DavixFile.cc.

18 {}
DavixFile::DavixFile ( const char *  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)

Definition at line 20 of file DavixFile.cc.

20  {
21  open(name, flags, perms);
22 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:160
DavixFile::DavixFile ( const std::string &  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)

Definition at line 24 of file DavixFile.cc.

References HLT_2018_cff::flags.

24  {
25  open(name.c_str(), flags, perms);
26 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:160
DavixFile::~DavixFile ( void  )
override

Definition at line 28 of file DavixFile.cc.

28  {
29  close();
30  return;
31 }
void close(void) override
Definition: DavixFile.cc:33

Member Function Documentation

void DavixFile::abort ( void  )
virtual

Definition at line 51 of file DavixFile.cc.

References cms::Exception::addContext(), and runTheMatrix::err.

51  {
52  if (m_davixPosix && m_fd) {
53  DavixError *err = nullptr;
54  m_davixPosix->close(m_fd, &err);
55  if (err) {
56  std::unique_ptr<DavixError> davixErrManaged(err);
57  cms::Exception ex("FileAbortError");
58  ex << "Davix::abort(name='" << m_name << ") failed with error " << err->getErrMsg().c_str() << " and error code "
59  << err->getStatus();
60  ex.addContext("Calling DavixFile::abort()");
61  throw ex;
62  }
63  }
64  return;
65 }
std::unique_ptr< Davix::DavPosix > m_davixPosix
Definition: DavixFile.h:40
std::string m_name
Definition: DavixFile.h:41
Davix_fd * m_fd
Definition: DavixFile.h:39
void DavixFile::close ( void  )
overridevirtual

Reimplemented from Storage.

Definition at line 33 of file DavixFile.cc.

References cms::Exception::addContext(), runTheMatrix::err, and eostools::move().

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

33  {
34  if (m_davixPosix && m_fd) {
35  auto davixPosix = std::move(m_davixPosix);
36  DavixError *err = nullptr;
37  davixPosix->close(m_fd, &err);
38  m_fd = nullptr;
39  if (err) {
40  std::unique_ptr<DavixError> davixErrManaged(err);
41  cms::Exception ex("FileCloseError");
42  ex << "Davix::close(name='" << m_name << ") failed with error " << err->getErrMsg().c_str() << " and error code "
43  << err->getStatus();
44  ex.addContext("Calling DavixFile::close()");
45  throw ex;
46  }
47  }
48  return;
49 }
std::unique_ptr< Davix::DavPosix > m_davixPosix
Definition: DavixFile.h:40
std::string m_name
Definition: DavixFile.h:41
Davix_fd * m_fd
Definition: DavixFile.h:39
def move(src, dest)
Definition: eostools.py:511
void DavixFile::configureDavixLogLevel ( )
static

Definition at line 67 of file DavixFile.cc.

References davixDebugInit.

67  {
68  long logLevel = 0;
69  char *logptr = nullptr;
70  char const *const davixDebug = std::getenv("Davix_Debug");
71  if (davixDebug != nullptr) {
72  logLevel = strtol(davixDebug, &logptr, 0);
73  if (errno) {
74  edm::LogWarning("DavixFile") << "Got error while converting "
75  << "Davix_Debug env variable to integer. "
76  "Will use default log level 0";
77  logLevel = 0;
78  }
79  if (logptr == davixDebug) {
80  edm::LogWarning("DavixFile") << "Failed to convert to integer "
81  << "Davix_Debug env variable; Will use default log level 0";
82  logLevel = 0;
83  } else if (*logptr != '\0') {
84  edm::LogWarning("DavixFile") << "Failed to parse extra junk "
85  << "from Davix_Debug env variable. Will use default log level 0";
86  logLevel = 0;
87  }
88  }
89  switch (logLevel) {
90  case 0:
91  std::call_once(davixDebugInit, davix_set_log_level, 0);
92  break;
93  case 1:
94  std::call_once(davixDebugInit, davix_set_log_level, DAVIX_LOG_WARNING);
95  break;
96  case 2:
97  std::call_once(davixDebugInit, davix_set_log_level, DAVIX_LOG_VERBOSE);
98  break;
99  case 3:
100  std::call_once(davixDebugInit, davix_set_log_level, DAVIX_LOG_DEBUG);
101  break;
102  default:
103  std::call_once(davixDebugInit, davix_set_log_level, DAVIX_LOG_ALL);
104  break;
105  }
106 }
static std::once_flag davixDebugInit
Definition: DavixFile.cc:14
void DavixFile::create ( const char *  name,
bool  exclusive = false,
int  perms = 0666 
)
virtual

Definition at line 144 of file DavixFile.cc.

References IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.

144  {
145  open(name,
147  perms);
148 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:160
void DavixFile::create ( const std::string &  name,
bool  exclusive = false,
int  perms = 0666 
)
virtual

Definition at line 150 of file DavixFile.cc.

References IOFlags::OpenCreate, IOFlags::OpenExclusive, IOFlags::OpenTruncate, and IOFlags::OpenWrite.

150  {
151  open(name.c_str(),
153  perms);
154 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:160
void DavixFile::open ( const char *  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)
virtual

Definition at line 160 of file DavixFile.cc.

References cms::Exception::addContext(), edm::errors::FileOpenError, Skims_PA_cff::name, IOFlags::OpenRead, and X509Authentication().

160  {
161  // Actual open
162  if ((name == nullptr) || (*name == 0)) {
164  ex << "Cannot open a file without name";
165  ex.addContext("Calling DavixFile::open()");
166  throw ex;
167  }
168  m_name = name;
169 
170  if ((flags & IOFlags::OpenRead) == 0) {
172  ex << "Must open file '" << name << "' at least for read";
173  ex.addContext("Calling DavixFile::open()");
174  throw ex;
175  }
176 
177  if (m_davixPosix && m_fd) {
179  ex << "Davix::open(name='" << m_name << "') failed on already open file";
180  ex.addContext("Calling DavixFile::open()");
181  throw ex;
182  }
184  // Translate our flags to system flags
185  int openflags = 0;
186 
187  if (flags & IOFlags::OpenRead)
188  openflags |= O_RDONLY;
189 
190  DavixError *davixErr = nullptr;
191  RequestParams davixReqParams;
192  // Set up X509 authentication
193  davixReqParams.setClientCertCallbackX509(&X509Authentication, nullptr);
194  // Set also CERT_DIR if it is set in envinroment, otherwise use default
195  const char *cert_dir = nullptr;
196  if ((cert_dir = std::getenv("X509_CERT_DIR")) == nullptr)
197  cert_dir = "/etc/grid-security/certificates";
198  davixReqParams.addCertificateAuthorityPath(cert_dir);
199 
200  m_davixPosix = std::make_unique<DavPosix>(new Context());
201  m_fd = m_davixPosix->open(&davixReqParams, name, openflags, &davixErr);
202 
203  // Check Davix Error
204  if (davixErr) {
205  std::unique_ptr<DavixError> davixErrManaged(davixErr);
207  ex << "Davix::open(name='" << m_name << "') failed with "
208  << "error '" << davixErr->getErrMsg().c_str() << " and error code " << davixErr->getStatus();
209  ex.addContext("Calling DavixFile::open()");
210  throw ex;
211  }
212  if (!m_fd) {
214  ex << "Davix::open(name='" << m_name << "') failed as fd is NULL";
215  ex.addContext("Calling DavixFile::open()");
216  throw ex;
217  }
218 }
static void configureDavixLogLevel()
Definition: DavixFile.cc:67
static int X509Authentication(void *userdata, const SessionInfo &info, X509Credential *cert, DavixError **davixErr)
Definition: DavixFile.cc:108
std::unique_ptr< Davix::DavPosix > m_davixPosix
Definition: DavixFile.h:40
std::string m_name
Definition: DavixFile.h:41
Davix_fd * m_fd
Definition: DavixFile.h:39
void DavixFile::open ( const std::string &  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)
virtual

Definition at line 156 of file DavixFile.cc.

References HLT_2018_cff::flags.

156  {
157  open(name.c_str(), flags, perms);
158 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:160
IOOffset DavixFile::position ( IOOffset  offset,
Relative  whence = SET 
)
overridevirtual

Implements Storage.

Definition at line 348 of file DavixFile.cc.

References cms::Exception::addContext(), mps_fire::result, and L1DTConfigBti_cff::SET.

348  {
349  DavixError *davixErr = nullptr;
350  if (whence != CURRENT && whence != SET && whence != END) {
351  cms::Exception ex("FilePositionError");
352  ex << "DavixFile::position() called with incorrect 'whence' parameter";
353  ex.addContext("Calling DavixFile::position()");
354  throw ex;
355  }
357  size_t mywhence = (whence == SET ? SEEK_SET : whence == CURRENT ? SEEK_CUR : SEEK_END);
358 
359  if ((result = m_davixPosix->lseek(m_fd, offset, mywhence, &davixErr)) == -1) {
360  cms::Exception ex("FilePositionError");
361  ex << "Davix::lseek(name='" << m_name << "', offset=" << offset << ", whence=" << mywhence << ") failed with error "
362  << davixErr->getErrMsg().c_str() << " and error code " << davixErr->getStatus() << " and "
363  << "call returned " << result;
364  ex.addContext("Calling DavixFile::position()");
365  throw ex;
366  }
367 
368  return result;
369 }
std::unique_ptr< Davix::DavPosix > m_davixPosix
Definition: DavixFile.h:40
int64_t IOOffset
Definition: IOTypes.h:19
std::string m_name
Definition: DavixFile.h:41
Davix_fd * m_fd
Definition: DavixFile.h:39
IOSize DavixFile::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 312 of file DavixFile.cc.

References cms::Exception::addContext(), fileCollector::done, edm::errors::FileReadError, and alignCSCRings::s.

312  {
313  DavixError *davixErr = nullptr;
314  m_davixPosix->fadvise(m_fd, 0, n, AdviseRandom);
315  IOSize done = 0;
316  while (done < n) {
317  ssize_t s = m_davixPosix->read(m_fd, (char *)into + done, n - done, &davixErr);
318  if (davixErr) {
319  std::unique_ptr<DavixError> davixErrManaged(davixErr);
321  ex << "Davix::read(name='" << m_name << "', n=" << (n - done) << ") failed with error "
322  << davixErr->getErrMsg().c_str() << " and error code " << davixErr->getStatus() << " and call returned " << s
323  << " bytes";
324  ex.addContext("Calling DavixFile::read()");
325  throw ex;
326  }
327  if (s < 0) {
329  ex << "Davix::read(name='" << m_name << "', n=" << (n - done) << ") failed and call returned " << s;
330  ex.addContext("Calling DavixFile::read()");
331  throw ex;
332  } else if (s == 0) {
333  // end of file
334  break;
335  }
336  done += s;
337  }
338  return done;
339 }
std::unique_ptr< Davix::DavPosix > m_davixPosix
Definition: DavixFile.h:40
std::string m_name
Definition: DavixFile.h:41
size_t IOSize
Definition: IOTypes.h:14
Davix_fd * m_fd
Definition: DavixFile.h:39
IOSize DavixFile::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 220 of file DavixFile.cc.

References cms::Exception::addContext(), IOBuffer::data(), edm::errors::FileReadError, mps_fire::i, alignCSCRings::s, IOBuffer::size(), and dqmMemoryStats::total.

220  {
221  assert(!buffers || into);
222 
223  // Davix does not support 0 buffers;
224  if (buffers == 0)
225  return 0;
226 
227  DavixError *davixErr = nullptr;
228 
229  std::vector<DavIOVecInput> input_vector(buffers);
230  std::vector<DavIOVecOuput> output_vector(buffers);
231  IOSize total = 0; // Total requested bytes
232  for (IOSize i = 0; i < buffers; ++i) {
233  input_vector[i].diov_size = into[i].size();
234  input_vector[i].diov_buffer = static_cast<char *>(into[i].data());
235  total += into[i].size();
236  }
237 
238  ssize_t s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
239  if (davixErr) {
240  std::unique_ptr<DavixError> davixErrManaged(davixErr);
242  ex << "Davix::readv(name='" << m_name << "', buffers=" << (buffers) << ") failed with error "
243  << davixErr->getErrMsg().c_str() << " and error code " << davixErr->getStatus() << " and call returned " << s
244  << " bytes";
245  ex.addContext("Calling DavixFile::readv()");
246  throw ex;
247  }
248  // Davix limits number of requests sent to the server
249  // to improve performance and it does range coalescing.
250  // So we can`t check what preadVec returns with what was requested.
251  // Example: If two ranges are overlapping, [10, 20] and [20, 30] which is
252  // coalesced into [10, 30] and it will contain one less byte than was requested.
253  // Only check if returned val <= 0 and make proper actions.
254  if (s < 0) {
256  ex << "Davix::readv(name='" << m_name << "') failed and call returned " << s;
257  ex.addContext("Calling DavixFile::readv()");
258  throw ex;
259  } else if (s == 0) {
260  // end of file
261  return 0;
262  }
263  return total;
264 }
void * data(void) const
Definition: IOBuffer.h:31
std::unique_ptr< Davix::DavPosix > m_davixPosix
Definition: DavixFile.h:40
IOSize size(void) const
Definition: IOBuffer.h:34
std::string m_name
Definition: DavixFile.h:41
size_t IOSize
Definition: IOTypes.h:14
Davix_fd * m_fd
Definition: DavixFile.h:39
IOSize DavixFile::readv ( IOPosBuffer into,
IOSize  buffers 
)
overridevirtual

Reimplemented from Storage.

Definition at line 266 of file DavixFile.cc.

References cms::Exception::addContext(), IOPosBuffer::data(), edm::errors::FileReadError, mps_fire::i, IOPosBuffer::offset(), alignCSCRings::s, IOPosBuffer::size(), and dqmMemoryStats::total.

266  {
267  assert(!buffers || into);
268 
269  // Davix does not support 0 buffers;
270  if (buffers == 0)
271  return 0;
272 
273  DavixError *davixErr = nullptr;
274 
275  std::vector<DavIOVecInput> input_vector(buffers);
276  std::vector<DavIOVecOuput> output_vector(buffers);
277  IOSize total = 0;
278  for (IOSize i = 0; i < buffers; ++i) {
279  input_vector[i].diov_offset = into[i].offset();
280  input_vector[i].diov_size = into[i].size();
281  input_vector[i].diov_buffer = static_cast<char *>(into[i].data());
282  total += into[i].size();
283  }
284  ssize_t s = m_davixPosix->preadVec(m_fd, input_vector.data(), output_vector.data(), buffers, &davixErr);
285  if (davixErr) {
286  std::unique_ptr<DavixError> davixErrManaged(davixErr);
288  ex << "Davix::readv(name='" << m_name << "', n=" << buffers << ") failed with error "
289  << davixErr->getErrMsg().c_str() << " and error code " << davixErr->getStatus() << " and call returned " << s
290  << " bytes";
291  ex.addContext("Calling DavixFile::readv()");
292  throw ex;
293  }
294  // Davix limits number of requests sent to the server
295  // to improve performance and it does range coalescing.
296  // So we can`t check what preadVec returns with what was requested.
297  // Example: If two ranges are overlapping, [10, 20] and [20, 30] which is
298  // coalesced into [10, 30] and it will contain one less byte than was requested.
299  // Only check if returned val <= 0 and make proper actions.
300  if (s < 0) {
302  ex << "Davix::readv(name='" << m_name << "', n=" << buffers << ") failed and call returned " << s;
303  ex.addContext("Calling DavixFile::readv()");
304  throw ex;
305  } else if (s == 0) {
306  // end of file
307  return 0;
308  }
309  return total;
310 }
std::unique_ptr< Davix::DavPosix > m_davixPosix
Definition: DavixFile.h:40
IOOffset offset(void) const
Definition: IOPosBuffer.h:39
void * data(void) const
Definition: IOPosBuffer.h:42
IOSize size(void) const
Definition: IOPosBuffer.h:45
std::string m_name
Definition: DavixFile.h:41
size_t IOSize
Definition: IOTypes.h:14
Davix_fd * m_fd
Definition: DavixFile.h:39
void DavixFile::resize ( IOOffset  size)
overridevirtual

Implements Storage.

Definition at line 371 of file DavixFile.cc.

References cms::Exception::addContext().

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

371  {
372  cms::Exception ex("FileResizeError");
373  ex << "DavixFile::resize(name='" << m_name << "') not implemented";
374  ex.addContext("Calling DavixFile::resize()");
375  throw ex;
376 }
std::string m_name
Definition: DavixFile.h:41
IOSize DavixFile::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 341 of file DavixFile.cc.

References cms::Exception::addContext(), and edm::errors::FileWriteError.

341  {
343  ex << "DavixFile::write(name='" << m_name << "') not implemented";
344  ex.addContext("Calling DavixFile::write()");
345  throw ex;
346 }
std::string m_name
Definition: DavixFile.h:41

Member Data Documentation

std::unique_ptr<Davix::DavPosix> DavixFile::m_davixPosix
private

Definition at line 40 of file DavixFile.h.

Davix_fd* DavixFile::m_fd
private

Definition at line 39 of file DavixFile.h.

std::string DavixFile::m_name
private

Definition at line 41 of file DavixFile.h.