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)
 
virtual 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)
 
virtual IOOffset position (IOOffset offset, Relative whence=SET) override
 
virtual IOSize read (void *into, IOSize n) override
 
virtual IOSize readv (IOBuffer *into, IOSize buffers) override
 
virtual IOSize readv (IOPosBuffer *into, IOSize buffers) override
 
virtual void resize (IOOffset size) override
 
virtual IOSize write (const void *from, IOSize n) override
 
 ~DavixFile (void)
 
- 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)
 
virtual ~Storage (void)
 
- 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:162
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:162
DavixFile::~DavixFile ( void  )

Definition at line 29 of file DavixFile.cc.

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

Member Function Documentation

void DavixFile::abort ( void  )
virtual

Definition at line 50 of file DavixFile.cc.

References cms::Exception::addContext().

50  {
51  if (m_davixPosix && m_fd) {
52  DavixError *err = nullptr;
53  m_davixPosix->close(m_fd, &err);
54  if (err) {
55  std::unique_ptr<DavixError> davixErrManaged(err);
56  cms::Exception ex("FileAbortError");
57  ex << "Davix::abort(name='" << m_name << ") failed with error "
58  << err->getErrMsg().c_str() << " and error code " << err->getStatus();
59  ex.addContext("Calling DavixFile::abort()");
60  throw ex;
61  }
62  }
63  return;
64 }
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().

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  DavixError *err = nullptr;
37  m_davixPosix->close(m_fd, &err);
38  if (err) {
39  std::unique_ptr<DavixError> davixErrManaged(err);
40  cms::Exception ex("FileCloseError");
41  ex << "Davix::close(name='" << m_name << ") failed with error "
42  << err->getErrMsg().c_str() << " and error code " << err->getStatus();
43  ex.addContext("Calling DavixFile::close()");
44  throw ex;
45  }
46  }
47  return;
48 }
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::configureDavixLogLevel ( )
static

Definition at line 66 of file DavixFile.cc.

References davixDebugInit, and NULL.

66  {
67  long logLevel = 0;
68  char *logptr = nullptr;
69  char const * const davixDebug = getenv("Davix_Debug");
70  if (davixDebug != NULL) {
71  logLevel = strtol(davixDebug, &logptr, 0);
72  if (errno) {
73  edm::LogWarning("DavixFile") << "Got error while converting "
74  << "Davix_Debug env variable to integer. "
75  "Will use default log level 0";
76  logLevel = 0;
77  }
78  if (logptr == davixDebug) {
79  edm::LogWarning("DavixFile") << "Failed to convert to integer "
80  << "Davix_Debug env variable; Will use default log level 0";
81  logLevel = 0;
82  } else if (*logptr != '\0') {
83  edm::LogWarning("DavixFile") << "Failed to parse extra junk "
84  << "from Davix_Debug env variable. Will use default log level 0";
85  logLevel = 0;
86  }
87  }
88  switch (logLevel) {
89  case 0:
90  std::call_once(davixDebugInit, davix_set_log_level, 0);
91  break;
92  case 1:
93  std::call_once(davixDebugInit, davix_set_log_level, DAVIX_LOG_WARNING);
94  break;
95  case 2:
96  std::call_once(davixDebugInit, davix_set_log_level, DAVIX_LOG_VERBOSE);
97  break;
98  case 3:
99  std::call_once(davixDebugInit, davix_set_log_level, DAVIX_LOG_DEBUG);
100  break;
101  default:
102  std::call_once(davixDebugInit, davix_set_log_level, DAVIX_LOG_ALL);
103  break;
104  }
105 }
#define NULL
Definition: scimark2.h:8
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  {
146  (exclusive ? IOFlags::OpenExclusive : 0)),
147  perms);
148 }
virtual void open(const char *name, int flags=IOFlags::OpenRead, int perms=0666)
Definition: DavixFile.cc:162
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.

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

Definition at line 162 of file DavixFile.cc.

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

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

References flags.

158  {
159  open(name.c_str(), flags, perms);
160 }
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:162
IOOffset DavixFile::position ( IOOffset  offset,
Relative  whence = SET 
)
overridevirtual

Implements Storage.

Definition at line 352 of file DavixFile.cc.

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

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

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

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

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

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

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

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

376  {
377  cms::Exception ex("FileResizeError");
378  ex << "DavixFile::resize(name='" << m_name << "') not implemented";
379  ex.addContext("Calling DavixFile::resize()");
380  throw ex;
381 }
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 345 of file DavixFile.cc.

References cms::Exception::addContext().

345  {
346  cms::Exception ex("FileWriteError");
347  ex << "DavixFile::write(name='" << m_name << "') not implemented";
348  ex.addContext("Calling DavixFile::write()");
349  throw ex;
350 }
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.