Newer
Older
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* FileDescriptor.cpp file I/O using POSIX I/O library
* Author: William F Godoy godoywf@ornl.gov
#include <fcntl.h> // open
#include <sys/stat.h> // open
#include <sys/types.h> // open
/// \cond EXCLUDE_FROM_DOXYGEN
#include <ios> //std::ios_base::failure
{
namespace transport
{
FileDescriptor::FileDescriptor(MPI_Comm mpiComm, const bool debugMode)
{
}
FileDescriptor::~FileDescriptor()
{
{
close(m_FileDescriptor);
}
void FileDescriptor::Open(const std::string &name, const OpenMode openMode)
if (m_DebugMode)
{
if (name.empty())
{
throw std::invalid_argument(
"ERROR: file name is empty, in call to FilePointer Open\n");
}
}
m_Profiler.Timers.at("open").Resume();
m_FileDescriptor = open(m_Name.c_str(), O_WRONLY | O_CREAT, 0777);
m_Profiler.Timers.at("open").Resume();
// TODO we need to change this to read/write
m_FileDescriptor = open(m_Name.c_str(), O_WRONLY | O_APPEND);
m_Profiler.Timers.at("open").Resume();
m_FileDescriptor = open(m_Name.c_str(), O_RDONLY);
if (m_FileDescriptor == -1)
{
throw std::ios_base::failure("ERROR: couldn't open file " + m_Name +
", check permissions or existence, in "
"call to FileDescriptor Open\n");
void FileDescriptor::Write(const char *buffer, size_t size)
auto lf_Write = [&](const char *buffer, size_t size) {
if (m_Profiler.IsActive)
{
m_Profiler.Timers.at("write").Resume();
}
auto writtenSize = write(m_FileDescriptor, buffer, size);
if (m_Profiler.IsActive)
{
m_Profiler.Timers.at("write").Pause();
}
if (writtenSize == -1)
{
throw std::ios_base::failure("ERROR: couldn't write to file " +
m_Name +
", in call to FileDescriptor Write\n");
if (static_cast<size_t>(writtenSize) != size)
{
throw std::ios_base::failure(
"ERROR: written size + " + std::to_string(writtenSize) +
" is not equal to intended size " + std::to_string(size) +
" in file " + m_Name + ", in call to FileDescriptor Write\n");
}
};
if (size > DefaultMaxFileBatchSize)
{
const size_t batches = size / DefaultMaxFileBatchSize;
const size_t remainder = size % DefaultMaxFileBatchSize;
size_t position = 0;
for (size_t b = 0; b < batches; ++b)
{
lf_Write(&buffer[position], DefaultMaxFileBatchSize);
position += DefaultMaxFileBatchSize;
lf_Write(&buffer[position], remainder);
}
else
{
lf_Write(buffer, size);
m_Profiler.Timers.at("close").Resume();
const int status = close(m_FileDescriptor);
m_Profiler.Timers.at("close").Pause();
throw std::ios_base::failure("ERROR: couldn't close file " + m_Name +
", in call to FileDescriptor Close\n");
} // end namespace adios