Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cpp/include/cucim/filesystem/cufile_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ bool EXPORT_VISIBLE is_gds_available();
* - "r": O_RDONLY
* - "r+": O_RDWR
* - "w": O_RDWR | O_CREAT | O_TRUNC
* - "a": O_RDWR | O_CREAT
* In addition to above flags, the method append O_CLOEXEC and O_DIRECT by default.
*
* Append mode ("a") is not supported because CuFileDriver exposes explicit-offset pread()/pwrite() APIs.
*
* The following is optional flags that can be added to above string:
* - 'p': Use POSIX APIs only (first try to open with O_DIRECT). It does not use GDS.
* - 'n': Do not add O_DIRECT flag.
Expand All @@ -56,6 +57,7 @@ std::shared_ptr<CuFileDriver> EXPORT_VISIBLE open(const char* file_path, const c
* Open file with existing file descriptor.
*
* @param fd A file descriptor. To use GDS, fd needs to be opened with O_DIRECT flag.
* File descriptors opened with O_APPEND are not supported.
* @param no_gds true if you do not want to use GDS. Default value is `false`.
* @param use_mmap true if you want to use memory-mapped IO. This flag is supported only for the read-only file descriptor. Default value is `false`.
* @return A std::shared_ptr object of CuFileDriver.
Expand Down
14 changes: 11 additions & 3 deletions cpp/src/filesystem/cufile_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace cucim::filesystem
{
static constexpr unsigned int PAGE_SIZE = 4096;
static constexpr uint64_t DEFAULT_MAX_CACHE_SIZE = 128 << 20; // 128MiB
static constexpr char kAppendModeUnsupportedMessage[] =
"Append mode is not supported because CuFileDriver uses explicit-offset pread()/pwrite() semantics.";
static CuFileStub s_cufile_stub;
static CuFileDriverInitializer s_cufile_initializer;
thread_local static CuFileDriverCache s_cufile_cache;
Expand Down Expand Up @@ -76,9 +78,6 @@ static int get_file_flags(const char* flags)
case 'w':
file_flags = O_RDWR | O_CREAT | O_TRUNC;
break;
case 'a':
file_flags = O_RDWR | O_CREAT;
break;
default:
return -1;
}
Expand All @@ -96,6 +95,11 @@ bool is_gds_available()

std::shared_ptr<CuFileDriver> open(const char* file_path, const char* flags, mode_t mode)
{
if (flags != nullptr && flags[0] == 'a')
{
throw std::invalid_argument(kAppendModeUnsupportedMessage);
}

bool use_o_direct = true;
bool no_gds = false;
bool use_mmap = false;
Expand Down Expand Up @@ -189,6 +193,10 @@ CuFileDriver::CuFileDriver(int fd, bool no_gds, bool use_mmap, const char* file_
{
throw std::runtime_error(fmt::format("[Error] fcntl failed for fd {} ({})", fd, std::strerror(errno)));
}
if (flags & O_APPEND)
{
throw std::invalid_argument(kAppendModeUnsupportedMessage);
}
file_flags_ = flags;

FileHandleType file_type = (flags & O_DIRECT) ? FileHandleType::kPosixODirect : FileHandleType::kPosix;
Expand Down
14 changes: 14 additions & 0 deletions cpp/tests/test_cufile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <chrono>
#include <cstdlib>
#include <fcntl.h>
#include <stdexcept>
#include <unistd.h>
#include <string_view>
// Test
Expand Down Expand Up @@ -58,6 +59,19 @@ static void create_test_file(const char* file_name, int size)
close(fd);
}

TEST_CASE("Append mode should be rejected", "[test_cufile.cpp]")
{
std::string output_file = fmt::format("{}/test_cufile_append.raw", g_config.temp_folder);
create_test_file(output_file.c_str(), 32);

REQUIRE_THROWS_AS(cucim::filesystem::open(output_file.c_str(), "a"), std::invalid_argument);

int append_fd = ::open(output_file.c_str(), O_RDWR | O_APPEND);
REQUIRE(append_fd >= 0);
REQUIRE_THROWS_AS(cucim::filesystem::open(append_fd, true, false), std::invalid_argument);
::close(append_fd);
}

TEST_CASE("Verify libcufile usage", "[test_cufile.cpp]")
{
cudaError_t cuda_status;
Expand Down
1 change: 1 addition & 0 deletions python/pybind11/filesystem/cufile_pydoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Constructor of CuFileDriver.

Args:
fd: A file descriptor (in `int` type) which is available through `os.open()` method.
File descriptors opened with `os.O_APPEND` are not supported.
no_gds: If True, use POSIX APIs only even when GDS can be supported for the file.
use_mmap: If True, use memory-mapped IO. This flag is supported only for the read-only file descriptor. Default value is `False`.
file_path: A file path for the file descriptor. It would retrieve the absolute file path of the file descriptor if not specified.
Expand Down
4 changes: 2 additions & 2 deletions python/pybind11/filesystem/filesystem_pydoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ Open file with specific flags and mode.

- "w": os.O_RDWR | os.O_CREAT | os.O_TRUNC

- "a": os.O_RDWR | os.O_CREAT

In addition to above flags, the method append os.O_CLOEXEC and os.O_DIRECT by default.

Append mode ("a") is not supported because CuFileDriver exposes explicit-offset pread()/pwrite() APIs.

The following is optional flags that can be added to above string:

- 'p': Use POSIX APIs only (first try to open with O_DIRECT). It does not use GDS.
Expand Down