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 }
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:164
DavixFile::DavixFile ( const std::string &  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)

Definition at line 24 of file DavixFile.cc.

References flags.

25  {
26  open(name.c_str(), flags, perms);
27 }
std::vector< Variable::Flags > flags
Definition: MVATrainer.cc:135
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:164
DavixFile::~DavixFile ( void  )
override

Definition at line 29 of file DavixFile.cc.

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

Member Function Documentation

void DavixFile::abort ( void  )
virtual

Definition at line 52 of file DavixFile.cc.

References cms::Exception::addContext().

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

Reimplemented from Storage.

Definition at line 34 of file DavixFile.cc.

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

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

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

Definition at line 68 of file DavixFile.cc.

References davixDebugInit.

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

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

146  {
148  (exclusive ? IOFlags::OpenExclusive : 0)),
149  perms);
150 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:164
void DavixFile::create ( const std::string &  name,
bool  exclusive = false,
int  perms = 0666 
)
virtual

Definition at line 152 of file DavixFile.cc.

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

153  {
155  (exclusive ? IOFlags::OpenExclusive : 0)),
156  perms);
157 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:164
void DavixFile::open ( const char *  name,
int  flags = IOFlags::OpenRead,
int  perms = 0666 
)
virtual

Definition at line 164 of file DavixFile.cc.

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

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

Definition at line 159 of file DavixFile.cc.

References flags.

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

Implements Storage.

Definition at line 354 of file DavixFile.cc.

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

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

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

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

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

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

Reimplemented from Storage.

Definition at line 270 of file DavixFile.cc.

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

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

Implements Storage.

Definition at line 378 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().

378  {
379  cms::Exception ex("FileResizeError");
380  ex << "DavixFile::resize(name='" << m_name << "') not implemented";
381  ex.addContext("Calling DavixFile::resize()");
382  throw ex;
383 }
std::string m_name
Definition: DavixFile.h:46
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 347 of file DavixFile.cc.

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

347  {
349  ex << "DavixFile::write(name='" << m_name << "') not implemented";
350  ex.addContext("Calling DavixFile::write()");
351  throw ex;
352 }
std::string m_name
Definition: DavixFile.h:46

Member Data Documentation

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

Definition at line 45 of file DavixFile.h.

Davix_fd* DavixFile::m_fd
private

Definition at line 44 of file DavixFile.h.

std::string DavixFile::m_name
private

Definition at line 46 of file DavixFile.h.