Skip to content

Commit

Permalink
file_handle.hpp: move help functions to .cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
madsbk committed Oct 30, 2024
1 parent c0a1c55 commit d05ae8a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 52 deletions.
54 changes: 3 additions & 51 deletions cpp/include/kvikio/file_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,10 @@
*/
#pragma once

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <optional>
#include <stdexcept>
#include <system_error>
#include <utility>
Expand All @@ -42,18 +36,6 @@
namespace kvikio {
namespace detail {

/**
* @brief Parse open file flags given as a string and return oflags
*
* @param flags The flags
* @param o_direct Append O_DIRECT to the open flags
* @return oflags
*
* @throw std::invalid_argument if the specified flags are not supported.
* @throw std::invalid_argument if `o_direct` is true, but `O_DIRECT` is not supported.
*/
int open_fd_parse_flags(const std::string& flags, bool o_direct);

/**
* @brief Open file using `open(2)`
*
Expand All @@ -62,46 +44,16 @@ int open_fd_parse_flags(const std::string& flags, bool o_direct);
* @param mode Access modes
* @return File descriptor
*/
inline int open_fd(const std::string& file_path,
const std::string& flags,
bool o_direct,
mode_t mode)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
int fd = ::open(file_path.c_str(), open_fd_parse_flags(flags, o_direct), mode);
if (fd == -1) { throw std::system_error(errno, std::generic_category(), "Unable to open file"); }
return fd;
}
int open_fd(const std::string& file_path, const std::string& flags, bool o_direct, mode_t mode);

/**
* @brief Get the flags of the file descriptor (see `open(2)`)
*
* @return Open flags
*/
[[nodiscard]] inline int open_flags(int fd)
{
int ret = fcntl(fd, F_GETFL); // NOLINT(cppcoreguidelines-pro-type-vararg)
if (ret == -1) {
throw std::system_error(errno, std::generic_category(), "Unable to retrieve open flags");
}
return ret;
}
[[nodiscard]] int open_flags(int fd);

/**
* @brief Get file size from file descriptor `fstat(3)`
*
* @param file_descriptor Open file descriptor
* @return The number of bytes
*/
[[nodiscard]] inline std::size_t get_file_size(int file_descriptor)
{
struct stat st {};
int ret = fstat(file_descriptor, &st);
if (ret == -1) {
throw std::system_error(errno, std::generic_category(), "Unable to query file size");
}
return static_cast<std::size_t>(st.st_size);
}
[[nodiscard]] std::size_t get_file_size(int file_descriptor);

} // namespace detail

Expand Down
44 changes: 43 additions & 1 deletion cpp/src/file_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@
*/

#include <fcntl.h>
#include <sys/stat.h>
#include <stdexcept>

#include <kvikio/file_handle.hpp>

namespace kvikio::detail {
namespace {

/**
* @brief Parse open file flags given as a string and return oflags
*
* @param flags The flags
* @param o_direct Append O_DIRECT to the open flags
* @return oflags
*
* @throw std::invalid_argument if the specified flags are not supported.
* @throw std::invalid_argument if `o_direct` is true, but `O_DIRECT` is not supported.
*/
int open_fd_parse_flags(const std::string& flags, bool o_direct)
{
int file_flags = -1;
Expand Down Expand Up @@ -48,4 +59,35 @@ int open_fd_parse_flags(const std::string& flags, bool o_direct)
}
return file_flags;
}
} // namespace

namespace kvikio::detail {

int open_fd(const std::string& file_path, const std::string& flags, bool o_direct, mode_t mode)
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
int fd = ::open(file_path.c_str(), open_fd_parse_flags(flags, o_direct), mode);
if (fd == -1) { throw std::system_error(errno, std::generic_category(), "Unable to open file"); }
return fd;
}

int open_flags(int fd)
{
int ret = fcntl(fd, F_GETFL); // NOLINT(cppcoreguidelines-pro-type-vararg)
if (ret == -1) {
throw std::system_error(errno, std::generic_category(), "Unable to retrieve open flags");
}
return ret;
}

std::size_t get_file_size(int file_descriptor)
{
struct stat st {};
int ret = fstat(file_descriptor, &st);
if (ret == -1) {
throw std::system_error(errno, std::generic_category(), "Unable to query file size");
}
return static_cast<std::size_t>(st.st_size);
}

} // namespace kvikio::detail

0 comments on commit d05ae8a

Please sign in to comment.